diff options
author | David Robillard <d@drobilla.net> | 2021-09-14 17:19:12 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2021-09-14 17:19:12 -0400 |
commit | 19235b7127bcf5597fb0436deb45c2e6af5843c6 (patch) | |
tree | bb0207a4799f67933cd793e0501d09a10822c5ec /test/failing_allocator.c | |
parent | 9572ea596d07147dbfb6db7772623e740ce28652 (diff) | |
download | zix-19235b7127bcf5597fb0436deb45c2e6af5843c6.tar.gz zix-19235b7127bcf5597fb0436deb45c2e6af5843c6.tar.bz2 zix-19235b7127bcf5597fb0436deb45c2e6af5843c6.zip |
Make ZixAllocator a single flat struct
I can never decide between these two patterns for polymorphic objects in C, but
this one seems more appropriate here since it's more conducive to inheritance.
Diffstat (limited to 'test/failing_allocator.c')
-rw-r--r-- | test/failing_allocator.c | 68 |
1 files changed, 36 insertions, 32 deletions
diff --git a/test/failing_allocator.c b/test/failing_allocator.c index d00f2f1..eda762b 100644 --- a/test/failing_allocator.c +++ b/test/failing_allocator.c @@ -8,73 +8,77 @@ #include <stdbool.h> #include <stddef.h> +#include <stdint.h> static bool -attempt(ZixFailingAllocatorState* const state) +attempt(ZixFailingAllocator* const allocator) { - ++state->n_allocations; + ++allocator->n_allocations; - if (!state->n_remaining) { + if (!allocator->n_remaining) { return false; } - --state->n_remaining; + --allocator->n_remaining; return true; } ZIX_MALLOC_FUNC static void* -zix_failing_malloc(ZixAllocatorHandle* const handle, const size_t size) +zix_failing_malloc(ZixAllocator* const allocator, const size_t size) { - ZixFailingAllocatorState* const state = (ZixFailingAllocatorState*)handle; - const ZixAllocator* const base = zix_default_allocator(); + ZixFailingAllocator* const state = (ZixFailingAllocator*)allocator; + ZixAllocator* const base = zix_default_allocator(); - return attempt(state) ? base->malloc(base->handle, size) : NULL; + return attempt(state) ? base->malloc(base, size) : NULL; } ZIX_MALLOC_FUNC static void* -zix_failing_calloc(ZixAllocatorHandle* const handle, - const size_t nmemb, - const size_t size) +zix_failing_calloc(ZixAllocator* const allocator, + const size_t nmemb, + const size_t size) { - ZixFailingAllocatorState* const state = (ZixFailingAllocatorState*)handle; - const ZixAllocator* const base = zix_default_allocator(); + ZixFailingAllocator* const state = (ZixFailingAllocator*)allocator; + ZixAllocator* const base = zix_default_allocator(); - return attempt(state) ? base->calloc(base->handle, nmemb, size) : NULL; + return attempt(state) ? base->calloc(base, nmemb, size) : NULL; } static void* -zix_failing_realloc(ZixAllocatorHandle* const handle, - void* const ptr, - const size_t size) +zix_failing_realloc(ZixAllocator* const allocator, + void* const ptr, + const size_t size) { - ZixFailingAllocatorState* const state = (ZixFailingAllocatorState*)handle; - const ZixAllocator* const base = zix_default_allocator(); + ZixFailingAllocator* const state = (ZixFailingAllocator*)allocator; + ZixAllocator* const base = zix_default_allocator(); - return attempt(state) ? base->realloc(base->handle, ptr, size) : NULL; + return attempt(state) ? base->realloc(base, ptr, size) : NULL; } static void -zix_failing_free(ZixAllocatorHandle* const handle, void* const ptr) +zix_failing_free(ZixAllocator* const allocator, void* const ptr) { - (void)handle; + (void)allocator; - const ZixAllocator* const base = zix_default_allocator(); + ZixAllocator* const base = zix_default_allocator(); - base->free(base->handle, ptr); + base->free(base, ptr); } ZIX_CONST_FUNC -ZixAllocator -zix_failing_allocator(ZixFailingAllocatorState* const state) +ZixFailingAllocator +zix_failing_allocator(void) { - const ZixAllocator failing_allocator = { - state, - zix_failing_malloc, - zix_failing_calloc, - zix_failing_realloc, - zix_failing_free, + ZixFailingAllocator failing_allocator = { + { + zix_failing_malloc, + zix_failing_calloc, + zix_failing_realloc, + zix_failing_free, + }, + 0, + SIZE_MAX, }; return failing_allocator; |