[plt-scheme] Binding C-Structs Containing Fixed-Size Arrays with the FFI

From: Jakub Piotr Cłapa (jpc-ml at zenburn.net)
Date: Mon Jan 26 18:19:50 EST 2009

On 1/26/09 9:36 PM, Eli Barzilay wrote:
> On Jan 26, Jakub Piotr C?apa wrote:
>> You may want to check these threads:
>>
>> http://thread.gmane.org/gmane.lisp.scheme.plt/12987
>> http://thread.gmane.org/gmane.lisp.scheme.plt/28412/focus=28509
>> http://list.cs.brown.edu/pipermail/plt-scheme/2007-March/016671.html
>
> The quick version is: libffi doesn't have a way to do so.  You need to
> either fake it with structs like Matthew showed, or you can do it more
> manually: malloc a piece of memory and `ptr-ref' parts of it directly.
> But that won't work inside a struct -- I don't know of any API in
> libffi to create a type with a given size/alignment.

This is true but:
1. _path-type [1] worked quite well in my case.
2. The problem is mostly with homogeneus arrays inside structs which
    AFAIK always have the same alignment as their basic element type.
    (it may not be true with SIMD but in those cases even plain C does
    not give you correct aligments without manual tweaking)

So even if the solution is not elegant (I don't like it either) it works 
in all practical situations and saves the day. As far as I understand 
you also do not know of any better means to do it.

Considering the above I suggested to mention this solution (and 
obviously any of it's deficiencies we know of) in the FFI manual.

[1]: http://thread.gmane.org/gmane.lisp.scheme.plt/28412/focus=28509

P.S. Couldn't we just ignore libffi struct handling and just do it in 
Scheme like Scheme->C did? (they compiled a test C program which 
computed and printed out the needed aligments and sizes)
libffi would only be used for calls and callbacks (this seems to be done 
very well). I understand this is much work (and may not be worth the 
trouble) but are there any formal shortcomings I don't see?

-- 
regards,
Jakub Piotr Cłapa


Posted on the users mailing list.