2 malloc.c -- override *alloc() to allow testing special cases
3 Copyright (C) 2015 Dieter Baron and Thomas Klausner
5 This file is part of ckmame, a program to check rom sets for MAME.
6 The authors can be contacted at <ckmame@nih.at>
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in
15 the documentation and/or other materials provided with the
17 3. The name of the author may not be used to endorse or promote
18 products derived from this software without specific prior
21 THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
22 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 /* #include <string.h> */
44 #if !defined(RTLD_NEXT)
45 #define RTLD_NEXT RTLD_DEFAULT
48 #if defined(HAVE___PROGNAME)
49 extern char *__progname;
52 #if defined(HAVE_GETPROGNAME)
58 #if defined(HAVE___PROGNAME)
66 static int inited = 0;
67 static size_t count = 0;
68 static size_t max_count = 0;
69 static size_t min_size = 0;
70 static void *(*real_malloc)(size_t size) = NULL;
71 static void *(*real_calloc)(size_t number, size_t size) = NULL;
72 static void *(*real_realloc)(void *ptr, size_t size) = NULL;
74 static const char *myname = NULL;
76 /* TODO: add sentinel -- check if particular size is malloced before doing other stuff */
77 /* TODO: catch free for sentinel too */
78 /* TODO: optionally, catch malloc of particular size */
84 myname = getprogname();
87 if ((foo=getenv("MALLOC_MAX_COUNT")) != NULL)
88 max_count = strtoul(foo, NULL, 0);
89 if ((foo=getenv("MALLOC_MIN_SIZE")) != NULL)
90 min_size = strtoul(foo, NULL, 0);
91 real_calloc = dlsym(RTLD_NEXT, "calloc");
94 real_malloc = dlsym(RTLD_NEXT, "malloc");
97 real_realloc = dlsym(RTLD_NEXT, "realloc");
104 calloc(size_t number, size_t size)
112 if (number >= min_size/size && count >= max_count) {
117 ret = real_calloc(number, size);
118 if (size >= min_size) {
134 if (size >= min_size && count >= max_count) {
139 ret = real_malloc(size);
140 if (size >= min_size) {
148 realloc(void *ptr, size_t size)
156 if (size >= min_size && count >= max_count) {
161 ret = real_realloc(ptr, size);
162 if (size >= min_size) {