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

From: Brian Chin (naerbnic at ucla.edu)
Date: Sat Nov 10 15:29:12 EST 2007

On Nov 10, 2007, at 12:07 PM, hendrik at topoi.pooq.com wrote:

> On Sat, Nov 10, 2007 at 11:01:11AM -0800, Brian Chin 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.
>
> in C, the sizeof() of a type is not the number of bytes it takes  
> but the
> spacing between adjacent elements if it is packed into an array.  This
> may be different from the actual size of a value of that type.--

Ah, excellent point. Instead of just using the raw type, I create as  
a new basetype a struct type with a single element (via make-cstruct- 
type), which should solve those alignment issues, unless there's a  
platform where the type with the largest sizeof will not have the  
largest aligned size.

For those who are curious about the code, I've posted my version of  
foreign.ss here. All of the changes are near the definition to define- 
cstruct and define-cunion:

http://www.cs.ucla.edu/~naerbnic/foreign.ss

Have fun!
- Brian Chin




Posted on the users mailing list.