lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

John Belmonte wrote:
> Roberto wrote:
> > >     union L_Umaxalign { double d; void *s; long l; };
> > >
> > > should probably be:
> > >
> > >     union L_Umaxalign { lua_Number d; void *s; long l; };
> >
> > This double is to ensure proper alignment for userdata and strings
> > *outside* Lua. (Notice that strings will always be aligned, too.)
> I don't quite understand the distinction of "outside Lua".  Isn't userdata
> always allocated from C?  Can't C make references to strings inside Lua?

Wasn't it you that tried to implement Perl like pack/unpack functions
and was unsatisfied because strings were not properly aligned? ;-)

Strings are allocated by Lua as is memory from lua_newuserdata.  This
L_Umaxalign makes sure that these memory blocks get the maximum align-
ment so that any data may be stored within them.

> Anyway, I think that union is not generally sufficient.  On my machine the
> largest data type requiring alignment is 16 bytes (a vector).

??? And how is this "vector" used in C?  Are there really people making
long long 128 bit and then even force a 128 bit alignment?

> (By no coincidence this is also the alignment of the stack and heap.)

Heap and stack alignments are often larger then the actual required
object alignment.  Mostly because of cache issues.

Ciao, ET.