Hello,
it seems to me that it is not straightforward to use DL to wrap a that
kind of structure:
typedef _foo {
char * bar;
unsigned int qux;
} Foo;
Foo ** foos;
I tried to define foos = struct[ “Food ** foos” ] but i’m not able to
access foos[0] at a later time.
Am i doing something wrong ?
···
–
Pierre Baillet
If you want the answers, you better get ready for the fire
System of a Down
At Tue, 26 Nov 2002 17:55:46 +0900,
typedef _foo {
char * bar;
unsigned int qux;
} Foo;
Foo ** foos;
I tried to define foos = struct[ “Food ** foos” ] but i’m not able to
access foos[0] at a later time.
“struct[…]” creates something like a wrapper class for struct Foo.
The following example shows how to access foos[0], foos[1],…
— test.c —
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char bar[10];
unsigned int qux;
} Foo;
/* create_foos() creates Foos */
Foo **create_foos(int n){
Foo **foos;
int i;
foos = (Foo**)malloc(sizeof(Foo*) * n);
for( i=0; i<n; i++ ){
foos[i] = (Foo*)malloc(sizeof(Foo));
sprintf(foos[i]->bar,“%d”,i);
foos[i]->qux = i;
}
return foos;
}
– test.rb –
require ‘dl/import’
require ‘dl/struct’
module Foo
extend DL::Importable
dlload ‘test.so’
typealias “Foo”, “void*”
extern “Foo **create_foos(int)”
Foo = struct [
“char bar[10]”,
“int qux”,
]
end
n = 10
foos = Foo::create_foos(n)
ptr = foos # “ptr = (void*)(foos)” in C layer
for i in 0…(n-1)
foo = ptr.ptr # “foo = *ptr” in C layer
foo = Foo::Foo.new(foo) # wrap the pointer
p [foo.bar.collect{|c| c.chr}.join, foo.qux]
ptr += DL.sizeof(“P”) # increment the pointer
end
···
–
Takaaki Tateishi ttate@kt.jaist.ac.jp