[cffi-devel] C struct in C struct ?

James Bielman jamesjb at jamesjb.com
Sat May 6 22:50:08 UTC 2006


Frank Goenninger <fgoenninger at prion.de> writes:

> I do habe C structs lile these:
>
> struct a_struct
> {
>     int a;
> };
>
> struct b_struct
> {
>    int b;
>    struct a_struct a;
> };
>
>
> How would I model b_struct using defcstruct ?

It looks like what you would think:

(defcstruct a-struct
  (a :int))

(defcstruct b-struct
  (b :int)
  (a a-struct))

It's confusing, because CFFI tries to avoid passing aggregate C
objects by value.  For instance, if you declare a function argument as
having type A-STRUCT, you get a pointer instead, because you can't
pass structures by value in CFFI.

But in a structure, you do need to able to include a structure, not
just a pointer to it.  So the obvious thing does what you want.

In retrospect, the foreign type A-STRUCT should probably always mean
the structure itself by value, and declaring a function like:

(defcfun struct-by-value :void
  (a a-struct))

should probably just be an error, instead of the DWIM-ish behavior of
canonicalizing A-STRUCT to :POINTER.  (And someday the Lisp
implementations may be extended to pass structures by value, and we
will want to support that.)

Ideally, :POINTER should probably become a parameterized type, so that
function would instead be:

(defcfun struct-by-pointer :void
  (a (:pointer a-struct))

which will help us down the road when we implement optional pointer
type checking.

Any thoughts?  This would be a pretty incompatible change, but it's
probably better to fix it soon then let more code get written that
takes advantage of this (IMHO) misfeature.

James



More information about the cffi-devel mailing list