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"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_mm_legacy, xbt,
14 "Logging specific to mm_legacy in mmalloc");
16 /* The mmalloc() package can use a single implicit malloc descriptor
17 for mmalloc/mrealloc/mfree operations which do not supply an explicit
18 descriptor. This allows mmalloc() to provide
19 backwards compatibility with the non-mmap'd version. */
20 xbt_mheap_t __mmalloc_default_mdp = NULL;
23 static xbt_mheap_t __mmalloc_current_heap = NULL; /* The heap we are currently using. */
25 #include "xbt_modinter.h"
27 xbt_mheap_t mmalloc_get_current_heap(void)
29 return __mmalloc_current_heap;
32 void mmalloc_set_current_heap(xbt_mheap_t new_heap)
34 __mmalloc_current_heap = new_heap;
37 #ifdef MMALLOC_WANT_OVERIDE_LEGACY
38 void *malloc(size_t n)
40 xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
43 void *ret = mmalloc(mdp, n);
49 void *calloc(size_t nmemb, size_t size)
51 size_t total_size = nmemb * size;
52 xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
55 void *ret = mmalloc(mdp, total_size);
58 /* Fill the allocated memory with zeroes to mimic calloc behaviour */
59 memset(ret, '\0', total_size);
64 void *realloc(void *p, size_t s)
67 xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
72 ret = mrealloc(mdp, p, s);
74 ret = mmalloc(mdp, s);
85 xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
93 /* Make sure it works with md==NULL */
95 /* Safety gap from the heap's break address.
96 * Try to increase this first if you experience strange errors under
98 #define HEAP_OFFSET (128UL<<20)
100 xbt_mheap_t mmalloc_get_default_md(void)
102 xbt_assert(__mmalloc_default_mdp);
103 return __mmalloc_default_mdp;
106 static void mmalloc_fork_prepare(void)
108 xbt_mheap_t mdp = NULL;
109 if ((mdp =__mmalloc_default_mdp)){
115 mdp = mdp->next_mdesc;
120 static void mmalloc_fork_parent(void)
122 xbt_mheap_t mdp = NULL;
123 if ((mdp =__mmalloc_default_mdp)){
127 mdp = mdp->next_mdesc;
132 static void mmalloc_fork_child(void)
134 struct mdesc* mdp = NULL;
135 if ((mdp =__mmalloc_default_mdp)){
138 mdp = mdp->next_mdesc;
143 /* Initialize the default malloc descriptor. */
144 void *mmalloc_preinit(void)
147 if (__mmalloc_default_mdp == NULL) {
148 unsigned long mask = ~((unsigned long)getpagesize() - 1);
149 void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
150 __mmalloc_default_mdp = mmalloc_attach(-1, addr);
151 /* Fixme? only the default mdp in protected against forks */
152 res = xbt_os_thread_atfork(mmalloc_fork_prepare,
153 mmalloc_fork_parent, mmalloc_fork_child);
155 THROWF(system_error,0,"xbt_os_thread_atfork() failed: return value %d",res);
157 xbt_assert(__mmalloc_default_mdp != NULL);
159 return __mmalloc_default_mdp;
162 void mmalloc_postexit(void)
164 /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
165 // mmalloc_detach(__mmalloc_default_mdp);
166 mmalloc_detach_no_free(__mmalloc_default_mdp);
169 int mmalloc_compare_heap(xbt_mheap_t mdp1, xbt_mheap_t mdp2, void *std_heap_addr){
171 if(mdp1 == NULL && mdp2 == NULL){
172 XBT_DEBUG("Malloc descriptors null");
178 int errors = mmalloc_compare_mdesc(mdp1, mdp2, std_heap_addr);
184 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2, void *std_heap_addr){
188 if(mdp1->headersize != mdp2->headersize){
189 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
190 XBT_DEBUG("Different size of the file header for the mapped files");
197 if(mdp1->refcount != mdp2->refcount){
198 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
199 XBT_DEBUG("Different number of processes that attached the heap");
206 if(strcmp(mdp1->magic, mdp2->magic) != 0){
207 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
208 XBT_DEBUG("Different magic number");
215 if(mdp1->flags != mdp2->flags){
216 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
217 XBT_DEBUG("Different flags");
224 if(mdp1->heapsize != mdp2->heapsize){
225 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
226 XBT_DEBUG("Different number of info entries");
233 //XBT_DEBUG("Heap size : %zu", mdp1->heapsize);
235 if(mdp1->heapbase != mdp2->heapbase){
236 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
237 XBT_DEBUG("Different first block of the heap");
245 if(mdp1->heapindex != mdp2->heapindex){
246 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
247 XBT_DEBUG("Different index for the heap table : %zu - %zu", mdp1->heapindex, mdp2->heapindex);
254 //XBT_DEBUG("Heap index : %zu", mdp1->heapindex);
256 if(mdp1->base != mdp2->base){
257 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
258 XBT_DEBUG("Different base address of the memory region");
265 if(mdp1->breakval != mdp2->breakval){
266 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
267 XBT_DEBUG("Different current location in the memory region");
274 if(mdp1->top != mdp2->top){
275 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
276 XBT_DEBUG("Different end of the current location in the memory region");
283 if(mdp1->heaplimit != mdp2->heaplimit){
284 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
285 XBT_DEBUG("Different limit of valid info table indices");
292 if(mdp1->fd != mdp2->fd){
293 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
294 XBT_DEBUG("Different file descriptor for the file to which this malloc heap is mapped");
301 if(mdp1->version != mdp2->version){
302 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
303 XBT_DEBUG("Different version of the mmalloc package");
311 size_t block_free1, block_free2 , next_block_free, first_block_free, block_free ;
313 void *addr_block1, *addr_block2;
317 /* Search index of the first free block */
319 block_free1 = mdp1->heapindex;
320 block_free2 = mdp2->heapindex;
322 while(mdp1->heapinfo[block_free1].free.prev != 0){
323 block_free1 = mdp1->heapinfo[block_free1].free.prev;
326 while(mdp2->heapinfo[block_free2].free.prev != 0){
327 block_free2 = mdp1->heapinfo[block_free2].free.prev;
330 if(block_free1 != block_free2){
331 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
332 XBT_DEBUG("Different first free block");
339 first_block_free = block_free1;
341 if(mdp1->heapinfo[first_block_free].free.size != mdp2->heapinfo[first_block_free].free.size){
342 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
343 XBT_DEBUG("Different size (in blocks) of the first free cluster");
350 /* Check busy blocks (circular checking)*/
352 i = first_block_free + mdp1->heapinfo[first_block_free].free.size;
354 if(mdp1->heapinfo[first_block_free].free.next != mdp2->heapinfo[first_block_free].free.next){
355 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
356 XBT_DEBUG("Different next block free");
363 block_free = first_block_free;
364 next_block_free = mdp1->heapinfo[first_block_free].free.next;
366 if(next_block_free == 0)
367 next_block_free = mdp1->heaplimit;
369 while(i != first_block_free){
371 while(i<next_block_free){
373 if(mdp1->heapinfo[i].busy.type != mdp2->heapinfo[i].busy.type){
374 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
375 XBT_DEBUG("Different type of busy block");
382 addr_block1 = (char *)mdp1 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE);
383 addr_block2 = (char *)mdp2 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE);
385 switch(mdp1->heapinfo[i].busy.type){
387 if(mdp1->heapinfo[i].busy.info.block.size != mdp2->heapinfo[i].busy.info.block.size){
388 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
389 XBT_DEBUG("Different size of a large cluster");
395 if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy.info.block.size * BLOCKSIZE)) != 0){
396 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
397 XBT_DEBUG("Different data in block %zu (size = %zu) (addr_block1 = %p (current = %p) - addr_block2 = %p)", i, mdp1->heapinfo[i].busy.info.block.size, addr_block1, (char *)std_heap_addr + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE), addr_block2);
404 i = i+mdp1->heapinfo[i].busy.info.block.size;
408 if(mdp1->heapinfo[i].busy.info.frag.nfree != mdp2->heapinfo[i].busy.info.frag.nfree){
409 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
410 XBT_DEBUG("Different free fragments in the fragmented block %zu", i);
416 if(mdp1->heapinfo[i].busy.info.frag.first != mdp2->heapinfo[i].busy.info.frag.first){
417 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
418 XBT_DEBUG("Different first free fragments in the block %zu", i);
424 frag_size = pow(2,mdp1->heapinfo[i].busy.type);
425 for(j=0 ; j< (BLOCKSIZE/frag_size); j++){
426 if(memcmp((char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size), frag_size) != 0){
427 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
428 XBT_DEBUG("Different data in fragment %zu (addr_frag1 = %p - addr_frag2 = %p) of block %zu", j + 1, (char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size), i);
446 if( i != first_block_free){
448 if(mdp1->heapinfo[block_free].free.next != mdp2->heapinfo[block_free].free.next){
449 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
450 XBT_DEBUG("Different next block free");
457 block_free = mdp1->heapinfo[block_free].free.next;
458 next_block_free = mdp1->heapinfo[block_free].free.next;
460 i = block_free + mdp1->heapinfo[block_free].free.size;
462 if((next_block_free == 0) && (i != mdp1->heaplimit)){
464 while(i < mdp1->heaplimit){
466 if(mdp1->heapinfo[i].busy.type != mdp2->heapinfo[i].busy.type){
467 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
468 XBT_DEBUG("Different type of busy block");
475 addr_block1 = (char *)mdp1 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE);
476 addr_block2 = (char *)mdp2 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE);
478 switch(mdp1->heapinfo[i].busy.type){
480 if(mdp1->heapinfo[i].busy.info.block.size != mdp2->heapinfo[i].busy.info.block.size){
481 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
482 XBT_DEBUG("Different size of a large cluster");
488 if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy.info.block.size * BLOCKSIZE)) != 0){
489 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
490 XBT_DEBUG("Different data in block %zu (addr_block1 = %p (current = %p) - addr_block2 = %p)", i, addr_block1, (char *)std_heap_addr + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE), addr_block2);
498 i = i+mdp1->heapinfo[i].busy.info.block.size;
502 if(mdp1->heapinfo[i].busy.info.frag.nfree != mdp2->heapinfo[i].busy.info.frag.nfree){
503 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
504 XBT_DEBUG("Different free fragments in the fragmented block %zu", i);
510 if(mdp1->heapinfo[i].busy.info.frag.first != mdp2->heapinfo[i].busy.info.frag.first){
511 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
512 XBT_DEBUG("Different first free fragments in the block %zu", i);
518 frag_size = pow(2,mdp1->heapinfo[i].busy.type);
519 for(j=0 ; j< (BLOCKSIZE/frag_size); j++){
520 if(memcmp((char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size), frag_size) != 0){
521 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
522 XBT_DEBUG("Different data in fragment %zu (addr_frag1 = %p - addr_frag2 = %p) of block %zu", j + 1, (char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size), i);
548 void mmalloc_display_info_heap(xbt_mheap_t h){
552 /* Useless prototype to make gcc happy */
553 void *valloc(size_t size);
555 void *valloc(size_t size)
556 { //FIXME: won't work
557 return mvalloc(NULL, size);