Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / src / xbt / mmalloc / mm_legacy.c
1 /* Copyright (c) 2010-2019. 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 "mmprivate.h"
15 #include "src/internal_config.h"
16 #include "src/mc/mc_base.h"
17 #include "src/mc/remote/mc_protocol.h"
18 #include "src/xbt_modinter.h"
19 #include <math.h>
20
21 /* ***** Whether to use `mmalloc` of the underlying 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 SIMGRID_HAVE_MC
55
56 /* ***** Temporary allocator
57  *
58  * This is used before we have found the real malloc implementation with dlsym.
59  */
60
61 #ifdef __FreeBSD__ /* FreeBSD require more memory, other might */
62 # define BUFFER_SIZE 256
63 #else /* Valid on: Linux */
64 # define BUFFER_SIZE 32
65 #endif
66 static size_t fake_alloc_index;
67 static uint64_t buffer[BUFFER_SIZE];
68
69 /* Fake implementations, they are used to fool dlsym:
70  * dlsym used calloc and falls back to some other mechanism
71  * if this fails.
72  */
73 static void* mm_fake_malloc(size_t n)
74 {
75   // How many uint64_t do w need?
76   size_t count = n / sizeof(uint64_t);
77   if (n % sizeof(uint64_t))
78     count++;
79   // Check that we have enough available memory:
80   if (fake_alloc_index + count >= BUFFER_SIZE)
81     exit(127);
82   // Allocate it:
83   uint64_t* res = buffer + fake_alloc_index;
84   fake_alloc_index += count;
85   return res;
86 }
87
88 static void* mm_fake_calloc(size_t nmemb, size_t size)
89 {
90   // This is fresh .bss data, we don't need to clear it:
91   size_t n = nmemb * size;
92   return mm_fake_malloc(n);
93 }
94
95 static void* mm_fake_realloc(XBT_ATTRIB_UNUSED void* p, size_t s)
96 {
97   return mm_fake_malloc(s);
98 }
99
100 static void mm_fake_free(XBT_ATTRIB_UNUSED void* p)
101 {
102   // Nothing to do
103 }
104
105 /* Function signatures for the main malloc functions: */
106 typedef void* (*mm_malloc_t)(size_t size);
107 typedef void  (*mm_free_t)(void*);
108 typedef void* (*mm_calloc_t)(size_t nmemb, size_t size);
109 typedef void* (*mm_realloc_t)(void *ptr, size_t size);
110
111 /* Function pointers to the real/next implementations: */
112 static mm_malloc_t mm_real_malloc;
113 static mm_free_t mm_real_free;
114 static mm_calloc_t mm_real_calloc;
115 static mm_realloc_t mm_real_realloc;
116
117 static int mm_initializing;
118 static int mm_initialized;
119
120 /** Constructor functions used to initialize the malloc implementation
121  */
122 XBT_ATTRIB_CONSTRUCTOR(101) static void mm_legacy_constructor()
123 {
124   if (mm_initialized)
125     return;
126   mm_initializing = 1;
127   __malloc_use_mmalloc = getenv(MC_ENV_VARIABLE) ? 1 : 0;
128   if (__malloc_use_mmalloc) {
129     __mmalloc_current_heap = mmalloc_preinit();
130   } else {
131 #if HAVE_DLFUNC
132     mm_real_realloc  = (void *(*)(void *, size_t))dlfunc(RTLD_NEXT, "realloc");
133     mm_real_malloc   = (void *(*)(size_t))dlfunc(RTLD_NEXT, "malloc");
134     mm_real_free     = (void (*)(void *))dlfunc(RTLD_NEXT, "free");
135     mm_real_calloc   = (void *(*)(size_t, size_t))dlfunc(RTLD_NEXT, "calloc");
136 #else
137     mm_real_realloc  = dlsym(RTLD_NEXT, "realloc");
138     mm_real_malloc   = dlsym(RTLD_NEXT, "malloc");
139     mm_real_free     = dlsym(RTLD_NEXT, "free");
140     mm_real_calloc   = dlsym(RTLD_NEXT, "calloc");
141 #endif
142   }
143   mm_initializing = 0;
144   mm_initialized = 1;
145 }
146
147 /* ***** malloc/free implementation
148  *
149  * They call either the underlying/native/RTLD_NEXT implementation (non MC mode)
150  * or the mm implementation (MC mode).
151  *
152  * If we are initializing the malloc subsystem, we call the fake/dummy `malloc`
153  * implementation. This is necessary because `dlsym` calls `malloc` and friends.
154  */
155
156 #define GET_HEAP() __mmalloc_current_heap
157
158 void* malloc_no_memset(size_t n)
159 {
160   if (!mm_initialized) {
161     if (mm_initializing)
162       return mm_fake_malloc(n);
163     mm_legacy_constructor();
164   }
165
166   if (!__malloc_use_mmalloc) {
167     return mm_real_malloc(n);
168   }
169
170   xbt_mheap_t mdp = GET_HEAP();
171   if (!mdp)
172     return NULL;
173
174   LOCK(mdp);
175   void *ret = mmalloc_no_memset(mdp, n);
176   UNLOCK(mdp);
177   return ret;
178 }
179
180 void *malloc(size_t n)
181 {
182   if (!mm_initialized) {
183     if (mm_initializing)
184       return mm_fake_malloc(n);
185     mm_legacy_constructor();
186   }
187
188   if (!__malloc_use_mmalloc) {
189     return mm_real_malloc(n);
190   }
191
192   xbt_mheap_t mdp = GET_HEAP();
193   if (!mdp)
194     return NULL;
195
196   LOCK(mdp);
197   void *ret = mmalloc(mdp, n);
198   UNLOCK(mdp);
199   return ret;
200 }
201
202 void *calloc(size_t nmemb, size_t size)
203 {
204   if (!mm_initialized) {
205     if (mm_initializing)
206       return mm_fake_calloc(nmemb, size);
207     mm_legacy_constructor();
208   }
209
210   if (!__malloc_use_mmalloc) {
211     return mm_real_calloc(nmemb, size);
212   }
213
214   xbt_mheap_t mdp = GET_HEAP();
215   if (!mdp)
216     return NULL;
217
218   LOCK(mdp);
219   void *ret = mmalloc(mdp, nmemb*size);
220   UNLOCK(mdp);
221   // This was already done in the callee:
222   if(!(mdp->options & XBT_MHEAP_OPTION_MEMSET)) {
223     memset(ret, 0, nmemb * size);
224   }
225   return ret;
226 }
227
228 void *realloc(void *p, size_t s)
229 {
230   if (!mm_initialized) {
231     if (mm_initializing)
232       return mm_fake_realloc(p, s);
233     mm_legacy_constructor();
234   }
235
236   if (!__malloc_use_mmalloc) {
237     return mm_real_realloc(p, s);
238   }
239
240   xbt_mheap_t mdp = GET_HEAP();
241   if (!mdp)
242     return NULL;
243
244   LOCK(mdp);
245   void* ret = mrealloc(mdp, p, s);
246   UNLOCK(mdp);
247   return ret;
248 }
249
250 void free(void *p)
251 {
252   if (!mm_initialized) {
253     if (mm_initializing)
254       return mm_fake_free(p);
255     mm_legacy_constructor();
256   }
257
258   if (!__malloc_use_mmalloc) {
259     mm_real_free(p);
260     return;
261   }
262
263   if (!p)
264     return;
265
266   xbt_mheap_t mdp = GET_HEAP();
267   LOCK(mdp);
268   mfree(mdp, p);
269   UNLOCK(mdp);
270 }
271 #endif /* SIMGRID_HAVE_MC */