diff options
author | David Robillard <d@drobilla.net> | 2021-09-15 23:07:06 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2021-09-16 15:04:18 -0400 |
commit | dcd887eb8326ec195149c7fd6711ddb715c4ad1f (patch) | |
tree | 48ee1bf8f6c02158527cb5eecee02ffc44f0c327 /test | |
parent | 1f5c42737fff4f222be0edb628fede2eb4dfeb91 (diff) | |
download | zix-dcd887eb8326ec195149c7fd6711ddb715c4ad1f.tar.gz zix-dcd887eb8326ec195149c7fd6711ddb715c4ad1f.tar.bz2 zix-dcd887eb8326ec195149c7fd6711ddb715c4ad1f.zip |
Add a simple bump pointer allocator
Diffstat (limited to 'test')
-rw-r--r-- | test/allocator_test.c | 60 | ||||
-rw-r--r-- | test/ring_test.c | 1 |
2 files changed, 60 insertions, 1 deletions
diff --git a/test/allocator_test.c b/test/allocator_test.c index 5275ce8..b4aec16 100644 --- a/test/allocator_test.c +++ b/test/allocator_test.c @@ -4,6 +4,7 @@ #undef NDEBUG #include "zix/allocator.h" +#include "zix/bump_allocator.h" #include <assert.h> #include <stdint.h> @@ -53,9 +54,68 @@ test_allocator(void) zix_free(allocator, malloced); } +static void +test_bump_allocator(void) +{ + char buffer[1024] = {0}; + ZixBumpAllocator allocator = zix_bump_allocator(sizeof(buffer), buffer); + + assert(!zix_malloc(&allocator.base, 1025)); + + char* const malloced = (char*)zix_malloc(&allocator.base, 3); + assert(malloced >= buffer); + assert(malloced <= buffer + sizeof(buffer)); + assert((uintptr_t)malloced % sizeof(uintmax_t) == 0u); + + assert(!zix_calloc(&allocator.base, 1017, 1)); + + char* const calloced = (char*)zix_calloc(&allocator.base, 4, 1); + assert(calloced > malloced); + assert(calloced <= buffer + sizeof(buffer)); + assert((uintptr_t)calloced % sizeof(uintmax_t) == 0u); + assert(!calloced[0]); + assert(!calloced[1]); + assert(!calloced[2]); + assert(!calloced[3]); + + char* const realloced = (char*)zix_realloc(&allocator.base, calloced, 8); + assert(realloced == calloced); + + assert(!zix_realloc(&allocator.base, malloced, 8)); // Not the top + assert(!zix_realloc(&allocator.base, realloced, 4089)); // No space + + zix_free(&allocator.base, realloced); + + char* const reclaimed = (char*)zix_malloc(&allocator.base, 512); + assert(reclaimed); + assert(reclaimed == realloced); + + assert(!zix_aligned_alloc(&allocator.base, sizeof(uintmax_t), 1024)); + assert(!zix_aligned_alloc(&allocator.base, 1024, 1024)); + assert(!zix_aligned_alloc(&allocator.base, 2048, 2048)); + assert(!zix_aligned_alloc(&allocator.base, 4096, 4096)); + assert(!zix_aligned_alloc(&allocator.base, 8192, 8192)); + assert(!zix_aligned_alloc(&allocator.base, 4096, 4096)); + assert(!zix_aligned_alloc(&allocator.base, 2048, 2048)); + assert(!zix_aligned_alloc(&allocator.base, 1024, 1024)); + assert(!zix_aligned_alloc(&allocator.base, 512, 512)); + + char* const aligned = (char*)zix_aligned_alloc(&allocator.base, 128, 128); + assert(aligned); + assert(aligned >= reclaimed); + assert(aligned <= buffer + sizeof(buffer)); + assert((uintptr_t)aligned % 128 == 0u); + + zix_aligned_free(&allocator.base, aligned); + zix_free(&allocator.base, reclaimed); // Correct, but a noop + zix_free(&allocator.base, malloced); // Correct, but a noop +} + int main(void) { test_allocator(); + test_bump_allocator(); + return 0; } diff --git a/test/ring_test.c b/test/ring_test.c index ba2a4d1..2b87efa 100644 --- a/test/ring_test.c +++ b/test/ring_test.c @@ -5,7 +5,6 @@ #include "failing_allocator.h" -#include "zix/allocator.h" #include "zix/attributes.h" #include "zix/ring.h" #include "zix/thread.h" |