Skip to content

Commit

Permalink
Macros for memory allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
SChernykh committed Oct 22, 2021
1 parent 24b3f50 commit 668936f
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/include/robin_hood.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,19 @@ static Counts& counts() {
# define ROBIN_HOOD_PRIVATE_DEFINITION_NODISCARD()
#endif

// malloc/calloc/free macros
#ifndef ROBIN_HOOD_MALLOC
# define ROBIN_HOOD_MALLOC(size) std::malloc(size)
#endif

#ifndef ROBIN_HOOD_CALLOC
# define ROBIN_HOOD_CALLOC(count, size) std::calloc((count), (size))
#endif

#ifndef ROBIN_HOOD_FREE
# define ROBIN_HOOD_FREE(ptr) std::free(ptr)
#endif

namespace robin_hood {

#if ROBIN_HOOD(CXX) >= ROBIN_HOOD(CXX14)
Expand Down Expand Up @@ -405,7 +418,7 @@ class BulkPoolAllocator {
while (mListForFree) {
T* tmp = *mListForFree;
ROBIN_HOOD_LOG("std::free")
std::free(mListForFree);
ROBIN_HOOD_FREE(mListForFree);
mListForFree = reinterpret_cast_no_cast_align_warning<T**>(tmp);
}
mHead = nullptr;
Expand Down Expand Up @@ -441,7 +454,7 @@ class BulkPoolAllocator {
if (numBytes < ALIGNMENT + ALIGNED_SIZE) {
// not enough data for at least one element. Free and return.
ROBIN_HOOD_LOG("std::free")
std::free(ptr);
ROBIN_HOOD_FREE(ptr);
} else {
ROBIN_HOOD_LOG("add to buffer")
add(ptr, numBytes);
Expand Down Expand Up @@ -510,7 +523,7 @@ class BulkPoolAllocator {
size_t const bytes = ALIGNMENT + ALIGNED_SIZE * numElementsToAlloc;
ROBIN_HOOD_LOG("std::malloc " << bytes << " = " << ALIGNMENT << " + " << ALIGNED_SIZE
<< " * " << numElementsToAlloc)
add(assertNotNull<std::bad_alloc>(std::malloc(bytes)), bytes);
add(assertNotNull<std::bad_alloc>(ROBIN_HOOD_MALLOC(bytes)), bytes);
return mHead;
}

Expand Down Expand Up @@ -547,7 +560,7 @@ struct NodeAllocator<T, MinSize, MaxSize, true> {
// we are not using the data, so just free it.
void addOrFree(void* ptr, size_t ROBIN_HOOD_UNUSED(numBytes) /*unused*/) noexcept {
ROBIN_HOOD_LOG("std::free")
std::free(ptr);
ROBIN_HOOD_FREE(ptr);
}
};

Expand Down Expand Up @@ -1594,7 +1607,7 @@ class Table
<< numElementsWithBuffer << ")")
mHashMultiplier = o.mHashMultiplier;
mKeyVals = static_cast<Node*>(
detail::assertNotNull<std::bad_alloc>(std::malloc(numBytesTotal)));
detail::assertNotNull<std::bad_alloc>(ROBIN_HOOD_MALLOC(numBytesTotal)));
// no need for calloc because clonData does memcpy
mInfo = reinterpret_cast<uint8_t*>(mKeyVals + numElementsWithBuffer);
mNumElements = o.mNumElements;
Expand Down Expand Up @@ -1643,15 +1656,15 @@ class Table
if (0 != mMask) {
// only deallocate if we actually have data!
ROBIN_HOOD_LOG("std::free")
std::free(mKeyVals);
ROBIN_HOOD_FREE(mKeyVals);
}

auto const numElementsWithBuffer = calcNumElementsWithBuffer(o.mMask + 1);
auto const numBytesTotal = calcNumBytesTotal(numElementsWithBuffer);
ROBIN_HOOD_LOG("std::malloc " << numBytesTotal << " = calcNumBytesTotal("
<< numElementsWithBuffer << ")")
mKeyVals = static_cast<Node*>(
detail::assertNotNull<std::bad_alloc>(std::malloc(numBytesTotal)));
detail::assertNotNull<std::bad_alloc>(ROBIN_HOOD_MALLOC(numBytesTotal)));

// no need for calloc here because cloneData performs a memcpy.
mInfo = reinterpret_cast<uint8_t*>(mKeyVals + numElementsWithBuffer);
Expand Down Expand Up @@ -2226,7 +2239,7 @@ class Table
if (oldKeyVals != reinterpret_cast_no_cast_align_warning<Node*>(&mMask)) {
// don't destroy old data: put it into the pool instead
if (forceFree) {
std::free(oldKeyVals);
ROBIN_HOOD_FREE(oldKeyVals);
} else {
DataPool::addOrFree(oldKeyVals, calcNumBytesTotal(oldMaxElementsWithBuffer));
}
Expand Down Expand Up @@ -2313,7 +2326,7 @@ class Table
ROBIN_HOOD_LOG("std::calloc " << numBytesTotal << " = calcNumBytesTotal("
<< numElementsWithBuffer << ")")
mKeyVals = reinterpret_cast<Node*>(
detail::assertNotNull<std::bad_alloc>(std::calloc(1, numBytesTotal)));
detail::assertNotNull<std::bad_alloc>(ROBIN_HOOD_CALLOC(1, numBytesTotal)));
mInfo = reinterpret_cast<uint8_t*>(mKeyVals + numElementsWithBuffer);

// set sentinel
Expand Down Expand Up @@ -2461,7 +2474,7 @@ class Table
// [-Werror=free-nonheap-object]
if (mKeyVals != reinterpret_cast_no_cast_align_warning<Node*>(&mMask)) {
ROBIN_HOOD_LOG("std::free")
std::free(mKeyVals);
ROBIN_HOOD_FREE(mKeyVals);
}
}

Expand Down

0 comments on commit 668936f

Please sign in to comment.