Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Separate mmalloc from xbt
[simgrid.git] / src / xbt / mmalloc / mm_legacy.c
1 /* Copyright (c) 2010-2022. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 /* Redefine the classical malloc/free/realloc functions so that they fit well in the mmalloc framework */
7 #define _GNU_SOURCE
8
9 #include "mmprivate.h"
10
11 #include <dlfcn.h>
12 #include <math.h>
13 #include <stdlib.h>
14
15 /* ***** Whether to use `mmalloc` of the underlying malloc ***** */
16
17 static int __malloc_use_mmalloc;
18 int mmalloc_pagesize = 0;
19
20 int malloc_use_mmalloc(void)
21 {
22   return __malloc_use_mmalloc;
23 }
24
25 /* ***** Current heap ***** */
26
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;
32
33 /* The heap we are currently using. */
34 static xbt_mheap_t __mmalloc_current_heap = NULL;
35
36 xbt_mheap_t mmalloc_get_current_heap(void)
37 {
38   return __mmalloc_current_heap;
39 }
40
41 /* Override the malloc-like functions if MC is activated at compile time */
42 /* ***** Temporary allocator
43  *
44  * This is used before we have found the real malloc implementation with dlsym.
45  */
46
47 #ifdef __FreeBSD__ /* FreeBSD require more memory, other might */
48 # define BUFFER_SIZE 256
49 #else /* Valid on: Linux */
50 # define BUFFER_SIZE 32
51 #endif
52 static size_t fake_alloc_index;
53 static uint64_t buffer[BUFFER_SIZE];
54
55 /* Fake implementations, they are used to fool dlsym:
56  * dlsym used calloc and falls back to some other mechanism
57  * if this fails.
58  */
59 static void* mm_fake_malloc(size_t n)
60 {
61   // How many uint64_t do w need?
62   size_t count = n / sizeof(uint64_t);
63   if (n % sizeof(uint64_t))
64     count++;
65   // Check that we have enough available memory:
66   if (fake_alloc_index + count >= BUFFER_SIZE)
67     exit(127);
68   // Allocate it:
69   uint64_t* res = buffer + fake_alloc_index;
70   fake_alloc_index += count;
71   return res;
72 }
73
74 static void* mm_fake_calloc(size_t nmemb, size_t size)
75 {
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);
79 }
80
81 static void* mm_fake_realloc(XBT_ATTRIB_UNUSED void* p, size_t s)
82 {
83   return mm_fake_malloc(s);
84 }
85
86 static void mm_fake_free(XBT_ATTRIB_UNUSED void* p)
87 {
88   // Nothing to do
89 }
90
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);
96
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;
102
103 static int mm_initializing;
104 static int mm_initialized;
105
106 /** Constructor functions used to initialize the malloc implementation
107  */
108 XBT_ATTRIB_CONSTRUCTOR(101) static void mm_legacy_constructor()
109 {
110   if (mm_initialized)
111     return;
112   mm_initializing = 1;
113   __malloc_use_mmalloc = getenv(MC_ENV_SOCKET_FD) ? 1 : 0;
114   if (__malloc_use_mmalloc) {
115     __mmalloc_current_heap = mmalloc_preinit();
116   } else {
117 #if HAVE_DLFUNC
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");
122 #else
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");
127 #endif
128   }
129   mmalloc_pagesize = getpagesize();
130
131   mm_initializing = 0;
132   mm_initialized = 1;
133 }
134
135 /* ***** malloc/free implementation
136  *
137  * They call either the underlying/native/RTLD_NEXT implementation (non MC mode)
138  * or the mm implementation (MC mode).
139  *
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.
142  */
143
144 #define GET_HEAP() __mmalloc_current_heap
145
146 void *malloc(size_t n)
147 {
148   if (!mm_initialized) {
149     if (mm_initializing)
150       return mm_fake_malloc(n);
151     mm_legacy_constructor();
152   }
153
154   if (!__malloc_use_mmalloc) {
155     return mm_real_malloc(n);
156   }
157
158   xbt_mheap_t mdp = GET_HEAP();
159   if (!mdp)
160     return NULL;
161
162   LOCK(mdp);
163   void *ret = mmalloc(mdp, n);
164   UNLOCK(mdp);
165   return ret;
166 }
167
168 void *calloc(size_t nmemb, size_t size)
169 {
170   if (!mm_initialized) {
171     if (mm_initializing)
172       return mm_fake_calloc(nmemb, size);
173     mm_legacy_constructor();
174   }
175
176   if (!__malloc_use_mmalloc) {
177     return mm_real_calloc(nmemb, size);
178   }
179
180   xbt_mheap_t mdp = GET_HEAP();
181   if (!mdp)
182     return NULL;
183
184   LOCK(mdp);
185   void *ret = mmalloc(mdp, nmemb*size);
186   UNLOCK(mdp);
187   // This was already done in the callee:
188   if(!(mdp->options & XBT_MHEAP_OPTION_MEMSET)) {
189     memset(ret, 0, nmemb * size);
190   }
191   return ret;
192 }
193
194 void *realloc(void *p, size_t s)
195 {
196   if (!mm_initialized) {
197     if (mm_initializing)
198       return mm_fake_realloc(p, s);
199     mm_legacy_constructor();
200   }
201
202   if (!__malloc_use_mmalloc) {
203     return mm_real_realloc(p, s);
204   }
205
206   xbt_mheap_t mdp = GET_HEAP();
207   if (!mdp)
208     return NULL;
209
210   LOCK(mdp);
211   void* ret = mrealloc(mdp, p, s);
212   UNLOCK(mdp);
213   return ret;
214 }
215
216 void free(void *p)
217 {
218   if (!mm_initialized) {
219     if (mm_initializing)
220       return mm_fake_free(p);
221     mm_legacy_constructor();
222   }
223
224   if (!__malloc_use_mmalloc) {
225     mm_real_free(p);
226     return;
227   }
228
229   if (!p)
230     return;
231
232   xbt_mheap_t mdp = GET_HEAP();
233   LOCK(mdp);
234   mfree(mdp, p);
235   UNLOCK(mdp);
236 }