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 "src/mc/mc_base.h"
15 #include "mmprivate.h"
16 #include "src/xbt_modinter.h"
17 #include "src/internal_config.h"
19 #include "src/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 /* Override the malloc-like functions if MC is activated at compile time */
56 /* ***** Temporary allocator
58 * This is used before we have found the real malloc implementation with dlsym.
61 #define BUFFER_SIZE 32
62 static size_t fake_alloc_index;
63 static uint64_t buffer[BUFFER_SIZE];
65 /* Fake implementations, they are used to fool dlsym:
66 * dlsym used calloc and falls back to some other mechanism
69 static void* mm_fake_malloc(size_t n)
71 // How many uint64_t do w need?
72 size_t count = n / sizeof(uint64_t);
73 if (n % sizeof(uint64_t))
75 // Check that we have enough availabel memory:
76 if (fake_alloc_index + count >= BUFFER_SIZE)
79 uint64_t* res = buffer + fake_alloc_index;
80 fake_alloc_index += count;
84 static void* mm_fake_calloc(size_t nmemb, size_t size)
86 // This is fresh .bss data, we don't need to clear it:
87 size_t n = nmemb * size;
88 return mm_fake_malloc(n);
91 static void* mm_fake_realloc(void *p, size_t s)
93 return mm_fake_malloc(s);
96 static void mm_fake_free(void *p)
100 /* Function signatures for the main malloc functions: */
101 typedef void* (*mm_malloc_t)(size_t size);
102 typedef void (*mm_free_t)(void*);
103 typedef void* (*mm_calloc_t)(size_t nmemb, size_t size);
104 typedef void* (*mm_realloc_t)(void *ptr, size_t size);
106 /* Function pointers to the real/next implementations: */
107 static mm_malloc_t mm_real_malloc;
108 static mm_free_t mm_real_free;
109 static mm_calloc_t mm_real_calloc;
110 static mm_realloc_t mm_real_realloc;
112 static int mm_initializing;
113 static int mm_initialized;
115 /** Constructor functions used to initialize the malloc implementation
117 static void __attribute__((constructor(101))) mm_legacy_constructor()
122 __malloc_use_mmalloc = getenv(MC_ENV_VARIABLE) ? 1 : 0;
123 if (__malloc_use_mmalloc) {
124 __mmalloc_current_heap = mmalloc_preinit();
126 mm_real_realloc = dlsym(RTLD_NEXT, "realloc");
127 mm_real_malloc = dlsym(RTLD_NEXT, "malloc");
128 mm_real_free = dlsym(RTLD_NEXT, "free");
129 mm_real_calloc = dlsym(RTLD_NEXT, "calloc");
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_no_memset(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_no_memset(mdp, n);
168 void *malloc(size_t n)
170 if (!mm_initialized) {
172 return mm_fake_malloc(n);
173 mm_legacy_constructor();
176 if (!__malloc_use_mmalloc) {
177 return mm_real_malloc(n);
180 xbt_mheap_t mdp = GET_HEAP();
185 void *ret = mmalloc(mdp, n);
190 void *calloc(size_t nmemb, size_t size)
192 if (!mm_initialized) {
194 return mm_fake_calloc(nmemb, size);
195 mm_legacy_constructor();
198 if (!__malloc_use_mmalloc) {
199 return mm_real_calloc(nmemb, size);
202 xbt_mheap_t mdp = GET_HEAP();
207 void *ret = mmalloc(mdp, nmemb*size);
209 // This was already done in the callee:
210 if(!(mdp->options & XBT_MHEAP_OPTION_MEMSET)) {
211 memset(ret, 0, nmemb * size);
216 void *realloc(void *p, size_t s)
218 if (!mm_initialized) {
220 return mm_fake_realloc(p, s);
221 mm_legacy_constructor();
224 if (!__malloc_use_mmalloc) {
225 return mm_real_realloc(p, s);
228 xbt_mheap_t mdp = GET_HEAP();
233 void* ret = mrealloc(mdp, p, s);
240 if (!mm_initialized) {
242 return mm_fake_free(p);
243 mm_legacy_constructor();
246 if (!__malloc_use_mmalloc) {
254 xbt_mheap_t mdp = GET_HEAP();