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 "../../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 // How many uint64_t do w need?
71 size_t count = n / sizeof(uint64_t);
72 if (n % sizeof(uint64_t))
74 // Check that we have enough availabel memory:
75 if (fake_alloc_index + count >= BUFFER_SIZE)
78 uint64_t* res = buffer + fake_alloc_index;
79 fake_alloc_index += count;
83 static void* mm_fake_calloc(size_t nmemb, size_t size)
85 // This is fresh .bss data, we don't need to clear it:
86 size_t n = nmemb * size;
87 return mm_fake_malloc(n);
90 static void* mm_fake_realloc(void *p, size_t s)
92 return mm_fake_malloc(s);
95 static void mm_fake_free(void *p)
99 /* Function signatures for the main malloc functions: */
100 typedef void* (*mm_malloc_t)(size_t size);
101 typedef void (*mm_free_t)(void*);
102 typedef void* (*mm_calloc_t)(size_t nmemb, size_t size);
103 typedef void* (*mm_realloc_t)(void *ptr, size_t size);
105 /* Function pointers to the real/next implementations: */
106 static mm_malloc_t mm_real_malloc;
107 static mm_free_t mm_real_free;
108 static mm_calloc_t mm_real_calloc;
109 static mm_realloc_t mm_real_realloc;
111 static int mm_initializing;
112 static int mm_initialized;
114 /** Constructor functions used to initialize the malloc implementation
116 static void __attribute__((constructor(101))) mm_legacy_constructor()
121 __malloc_use_mmalloc = getenv(MC_ENV_VARIABLE) ? 1 : 0;
122 if (__malloc_use_mmalloc) {
123 __mmalloc_current_heap = mmalloc_preinit();
125 mm_real_realloc = dlsym(RTLD_NEXT, "realloc");
126 mm_real_malloc = dlsym(RTLD_NEXT, "malloc");
127 mm_real_free = dlsym(RTLD_NEXT, "free");
128 mm_real_calloc = dlsym(RTLD_NEXT, "calloc");
134 /* ***** malloc/free implementation
136 * They call either the underlying/native/RTLD_NEXT implementation (non MC mode)
137 * or the mm implementation (MC mode).
139 * If we are initializing the malloc subsystem, we call the fake/dummy `malloc`
140 * implementation. This is necessary because `dlsym` calls `malloc` and friends.
143 #define GET_HEAP() __mmalloc_current_heap
145 void* malloc_no_memset(size_t n)
147 if (!mm_initialized) {
149 return mm_fake_malloc(n);
150 mm_legacy_constructor();
153 if (!__malloc_use_mmalloc) {
154 return mm_real_malloc(n);
157 xbt_mheap_t mdp = GET_HEAP();
162 void *ret = mmalloc_no_memset(mdp, n);
167 void *malloc(size_t n)
169 if (!mm_initialized) {
171 return mm_fake_malloc(n);
172 mm_legacy_constructor();
175 if (!__malloc_use_mmalloc) {
176 return mm_real_malloc(n);
179 xbt_mheap_t mdp = GET_HEAP();
184 void *ret = mmalloc(mdp, n);
189 void *calloc(size_t nmemb, size_t size)
191 if (!mm_initialized) {
193 return mm_fake_calloc(nmemb, size);
194 mm_legacy_constructor();
197 if (!__malloc_use_mmalloc) {
198 return mm_real_calloc(nmemb, size);
201 xbt_mheap_t mdp = GET_HEAP();
206 void *ret = mmalloc(mdp, nmemb*size);
208 // This was already done in the callee:
209 if(!(mdp->options & XBT_MHEAP_OPTION_MEMSET)) {
210 memset(ret, 0, nmemb * size);
215 void *realloc(void *p, size_t s)
217 if (!mm_initialized) {
219 return mm_fake_realloc(p, s);
220 mm_legacy_constructor();
223 if (!__malloc_use_mmalloc) {
224 return mm_real_realloc(p, s);
227 xbt_mheap_t mdp = GET_HEAP();
232 void* ret = mrealloc(mdp, p, s);
239 if (!mm_initialized) {
241 return mm_fake_free(p);
242 mm_legacy_constructor();
245 if (!__malloc_use_mmalloc) {
253 xbt_mheap_t mdp = GET_HEAP();
258 #endif /* WANT_MALLOC_OVERRIDE */