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

From: Henk Boom (lunarc.lists at gmail.com)
Date: Mon Jan 26 21:06:21 EST 2009

2009/1/26 Jakub Piotr Cłapa <jpc-ml at zenburn.net>:
> 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.

Since it is returned by copying into a scheme value, there is no way
to modify an existing _path-type in-place, which is useful for me. In
my case I need to produce a 'mass' struct which contains, among other
things, an inertia matrix as a direct float array. The interface for
using them looks like:

ODE_API void dBodySetMass (dBodyID, const dMass *mass);
ODE_API void dBodyGetMass (dBodyID, dMass *mass);

For my current project I will likely not need to manipulate the
inertia matrix directly, as there are utility functions to initialize
it for common shapes, but for the general case (i.e. when I release
bindings) I need to. Having a way to get the array by reference from
the struct (like my earlier broken memory-corrupting one does) lets
you modify the matrix just as you would with any regular scheme
object.

I don't mean to say your solution is a bad one, but I don't think it
works in all practical situations like you say it does.

    Henk


Posted on the users mailing list.