
In GHC, how can I allocate a chunk of memory aligned to some block size (say, 512 or 1024 bytes)? I tried to specify it in the "alignment" method in the Storable typeclass, but that does not seem to work. Is Storable.alignment really used in GHC? If so, is there a code example that allocates aligned memory in this way? For the moment I am using the C function memalign() like this: foreign import ccall "static stdlib.h" memalign :: CInt -> CInt -> IO (Ptr CChar) .... do ptr <- memalign alignment size fptr <- newForeignPtr finalizerFree ptr Is it safe to do so? Thanks, Peng

Li, Peng wrote:
In GHC, how can I allocate a chunk of memory aligned to some block size (say, 512 or 1024 bytes)? I tried to specify it in the "alignment" method in the Storable typeclass, but that does not seem to work. Is Storable.alignment really used in GHC? If so, is there a code example that allocates aligned memory in this way?
For the moment I am using the C function memalign() like this:
foreign import ccall "static stdlib.h" memalign :: CInt -> CInt -> IO (Ptr CChar) .... do ptr <- memalign alignment size fptr <- newForeignPtr finalizerFree ptr
Is it safe to do so?
Yes, that's safe. Storable.alignment is not used, it is there for informational purposes only - i.e. what alignment does this type need. Foreign.malloc should return memory aligned to the maximum alignment required by any primitive type, and any C structure type (the same as malloc(), in other words). Cheers, Simon

For the moment I am using the C function memalign() like this:
foreign import ccall "static stdlib.h" memalign :: CInt -> CInt -> IO (Ptr CChar)
iirc, memalign is not provided on windows, nor by mingw. so this wouldn't be portable. claus
Storable.alignment is not used, it is there for informational purposes only - i.e. what alignment does this type need. Foreign.malloc should return memory aligned to the maximum alignment required by any primitive type, and any C structure type (the same as malloc(), in other words).

* Li, Peng
In GHC, how can I allocate a chunk of memory aligned to some block size (say, 512 or 1024 bytes)? I tried to specify it in the "alignment" method in the Storable typeclass, but that does not seem to work. Is Storable.alignment really used in GHC? If so, is there a code example that allocates aligned memory in this way? For the moment I am using the C function memalign() like this:
From looking at the source, it looks like you may be right...allocation seems to only invoke plain malloc with just the size. However, as Claus already pointed out, you should be aware that memalign() isn't portable. Try using posix_memalign() if available, although even this function need not be provided on every platform...
On the other hand, a system usually provides at least page-aligned memory if you request a chunk equal or larger to the page size, and page aligment should be sufficient for most of your needs. You could even build a small allocator on top for your favourite alignment :-D (I'm currently on a machine with page size=4k and no *memalign()). Volker -- http://www-i2.informatik.rwth-aachen.de/stolz/ *** PGP *** S/MIME "All the excitement lies in pattern matching." (SPJ et al.)
participants (4)
-
Claus Reinke
-
Li, Peng
-
Simon Marlow
-
Volker Stolz