LockedPool: Fix LockedPool::free(nullptr) to be a no-op

It was documented as such, but nullptr is never an address within an
Arena, so it would fall through and trigger the std::runtime_error.
This commit is contained in:
Jack Grigg 2020-09-28 15:46:00 +01:00
parent b2b5cccf39
commit fa6921c480
2 changed files with 11 additions and 0 deletions

View File

@ -320,6 +320,11 @@ void* LockedPool::alloc(size_t size)
void LockedPool::free(void *ptr)
{
// Freeing the nullptr pointer is OK.
if (ptr == nullptr) {
return;
}
std::lock_guard<std::mutex> lock(mutex);
// TODO we can do better than this linear search by keeping a map of arena
// extents to arena, and looking up the address.

View File

@ -124,6 +124,9 @@ BOOST_AUTO_TEST_CASE(arena_tests)
BOOST_CHECK(b.stats().total == synth_size);
BOOST_CHECK(b.stats().free == synth_size);
// Check that Arena::free may be called on nullptr.
b.free(nullptr);
}
/** Mock LockedPageAllocator for testing */
@ -229,6 +232,9 @@ BOOST_AUTO_TEST_CASE(lockedpool_tests_live)
BOOST_CHECK(pool.stats().total <= (initial.total + LockedPool::ARENA_SIZE));
// Usage must be back to where it started
BOOST_CHECK(pool.stats().used == initial.used);
// Check that LockedPool::free may be called on nullptr.
pool.free(nullptr);
}
BOOST_AUTO_TEST_SUITE_END()