[gambit-list] Re: FFI and vectors

Marc Feeley feeley at iro.umontreal.ca
Wed Feb 15 19:57:08 EST 2006


 > From: "John Emhoff" <thehofffs at hotmail.com>
 > Date: February 7, 2006 6:24:16 PM EST (CA)
 > To: gambit-list at iro.umontreal.ca
 > Subject: FFI and vectors
 >
 > Is there a way I can convert a u32vector or whatever to the  
appropriate C type (unsigned int * or > void*) to give to a c- 
lambda?  The docs aren't too explicit on how to do this, as far as I  
can tell...

There is no builtin conversion between u32vectors and the C ___U32*  
pointer type.  I may add this in the future, but I haven't yet  
because the "correct" implementation (which copies the u32vector to a  
C heap allocated array of ___U32) is probably too slow for most  
applications.

A way around this is to pass a "scheme-object" and to cast it to a  
___U32*.  This will only work if you know that the garbage collector  
will not be called (this might move the object).  This (untested)  
code should do the trick:

(c-declare "int foo (___U32* v) { return v[0] + v[1] + v[2]; }")

(define foo
   (c-lambda (scheme-object) int "___result = foo (___CAST 
(___U32*,___BODY(___arg1)));"))

(pp (foo (u32vector 1 2 3)))


Marc




More information about the Gambit-list mailing list