1 /* mm_diff - Memory snapshooting and comparison */
3 /* Copyright (c) 2008-2012. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "xbt/ex_interface.h" /* internals of backtrace setup */
11 #include "xbt/mmalloc.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
14 "Logging specific to mm_diff in mmalloc");
16 xbt_dynar_t mc_heap_comparison_ignore;
17 xbt_dynar_t stacks_areas;
19 static void heap_area_pair_free(heap_area_pair_t pair);
20 static void heap_area_pair_free_voidp(void *d);
21 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
22 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
23 static heap_area_t new_heap_area(int block, int fragment);
25 static size_t heap_comparison_ignore_size(void *address);
26 static void add_heap_equality(xbt_dynar_t equals, void *a1, void *a2);
27 static void remove_heap_equality(xbt_dynar_t equals, int address, void *a);
29 static char* is_stack(void *address);
31 void mmalloc_backtrace_block_display(void* heapinfo, int block){
35 /* if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) { */
36 /* fprintf(stderr, "No backtrace available for that block, sorry.\n"); */
40 /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE); */
41 /* e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size; */
43 /* xbt_ex_setup_backtrace(&e); */
44 /* if (e.used == 0) { */
45 /* fprintf(stderr, "(backtrace not set)\n"); */
46 /* } else if (e.bt_strings == NULL) { */
47 /* fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
51 /* fprintf(stderr, "Backtrace of where the block %d was malloced (%d frames):\n", block ,e.used); */
52 /* for (i = 0; i < e.used; i++) /\* no need to display "xbt_backtrace_display" *\/{ */
53 /* fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
58 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
62 /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE); */
63 /* e.used = XBT_BACKTRACE_SIZE; */
65 /* xbt_ex_setup_backtrace(&e); */
66 /* if (e.used == 0) { */
67 /* fprintf(stderr, "(backtrace not set)\n"); */
68 /* } else if (e.bt_strings == NULL) { */
69 /* fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
73 /* fprintf(stderr, "Backtrace of where the fragment %d in block %d was malloced (%d frames):\n", frag, block ,e.used); */
74 /* for (i = 0; i < e.used; i++) /\* no need to display "xbt_backtrace_display" *\/{ */
75 /* fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
81 void mmalloc_backtrace_display(void *addr){
83 /* size_t block, frag_nb; */
86 /* xbt_mheap_t heap = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit(); */
88 /* block = (((char*) (addr) - (char*) heap -> heapbase) / BLOCKSIZE + 1); */
90 /* type = heap->heapinfo[block].type; */
93 /* case -1 : /\* Free block *\/ */
94 /* fprintf(stderr, "Asked to display the backtrace of a block that is free. I'm puzzled\n"); */
97 /* case 0: /\* Large block *\/ */
98 /* mmalloc_backtrace_block_display(heap->heapinfo, block); */
100 /* default: /\* Fragmented block *\/ */
101 /* frag_nb = RESIDUAL(addr, BLOCKSIZE) >> type; */
102 /* if(heap->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){ */
103 /* fprintf(stderr , "Asked to display the backtrace of a fragment that is free. I'm puzzled\n"); */
106 /* mmalloc_backtrace_fragment_display(heap->heapinfo, block, frag_nb); */
112 void *s_heap = NULL, *heapbase1 = NULL, *heapbase2 = NULL;
113 malloc_info *heapinfo1 = NULL, *heapinfo2 = NULL;
114 size_t heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
118 void init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2){
120 heaplimit = ((struct mdesc *)heap1)->heaplimit;
122 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
124 heapbase1 = (char *)heap1 + BLOCKSIZE;
125 heapbase2 = (char *)heap2 + BLOCKSIZE;
127 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
128 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
130 heapsize1 = heap1->heapsize;
131 heapsize2 = heap2->heapsize;
135 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t *stack1, xbt_dynar_t *stack2, xbt_dynar_t equals){
137 if(heap1 == NULL && heap1 == NULL){
138 XBT_DEBUG("Malloc descriptors null");
142 if(heap1->heaplimit != heap2->heaplimit){
143 XBT_DEBUG("Different limit of valid info table indices");
147 /* Start comparison */
148 size_t i1, i2, j1, j2, k, current_block, current_fragment;
149 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
150 void *snap_addr_block1, *snap_addr_block2;
152 int nb_diff1 = 0, nb_diff2 = 0;
154 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
156 int equal, res_compare = 0;
158 /* Check busy blocks*/
162 while(i1 <= heaplimit){
166 if(heapinfo1[i1].type == -1){ /* Free block */
171 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
172 snap_addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)heapbase1));
174 if(heapinfo1[i1].type == 0){ /* Large block */
176 if((xbt_dynar_length(*stack1) < xbt_dynar_length(stacks_areas)) && ((stack_name = is_stack(addr_block1)) != NULL)){
177 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
178 stack->address = snap_addr_block1;
179 stack->process_name = strdup(stack_name);
180 stack->size = heapinfo1[i1].busy_block.busy_size;
181 xbt_dynar_push(*stack1, &stack);
185 if(heapinfo1[i1].busy_block.equal_to != NULL){
194 /* Try first to associate to same block in the other heap */
195 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
197 if(heapinfo2[current_block].busy_block.equal_to == NULL){
199 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
200 snap_addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
202 if((xbt_dynar_length(*stack2) < xbt_dynar_length(stacks_areas)) && ((stack_name = is_stack(addr_block2)) != NULL)){
203 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
204 stack->address = snap_addr_block2;
205 stack->process_name = strdup(stack_name);
206 stack->size = heapinfo2[current_block].busy_block.busy_size;
207 xbt_dynar_push(*stack2, &stack);
211 res_compare = compare_area(addr_block1, addr_block2, previous, equals);
213 if(res_compare == 0 || res_compare == -1){
214 for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
215 heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
216 for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
217 heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
219 match_equals(previous, equals);
220 i1 = i1 + heapinfo1[current_block].busy_block.size;
223 xbt_dynar_reset(previous);
229 while(i2 <= heaplimit && !equal){
231 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
232 snap_addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
234 if((xbt_dynar_length(*stack2) < xbt_dynar_length(stacks_areas)) && ((stack_name = is_stack(addr_block2)) != NULL)){
235 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
236 stack->address = snap_addr_block2;
237 stack->process_name = strdup(stack_name);
238 stack->size = heapinfo2[i2].busy_block.busy_size;
239 xbt_dynar_push(*stack2, &stack);
243 if(i2 == current_block){
248 if(heapinfo2[i2].type != 0){
253 if(heapinfo2[i2].busy_block.equal_to != NULL){
258 res_compare = compare_area(addr_block1, addr_block2, previous, equals);
260 if(res_compare == 0 || res_compare == -1){
261 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
262 heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
263 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
264 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
266 match_equals(previous, equals);
267 i1 = i1 + heapinfo1[i1].busy_block.size;
270 xbt_dynar_reset(previous);
277 XBT_DEBUG("Block %zu not found (size_used = %zu)", i1, heapinfo1[i1].busy_block.busy_size);
282 }else{ /* Fragmented block */
284 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
286 current_fragment = j1;
288 if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
291 if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
294 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
299 /* Try first to associate to same fragment in the other heap */
300 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
302 if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){
304 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
305 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << ((xbt_mheap_t)s_heap)->heapinfo[current_block].type));
307 res_compare = compare_area(addr_frag1, addr_frag2, previous, equals);
309 if(res_compare == 0){
311 match_equals(previous, equals);
314 xbt_dynar_reset(previous);
319 while(i2 <= heaplimit && !equal){
322 if(heapinfo2[i2].type <= 0){
327 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
329 if(heapinfo2[i2].type == heapinfo1[i1].type && i2 == current_block && j2 == current_fragment)
332 if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)
335 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
336 addr_frag2 = (void*) ((char *)addr_block2 + (j2 << ((xbt_mheap_t)s_heap)->heapinfo[i2].type));
338 res_compare = compare_area(addr_frag1, addr_frag2, previous, equals);
340 if(res_compare == 0){
342 match_equals(previous, equals);
343 xbt_dynar_reset(previous);
347 xbt_dynar_reset(previous);
355 if(heapinfo1[i1].busy_frag.equal_to[j1] == NULL){
356 XBT_DEBUG("Block %zu, fragment %zu not found (size_used = %d)", i1, j1, heapinfo1[i1].busy_frag.frag_size[j1]);
371 /* All blocks/fragments are equal to another block/fragment ? */
375 if(heapinfo1[i].type == 0){
376 if(current_block == heaplimit){
377 if(heapinfo1[i].busy_block.busy_size > 0){
378 if(heapinfo1[i].busy_block.equal_to == NULL){
379 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
380 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
381 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
382 //mmalloc_backtrace_block_display((void*)heapinfo1, i);
388 xbt_free(heapinfo1[i].busy_block.equal_to);
389 heapinfo1[i].busy_block.equal_to = NULL;
391 if(heapinfo1[i].type > 0){
392 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
393 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
394 if(current_block == heaplimit){
395 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
396 if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
397 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
398 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
399 XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
400 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
406 xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
407 heapinfo1[i].busy_frag.equal_to[j] = NULL;
416 if(heapinfo2[i].type == 0){
417 if(current_block == heaplimit){
418 if(heapinfo2[i].busy_block.busy_size > 0){
419 if(heapinfo2[i].busy_block.equal_to == NULL){
420 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
421 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
422 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
423 //mmalloc_backtrace_block_display((void*)heapinfo2, i);
429 xbt_free(heapinfo2[i].busy_block.equal_to);
430 heapinfo2[i].busy_block.equal_to = NULL;
432 if(heapinfo2[i].type > 0){
433 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
434 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
435 if(current_block == heaplimit){
436 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
437 if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
438 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
439 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
440 XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
441 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
447 xbt_free(heapinfo2[i].busy_frag.equal_to[j]);
448 heapinfo2[i].busy_frag.equal_to[j] = NULL;
454 xbt_dynar_free(&previous);
456 s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
457 heapinfo1 = NULL, heapinfo2 = NULL;
458 heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
460 return ((nb_diff1 > 0) || (nb_diff2 > 0));
463 static heap_area_t new_heap_area(int block, int fragment){
464 heap_area_t area = NULL;
465 area = xbt_new0(s_heap_area_t, 1);
467 area->fragment = fragment;
472 static size_t heap_comparison_ignore_size(void *address){
474 unsigned int cursor = 0;
476 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
477 mc_heap_ignore_region_t region;
480 cursor = (start + end) / 2;
481 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
482 if(region->address == address)
484 if(region->address < address)
486 if(region->address > address)
494 int compare_area(void *area1, void* area2, xbt_dynar_t previous, xbt_dynar_t equals){
496 size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
497 void *address_pointed1, *address_pointed2;
499 size_t block1, frag1, block2, frag2, size;
500 int check_ignore = 0;
502 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
503 void *area1_to_compare, *area2_to_compare;
507 if(previous == NULL){
508 previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
512 block1 = ((char*)area1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
513 block2 = ((char*)area2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
515 if(((char *)area1 < (char*)((xbt_mheap_t)s_heap)->heapbase) || (block1 > heapsize1) || (block1 < 1) || ((char *)area2 < (char*)((xbt_mheap_t)s_heap)->heapbase) || (block2 > heapsize2) || (block2 < 1))
518 addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)heapbase1));
519 addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)heapbase2));
521 if(heapinfo1[block1].type == heapinfo2[block2].type){
523 if(heapinfo1[block1].type == -1){
525 }else if(heapinfo1[block1].type == 0){
526 if(heapinfo1[block1].busy_block.size != heapinfo2[block2].busy_block.size)
528 if(heapinfo1[block1].busy_block.busy_size != heapinfo2[block2].busy_block.busy_size)
530 if(!add_heap_area_pair(previous, block1, -1, block2, -1))
533 size = heapinfo1[block1].busy_block.busy_size;
537 area1_to_compare = addr_block1;
538 area2_to_compare = addr_block2;
540 if((ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)) && heapinfo1[block1].busy_block.ignore == 1)
543 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
544 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
546 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2])
548 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2))
551 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
552 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
554 area1_to_compare = addr_frag1;
555 area2_to_compare = addr_frag2;
557 size = heapinfo1[block1].busy_frag.frag_size[frag1];
562 if((ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)) && heapinfo1[block1].busy_frag.ignore[frag1] == 1)
565 }else if((heapinfo1[block1].type > 0) && (heapinfo2[block2].type > 0)){
566 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
567 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
569 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2])
571 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2))
574 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
575 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
577 area1_to_compare = addr_frag1;
578 area2_to_compare = addr_frag2;
580 size = heapinfo1[block1].busy_frag.frag_size[frag1];
585 if((ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)) && heapinfo1[block1].busy_frag.ignore[frag1] == 1)
593 if((ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)) && check_ignore){
595 if((ignore1 = heap_comparison_ignore_size((char *)area1 + i)) > 0){
596 if((ignore2 = heap_comparison_ignore_size((char *)area2 + i)) == ignore1){
604 if(memcmp(((char *)area1_to_compare) + i, ((char *)area2_to_compare) + i, 1) != 0){
606 /* Check pointer difference */
607 pointer_align = (i / sizeof(void*)) * sizeof(void*);
608 address_pointed1 = *((void **)((char *)area1_to_compare + pointer_align));
609 address_pointed2 = *((void **)((char *)area2_to_compare + pointer_align));
611 res_compare = compare_area(address_pointed1, address_pointed2, previous, equals);
616 i = pointer_align + sizeof(void *);
626 match_equals(previous, equals);
633 static void heap_area_pair_free(heap_area_pair_t pair){
640 static void heap_area_pair_free_voidp(void *d)
642 heap_area_pair_free((heap_area_pair_t) * (void **) d);
645 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
647 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
648 heap_area_pair_t pair = NULL;
649 pair = xbt_new0(s_heap_area_pair_t, 1);
650 pair->block1 = block1;
651 pair->fragment1 = fragment1;
652 pair->block2 = block2;
653 pair->fragment2 = fragment2;
655 xbt_dynar_push(list, &pair);
663 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
665 unsigned int cursor = 0;
666 heap_area_pair_t current_pair;
668 xbt_dynar_foreach(list, cursor, current_pair){
669 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
676 void match_equals(xbt_dynar_t list, xbt_dynar_t equals){
678 unsigned int cursor = 0;
679 heap_area_pair_t current_pair;
680 heap_area_t previous_area;
682 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
684 xbt_dynar_foreach(list, cursor, current_pair){
686 if(current_pair->fragment1 != -1){
688 real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
689 real_addr_frag1 = (void*) ((char *)real_addr_block1 + (current_pair->fragment1 << heapinfo1[current_pair->block1].type));
690 real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
691 real_addr_frag2 = (void*) ((char *)real_addr_block2 + (current_pair->fragment2 << heapinfo2[current_pair->block2].type));
693 if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){
694 remove_heap_equality(equals, 1, real_addr_frag1);
695 previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
696 xbt_free(heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
697 heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
698 xbt_free(previous_area);
700 if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){
701 remove_heap_equality(equals, 2, real_addr_frag2);
702 previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
703 xbt_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
704 heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
705 xbt_free(previous_area);
708 if(real_addr_frag1 != real_addr_frag2)
709 add_heap_equality(equals, real_addr_frag1, real_addr_frag2);
711 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
712 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
716 real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
717 real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
719 if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
720 remove_heap_equality(equals, 1, real_addr_block1);
721 previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
722 xbt_free(heapinfo2[previous_area->block].busy_block.equal_to);
723 heapinfo2[previous_area->block].busy_block.equal_to = NULL;
724 xbt_free(previous_area);
726 if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
727 remove_heap_equality(equals, 2, real_addr_block2);
728 previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
729 xbt_free(heapinfo1[previous_area->block].busy_block.equal_to);
730 heapinfo1[previous_area->block].busy_block.equal_to = NULL;
731 xbt_free(previous_area);
734 if(real_addr_block1 != real_addr_block2)
735 add_heap_equality(equals, real_addr_block1, real_addr_block2);
737 heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
738 heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
747 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
750 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
752 if(heap1 == NULL && heap1 == NULL){
753 XBT_DEBUG("Malloc descriptors null");
757 if(heap1->heaplimit != heap2->heaplimit){
758 XBT_DEBUG("Different limit of valid info table indices");
762 /* Heap information */
763 heaplimit = ((struct mdesc *)heap1)->heaplimit;
765 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
767 heapbase1 = (char *)heap1 + BLOCKSIZE;
768 heapbase2 = (char *)heap2 + BLOCKSIZE;
770 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
771 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
773 heapsize1 = heap1->heapsize;
774 heapsize2 = heap2->heapsize;
776 /* Start comparison */
778 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
782 /* Check busy blocks*/
786 while(i <= heaplimit){
788 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
789 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
791 if(heapinfo1[i].type != heapinfo2[i].type){
793 distance += BLOCKSIZE;
794 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
799 if(heapinfo1[i].type == -1){ /* Free block */
804 if(heapinfo1[i].type == 0){ /* Large block */
806 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
807 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
808 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
809 XBT_DEBUG("Different larger of cluster at block %zu : %zu - %zu -> distance = %d", i, heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size, distance);
813 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
814 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
815 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
816 XBT_DEBUG("Different size used oin large cluster at block %zu : %zu - %zu -> distance = %d", i, heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size, distance);
822 //while(k < (heapinfo1[i].busy_block.busy_size)){
823 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
824 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
832 }else { /* Fragmented block */
834 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
836 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
837 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
839 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
844 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
845 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
846 XBT_DEBUG("Different size used in fragment %zu in block %zu : %d - %d -> distance = %d", j, i, heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j], distance);
852 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
853 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
854 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
874 static char* is_stack(void *address){
875 unsigned int cursor = 0;
876 stack_region_t stack;
878 xbt_dynar_foreach(stacks_areas, cursor, stack){
879 if(address == stack->address)
880 return stack->process_name;
886 static void add_heap_equality(xbt_dynar_t equals, void *a1, void *a2){
888 if(xbt_dynar_is_empty(equals)){
890 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
894 xbt_dynar_insert_at(equals, 0, &he);
898 unsigned int cursor = 0;
900 int end = xbt_dynar_length(equals) - 1;
901 heap_equality_t current_equality = NULL;
904 cursor = (start + end) / 2;
905 current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
906 if(current_equality->address1 == a1){
907 if(current_equality->address2 == a2)
909 if(current_equality->address2 < a2)
911 if(current_equality->address2 > a2)
914 if(current_equality->address1 < a1)
916 if(current_equality->address1 > a1)
920 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
924 if(current_equality->address1 < a1)
925 xbt_dynar_insert_at(equals, cursor + 1 , &he);
927 xbt_dynar_insert_at(equals, cursor, &he);
933 static void remove_heap_equality(xbt_dynar_t equals, int address, void *a){
935 unsigned int cursor = 0;
936 heap_equality_t current_equality;
942 int end = xbt_dynar_length(equals) - 1;
945 while(start <= end && found == 0){
946 cursor = (start + end) / 2;
947 current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
948 if(current_equality->address1 == a)
950 if(current_equality->address1 < a)
952 if(current_equality->address1 > a)
957 xbt_dynar_remove_at(equals, cursor, NULL);
961 xbt_dynar_foreach(equals, cursor, current_equality){
962 if(current_equality->address2 == a){
969 xbt_dynar_remove_at(equals, cursor, NULL);
975 int is_free_area(void *area, xbt_mheap_t heap){
977 void *sheap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
978 malloc_info *heapinfo = (malloc_info *)((char *)heap + ((uintptr_t)((char *)heap->heapinfo - (char *)sheap)));
979 size_t heapsize = heap->heapsize;
981 /* Get block number */
982 size_t block = ((char*)area - (char*)((xbt_mheap_t)sheap)->heapbase) / BLOCKSIZE + 1;
985 /* Check if valid block number */
986 if((char *)area < (char*)((xbt_mheap_t)sheap)->heapbase || block > heapsize || block < 1)
989 if(heapinfo[block].type < 0)
992 if(heapinfo[block].type == 0)
995 if(heapinfo[block].type > 0){
996 fragment = ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
997 if(heapinfo[block].busy_frag.frag_size[fragment] == 0)