Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Check for a dlfunc() function to get handle if present
[simgrid.git] / src / xbt / mmalloc / mm_legacy.c
1 /* Copyright (c) 2010-2015. 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 #define _GNU_SOURCE
9
10 #include <stdlib.h>
11
12 #include <dlfcn.h>
13
14 #include "src/mc/mc_base.h"
15 #include "mmprivate.h"
16 #include "src/xbt_modinter.h"
17 #include "src/internal_config.h"
18 #include <math.h>
19 #include "src/mc/mc_protocol.h"
20
21 /* ***** Whether to use `mmalloc` of the undrlying malloc ***** */
22
23 static int __malloc_use_mmalloc;
24
25 int malloc_use_mmalloc(void)
26 {
27   return __malloc_use_mmalloc;
28 }
29
30 /* ***** Current heap ***** */
31
32 /* The mmalloc() package can use a single implicit malloc descriptor
33    for mmalloc/mrealloc/mfree operations which do not supply an explicit
34    descriptor.  This allows mmalloc() to provide
35    backwards compatibility with the non-mmap'd version. */
36 xbt_mheap_t __mmalloc_default_mdp = NULL;
37
38 /* The heap we are currently using. */
39 static xbt_mheap_t __mmalloc_current_heap = NULL;
40
41 xbt_mheap_t mmalloc_get_current_heap(void)
42 {
43   return __mmalloc_current_heap;
44 }
45
46 xbt_mheap_t mmalloc_set_current_heap(xbt_mheap_t new_heap)
47 {
48   xbt_mheap_t heap = __mmalloc_current_heap;
49   __mmalloc_current_heap = new_heap;
50   return heap;
51 }
52
53 /* Override the malloc-like functions if MC is activated at compile time */
54 #if HAVE_MC
55
56 /* ***** Temporary allocator
57  *
58  * This is used before we have found the real malloc implementation with dlsym.
59  */
60
61 #define BUFFER_SIZE 32
62 static size_t fake_alloc_index;
63 static uint64_t buffer[BUFFER_SIZE];
64
65 /* Fake implementations, they are used to fool dlsym:
66  * dlsym used calloc and falls back to some other mechanism
67  * if this fails.
68  */
69 static void* mm_fake_malloc(size_t n)
70 {
71   // How many uint64_t do w need?
72   size_t count = n / sizeof(uint64_t);
73   if (n % sizeof(uint64_t))
74     count++;
75   // Check that we have enough availabel memory:
76   if (fake_alloc_index + count >= BUFFER_SIZE)
77     exit(127);
78   // Allocate it:
79   uint64_t* res = buffer + fake_alloc_index;
80   fake_alloc_index += count;
81   return res;
82 }
83
84 static void* mm_fake_calloc(size_t nmemb, size_t size)
85 {
86   // This is fresh .bss data, we don't need to clear it:
87   size_t n = nmemb * size;
88   return mm_fake_malloc(n);
89 }
90
91 static void* mm_fake_realloc(void *p, size_t s)
92 {
93   return mm_fake_malloc(s);
94 }
95
96 static void mm_fake_free(void *p)
97 {
98 }
99
100 /* Function signatures for the main malloc functions: */
101 typedef void* (*mm_malloc_t)(size_t size);
102 typedef void  (*mm_free_t)(void*);
103 typedef void* (*mm_calloc_t)(size_t nmemb, size_t size);
104 typedef void* (*mm_realloc_t)(void *ptr, size_t size);
105
106 /* Function pointers to the real/next implementations: */
107 static mm_malloc_t mm_real_malloc;
108 static mm_free_t mm_real_free;
109 static mm_calloc_t mm_real_calloc;
110 static mm_realloc_t mm_real_realloc;
111
112 static int mm_initializing;
113 static int mm_initialized;
114
115 /** Constructor functions used to initialize the malloc implementation
116  */
117 static void __attribute__((constructor(101))) mm_legacy_constructor()
118 {
119   if (mm_initialized)
120     return;
121   mm_initializing = 1;
122   __malloc_use_mmalloc = getenv(MC_ENV_VARIABLE) ? 1 : 0;
123   if (__malloc_use_mmalloc) {
124     __mmalloc_current_heap = mmalloc_preinit();
125   } else {
126 #if HAVE_DLFUNC
127     mm_real_realloc  = (void *(*)(void *, size_t))dlfunc(RTLD_NEXT, "realloc");
128     mm_real_malloc   = (void *(*)(size_t))dlfunc(RTLD_NEXT, "malloc");
129     mm_real_free     = (void (*)(void *))dlfunc(RTLD_NEXT, "free");
130     mm_real_calloc   = (void *(*)(size_t, size_t))dlfunc(RTLD_NEXT, "calloc");
131 #else
132     mm_real_realloc  = dlsym(RTLD_NEXT, "realloc");
133     mm_real_malloc   = dlsym(RTLD_NEXT, "malloc");
134     mm_real_free     = dlsym(RTLD_NEXT, "free");
135     mm_real_calloc   = dlsym(RTLD_NEXT, "calloc");
136 #endif
137   }
138   mm_initializing = 0;
139   mm_initialized = 1;
140 }
141
142 /* ***** malloc/free implementation
143  *
144  * They call either the underlying/native/RTLD_NEXT implementation (non MC mode)
145  * or the mm implementation (MC mode).
146  *
147  * If we are initializing the malloc subsystem, we call the fake/dummy `malloc`
148  * implementation. This is necessary because `dlsym` calls `malloc` and friends.
149  */
150
151 #define GET_HEAP() __mmalloc_current_heap
152
153 void* malloc_no_memset(size_t n)
154 {
155   if (!mm_initialized) {
156     if (mm_initializing)
157       return mm_fake_malloc(n);
158     mm_legacy_constructor();
159   }
160
161   if (!__malloc_use_mmalloc) {
162     return mm_real_malloc(n);
163   }
164
165   xbt_mheap_t mdp = GET_HEAP();
166   if (!mdp)
167     return NULL;
168
169   LOCK(mdp);
170   void *ret = mmalloc_no_memset(mdp, n);
171   UNLOCK(mdp);
172   return ret;
173 }
174
175 void *malloc(size_t n)
176 {
177   if (!mm_initialized) {
178     if (mm_initializing)
179       return mm_fake_malloc(n);
180     mm_legacy_constructor();
181   }
182
183   if (!__malloc_use_mmalloc) {
184     return mm_real_malloc(n);
185   }
186
187   xbt_mheap_t mdp = GET_HEAP();
188   if (!mdp)
189     return NULL;
190
191   LOCK(mdp);
192   void *ret = mmalloc(mdp, n);
193   UNLOCK(mdp);
194   return ret;
195 }
196
197 void *calloc(size_t nmemb, size_t size)
198 {
199   if (!mm_initialized) {
200     if (mm_initializing)
201       return mm_fake_calloc(nmemb, size);
202     mm_legacy_constructor();
203   }
204
205   if (!__malloc_use_mmalloc) {
206     return mm_real_calloc(nmemb, size);
207   }
208
209   xbt_mheap_t mdp = GET_HEAP();
210   if (!mdp)
211     return NULL;
212
213   LOCK(mdp);
214   void *ret = mmalloc(mdp, nmemb*size);
215   UNLOCK(mdp);
216   // This was already done in the callee:
217   if(!(mdp->options & XBT_MHEAP_OPTION_MEMSET)) {
218     memset(ret, 0, nmemb * size);
219   }
220   return ret;
221 }
222
223 void *realloc(void *p, size_t s)
224 {
225   if (!mm_initialized) {
226     if (mm_initializing)
227       return mm_fake_realloc(p, s);
228     mm_legacy_constructor();
229   }
230
231   if (!__malloc_use_mmalloc) {
232     return mm_real_realloc(p, s);
233   }
234
235   xbt_mheap_t mdp = GET_HEAP();
236   if (!mdp)
237     return NULL;
238
239   LOCK(mdp);
240   void* ret = mrealloc(mdp, p, s);
241   UNLOCK(mdp);
242   return ret;
243 }
244
245 void free(void *p)
246 {
247   if (!mm_initialized) {
248     if (mm_initializing)
249       return mm_fake_free(p);
250     mm_legacy_constructor();
251   }
252
253   if (!__malloc_use_mmalloc) {
254     mm_real_free(p);
255     return;
256   }
257
258   if (!p)
259     return;
260
261   xbt_mheap_t mdp = GET_HEAP();
262   LOCK(mdp);
263   mfree(mdp, p);
264   UNLOCK(mdp);
265 }
266 #endif /* HAVE_MC */