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

From: hendrik at topoi.pooq.com (hendrik at topoi.pooq.com)
Date: Sat Nov 10 15:15:38 EST 2007

On Sat, Nov 10, 2007 at 01:09:40PM -0700, Jon Rafkind wrote:
> 
> > 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.

As I mentioned in another post (q.v.) sizeof(T) is actually the spacing 
between adjacent array elements of type T.

-- hendrik

> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme


Posted on the users mailing list.