#2917: alloca and allocaArray do not respect alignment
-----------------------------+----------------------------------------------
Reporter: guest | Owner:
Type: bug | Status: new
Priority: normal | Milestone: _|_
Component: Compiler (FFI) | Version: 6.12.3
Resolution: | Keywords:
Testcase: | Blockedby:
Difficulty: Unknown | Os: Unknown/Multiple
Blocking: | Architecture: Unknown/Multiple
Failure: None/Unknown |
-----------------------------+----------------------------------------------
Changes (by guest):
* cc: danield...@… (added)
* version: 6.10.1 => 6.12.3
Comment:
I wasted several hours to find out that my LLVM related Haskell code is
also affected by this problem (I found it in mallocArray). I already found
out, that LLVM's malloc intrinsic is broken in this way, but I expected
Haskell's malloc is correct in this respect. My malloc(C) manpage says:
"For calloc() and malloc(), return a pointer to the allocated
memory, which is suitably aligned for any kind of variable." But sometimes
it returns pointers that are not divisible by 16, only by 8. Thus it
seems, that malloc or its manpage is buggy.
My solution is to allocate a memory chunk that is 16+sizeof(ptr) bytes
larger. Then I choose a start address within that chunk that is divisible
by 16 and leaves space for a pointer to the full allocated chunk, that I
store just before that 16-byte-aligned address.
{{{
#include <stdlib.h>
void *aligned_malloc(size_t size) {
const int ptrsize = sizeof(void *);
void *ptr = malloc(size+16+ptrsize);
if (ptr) {
void **alignedptr = (void **)((size_t)(ptr+16+ptrsize) & (-16));
*(alignedptr-1) = ptr;
return alignedptr;
} else {
return NULL;
}
};
void aligned_free(void *alignedptr) {
void **sptr = (void **) alignedptr;
void *ptr = *(sptr - 1);
free(ptr);
};
}}}
--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/2917#comment:23>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs