[plt-scheme] "define-cunion" prototype for FFI

From: Jon Rafkind (workmin at ccs.neu.edu)
Date: Sat Nov 10 15:09:40 EST 2007

> Correct me if my understanding is flawed, but unions are notably
> simpler than structs, given that most of the alignment issues can be
> avoided since all of the "fields" have a pointer offset of 0. In that
> regard, the only remaining issues is ensuring that the union type has
> the correct size, which should be the maximum size of it's members. So
> in the end, it looks like I'm working on the same level as
> define-cstruct, which would at first examination be the natural level
> to put it.
>
It might take some extra trickery to match the alignment that gcc
produces. For example a struct like
struct{
   int x;
   char y[ 7 ];
};
has a size of 12, as reported by sizeof, which 'ctype-sizeof' also
reports for an equivalent structure in ffi.

But for a union
union{
   int x;
   char y[ 7 ];
};
Now gcc says the size is 8. I didn't see a way to get the ffi to realize
the same thing. It might be safe to round up the largest member, char
y[7], to the next multiple of the machines word size, 4 for 32 and
64-bit, but I don't know if that is gcc specific.
> It may be easier to understand this if I made the code available. If
> anyone is interested in seeing the current incarnation of the code,
> I'm more than willing to post it up somewhere. Any comments,
> complains, or insults are appreciated :-).
Yes, it would be good to see the code.


Posted on the users mailing list.