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 #ifdef __FreeBSD__ /* FreeBSD require more memory, other might */
48 # define BUFFER_SIZE 256
49 #else /* Valid on: Linux */
50 # define BUFFER_SIZE 32
52 static size_t fake_alloc_index;
53 static uint64_t buffer[BUFFER_SIZE];
55 /* Fake implementations, they are used to fool dlsym:
56 * dlsym used calloc and falls back to some other mechanism
59 static void* mm_fake_malloc(size_t n)
61 // How many uint64_t do w need?
62 size_t count = n / sizeof(uint64_t);
63 if (n % sizeof(uint64_t))
65 // Check that we have enough available memory:
66 if (fake_alloc_index + count >= BUFFER_SIZE)
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) ? 1 : 0;
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");
129 mmalloc_pagesize = getpagesize();
135 /* ***** malloc/free implementation
137 * They call either the underlying/native/RTLD_NEXT implementation (non MC mode)
138 * or the mm implementation (MC mode).
140 * If we are initializing the malloc subsystem, we call the fake/dummy `malloc`
141 * implementation. This is necessary because `dlsym` calls `malloc` and friends.
144 #define GET_HEAP() __mmalloc_current_heap
146 void *malloc(size_t n)
148 if (!mm_initialized) {
150 return mm_fake_malloc(n);
151 mm_legacy_constructor();
154 if (!__malloc_use_mmalloc) {
155 return mm_real_malloc(n);
158 xbt_mheap_t mdp = GET_HEAP();
163 void *ret = mmalloc(mdp, n);
168 void *calloc(size_t nmemb, size_t size)
170 if (!mm_initialized) {
172 return mm_fake_calloc(nmemb, size);
173 mm_legacy_constructor();
176 if (!__malloc_use_mmalloc) {
177 return mm_real_calloc(nmemb, size);
180 xbt_mheap_t mdp = GET_HEAP();
185 void *ret = mmalloc(mdp, nmemb*size);
187 // This was already done in the callee:
188 if(!(mdp->options & XBT_MHEAP_OPTION_MEMSET)) {
189 memset(ret, 0, nmemb * size);
194 void *realloc(void *p, size_t s)
196 if (!mm_initialized) {
198 return mm_fake_realloc(p, s);
199 mm_legacy_constructor();
202 if (!__malloc_use_mmalloc) {
203 return mm_real_realloc(p, s);
206 xbt_mheap_t mdp = GET_HEAP();
211 void* ret = mrealloc(mdp, p, s);
218 if (!mm_initialized) {
220 return mm_fake_free(p);
221 mm_legacy_constructor();
224 if (!__malloc_use_mmalloc) {
232 xbt_mheap_t mdp = GET_HEAP();