From cf3b3048da0f80d3643ba0a9bc61a29ca5c5cf8b Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 10 Sep 2021 20:54:08 -0400 Subject: Test failed Ring allocation --- test/failing_allocator.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 test/failing_allocator.c (limited to 'test/failing_allocator.c') diff --git a/test/failing_allocator.c b/test/failing_allocator.c new file mode 100644 index 0000000..bb569de --- /dev/null +++ b/test/failing_allocator.c @@ -0,0 +1,94 @@ +/* + Copyright 2021 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "failing_allocator.h" + +#include "zix/allocator.h" +#include "zix/attributes.h" + +#include +#include + +static bool +attempt(ZixFailingAllocatorState* const state) +{ + ++state->n_allocations; + + if (!state->n_remaining) { + return false; + } + + --state->n_remaining; + return true; +} + +ZIX_MALLOC_FUNC +static void* +zix_failing_malloc(ZixAllocatorHandle* const handle, const size_t size) +{ + ZixFailingAllocatorState* const state = (ZixFailingAllocatorState*)handle; + const ZixAllocator* const base = zix_default_allocator(); + + return attempt(state) ? base->malloc(base->handle, size) : NULL; +} + +ZIX_MALLOC_FUNC +static void* +zix_failing_calloc(ZixAllocatorHandle* const handle, + const size_t nmemb, + const size_t size) +{ + ZixFailingAllocatorState* const state = (ZixFailingAllocatorState*)handle; + const ZixAllocator* const base = zix_default_allocator(); + + return attempt(state) ? base->calloc(base->handle, nmemb, size) : NULL; +} + +static void* +zix_failing_realloc(ZixAllocatorHandle* const handle, + void* const ptr, + const size_t size) +{ + ZixFailingAllocatorState* const state = (ZixFailingAllocatorState*)handle; + const ZixAllocator* const base = zix_default_allocator(); + + return attempt(state) ? base->realloc(base->handle, ptr, size) : NULL; +} + +static void +zix_failing_free(ZixAllocatorHandle* const handle, void* const ptr) +{ + (void)handle; + + const ZixAllocator* const base = zix_default_allocator(); + + base->free(base->handle, ptr); +} + +ZIX_CONST_FUNC +ZixAllocator +zix_failing_allocator(ZixFailingAllocatorState* const state) +{ + const ZixAllocator failing_allocator = { + state, + zix_failing_malloc, + zix_failing_calloc, + zix_failing_realloc, + zix_failing_free, + }; + + return failing_allocator; +} -- cgit v1.2.1