1 /* Copyright (c) 2010-2014. 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 "../../mc/mc_base.h"
15 #include "mmprivate.h"
16 #include "xbt_modinter.h"
17 #include "internal_config.h"
19 #include "../mc/mc_protocol.h"
21 /* ***** Whether to use `mmalloc` of the undrlying 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 #ifdef MMALLOC_WANT_OVERRIDE_LEGACY
55 /* ***** Temporary allocator
57 * This is used before we have found the real malloc implementation with dlsym.
60 #define BUFFER_SIZE 32
61 static size_t fake_alloc_index;
62 static uint64_t buffer[BUFFER_SIZE];
64 /* Fake implementations, they are used to fool dlsym:
65 * dlsym used calloc and falls back to some other mechanism
68 static void* mm_fake_malloc(size_t n)
70 size_t count = n / sizeof(uint64_t);
73 if (fake_alloc_index + count < BUFFER_SIZE) {
74 uint64_t* res = buffer + fake_alloc_index;
75 fake_alloc_index += count;
81 static void* mm_fake_calloc(size_t nmemb, size_t size)
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);
88 static void* mm_fake_realloc(void *p, size_t s)
90 return mm_fake_malloc(s);
93 static void mm_fake_free(void *p)
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);
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;
109 static int mm_initializing;
110 static int mm_initialized;
112 /** Constructor functions used to initialize the malloc implementation
114 static void __attribute__((constructor(101))) mm_legacy_constructor()
119 __malloc_use_mmalloc = getenv(MC_ENV_VARIABLE) ? 1 : 0;
120 if (__malloc_use_mmalloc) {
121 __mmalloc_current_heap = mmalloc_preinit();
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");
132 /* ***** malloc/free implementation
134 * They call either the underlying/native/RTLD_NEXT implementation (non MC mode)
135 * or the mm implementation (MC mode).
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.
141 #define GET_HEAP() __mmalloc_current_heap
143 void* malloc_no_memset(size_t n)
145 if (!mm_initialized) {
147 return mm_fake_malloc(n);
148 mm_legacy_constructor();
151 if (!__malloc_use_mmalloc) {
152 return mm_real_malloc(n);
155 xbt_mheap_t mdp = GET_HEAP();
160 void *ret = mmalloc_no_memset(mdp, n);
165 void *malloc(size_t n)
167 if (!mm_initialized) {
169 return mm_fake_malloc(n);
170 mm_legacy_constructor();
173 if (!__malloc_use_mmalloc) {
174 return mm_real_malloc(n);
177 xbt_mheap_t mdp = GET_HEAP();
182 void *ret = mmalloc(mdp, n);
187 void *calloc(size_t nmemb, size_t size)
189 if (!mm_initialized) {
191 return mm_fake_calloc(nmemb, size);
192 mm_legacy_constructor();
195 if (!__malloc_use_mmalloc) {
196 return mm_real_calloc(nmemb, size);
199 xbt_mheap_t mdp = GET_HEAP();
204 void *ret = mmalloc(mdp, nmemb*size);
206 // This was already done in the callee:
207 if(!(mdp->options & XBT_MHEAP_OPTION_MEMSET)) {
208 memset(ret, 0, nmemb * size);
213 void *realloc(void *p, size_t s)
215 if (!mm_initialized) {
217 return mm_fake_realloc(p, s);
218 mm_legacy_constructor();
221 if (!__malloc_use_mmalloc) {
222 return mm_real_realloc(p, s);
225 xbt_mheap_t mdp = GET_HEAP();
230 void* ret = mrealloc(mdp, p, s);
237 if (!mm_initialized) {
239 return mm_fake_free(p);
240 mm_legacy_constructor();
243 if (!__malloc_use_mmalloc) {
251 xbt_mheap_t mdp = GET_HEAP();
256 #endif /* WANT_MALLOC_OVERRIDE */