Allocating aligned memory
by Ondrej Mocny · in Torque Game Engine · 07/11/2012 (7:27 am) · 3 replies
Hi,
for the platform I'm working on I need some memory allocations to be 8-byte aligned. There is no "memalign" function in platformMemory.cc. I would actually prefer all allocations to be 8-byte aligned because that's actually what malloc does on this platform. But I can't find any setting influencing this. TORQUE_BYTE_ALIGNMENT has effect only in FrameAllocator.
For now I'm just allocating 4 bytes more and aligning it if needed but I don't like that :) I also prefer not to use the default allocator.
Ondrej
for the platform I'm working on I need some memory allocations to be 8-byte aligned. There is no "memalign" function in platformMemory.cc. I would actually prefer all allocations to be 8-byte aligned because that's actually what malloc does on this platform. But I can't find any setting influencing this. TORQUE_BYTE_ALIGNMENT has effect only in FrameAllocator.
For now I'm just allocating 4 bytes more and aligning it if needed but I don't like that :) I also prefer not to use the default allocator.
Ondrej
#2
- made sure dRealMalloc() and dRealFree() are allocating aligned memory
- made sure Memory::PageRecord and Memory::Header have size aligned
- in Memory::checkUnusedAlloc() I aligned the skipped size to the next header, so that it ends up on aligned address again
08/03/2012 (1:02 pm)
Ok, I digged into the allocator and solved it by myself. This is what I did:- made sure dRealMalloc() and dRealFree() are allocating aligned memory
- made sure Memory::PageRecord and Memory::Header have size aligned
- in Memory::checkUnusedAlloc() I aligned the skipped size to the next header, so that it ends up on aligned address again
#3
08/03/2012 (1:03 pm)
Memory::checkUnusedAlloc() and Memory::allocPage() are the only functions where new free blocks are created. While freeing memory blocks are only merged together which doesn't break alignment.
Ondrej Mocny
Grip Games