aboutsummaryrefslogtreecommitdiffstats
path: root/test/failing_allocator.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-10-27 14:15:31 -0400
committerDavid Robillard <d@drobilla.net>2022-01-28 21:57:24 -0500
commit30487c277ac5d4be5786733ca7b98adb4c810ae9 (patch)
treef1b00a7725d74a594fcd91de2aea924485356528 /test/failing_allocator.c
parent56cceb103dc633d6af957472945e792187a5dd4e (diff)
downloadserd-30487c277ac5d4be5786733ca7b98adb4c810ae9.tar.gz
serd-30487c277ac5d4be5786733ca7b98adb4c810ae9.tar.bz2
serd-30487c277ac5d4be5786733ca7b98adb4c810ae9.zip
Add custom allocator support
Diffstat (limited to 'test/failing_allocator.c')
-rw-r--r--test/failing_allocator.c121
1 files changed, 121 insertions, 0 deletions
diff --git a/test/failing_allocator.c b/test/failing_allocator.c
new file mode 100644
index 00000000..e04f3eaf
--- /dev/null
+++ b/test/failing_allocator.c
@@ -0,0 +1,121 @@
+/*
+ Copyright 2021 David Robillard <d@drobilla.net>
+
+ 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 "serd/serd.h"
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+static bool
+attempt(SerdFailingAllocator* const allocator)
+{
+ ++allocator->n_allocations;
+
+ if (!allocator->n_remaining) {
+ return false;
+ }
+
+ --allocator->n_remaining;
+ return true;
+}
+
+SERD_MALLOC_FUNC
+static void*
+serd_failing_malloc(SerdAllocator* const allocator, const size_t size)
+{
+ SerdFailingAllocator* const state = (SerdFailingAllocator*)allocator;
+ SerdAllocator* const base = serd_default_allocator();
+
+ return attempt(state) ? base->malloc(base, size) : NULL;
+}
+
+SERD_MALLOC_FUNC
+static void*
+serd_failing_calloc(SerdAllocator* const allocator,
+ const size_t nmemb,
+ const size_t size)
+{
+ SerdFailingAllocator* const state = (SerdFailingAllocator*)allocator;
+ SerdAllocator* const base = serd_default_allocator();
+
+ return attempt(state) ? base->calloc(base, nmemb, size) : NULL;
+}
+
+static void*
+serd_failing_realloc(SerdAllocator* const allocator,
+ void* const ptr,
+ const size_t size)
+{
+ SerdFailingAllocator* const state = (SerdFailingAllocator*)allocator;
+ SerdAllocator* const base = serd_default_allocator();
+
+ return attempt(state) ? base->realloc(base, ptr, size) : NULL;
+}
+
+static void
+serd_failing_free(SerdAllocator* const allocator, void* const ptr)
+{
+ (void)allocator;
+
+ SerdAllocator* const base = serd_default_allocator();
+
+ base->free(base, ptr);
+}
+
+SERD_MALLOC_FUNC
+static void*
+serd_failing_aligned_alloc(SerdAllocator* const allocator,
+ const size_t alignment,
+ const size_t size)
+{
+ SerdFailingAllocator* const state = (SerdFailingAllocator*)allocator;
+ SerdAllocator* const base = serd_default_allocator();
+
+ return attempt(state) ? base->aligned_alloc(base, alignment, size) : NULL;
+}
+
+static void
+serd_failing_aligned_free(SerdAllocator* const allocator, void* const ptr)
+{
+ (void)allocator;
+
+ SerdAllocator* const base = serd_default_allocator();
+
+ base->aligned_free(base, ptr);
+}
+
+SERD_CONST_FUNC
+SerdFailingAllocator
+serd_failing_allocator(void)
+{
+ SerdFailingAllocator failing_allocator = {
+ {
+ serd_failing_malloc,
+ serd_failing_calloc,
+ serd_failing_realloc,
+ serd_failing_free,
+ serd_failing_aligned_alloc,
+ serd_failing_aligned_free,
+ },
+ 0,
+ SIZE_MAX,
+ };
+
+ return failing_allocator;
+}