[plt-scheme] ffi equivalent of array in struct

From: Jon Rafkind (workmin at ccs.neu.edu)
Date: Sun Apr 23 23:30:20 EDT 2006

>>Im creating objects that I dont want the GC to see so I use (malloc
>>'raw) but I have to give the size of the entire object to malloc as
>>opposed to this:
>>
>>(let ((f (malloc 'raw _Foo))
>>   (set-Foo-blah! f (malloc 'raw _int 10)))
>>    
>>
>
>Whether you use _Foo or _int with 10, the result is the same -- the
>type and the number are just used for the allocation, and there is no
>type information with the pointer you're receiving.  (Which is another
>thing that should change with the rewrite I mention above.)
>
>  
>
Well but if I use _Foo then _Foo would have to have all 10 int's in 
there. I assume you mean this can be solved with the make-cstruct-type 
thing below.

>  
>
>>Obviously this doenst work but its sort of annoying to precalculate
>>the size of the struct in C and use that as the size argument to
>>malloc in scheme. Im guessing the way to do this is some trickery
>>with the _bytes type? [...]
>>    
>>
>
>You can use `make-cstruct-type' with a list of 10 _int values, that
>will create a struct type of the desired size.
>
>  
>
Do I have to list them all? (make-cstruct-type _int _int _int ...) ? The 
real problem is a struct with 65536 chars in it. I dont want to have to 
type all those..

Or I suppose I could make cstruct's which contain other cstructs and 
balloon upto the size I wanted anyway:
(define 4bytes _int)
(define 8bytes (make-cstruct-type 4bytes 4bytes))
(define 16bytes (make-cstruct-type 8bytes 8bytes))
(define 32bytes (make-cstruct-type 16bytes 16bytes))
Almost there!
(define 65536bytes (make-struct-type 32000bytes 32000bytes))





Posted on the users mailing list.