Logo AND Algorithmique Numérique Distribuée

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