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 struct mdesc *__mmalloc_default_mdp;
23 static void *__mmalloc_current_heap = NULL; /* The heap we are currently using. */
25 #include "xbt_modinter.h"
27 void *mmalloc_get_current_heap(void)
29 return __mmalloc_current_heap;
32 void mmalloc_set_current_heap(void *new_heap)
34 __mmalloc_current_heap = new_heap;
37 #ifdef MMALLOC_WANT_OVERIDE_LEGACY
38 void *malloc(size_t n)
40 void *mdp = __mmalloc_current_heap;
46 void *ret = mmalloc(mdp, n);
52 void *calloc(size_t nmemb, size_t size)
54 size_t total_size = nmemb * size;
55 void *mdp = __mmalloc_current_heap;
61 void *ret = mmalloc(mdp, total_size);
64 /* Fill the allocated memory with zeroes to mimic calloc behaviour */
65 memset(ret, '\0', total_size);
70 void *realloc(void *p, size_t s)
73 void *mdp = __mmalloc_current_heap;
81 ret = mrealloc(mdp, p, s);
83 ret = mmalloc(mdp, s);
94 void *mdp = __mmalloc_current_heap;
104 /* Make sure it works with md==NULL */
106 /* Safety gap from the heap's break address.
107 * Try to increase this first if you experience strange errors under
109 #define HEAP_OFFSET (128UL<<20)
111 void *mmalloc_get_default_md(void)
113 xbt_assert(__mmalloc_default_mdp);
114 return __mmalloc_default_mdp;
117 static void mmalloc_fork_prepare(void)
119 struct mdesc* mdp = NULL;
120 if ((mdp =__mmalloc_default_mdp)){
126 mdp = mdp->next_mdesc;
131 static void mmalloc_fork_parent(void)
133 struct mdesc* mdp = NULL;
134 if ((mdp =__mmalloc_default_mdp)){
138 mdp = mdp->next_mdesc;
143 static void mmalloc_fork_child(void)
145 struct mdesc* mdp = NULL;
146 if ((mdp =__mmalloc_default_mdp)){
149 mdp = mdp->next_mdesc;
154 /* Initialize the default malloc descriptor. */
155 void mmalloc_preinit(void)
158 if (!__mmalloc_default_mdp) {
159 unsigned long mask = ~((unsigned long)getpagesize() - 1);
160 void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
161 __mmalloc_default_mdp = mmalloc_attach(-1, addr);
162 /* Fixme? only the default mdp in protected against forks */
163 res = xbt_os_thread_atfork(mmalloc_fork_prepare,
164 mmalloc_fork_parent, mmalloc_fork_child);
166 THROWF(system_error,0,"xbt_os_thread_atfork() failed: return value %d",res);
168 xbt_assert(__mmalloc_default_mdp != NULL);
171 void mmalloc_postexit(void)
173 /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
174 // mmalloc_detach(__mmalloc_default_mdp);
175 mmalloc_pre_detach(__mmalloc_default_mdp);
178 int mmalloc_compare_heap(void *h1, void *h2, void *std_heap_addr){
180 if(h1 == NULL && h2 == NULL){
181 XBT_DEBUG("Malloc descriptors null");
189 struct mstats ms1 = mmstats(h1);
190 struct mstats ms2 = mmstats(h2);
192 if(ms1.chunks_used != ms2.chunks_used){
193 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
194 XBT_DEBUG("Different chunks allocated by the user : %zu - %zu", ms1.chunks_used, ms2.chunks_used);
201 if(ms1.bytes_used != ms2.bytes_used){
202 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
203 XBT_DEBUG("Different byte total of user-allocated chunks : %zu - %zu", ms1.bytes_used, ms2.bytes_used);
210 if(ms1.bytes_free != ms2.bytes_free){
211 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
212 XBT_DEBUG("Different byte total of chunks in the free list : %zu - %zu", ms1.bytes_free, ms2.bytes_free);
219 if(ms1.chunks_free != ms2.chunks_free){
220 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
221 XBT_DEBUG("Different chunks in the free list : %zu - %zu", ms1.chunks_free, ms2.chunks_free);
228 struct mdesc *mdp1, *mdp2;
229 mdp1 = MD_TO_MDP(h1);
230 mdp2 = MD_TO_MDP(h2);
232 int res = mmalloc_compare_mdesc(mdp1, mdp2, std_heap_addr);
234 return ((errors + res ) > 0);
238 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2, void *std_heap_addr){
242 if(mdp1->headersize != mdp2->headersize){
243 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
244 XBT_DEBUG("Different size of the file header for the mapped files");
251 if(mdp1->refcount != mdp2->refcount){
252 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
253 XBT_DEBUG("Different number of processes that attached the heap");
260 if(strcmp(mdp1->magic, mdp2->magic) != 0){
261 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
262 XBT_DEBUG("Different magic number");
269 if(mdp1->flags != mdp2->flags){
270 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
271 XBT_DEBUG("Different flags");
278 if(mdp1->heapsize != mdp2->heapsize){
279 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
280 XBT_DEBUG("Different number of info entries");
287 //XBT_DEBUG("Heap size : %zu", mdp1->heapsize);
289 if(mdp1->heapbase != mdp2->heapbase){
290 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
291 XBT_DEBUG("Different first block of the heap");
299 if(mdp1->heapindex != mdp2->heapindex){
300 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
301 XBT_DEBUG("Different index for the heap table : %zu - %zu", mdp1->heapindex, mdp2->heapindex);
308 //XBT_DEBUG("Heap index : %zu", mdp1->heapindex);
310 if(mdp1->base != mdp2->base){
311 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
312 XBT_DEBUG("Different base address of the memory region");
319 if(mdp1->breakval != mdp2->breakval){
320 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
321 XBT_DEBUG("Different current location in the memory region");
328 if(mdp1->top != mdp2->top){
329 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
330 XBT_DEBUG("Different end of the current location in the memory region");
337 if(mdp1->heaplimit != mdp2->heaplimit){
338 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
339 XBT_DEBUG("Different limit of valid info table indices");
346 if(mdp1->fd != mdp2->fd){
347 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
348 XBT_DEBUG("Different file descriptor for the file to which this malloc heap is mapped");
355 if(mdp1->version != mdp2->version){
356 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
357 XBT_DEBUG("Different version of the mmalloc package");
365 size_t block_free1, block_free2 , next_block_free, first_block_free, block_free ;
367 void *addr_block1, *addr_block2;
371 /* Search index of the first free block */
373 block_free1 = mdp1->heapindex;
374 block_free2 = mdp2->heapindex;
376 while(mdp1->heapinfo[block_free1].free.prev != 0){
377 block_free1 = mdp1->heapinfo[block_free1].free.prev;
380 while(mdp2->heapinfo[block_free2].free.prev != 0){
381 block_free2 = mdp1->heapinfo[block_free2].free.prev;
384 if(block_free1 != block_free2){
385 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
386 XBT_DEBUG("Different first free block");
393 first_block_free = block_free1;
395 if(mdp1->heapinfo[first_block_free].free.size != mdp2->heapinfo[first_block_free].free.size){
396 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
397 XBT_DEBUG("Different size (in blocks) of the first free cluster");
404 /* Check busy blocks (circular checking)*/
406 i = first_block_free + mdp1->heapinfo[first_block_free].free.size;
408 if(mdp1->heapinfo[first_block_free].free.next != mdp2->heapinfo[first_block_free].free.next){
409 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
410 XBT_DEBUG("Different next block free");
417 block_free = first_block_free;
418 next_block_free = mdp1->heapinfo[first_block_free].free.next;
420 if(next_block_free == 0)
421 next_block_free = mdp1->heaplimit;
423 while(i != first_block_free){
425 while(i<next_block_free){
427 if(mdp1->heapinfo[i].busy.type != mdp2->heapinfo[i].busy.type){
428 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
429 XBT_DEBUG("Different type of busy block");
436 addr_block1 = (char *)mdp1 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE);
437 addr_block2 = (char *)mdp2 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE);
439 switch(mdp1->heapinfo[i].busy.type){
441 if(mdp1->heapinfo[i].busy.info.block.size != mdp2->heapinfo[i].busy.info.block.size){
442 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
443 XBT_DEBUG("Different size of a large cluster");
449 if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy.info.block.size * BLOCKSIZE)) != 0){
450 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
451 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);
458 i = i+mdp1->heapinfo[i].busy.info.block.size;
462 if(mdp1->heapinfo[i].busy.info.frag.nfree != mdp2->heapinfo[i].busy.info.frag.nfree){
463 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
464 XBT_DEBUG("Different free fragments in the fragmented block %zu", i);
470 if(mdp1->heapinfo[i].busy.info.frag.first != mdp2->heapinfo[i].busy.info.frag.first){
471 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
472 XBT_DEBUG("Different first free fragments in the block %zu", i);
478 frag_size = pow(2,mdp1->heapinfo[i].busy.type);
479 for(j=0 ; j< (BLOCKSIZE/frag_size); j++){
480 if(memcmp((char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size), frag_size) != 0){
481 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
482 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);
500 if( i != first_block_free){
502 if(mdp1->heapinfo[block_free].free.next != mdp2->heapinfo[block_free].free.next){
503 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
504 XBT_DEBUG("Different next block free");
511 block_free = mdp1->heapinfo[block_free].free.next;
512 next_block_free = mdp1->heapinfo[block_free].free.next;
514 i = block_free + mdp1->heapinfo[block_free].free.size;
516 if((next_block_free == 0) && (i != mdp1->heaplimit)){
518 while(i < mdp1->heaplimit){
520 if(mdp1->heapinfo[i].busy.type != mdp2->heapinfo[i].busy.type){
521 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
522 XBT_DEBUG("Different type of busy block");
529 addr_block1 = (char *)mdp1 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE);
530 addr_block2 = (char *)mdp2 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE);
532 switch(mdp1->heapinfo[i].busy.type){
534 if(mdp1->heapinfo[i].busy.info.block.size != mdp2->heapinfo[i].busy.info.block.size){
535 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
536 XBT_DEBUG("Different size of a large cluster");
542 if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy.info.block.size * BLOCKSIZE)) != 0){
543 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
544 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);
552 i = i+mdp1->heapinfo[i].busy.info.block.size;
556 if(mdp1->heapinfo[i].busy.info.frag.nfree != mdp2->heapinfo[i].busy.info.frag.nfree){
557 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
558 XBT_DEBUG("Different free fragments in the fragmented block %zu", i);
564 if(mdp1->heapinfo[i].busy.info.frag.first != mdp2->heapinfo[i].busy.info.frag.first){
565 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
566 XBT_DEBUG("Different first free fragments in the block %zu", i);
572 frag_size = pow(2,mdp1->heapinfo[i].busy.type);
573 for(j=0 ; j< (BLOCKSIZE/frag_size); j++){
574 if(memcmp((char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size), frag_size) != 0){
575 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
576 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);
602 void mmalloc_display_info_heap(void *h){