malloc: add exception info to function header
[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 #include <new>
10
11 static void * (*real_malloc)(size_t) = NULL;
12 static void (*real_free)(void *ptr) = NULL;
13
14 static void __my_alloc_init(void)
15 {
16
17         real_malloc = (void *(*)(size_t))dlsym(RTLD_NEXT, "malloc");
18         real_free = (void (*)(void *))dlsym(RTLD_NEXT, "free");
19         if (real_malloc == NULL || real_free == NULL) {
20                 fprintf(stderr, "Error in `dlsym`: %s\n", dlerror());
21                 return;
22         }
23 }
24
25 void * myMalloc(size_t size)
26 {
27         if (real_malloc == NULL)
28                 __my_alloc_init();
29
30         return real_malloc(size);
31 }
32
33 void myFree(void *ptr)
34 {
35         if (real_free == NULL)
36                 __my_alloc_init();
37
38         real_free(ptr);
39 }
40
41 void * operator new(size_t size) throw(std::bad_alloc)
42 {
43         return myMalloc(size);
44 }
45
46 void operator delete(void *p) throw()
47 {
48         myFree(p);
49 }