1 /* Copyright (c) 2010-2022. 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;
18 int mmalloc_pagesize = 0;
20 int malloc_use_mmalloc(void)
22 return __malloc_use_mmalloc;
25 /* ***** Current heap ***** */
27 /* The mmalloc() package can use a single implicit malloc descriptor
28 for mmalloc/mrealloc/mfree operations which do not supply an explicit
29 descriptor. This allows mmalloc() to provide
30 backwards compatibility with the non-mmap'd version. */
31 xbt_mheap_t __mmalloc_default_mdp = NULL;
33 /* The heap we are currently using. */
34 static xbt_mheap_t __mmalloc_current_heap = NULL;
36 xbt_mheap_t mmalloc_get_current_heap(void)
38 return __mmalloc_current_heap;
41 /* Override the malloc-like functions if MC is activated at compile time */
42 /* ***** Temporary allocator
44 * This is used before we have found the real malloc implementation with dlsym.
47 static size_t fake_alloc_index;
48 static uint64_t buffer[mmalloc_preinit_buffer_size];
49 uint64_t* mmalloc_preinit_buffer = buffer;
51 /* Fake implementations, they are used to fool dlsym:
52 * dlsym used calloc and falls back to some other mechanism
55 static void* mm_fake_malloc(size_t n)
57 mmalloc_preinit_buffer = buffer;
59 // How many uint64_t do w need?
60 size_t count = n / sizeof(uint64_t);
61 if (n % sizeof(uint64_t))
63 // Check that we have enough available memory:
64 if (fake_alloc_index + count >= mmalloc_preinit_buffer_size) {
65 puts("mmalloc is not initialized yet, but the static buffer used as malloc replacement is already exhausted. "
66 "Please increase `mmalloc_preinit_buffer_size` in mm_legacy.c\n");
70 uint64_t* res = buffer + fake_alloc_index;
71 fake_alloc_index += count;
75 static void* mm_fake_calloc(size_t nmemb, size_t size)
77 // This is fresh .bss data, we don't need to clear it:
78 size_t n = nmemb * size;
79 return mm_fake_malloc(n);
82 static void* mm_fake_realloc(XBT_ATTRIB_UNUSED void* p, size_t s)
84 return mm_fake_malloc(s);
87 static void mm_fake_free(XBT_ATTRIB_UNUSED void* p)
92 /* Function signatures for the main malloc functions: */
93 typedef void* (*mm_malloc_t)(size_t size);
94 typedef void (*mm_free_t)(void*);
95 typedef void* (*mm_calloc_t)(size_t nmemb, size_t size);
96 typedef void* (*mm_realloc_t)(void *ptr, size_t size);
98 /* Function pointers to the real/next implementations: */
99 static mm_malloc_t mm_real_malloc;
100 static mm_free_t mm_real_free;
101 static mm_calloc_t mm_real_calloc;
102 static mm_realloc_t mm_real_realloc;
104 static int mm_initializing;
105 static int mm_initialized;
107 /** Constructor functions used to initialize the malloc implementation
109 XBT_ATTRIB_CONSTRUCTOR(101) static void mm_legacy_constructor()
114 __malloc_use_mmalloc = getenv(MC_ENV_SOCKET_FD) != NULL;
115 if (__malloc_use_mmalloc) {
116 __mmalloc_current_heap = mmalloc_preinit();
119 mm_real_realloc = (void *(*)(void *, size_t))dlfunc(RTLD_NEXT, "realloc");
120 mm_real_malloc = (void *(*)(size_t))dlfunc(RTLD_NEXT, "malloc");
121 mm_real_free = (void (*)(void *))dlfunc(RTLD_NEXT, "free");
122 mm_real_calloc = (void *(*)(size_t, size_t))dlfunc(RTLD_NEXT, "calloc");
124 mm_real_realloc = dlsym(RTLD_NEXT, "realloc");
125 mm_real_malloc = dlsym(RTLD_NEXT, "malloc");
126 mm_real_free = dlsym(RTLD_NEXT, "free");
127 mm_real_calloc = dlsym(RTLD_NEXT, "calloc");
130 mmalloc_pagesize = getpagesize();
136 /* ***** malloc/free implementation
138 * They call either the underlying/native/RTLD_NEXT implementation (non MC mode)
139 * or the mm implementation (MC mode).
141 * If we are initializing the malloc subsystem, we call the fake/dummy `malloc`
142 * implementation. This is necessary because `dlsym` calls `malloc` and friends.
145 #define GET_HEAP() __mmalloc_current_heap
147 void *malloc(size_t n)
149 if (!mm_initialized) {
151 return mm_fake_malloc(n);
152 mm_legacy_constructor();
155 if (!__malloc_use_mmalloc) {
156 return mm_real_malloc(n);
159 xbt_mheap_t mdp = GET_HEAP();
163 return mmalloc(mdp, n);
166 void *calloc(size_t nmemb, size_t size)
168 if (!mm_initialized) {
170 return mm_fake_calloc(nmemb, size);
171 mm_legacy_constructor();
174 if (!__malloc_use_mmalloc) {
175 return mm_real_calloc(nmemb, size);
178 xbt_mheap_t mdp = GET_HEAP();
182 void *ret = mmalloc(mdp, nmemb*size);
183 // This was already done in the callee:
184 if(!(mdp->options & XBT_MHEAP_OPTION_MEMSET)) {
185 memset(ret, 0, nmemb * size);
190 void *realloc(void *p, size_t s)
192 if (!mm_initialized) {
194 return mm_fake_realloc(p, s);
195 mm_legacy_constructor();
198 if (!__malloc_use_mmalloc) {
199 return mm_real_realloc(p, s);
202 xbt_mheap_t mdp = GET_HEAP();
206 return mrealloc(mdp, p, s);
211 if (!mm_initialized) {
213 return mm_fake_free(p);
214 mm_legacy_constructor();
217 if (!__malloc_use_mmalloc) {
225 xbt_mheap_t mdp = GET_HEAP();