views:

284

answers:

3

I'm developing a general purpose library which uses Win32's HeapAlloc

MSDN doesn't mention alignment guarantees for Win32's HeapAlloc, but I really need to know what alignment it uses, so I can avoid excessive padding.

On my machine (vista, x86), all allocations are aligned at 8 bytes. Is this true for other platforms as well?

+2  A: 

The alignment will be such that the returned address can be cast to a pointer of any type. Otherwise you will not be able to use the memory in your application.

David Harris
If I understand correctly your answer, the fact that the returned address can be casted to a pointer of any type isn't related to the alignment of the allocated memory.
Matteo Italia
@Matteo: it is related. If it was not aligned for a type T, then a cast to `T*` would be invalid (it'd compile, but it wouldn't be guaranteed to work). So because it can be cast to any type, it must be aligned strictly enough to work with any type.
jalf
Ok, I understood your answer in another way, so what I said before do not apply; still, the OS cannot guarantee that it is aligned for *any* type, otherwise only a NULL pointer would be ok :) . Probably it's guaranteed to work for the biggest type which requires alignment known to people who wrote the allocator (or to the biggest type which requires alignment that the people who wrote the allocator are willing to support directly).
Matteo Italia
Unfortunately, this guarantee is not valid: on vista32, the alignment is 8 bytes, which is suitable for doubles and long longs, but it isn't suitable for the SSE intrinsic types, which require 16 byte alignment, so one needs to manually align it.
Koert
+1  A: 

Surprisingly, Google turns up evidence that HeapAlloc is not always SSE-compliant:

HeapAlloc() has all the objects always 8-byte aligned, no matter what their size is (but not 16-byte-aligned, for SSE).

The post is from mid 2008, suggesting that recent Windows XP suffers from this bug.

See also http://support.microsoft.com/kb/286470:

The Windows heap managers (all versions) have always guaranteed that the heap allocations have a start address that is 8-byte aligned (on 64-bit platforms the alignment is 16-bytes).

Potatoswatter
Yep, SSE types live a kind of shadow existence where their alignment is rarely respected by allocation functions. I'm not sure why, but it seems to be the case on both Windows and Linux. By default, you don't get better alignment guarantees than 8 bytes.
jalf
@jalf: strange! I recall in 1999 when AltiVec was introduced, Apple immediately respecified their allocation functions `NewPtr` and `NewHandle` for 16-byte alignment. It's not like you break old software that way!
Potatoswatter
+1  A: 

The HeapAlloc function does not specify the alignment guarantees in the MSDN page, but I'm inclined to think that it should have the same guarantees of GlobalAlloc, which is guaranteed to return memory 8-byte aligned (although relying on undocumented features is evil); after all, it's explicitly said that Global/LocalAlloc are just wrappers around HeapAlloc (although they may discard the first n bytes to get aligned memory - but I think that it's very unlikely).

If you really want to be sure, just use GlobalAlloc, or even VirtualAlloc, whose granularity is the page granularity, which is usually 4 KB (IIRC), but in this case for small allocations you'll waste a lot of memory.

By the way, if you use C++ new operator, you are guaranteed to get memory aligned correctly for the type you specified: this could be the way to go.

Matteo Italia