1 /* Copyright (c) 2010-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
7 /* Redefine the classical malloc/free/realloc functions so that they fit well in the mmalloc framework */
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"
21 /* ***** Whether to use `mmalloc` of the underlying malloc ***** */
23 static int __malloc_use_mmalloc;
25 int malloc_use_mmalloc(void)
27 return __malloc_use_mmalloc;
30 /* ***** Current heap ***** */
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;
38 /* The heap we are currently using. */
39 static xbt_mheap_t __mmalloc_current_heap = NULL;
41 xbt_mheap_t mmalloc_get_current_heap(void)
43 return __mmalloc_current_heap;
46 xbt_mheap_t mmalloc_set_current_heap(xbt_mheap_t new_heap)
48 xbt_mheap_t heap = __mmalloc_current_heap;
49 __mmalloc_current_heap = new_heap;
53 /* Override the malloc-like functions if MC is activated at compile time */
56 /* ***** Temporary allocator
58 * This is used before we have found the real malloc implementation with dlsym.
61 #ifdef __FreeBSD__ /* FreeBSD require more memory, other might */
62 # define BUFFER_SIZE 256
63 #else /* Valid on: Linux */
64 # define BUFFER_SIZE 32
66 static size_t fake_alloc_index;
67 static uint64_t buffer[BUFFER_SIZE];
69 /* Fake implementations, they are used to fool dlsym:
70 * dlsym used calloc and falls back to some other mechanism
73 static void* mm_fake_malloc(size_t n)
75 // How many uint64_t do w need?
76 size_t count = n / sizeof(uint64_t);
77 if (n % sizeof(uint64_t))
79 // Check that we have enough available memory:
80 if (fake_alloc_index + count >= BUFFER_SIZE)
83 uint64_t* res = buffer + fake_alloc_index;
84 fake_alloc_index += count;
88 static void* mm_fake_calloc(size_t nmemb, size_t size)
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);
95 static void* mm_fake_realloc(void *p, size_t s)
97 return mm_fake_malloc(s);
100 static void mm_fake_free(void *p)
104 /* Function signatures for the main malloc functions: */
105 typedef void* (*mm_malloc_t)(size_t size);
106 typedef void (*mm_free_t)(void*);
107 typedef void* (*mm_calloc_t)(size_t nmemb, size_t size);
108 typedef void* (*mm_realloc_t)(void *ptr, size_t size);
110 /* Function pointers to the real/next implementations: */
111 static mm_malloc_t mm_real_malloc;
112 static mm_free_t mm_real_free;
113 static mm_calloc_t mm_real_calloc;
114 static mm_realloc_t mm_real_realloc;
116 static int mm_initializing;
117 static int mm_initialized;
119 /** Constructor functions used to initialize the malloc implementation
121 static void __attribute__((constructor(101))) mm_legacy_constructor()
126 __malloc_use_mmalloc = getenv(MC_ENV_VARIABLE) ? 1 : 0;
127 if (__malloc_use_mmalloc) {
128 __mmalloc_current_heap = mmalloc_preinit();
131 mm_real_realloc = (void *(*)(void *, size_t))dlfunc(RTLD_NEXT, "realloc");
132 mm_real_malloc = (void *(*)(size_t))dlfunc(RTLD_NEXT, "malloc");
133 mm_real_free = (void (*)(void *))dlfunc(RTLD_NEXT, "free");
134 mm_real_calloc = (void *(*)(size_t, size_t))dlfunc(RTLD_NEXT, "calloc");
136 mm_real_realloc = dlsym(RTLD_NEXT, "realloc");
137 mm_real_malloc = dlsym(RTLD_NEXT, "malloc");
138 mm_real_free = dlsym(RTLD_NEXT, "free");
139 mm_real_calloc = dlsym(RTLD_NEXT, "calloc");
146 /* ***** malloc/free implementation
148 * They call either the underlying/native/RTLD_NEXT implementation (non MC mode)
149 * or the mm implementation (MC mode).
151 * If we are initializing the malloc subsystem, we call the fake/dummy `malloc`
152 * implementation. This is necessary because `dlsym` calls `malloc` and friends.
155 #define GET_HEAP() __mmalloc_current_heap
157 void* malloc_no_memset(size_t n)
159 if (!mm_initialized) {
161 return mm_fake_malloc(n);
162 mm_legacy_constructor();
165 if (!__malloc_use_mmalloc) {
166 return mm_real_malloc(n);
169 xbt_mheap_t mdp = GET_HEAP();
174 void *ret = mmalloc_no_memset(mdp, n);
179 void *malloc(size_t n)
181 if (!mm_initialized) {
183 return mm_fake_malloc(n);
184 mm_legacy_constructor();
187 if (!__malloc_use_mmalloc) {
188 return mm_real_malloc(n);
191 xbt_mheap_t mdp = GET_HEAP();
196 void *ret = mmalloc(mdp, n);
201 void *calloc(size_t nmemb, size_t size)
203 if (!mm_initialized) {
205 return mm_fake_calloc(nmemb, size);
206 mm_legacy_constructor();
209 if (!__malloc_use_mmalloc) {
210 return mm_real_calloc(nmemb, size);
213 xbt_mheap_t mdp = GET_HEAP();
218 void *ret = mmalloc(mdp, nmemb*size);
220 // This was already done in the callee:
221 if(!(mdp->options & XBT_MHEAP_OPTION_MEMSET)) {
222 memset(ret, 0, nmemb * size);
227 void *realloc(void *p, size_t s)
229 if (!mm_initialized) {
231 return mm_fake_realloc(p, s);
232 mm_legacy_constructor();
235 if (!__malloc_use_mmalloc) {
236 return mm_real_realloc(p, s);
239 xbt_mheap_t mdp = GET_HEAP();
244 void* ret = mrealloc(mdp, p, s);
251 if (!mm_initialized) {
253 return mm_fake_free(p);
254 mm_legacy_constructor();
257 if (!__malloc_use_mmalloc) {
265 xbt_mheap_t mdp = GET_HEAP();