[racket-dev] [racket] newbie: foreign C structure definition question

From: Hendrik Boom (hendrik at topoi.pooq.com)
Date: Thu Nov 18 22:44:00 EST 2010

On Thu, Nov 18, 2010 at 10:06:34PM -0500, Eli Barzilay wrote:
> [redirected to dev again]
> 15 minutes ago, Hendrik Boom wrote:
> > On Tue, Nov 16, 2010 at 11:37:17PM -0500, Eli Barzilay wrote:
> > > Three minutes ago, Jay McCarthy wrote:
> > > > We could use a syntax parameter in define-cstruct to communicate to
> > > > the vector syntax to give them the correct size in structs and make
> > > > my macro's approach the normal behavior.
> > > 
> > > * Types in a cstruct are plain expressions (that should evaluate to
> > >   ctype values).
> > > 
> > > * Arrays should be available as a type outside of cstructs too.
> > 
> > An array of, say, ten elements should be passes the same way as a
> > struct with the same ten elements.
> Yes, what I said earlier is that it really boils down to being able to
> define a libffi type descriptor with some manually specified size and
> alignment.  It's also how the current hackarounds all work.
> > That C doesn't do this is a flaw in its calling interface.
> And that actually is another problem for Sam when he implements this
> -- dealing with whatever situations make C expect the array to be
> passed by value (a real one, with the whole array on the stack).
> > But given that it's possible to pass references, it should be
> > possible to pass references to C arrays when the C funtion is coded
> > as if it expects an array (when, of course, it really expects a
> > reference).
> Right, this works assuming sane C, and IIRC, with small arrays it can
> expect the whole array to be on the stack.  (But maybe the rules for
> structs are the same, so it works out by size only.)

C defines that when an array is the actual parameter, the address of it 
is passed instead.  It depends on the fact that the type of the 
argunment is an array and nothing else.  If you pass a one-element array 
of integer (note the singular), you're supposed to get its address 
instead of the array itself.

Structures are passed in their entirety.

Of course, some compilers when functions with prototypes are called, may 
do incredibly convoluted things instead to attain greater efficiency.  
FFI would presumably have to match that complexity in terms of hardware 
register hacking.

-- hendrik

Posted on the dev mailing list.