TheCherno / Sparky

Cross-Platform High Performance 2D/3D game engine for people like me who like to write code.
Apache License 2.0
1.09k stars 222 forks source link

Memory allocation alignment bug #132

Open dbechrd opened 7 years ago

dbechrd commented 7 years ago

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/

jannisj1 commented 7 years ago

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.

dbechrd commented 7 years ago

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