1 /* Copyright (c) 2010-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 /* Redefine the classical malloc/free/realloc functions so that they fit well in the mmalloc framework */
15 /* ***** Whether to use `mmalloc` of the underlying malloc ***** */
17 static int __malloc_use_mmalloc;
19 int malloc_use_mmalloc(void)
21 return __malloc_use_mmalloc;
24 /* ***** Current heap ***** */
26 /* The mmalloc() package can use a single implicit malloc descriptor
27 for mmalloc/mrealloc/mfree operations which do not supply an explicit
28 descriptor. This allows mmalloc() to provide
29 backwards compatibility with the non-mmap'd version. */
30 xbt_mheap_t __mmalloc_default_mdp = NULL;
32 /* The heap we are currently using. */
33 static xbt_mheap_t __mmalloc_current_heap = NULL;
35 xbt_mheap_t mmalloc_get_current_heap(void)
37 return __mmalloc_current_heap;
40 /* Override the malloc-like functions if MC is activated at compile time */
41 /* ***** Temporary allocator
43 * This is used before we have found the real malloc implementation with dlsym.
46 static size_t fake_alloc_index;
47 static uint64_t buffer[mmalloc_preinit_buffer_size];
48 uint64_t* mmalloc_preinit_buffer = buffer;
50 /* Fake implementations, they are used to fool dlsym:
51 * dlsym used calloc and falls back to some other mechanism
54 static void* mm_fake_malloc(size_t n)
56 mmalloc_preinit_buffer = buffer;
58 // How many uint64_t do w need?
59 size_t count = n / sizeof(uint64_t);
60 if (n % sizeof(uint64_t))
62 // Check that we have enough available memory:
63 if (fake_alloc_index + count >= mmalloc_preinit_buffer_size) {
64 puts("mmalloc is not initialized yet, but the static buffer used as malloc replacement is already exhausted. "
65 "Please increase `mmalloc_preinit_buffer_size` in mm_legacy.c\n");
69 uint64_t* res = buffer + fake_alloc_index;
70 fake_alloc_index += count;
74 static void* mm_fake_calloc(size_t nmemb, size_t size)
76 // This is fresh .bss data, we don't need to clear it:
77 size_t n = nmemb * size;
78 return mm_fake_malloc(n);
81 static void* mm_fake_realloc(XBT_ATTRIB_UNUSED void* p, size_t s)
83 return mm_fake_malloc(s);
86 static void mm_fake_free(XBT_ATTRIB_UNUSED void* p)
91 /* Function signatures for the main malloc functions: */
92 typedef void* (*mm_malloc_t)(size_t size);
93 typedef void (*mm_free_t)(void*);
94 typedef void* (*mm_calloc_t)(size_t nmemb, size_t size);
95 typedef void* (*mm_realloc_t)(void *ptr, size_t size);
97 /* Function pointers to the real/next implementations: */
98 static mm_malloc_t mm_real_malloc;
99 static mm_free_t mm_real_free;
100 static mm_calloc_t mm_real_calloc;
101 static mm_realloc_t mm_real_realloc;
103 static int mm_initializing;
104 static int mm_initialized;
106 /** Constructor functions used to initialize the malloc implementation
108 XBT_ATTRIB_CONSTRUCTOR(101) static void mm_legacy_constructor()
113 __malloc_use_mmalloc = getenv(MC_ENV_SOCKET_FD) != NULL;
114 if (__malloc_use_mmalloc) {
115 __mmalloc_current_heap = mmalloc_preinit();
118 mm_real_realloc = (void *(*)(void *, size_t))dlfunc(RTLD_NEXT, "realloc");
119 mm_real_malloc = (void *(*)(size_t))dlfunc(RTLD_NEXT, "malloc");
120 mm_real_free = (void (*)(void *))dlfunc(RTLD_NEXT, "free");
121 mm_real_calloc = (void *(*)(size_t, size_t))dlfunc(RTLD_NEXT, "calloc");
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");
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(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();
161 return mmalloc(mdp, n);
164 void *calloc(size_t nmemb, size_t size)
166 if (!mm_initialized) {
168 return mm_fake_calloc(nmemb, size);
169 mm_legacy_constructor();
172 if (!__malloc_use_mmalloc) {
173 return mm_real_calloc(nmemb, size);
176 xbt_mheap_t mdp = GET_HEAP();
180 void *ret = mmalloc(mdp, nmemb*size);
181 // This was already done in the callee:
182 if(!(mdp->options & XBT_MHEAP_OPTION_MEMSET)) {
183 memset(ret, 0, nmemb * size);
188 void *realloc(void *p, size_t s)
190 if (!mm_initialized) {
192 return mm_fake_realloc(p, s);
193 mm_legacy_constructor();
196 if (!__malloc_use_mmalloc) {
197 return mm_real_realloc(p, s);
200 xbt_mheap_t mdp = GET_HEAP();
204 return mrealloc(mdp, p, s);
209 if (!mm_initialized) {
211 return mm_fake_free(p);
212 mm_legacy_constructor();
215 if (!__malloc_use_mmalloc) {
223 xbt_mheap_t mdp = GET_HEAP();