Logo AND Algorithmique Numérique Distribuée

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