Sparky icon indicating copy to clipboard operation
Sparky copied to clipboard

Memory allocation alignment bug

Open dbechrd opened this issue 8 years ago • 2 comments

I believe your memory allocater does not align memory properly. https://github.com/TheCherno/Sparky/blob/master/Sparky-core/src/sp/system/Allocator.cpp

The Allocate method: void* Allocator::Allocate(size_t size)

calls _aligned_malloc on line 24: byte* result = (byte*)SP_ALLOC(actualSize);

then proceeds to misalign the memory on line 27 before returning it to the caller:

result += sizeof(size_t);
return result;

The alignment needs to take the size_t preamble into account to ensure that the pointer returned to the caller is properly aligned. For an example of doing this properly, see: https://blog.molecular-matters.com/2012/08/27/memory-allocation-strategies-a-stack-like-lifo-allocator/

dbechrd avatar Jun 21 '17 16:06 dbechrd

That is correct, but the Allocator neither by name nor description implies that it returns an aligned block of memory. It just happens to use the _aligned_malloc function.

jannisj1 avatar Jun 21 '17 17:06 jannisj1

Agreed, just pointing it out. Feel free to close if not considered a bug. :)

dbechrd avatar Jun 21 '17 21:06 dbechrd