1 /* Copyright (c) 2010. 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 */
10 #include "gras_config.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_mm_legacy, xbt,
13 "Logging specific to mm_legacy in mmalloc");
15 static void *__mmalloc_current_heap = NULL; /* The heap we are currently using. */
17 #include "xbt_modinter.h"
19 void *mmalloc_get_current_heap(void)
21 return __mmalloc_current_heap;
24 void mmalloc_set_current_heap(void *new_heap)
26 __mmalloc_current_heap = new_heap;
29 #ifdef MMALLOC_WANT_OVERIDE_LEGACY
30 void *malloc(size_t n)
32 void *mdp = __mmalloc_current_heap;
38 void *ret = mmalloc(mdp, n);
44 void *calloc(size_t nmemb, size_t size)
46 size_t total_size = nmemb * size;
47 void *mdp = __mmalloc_current_heap;
53 void *ret = mmalloc(mdp, total_size);
56 /* Fill the allocated memory with zeroes to mimic calloc behaviour */
57 memset(ret, '\0', total_size);
62 void *realloc(void *p, size_t s)
65 void *mdp = __mmalloc_current_heap;
73 ret = mrealloc(mdp, p, s);
75 ret = mmalloc(mdp, s);
86 void *mdp = __mmalloc_current_heap;
96 /* Make sure it works with md==NULL */
98 /* Safety gap from the heap's break address.
99 * Try to increase this first if you experience strange errors under
101 #define HEAP_OFFSET (128UL<<20)
103 void *mmalloc_get_default_md(void)
105 xbt_assert(__mmalloc_default_mdp);
106 return __mmalloc_default_mdp;
109 static void mmalloc_fork_prepare(void)
111 struct mdesc* mdp = NULL;
112 if ((mdp =__mmalloc_default_mdp)){
118 mdp = mdp->next_mdesc;
123 static void mmalloc_fork_parent(void)
125 struct mdesc* mdp = NULL;
126 if ((mdp =__mmalloc_default_mdp)){
130 mdp = mdp->next_mdesc;
135 static void mmalloc_fork_child(void)
137 struct mdesc* mdp = NULL;
138 if ((mdp =__mmalloc_default_mdp)){
141 mdp = mdp->next_mdesc;
146 /* Initialize the default malloc descriptor. */
147 void mmalloc_preinit(void)
150 if (!__mmalloc_default_mdp) {
151 unsigned long mask = ~((unsigned long)getpagesize() - 1);
152 void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
153 __mmalloc_default_mdp = mmalloc_attach(-1, addr);
154 /* Fixme? only the default mdp in protected against forks */
155 res = xbt_os_thread_atfork(mmalloc_fork_prepare,
156 mmalloc_fork_parent, mmalloc_fork_child);
158 THROWF(system_error,0,"xbt_os_thread_atfork() failed: return value %d",res);
160 xbt_assert(__mmalloc_default_mdp != NULL);
163 void mmalloc_postexit(void)
165 /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
166 // mmalloc_detach(__mmalloc_default_mdp);
167 mmalloc_pre_detach(__mmalloc_default_mdp);
170 int mmalloc_compare_heap(void *h1, void *h2){
172 if(h1 == NULL && h2 == NULL){
173 XBT_DEBUG("Malloc descriptors null");
180 struct mstats ms1 = mmstats(h1);
181 struct mstats ms2 = mmstats(h2);
183 if(ms1.bytes_total != ms2.bytes_total){
184 XBT_DEBUG("Different total size of the heap");
188 if(ms1.chunks_used != ms2.chunks_used){
189 XBT_DEBUG("Different chunks allocated by the user");
193 if(ms1.bytes_used != ms2.bytes_used){
194 XBT_DEBUG("Different byte total of user-allocated chunks");
198 if(ms1.bytes_free != ms2.bytes_free){
199 XBT_DEBUG("Different byte total of chunks in the free list");
203 if(ms1.chunks_free != ms2.chunks_free){
204 XBT_DEBUG("Different chunks in the free list");
208 struct mdesc *mdp1, *mdp2;
209 mdp1 = MD_TO_MDP(h1);
210 mdp2 = MD_TO_MDP(h2);
212 if(mmalloc_compare_mdesc(mdp1, mdp2))
219 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2){
224 if(mdp1->headersize != mdp2->headersize){
225 XBT_DEBUG("Different size of the file header for the mapped files");
229 if(mdp1->refcount != mdp2->refcount){
230 XBT_DEBUG("Different number of processes that attached the heap");
234 if(strcmp(mdp1->magic, mdp2->magic) != 0){
235 XBT_DEBUG("Different magic number");
239 if(mdp1->flags != mdp2->flags){
240 XBT_DEBUG("Different flags");
244 if(mdp1->heapsize != mdp2->heapsize){
245 XBT_DEBUG("Different number of info entries");
249 // XBT_DEBUG("Heap size : %Zu", mdp1->heapsize);
251 if(mdp1->heapbase != mdp2->heapbase){
252 XBT_DEBUG("Different first block of the heap");
257 if(mdp1->heapindex != mdp2->heapindex){
258 XBT_DEBUG("Different index for the heap table");
262 //XBT_DEBUG("Heap index : %Zu", mdp1->heapindex);
264 if(mdp1->base != mdp2->base){
265 XBT_DEBUG("Different base address of the memory region");
269 if(mdp1->breakval != mdp2->breakval){
270 XBT_DEBUG("Different current location in the memory region");
274 if(mdp1->top != mdp2->top){
275 XBT_DEBUG("Different end of the current location in the memory region");
279 if(mdp1->heaplimit != mdp2->heaplimit){
280 XBT_DEBUG("Different limit of valid info table indices");
284 //XBT_DEBUG("Heap limit : %Zu", mdp1->heaplimit);
287 if(mdp1->fd != mdp2->fd){
288 XBT_DEBUG("Different file descriptor for the file to which this malloc heap is mapped");
292 if(mdp1->saved_errno != mdp2->saved_errno){
293 XBT_DEBUG("Different errno");
297 if(mdp1->version != mdp2->version){
298 XBT_DEBUG("Different version of the mmalloc package");
303 size_t block_free1, block_free2 , next_block_free, first_block_free ;
305 void *addr_block1, *addr_block2;
309 /* Search index of the first free block */
311 block_free1 = mdp1->heapindex;
312 block_free2 = mdp2->heapindex;
314 while(mdp1->heapinfo[block_free1].free.prev != 0){
315 block_free1 = mdp1->heapinfo[block_free1].free.prev;
318 while(mdp2->heapinfo[block_free2].free.prev != 0){
319 block_free2 = mdp1->heapinfo[block_free2].free.prev;
322 if(block_free1 != block_free2){
323 XBT_DEBUG("Different first free block");
327 first_block_free = block_free1;
329 if(mdp1->heapinfo[block_free1].free.size != mdp2->heapinfo[block_free2].free.size){
330 XBT_DEBUG("Different size (in blocks) of the first free cluster");
334 /* Check busy blocks (circular checking)*/
336 i = block_free1 + mdp1->heapinfo[block_free1].free.size;
337 next_block_free = mdp1->heapinfo[block_free1].free.next;
339 if(next_block_free == 0)
340 next_block_free = mdp1->heaplimit;
343 while(i != first_block_free){
345 while(i<next_block_free){
349 if(mdp1->heapinfo[i].busy.type != mdp2->heapinfo[i].busy.type){
350 XBT_DEBUG("Different type of busy block");
356 addr_block1 = ADDRESS(j);
358 addr_block2 = ADDRESS(j);
359 switch(mdp1->heapinfo[i].busy.type){
361 if(mdp1->heapinfo[i].busy.info.size != mdp2->heapinfo[i].busy.info.size){
362 XBT_DEBUG("Different size of a large cluster");
365 XBT_DEBUG("Block : %Zu", i);
366 if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy.info.size * BLOCKSIZE)) != 0){
367 XBT_DEBUG("Different data in block %Zu", i);
371 i = i+mdp1->heapinfo[i].busy.info.size;
374 if(mdp1->heapinfo[i].busy.info.frag.nfree != mdp2->heapinfo[i].busy.info.frag.nfree){
375 XBT_DEBUG("Different free fragments in a fragmented block");
378 if(mdp1->heapinfo[i].busy.info.frag.first != mdp2->heapinfo[i].busy.info.frag.first){
379 XBT_DEBUG("Different first free fragments of the block");
382 if(memcmp(addr_block1, addr_block2, BLOCKSIZE) != 0){
383 XBT_DEBUG("Different data in fragmented block %Zu", i);
395 block_free1 = mdp1->heapinfo[block_free1].free.next;
396 next_block_free = mdp1->heapinfo[block_free1].free.next;
397 if(i != first_block_free){
399 i = block_free1 + mdp1->heapinfo[block_free1].free.size;