summaryrefslogtreecommitdiffstats
path: root/test/test_malloc.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-09-14 21:41:48 -0400
committerDavid Robillard <d@drobilla.net>2021-09-14 23:55:44 -0400
commit80b01dd8344c2469e943726769c5ec6b8fc53401 (patch)
tree1acda5da5943d136bc74007bd5bed16abda7de36 /test/test_malloc.c
parent939a47fb457128358b1c6553893be26b9b4fe060 (diff)
downloadzix-80b01dd8344c2469e943726769c5ec6b8fc53401.tar.gz
zix-80b01dd8344c2469e943726769c5ec6b8fc53401.tar.bz2
zix-80b01dd8344c2469e943726769c5ec6b8fc53401.zip
Replace shared library malloc shim with explicit allocation testing
The old approach was generally annoying to deal with sometimes, and not particularly portable. This replaces it by using the new custom allocator interface with unit tests that specifically check that failed allocation is handled properly.
Diffstat (limited to 'test/test_malloc.c')
-rw-r--r--test/test_malloc.c84
1 files changed, 0 insertions, 84 deletions
diff --git a/test/test_malloc.c b/test/test_malloc.c
deleted file mode 100644
index 4274af2..0000000
--- a/test/test_malloc.c
+++ /dev/null
@@ -1,84 +0,0 @@
-// Copyright 2014-2020 David Robillard <d@drobilla.net>
-// SPDX-License-Identifier: ISC
-
-#ifndef __APPLE__
-# define _GNU_SOURCE
-#endif
-
-#include "test_malloc.h"
-
-#include <dlfcn.h>
-#include <stdbool.h>
-#include <stdlib.h>
-#include <string.h>
-
-static void* (*test_malloc_sys_malloc)(size_t size) = NULL;
-
-static size_t test_malloc_n_allocs = 0;
-static size_t test_malloc_fail_after = (size_t)-1;
-static volatile bool in_test_malloc_init = false;
-
-static void*
-test_malloc(size_t size)
-{
- if (in_test_malloc_init) {
- return NULL; // dlsym is asking for memory, but handles this fine
- }
-
- if (!test_malloc_sys_malloc) {
- test_malloc_init();
- }
-
- if (test_malloc_n_allocs < test_malloc_fail_after) {
- ++test_malloc_n_allocs;
- return test_malloc_sys_malloc(size);
- }
-
- return NULL;
-}
-
-void*
-malloc(size_t size)
-{
- return test_malloc(size);
-}
-
-void*
-calloc(size_t nmemb, size_t size)
-{
- void* ptr = test_malloc(nmemb * size);
- if (ptr) {
- memset(ptr, 0, nmemb * size);
- }
- return ptr;
-}
-
-void
-test_malloc_reset(size_t fail_after)
-{
- test_malloc_fail_after = fail_after;
- test_malloc_n_allocs = 0;
-}
-
-void
-test_malloc_init(void)
-{
- in_test_malloc_init = true;
-
- /* Avoid pedantic warnings about casting pointer to function pointer by
- casting dlsym instead. */
-
- typedef void* (*MallocFunc)(size_t);
- typedef MallocFunc (*MallocFuncGetter)(void*, const char*);
-
- MallocFuncGetter dlfunc = (MallocFuncGetter)dlsym;
- test_malloc_sys_malloc = (MallocFunc)dlfunc(RTLD_NEXT, "malloc");
-
- in_test_malloc_init = false;
-}
-
-size_t
-test_malloc_get_n_allocs(void)
-{
- return test_malloc_n_allocs;
-}