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

Allow compilation of ldlt tests if targeting Windows #117

Merged
merged 1 commit into from
Jun 19, 2023

Conversation

mjacobse
Copy link
Collaborator

Use Windows-specific _aligned_malloc and _aligned_free for implementing the AlignedAllocator in the ldlt tests on Windows.

I did consider making this completely platform-independent by upping the C-standard from C99 to C11 and just using aligned_alloc. Unfortunately, Microsoft does not support it (see https://learn.microsoft.com/en-us/cpp/standard-library/cstdlib?view=msvc-170#remarks-6) and so MinGW does not seem to provide it either. One could at least remove the posix-specific branch but that did not seem worth the standard change.

I believe another option would be to implement the aligning as in the SimpleAlignedAlloc:

T* allocate(std::size_t n)
{
// alloc sufficient space to allow for alignment and storage of base addr
size_t size = n*sizeof(T) + align;
void *mem = malloc( size+sizeof(void*) );
// align it, allowing for base addr beforehand
void *ptr = reinterpret_cast<void*>(
reinterpret_cast<char*>(mem) + sizeof(void*)
);
if(!std::align(align, n*sizeof(T), ptr, size)) throw std::bad_alloc();
// store base address to left of returned pointer
void **vptr = reinterpret_cast<void**>(ptr) - 1;
*vptr = mem;
// finally return pointer
return static_cast<T*>(ptr);
}
However, this was introduced in fea9ed8 for "testing purposes" and does not seem to be used or tested anywhere at the moment. The only mention of it is commented out:
//typedef SimpleAlignedAllocator<T> PoolAllocator;
So with those not entirely obvious reinterpret_casts for storing the original allocated pointer this did not seem like the best option to me either.

Use Windows-specific _aligned_malloc and _aligned_free for implementing
the AlignedAllocator in the ldlt tests on Windows. Unfortunately
this cannot be implemented in a platform-independent way, since
Microsoft does not support aligned_alloc. See
https://learn.microsoft.com/en-us/cpp/standard-library/cstdlib?view=msvc-170#remarks-6
@jfowkes jfowkes self-requested a review June 19, 2023 08:24
Copy link
Contributor

@jfowkes jfowkes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mjacobse, I agree that this is the cleanest solution given Microsoft's incomplete C11 support.

@jfowkes jfowkes merged commit 70c4219 into ralna:master Jun 19, 2023
@mjacobse mjacobse deleted the windows_aligned_allocator branch June 25, 2023 10:42
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

Successfully merging this pull request may close these issues.

2 participants