Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory allocation alignment bug #132

Open
dbechrd opened this issue Jun 21, 2017 · 2 comments
Open

Memory allocation alignment bug #132

dbechrd opened this issue Jun 21, 2017 · 2 comments

Comments

@dbechrd
Copy link

dbechrd commented Jun 21, 2017

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
Copy link
Contributor

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
Copy link
Author

dbechrd commented Jun 21, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants