diff options
author | David Robillard <d@drobilla.net> | 2014-12-18 21:52:28 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2014-12-18 21:52:28 +0000 |
commit | 17ea1744b6b98a2f94d2311b4f25896dcfe01f3f (patch) | |
tree | 8a4849e5b9d8551f50fd8264a630b1554efbc094 /test/test_malloc.c | |
parent | f683a30a854af0d24c37ecba4ed848d29bf5a4ea (diff) | |
download | zix-17ea1744b6b98a2f94d2311b4f25896dcfe01f3f.tar.gz zix-17ea1744b6b98a2f94d2311b4f25896dcfe01f3f.tar.bz2 zix-17ea1744b6b98a2f94d2311b4f25896dcfe01f3f.zip |
Add missing files.
git-svn-id: http://svn.drobilla.net/zix/trunk@99 df6676b4-ccc9-40e5-b5d6-7c4628a128e3
Diffstat (limited to 'test/test_malloc.c')
-rw-r--r-- | test/test_malloc.c | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/test/test_malloc.c b/test/test_malloc.c new file mode 100644 index 0000000..9b430fe --- /dev/null +++ b/test/test_malloc.c @@ -0,0 +1,90 @@ +/* + Copyright 2014 David Robillard <http://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. +*/ + +#define _GNU_SOURCE + +#include <dlfcn.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> + +#include <assert.h> +#include <stdio.h> + +#include "test_malloc.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; + +void* +malloc(size_t size) +{ + if (in_test_malloc_init) { + return NULL; // dlsym is asking for memory, but handles this fine + } else 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* +calloc(size_t nmemb, size_t size) +{ + void* ptr = 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; +} |