[Haskell-cafe] Storable class?

Bulat Ziganshin bulat.ziganshin at gmail.com
Tue Apr 22 18:05:10 EDT 2008


Hello Evan,

Wednesday, April 23, 2008, 1:48:30 AM, you wrote:

> The FFI doc doesn't really talk about the alignment method at all, so
> I don't really understand how to write one or how it's used.

write: easy. just specify how much data shoulkd be aligned. for
primitive datatypes this usually equals to datasize, for complex
structures this should be the same as maximum alignment of elements
involved:

instance Storable Float
  alignment = 4

instance Storable (a,b)
  alignment = maximum [alignment a, alignment b]

use: just align memory blocks allocated to store this datatype. usual
alignment technique is:

alloc a = (allocBytes (sizeOf a + alignment a - 1) + (alignment a - 1)) .&. (alignment a - 1)

well, many standard allocators such as C malloc, actually provide you
blocks with a maximum alignment required to store any (primitive)
type, so you don't need to worry about it


-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com



More information about the Haskell-Cafe mailing list