Pass continuations for success and failure cases (#788)
This PR provides a templated parameter to the allocation routines. This can be used to add special behaviour in both the successful allocation behaviour, and in the failing to allocate cases. The intent of this is two enable two future features * set_new_handler - so that the failure case doesn't just set ENOMEM, and return nullptr. But can handle both the Windows and C++ versions of (_)set_new_handler. * The success handler can be used to add checking, zeroing and in the future storing precise size information in metadata for each allocation.
This commit is contained in:
committed by
GitHub
parent
012138e29f
commit
d4c7e01cd7
@@ -5,20 +5,21 @@
|
||||
|
||||
using namespace snmalloc;
|
||||
|
||||
template<ZeroMem zero_mem>
|
||||
template<typename Conts>
|
||||
void test_alloc_dealloc(size_t count, size_t size, bool write)
|
||||
{
|
||||
{
|
||||
MeasureTime m;
|
||||
m << "Count: " << std::setw(6) << count << ", Size: " << std::setw(6)
|
||||
<< size << ", ZeroMem: " << (zero_mem == YesZero) << ", Write: " << write;
|
||||
<< size
|
||||
<< ", ZeroMem: " << std::is_same_v<Conts, Zero> << ", Write: " << write;
|
||||
|
||||
std::unordered_set<void*> set;
|
||||
|
||||
// alloc 1.5x objects
|
||||
for (size_t i = 0; i < ((count * 3) / 2); i++)
|
||||
{
|
||||
void* p = snmalloc::alloc<zero_mem>(size);
|
||||
void* p = snmalloc::alloc<Conts>(size);
|
||||
SNMALLOC_CHECK(set.find(p) == set.end());
|
||||
|
||||
if (write)
|
||||
@@ -40,7 +41,7 @@ void test_alloc_dealloc(size_t count, size_t size, bool write)
|
||||
// alloc 1x objects
|
||||
for (size_t i = 0; i < count; i++)
|
||||
{
|
||||
void* p = snmalloc::alloc<zero_mem>(size);
|
||||
void* p = snmalloc::alloc<Conts>(size);
|
||||
SNMALLOC_CHECK(set.find(p) == set.end());
|
||||
|
||||
if (write)
|
||||
@@ -67,18 +68,18 @@ int main(int, char**)
|
||||
|
||||
for (size_t size = 16; size <= 128; size <<= 1)
|
||||
{
|
||||
test_alloc_dealloc<NoZero>(1 << 15, size, false);
|
||||
test_alloc_dealloc<NoZero>(1 << 15, size, true);
|
||||
test_alloc_dealloc<YesZero>(1 << 15, size, false);
|
||||
test_alloc_dealloc<YesZero>(1 << 15, size, true);
|
||||
test_alloc_dealloc<Uninit>(1 << 15, size, false);
|
||||
test_alloc_dealloc<Uninit>(1 << 15, size, true);
|
||||
test_alloc_dealloc<Zero>(1 << 15, size, false);
|
||||
test_alloc_dealloc<Zero>(1 << 15, size, true);
|
||||
}
|
||||
|
||||
for (size_t size = 1 << 12; size <= 1 << 17; size <<= 1)
|
||||
{
|
||||
test_alloc_dealloc<NoZero>(1 << 10, size, false);
|
||||
test_alloc_dealloc<NoZero>(1 << 10, size, true);
|
||||
test_alloc_dealloc<YesZero>(1 << 10, size, false);
|
||||
test_alloc_dealloc<YesZero>(1 << 10, size, true);
|
||||
test_alloc_dealloc<Uninit>(1 << 10, size, false);
|
||||
test_alloc_dealloc<Uninit>(1 << 10, size, true);
|
||||
test_alloc_dealloc<Zero>(1 << 10, size, false);
|
||||
test_alloc_dealloc<Zero>(1 << 10, size, true);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user