Commit 39c3038c authored by Oleg Kolosov's avatar Oleg Kolosov

Use feature test macro for aligned_alloc

Reorder ifdef checks in BTreeImpl::growTree to make posix_memalign the
default case and hide aligned_alloc behind feature test macro. This
covers more systems and actually fixes the compatibility with Android.
parent 6ecd99b2
......@@ -288,27 +288,27 @@ void BTreeImpl::growTree(uint minCapacity) {
// aligned_alloc() function. Unfortunately, many platforms don't implement it. Luckily, there
// are usually alternatives.
#if __APPLE__
// OSX lacks aligned_alloc(), but has posix_memalign(). Fine.
void* allocPtr;
int error = posix_memalign(&allocPtr,
sizeof(BTreeImpl::NodeUnion), newCapacity * sizeof(BTreeImpl::NodeUnion));
if (error != 0) {
KJ_FAIL_SYSCALL("posix_memalign", error);
}
NodeUnion* newTree = reinterpret_cast<NodeUnion*>(allocPtr);
#elif _WIN32
#if _WIN32
// Windows lacks aligned_alloc() but has its own _aligned_malloc() (which requires freeing using
// _aligned_free()).
// WATCH OUT: The argument order for _aligned_malloc() is opposite of aligned_alloc()!
NodeUnion* newTree = reinterpret_cast<NodeUnion*>(
_aligned_malloc(newCapacity * sizeof(BTreeImpl::NodeUnion), sizeof(BTreeImpl::NodeUnion)));
KJ_ASSERT(newTree != nullptr, "memory allocation failed", newCapacity);
#else
#elif _ISOC11_SOURCE // macro available since glibc 2.16
// Let's use the C11 standard.
NodeUnion* newTree = reinterpret_cast<NodeUnion*>(
aligned_alloc(sizeof(BTreeImpl::NodeUnion), newCapacity * sizeof(BTreeImpl::NodeUnion)));
KJ_ASSERT(newTree != nullptr, "memory allocation failed", newCapacity);
#else
// OSX and Android lack aligned_alloc(), but have posix_memalign(). Fine.
void* allocPtr;
int error = posix_memalign(&allocPtr,
sizeof(BTreeImpl::NodeUnion), newCapacity * sizeof(BTreeImpl::NodeUnion));
if (error != 0) {
KJ_FAIL_SYSCALL("posix_memalign", error);
}
NodeUnion* newTree = reinterpret_cast<NodeUnion*>(allocPtr);
#endif
acopy(newTree, tree, treeCapacity);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment