Storable tuples and what is 'alignment'?
Alastair Reid
alastair@reid-consulting-uk.ltd.uk
06 Aug 2002 11:59:10 +0100
Andrew J Bromage <ajb@spamcop.net> writes:
> This number is called the "alignment", and a good rule of thumb for
> computing it is:
> instance Storable a where alignment a = sizeOf a `min` machine_word_size
The way we calculate it in GHC and Hugs is:
#define offsetof(ty,field) ((size_t)((char *)&((ty *)0)->field - (char *)(ty *)0))
print("%d", offsetof(struct { char c; $1 ty;},ty));
The simplest way of getting this value into some Haskell code is to
use hsc2hs (comes with GHC). Something like this should do you:
instance Storable Foo where
alignment _ = #const offsetof(struct { char c; $1 foo_ty;}
sizeOf _ = #const sizeof(foo_ty)
peek = ...
poke = ...
See
http://haskell.org/ghc/docs/latest/html/users_guide/hsc2hs.html
for more info about hsc2hs.
--
Alastair Reid alastair@reid-consulting-uk.ltd.uk
Reid Consulting (UK) Limited http://www.reid-consulting-uk.ltd.uk/alastair/