malloc: add myMalloc() and myFree()
[model-checker.git] / malloc.c
1 #include "common.h"
2
3 /* for RTLD_NEXT */
4 #ifndef __USE_GNU
5 #define __USE_GNU
6 #endif
7
8 #include <dlfcn.h>
9
10 static void * (*real_malloc)(size_t) = NULL;
11 static void (*real_free)(void *ptr) = NULL;
12
13 static void __my_alloc_init(void)
14 {
15
16         real_malloc = (void *(*)(size_t))dlsym(RTLD_NEXT, "malloc");
17         real_free = (void (*)(void *))dlsym(RTLD_NEXT, "free");
18         if (real_malloc == NULL || real_free == NULL) {
19                 fprintf(stderr, "Error in `dlsym`: %s\n", dlerror());
20                 return;
21         }
22 }
23
24 void *myMalloc(size_t size)
25 {
26         if (real_malloc == NULL)
27                 __my_alloc_init();
28
29         return real_malloc(size);
30 }
31
32 void myFree(void *ptr)
33 {
34         if (real_free == NULL)
35                 __my_alloc_init();
36
37         real_free(ptr);
38 }