1 /* Copyright (c) 2010-2022. 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 "mmprivate.h"
15 #include "src/internal_config.h"
16 #include "src/mc/remote/mc_protocol.h"
17 #include "xbt/xbt_modinter.h"
20 /* ***** Whether to use `mmalloc` of the underlying malloc ***** */
22 static int __malloc_use_mmalloc;
24 int malloc_use_mmalloc(void)
26 return __malloc_use_mmalloc;
29 /* ***** Current heap ***** */
31 /* The mmalloc() package can use a single implicit malloc descriptor
32 for mmalloc/mrealloc/mfree operations which do not supply an explicit
33 descriptor. This allows mmalloc() to provide
34 backwards compatibility with the non-mmap'd version. */
35 xbt_mheap_t __mmalloc_default_mdp = NULL;
37 /* The heap we are currently using. */
38 static xbt_mheap_t __mmalloc_current_heap = NULL;
40 xbt_mheap_t mmalloc_get_current_heap(void)
42 return __mmalloc_current_heap;
45 /* Override the malloc-like functions if MC is activated at compile time */
48 /* ***** Temporary allocator
50 * This is used before we have found the real malloc implementation with dlsym.
53 #ifdef __FreeBSD__ /* FreeBSD require more memory, other might */
54 # define BUFFER_SIZE 256
55 #else /* Valid on: Linux */
56 # define BUFFER_SIZE 32
58 static size_t fake_alloc_index;
59 static uint64_t buffer[BUFFER_SIZE];
61 /* Fake implementations, they are used to fool dlsym:
62 * dlsym used calloc and falls back to some other mechanism
65 static void* mm_fake_malloc(size_t n)
67 // How many uint64_t do w need?
68 size_t count = n / sizeof(uint64_t);
69 if (n % sizeof(uint64_t))
71 // Check that we have enough available memory:
72 if (fake_alloc_index + count >= BUFFER_SIZE)
75 uint64_t* res = buffer + fake_alloc_index;
76 fake_alloc_index += count;
80 static void* mm_fake_calloc(size_t nmemb, size_t size)
82 // This is fresh .bss data, we don't need to clear it:
83 size_t n = nmemb * size;
84 return mm_fake_malloc(n);
87 static void* mm_fake_realloc(XBT_ATTRIB_UNUSED void* p, size_t s)
89 return mm_fake_malloc(s);
92 static void mm_fake_free(XBT_ATTRIB_UNUSED 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 XBT_ATTRIB_CONSTRUCTOR(101) static void mm_legacy_constructor()
119 __malloc_use_mmalloc = getenv(MC_ENV_SOCKET_FD) ? 1 : 0;
120 if (__malloc_use_mmalloc) {
121 __mmalloc_current_heap = mmalloc_preinit();
124 mm_real_realloc = (void *(*)(void *, size_t))dlfunc(RTLD_NEXT, "realloc");
125 mm_real_malloc = (void *(*)(size_t))dlfunc(RTLD_NEXT, "malloc");
126 mm_real_free = (void (*)(void *))dlfunc(RTLD_NEXT, "free");
127 mm_real_calloc = (void *(*)(size_t, size_t))dlfunc(RTLD_NEXT, "calloc");
129 mm_real_realloc = dlsym(RTLD_NEXT, "realloc");
130 mm_real_malloc = dlsym(RTLD_NEXT, "malloc");
131 mm_real_free = dlsym(RTLD_NEXT, "free");
132 mm_real_calloc = dlsym(RTLD_NEXT, "calloc");
139 /* ***** malloc/free implementation
141 * They call either the underlying/native/RTLD_NEXT implementation (non MC mode)
142 * or the mm implementation (MC mode).
144 * If we are initializing the malloc subsystem, we call the fake/dummy `malloc`
145 * implementation. This is necessary because `dlsym` calls `malloc` and friends.
148 #define GET_HEAP() __mmalloc_current_heap
150 void *malloc(size_t n)
152 if (!mm_initialized) {
154 return mm_fake_malloc(n);
155 mm_legacy_constructor();
158 if (!__malloc_use_mmalloc) {
159 return mm_real_malloc(n);
162 xbt_mheap_t mdp = GET_HEAP();
167 void *ret = mmalloc(mdp, n);
172 void *calloc(size_t nmemb, size_t size)
174 if (!mm_initialized) {
176 return mm_fake_calloc(nmemb, size);
177 mm_legacy_constructor();
180 if (!__malloc_use_mmalloc) {
181 return mm_real_calloc(nmemb, size);
184 xbt_mheap_t mdp = GET_HEAP();
189 void *ret = mmalloc(mdp, nmemb*size);
191 // This was already done in the callee:
192 if(!(mdp->options & XBT_MHEAP_OPTION_MEMSET)) {
193 memset(ret, 0, nmemb * size);
198 void *realloc(void *p, size_t s)
200 if (!mm_initialized) {
202 return mm_fake_realloc(p, s);
203 mm_legacy_constructor();
206 if (!__malloc_use_mmalloc) {
207 return mm_real_realloc(p, s);
210 xbt_mheap_t mdp = GET_HEAP();
215 void* ret = mrealloc(mdp, p, s);
222 if (!mm_initialized) {
224 return mm_fake_free(p);
225 mm_legacy_constructor();
228 if (!__malloc_use_mmalloc) {
236 xbt_mheap_t mdp = GET_HEAP();
241 #endif /* SIMGRID_HAVE_MC */