diff options
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; |