From 939a47fb457128358b1c6553893be26b9b4fe060 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 14 Sep 2021 20:35:30 -0400 Subject: Add aligned allocation interface and use it in ZixBTree --- test/allocator_test.c | 9 +++++++++ test/failing_allocator.c | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+) (limited to 'test') diff --git a/test/allocator_test.c b/test/allocator_test.c index a6087e1..5275ce8 100644 --- a/test/allocator_test.c +++ b/test/allocator_test.c @@ -6,6 +6,7 @@ #include "zix/allocator.h" #include +#include static void test_allocator(void) @@ -40,6 +41,14 @@ test_allocator(void) assert(realloced[6] == 6); assert(realloced[7] == 7); + char* const aligned = (char*)zix_aligned_alloc(allocator, 4096, 4096); + assert((uintptr_t)aligned % 4096 == 0); + aligned[0] = 0; + aligned[3] = 3; + assert(aligned[0] == 0); + assert(aligned[3] == 3); + + zix_aligned_free(allocator, aligned); zix_free(allocator, realloced); zix_free(allocator, malloced); } diff --git a/test/failing_allocator.c b/test/failing_allocator.c index eda762b..684a8ec 100644 --- a/test/failing_allocator.c +++ b/test/failing_allocator.c @@ -66,6 +66,28 @@ zix_failing_free(ZixAllocator* const allocator, void* const ptr) base->free(base, ptr); } +ZIX_MALLOC_FUNC +static void* +zix_failing_aligned_alloc(ZixAllocator* const allocator, + const size_t alignment, + const size_t size) +{ + ZixFailingAllocator* const state = (ZixFailingAllocator*)allocator; + ZixAllocator* const base = zix_default_allocator(); + + return attempt(state) ? base->aligned_alloc(base, alignment, size) : NULL; +} + +static void +zix_failing_aligned_free(ZixAllocator* const allocator, void* const ptr) +{ + (void)allocator; + + ZixAllocator* const base = zix_default_allocator(); + + base->aligned_free(base, ptr); +} + ZIX_CONST_FUNC ZixFailingAllocator zix_failing_allocator(void) @@ -76,6 +98,8 @@ zix_failing_allocator(void) zix_failing_calloc, zix_failing_realloc, zix_failing_free, + zix_failing_aligned_alloc, + zix_failing_aligned_free, }, 0, SIZE_MAX, -- cgit v1.2.1