Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : comment backtrace and add ignore mechanism in mmalloc metadata
[simgrid.git] / src / xbt / mmalloc / mm_legacy.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /* Redefine the classical malloc/free/realloc functions so that they fit well in the mmalloc framework */
8
9 #include "mmprivate.h"
10 #include "internal_config.h"
11 #include <math.h>
12
13 //#define MM_LEGACY_VERBOSE 1 /* define this to see which version of malloc gets used */
14
15 /* The mmalloc() package can use a single implicit malloc descriptor
16    for mmalloc/mrealloc/mfree operations which do not supply an explicit
17    descriptor.  This allows mmalloc() to provide
18    backwards compatibility with the non-mmap'd version. */
19 xbt_mheap_t __mmalloc_default_mdp = NULL;
20
21
22 static xbt_mheap_t __mmalloc_current_heap = NULL;     /* The heap we are currently using. */
23
24 xbt_mheap_t mmalloc_get_current_heap(void)
25 {
26   return __mmalloc_current_heap;
27 }
28
29 void mmalloc_set_current_heap(xbt_mheap_t new_heap)
30 {
31   __mmalloc_current_heap = new_heap;
32 }
33
34
35 #ifdef MMALLOC_WANT_OVERRIDE_LEGACY
36 #ifdef HAVE_GNU_LD
37
38 #undef _GNU_SOURCE
39 #define _GNU_SOURCE 1
40 #include <dlfcn.h>
41
42 static void * (*real_malloc) (size_t);
43 static void * (*real_realloc) (void*,size_t);
44 static void * (*real_free) (void*);
45
46 static void mm_gnuld_legacy_init(void) { /* This function is called from mmalloc_preinit(); it works even if it's static because all mm is in mm.c */
47   real_malloc = (void * (*) (size_t)) dlsym(RTLD_NEXT, "malloc");
48   real_realloc = (void * (*) (void*,size_t)) dlsym(RTLD_NEXT, "realloc");
49   real_free = (void * (*) (void*)) dlsym(RTLD_NEXT, "free");
50   __mmalloc_current_heap = __mmalloc_default_mdp;
51 }
52
53 /* Hello pimple!
54  * DL needs some memory while resolving the malloc symbol, that is somehow problematic
55  * To that extend, we have a little area here living in .BSS that we return if asked for memory before the malloc is resolved.
56  */
57 int allocated_junk=0; /* keep track of whether our little area was already given to someone */
58 char junkarea[512];
59
60 /* This version use mmalloc if there is a current heap, or the legacy implem if not */
61 void *malloc(size_t n) {
62   xbt_mheap_t mdp = __mmalloc_current_heap;
63   void *ret;
64 #ifdef MM_LEGACY_VERBOSE
65   static int warned_raw = 0;
66   static int warned_mmalloc = 0;
67 #endif
68
69   if (mdp) {
70     LOCK(mdp);
71     ret = mmalloc(mdp, n);
72     UNLOCK(mdp);
73 #ifdef MM_LEGACY_VERBOSE
74     if (!warned_mmalloc) {
75       fprintf(stderr,"Using mmalloc; enabling the model-checker in cmake may have a bad impact on your simulation performance\n");
76       warned_mmalloc = 1;
77     }
78 #endif
79   } else {
80     if (!real_malloc) {
81       if (allocated_junk) {
82         fprintf(stderr,
83             "Panic: real malloc symbol not resolved yet, and I already gave my little private memory chunk away. "
84             "Damn LD, we must extend our code to have several such areas.\n");
85         exit(1);
86       } else if (n>512) {
87         fprintf(stderr,
88             "Panic: real malloc symbol not resolved yet, and I need %zu bytes while my little private memory chunk is only 512 bytes wide. "
89             "Damn LD, we must fix our code to extend this area.\n",n);
90         exit(1);
91       } else {
92         allocated_junk = 1;
93         return junkarea;
94       }
95     }
96 #ifdef MM_LEGACY_VERBOSE
97     if (!warned_raw) {
98       fprintf(stderr,"Using system malloc after interception; you seem to be currently model-checking\n");
99       warned_raw = 1;
100     }
101 #endif
102     ret = real_malloc(n);
103   }
104   return ret;
105 }
106
107
108 void *calloc(size_t nmemb, size_t size)
109 {
110   void *ret = malloc(nmemb*size);
111   memset(ret, 0, nmemb * size);
112   return ret;
113 }
114
115 void *realloc(void *p, size_t s)
116 {
117   xbt_mheap_t mdp = __mmalloc_current_heap;
118   void *ret;
119
120   if (mdp) {
121     LOCK(mdp);
122     ret = mrealloc(mdp, p, s);
123     UNLOCK(mdp);
124   } else {
125     ret = real_realloc(p,s);
126   }
127
128   return ret;
129 }
130
131 void free(void *p)
132 {
133   if (p==NULL)
134     return;
135   if (p!=junkarea) {
136     xbt_mheap_t mdp = __mmalloc_current_heap;
137
138     if (mdp) {
139       LOCK(mdp);
140       mfree(mdp, p);
141       UNLOCK(mdp);
142     } else {
143       real_free(p);
144     }
145   } else {
146     allocated_junk=0;
147   }
148 }
149
150
151 #else /* NO GNU_LD */
152 void *malloc(size_t n)
153 {
154   xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
155
156   LOCK(mdp);
157   void *ret = mmalloc(mdp, n);
158   UNLOCK(mdp);
159
160   return ret;
161 }
162
163 void *calloc(size_t nmemb, size_t size)
164 {
165   xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
166
167   LOCK(mdp);
168   void *ret = mmalloc(mdp, nmemb*size);
169   UNLOCK(mdp);
170   memset(ret, 0, nmemb * size);
171
172
173   return ret;
174 }
175
176 void *realloc(void *p, size_t s)
177 {
178   void *ret = NULL;
179   xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
180
181   LOCK(mdp);
182   ret = mrealloc(mdp, p, s);
183   UNLOCK(mdp);
184
185   return ret;
186 }
187
188 void free(void *p)
189 {
190   if (p != NULL) {
191     xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
192
193     LOCK(mdp);
194     mfree(mdp, p);
195     UNLOCK(mdp);
196   }
197 }
198 #endif /* NO GNU_LD */
199 #endif /* WANT_MALLOC_OVERRIDE */
200
201