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 int in_mc_comparison_ignore(int block, int fragment);
26 static size_t heap_comparison_ignore_size(void *address);
27 static void add_heap_equality(xbt_dynar_t *equals, void *a1, void *a2);
28 static void remove_heap_equality(xbt_dynar_t *equals, int address, void *a);
30 static char* is_stack(void *address);
32 void mmalloc_backtrace_block_display(void* heapinfo, int block){
36 /* if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) { */
37 /* fprintf(stderr, "No backtrace available for that block, sorry.\n"); */
41 /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE); */
42 /* e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size; */
44 /* xbt_ex_setup_backtrace(&e); */
45 /* if (e.used == 0) { */
46 /* fprintf(stderr, "(backtrace not set)\n"); */
47 /* } else if (e.bt_strings == NULL) { */
48 /* fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
52 /* fprintf(stderr, "Backtrace of where the block %d was malloced (%d frames):\n", block ,e.used); */
53 /* for (i = 0; i < e.used; i++) /\* no need to display "xbt_backtrace_display" *\/{ */
54 /* fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
59 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
63 /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE); */
64 /* e.used = XBT_BACKTRACE_SIZE; */
66 /* xbt_ex_setup_backtrace(&e); */
67 /* if (e.used == 0) { */
68 /* fprintf(stderr, "(backtrace not set)\n"); */
69 /* } else if (e.bt_strings == NULL) { */
70 /* fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
74 /* fprintf(stderr, "Backtrace of where the fragment %d in block %d was malloced (%d frames):\n", frag, block ,e.used); */
75 /* for (i = 0; i < e.used; i++) /\* no need to display "xbt_backtrace_display" *\/{ */
76 /* fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
82 void mmalloc_backtrace_display(void *addr){
84 /* size_t block, frag_nb; */
87 /* xbt_mheap_t heap = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit(); */
89 /* block = (((char*) (addr) - (char*) heap -> heapbase) / BLOCKSIZE + 1); */
91 /* type = heap->heapinfo[block].type; */
94 /* case -1 : /\* Free block *\/ */
95 /* fprintf(stderr, "Asked to display the backtrace of a block that is free. I'm puzzled\n"); */
98 /* case 0: /\* Large block *\/ */
99 /* mmalloc_backtrace_block_display(heap->heapinfo, block); */
101 /* default: /\* Fragmented block *\/ */
102 /* frag_nb = RESIDUAL(addr, BLOCKSIZE) >> type; */
103 /* if(heap->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){ */
104 /* fprintf(stderr , "Asked to display the backtrace of a fragment that is free. I'm puzzled\n"); */
107 /* mmalloc_backtrace_fragment_display(heap->heapinfo, block, frag_nb); */
113 void *s_heap = NULL, *heapbase1 = NULL, *heapbase2 = NULL;
114 malloc_info *heapinfo1 = NULL, *heapinfo2 = NULL;
115 size_t heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
119 void init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2){
121 heaplimit = ((struct mdesc *)heap1)->heaplimit;
123 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
125 heapbase1 = (char *)heap1 + BLOCKSIZE;
126 heapbase2 = (char *)heap2 + BLOCKSIZE;
128 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
129 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
131 heapsize1 = heap1->heapsize;
132 heapsize2 = heap2->heapsize;
136 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t *stack1, xbt_dynar_t *stack2, xbt_dynar_t *equals){
138 if(heap1 == NULL && heap1 == NULL){
139 XBT_DEBUG("Malloc descriptors null");
143 if(heap1->heaplimit != heap2->heaplimit){
144 XBT_DEBUG("Different limit of valid info table indices");
148 /* Start comparison */
149 size_t i1, i2, j1, j2, k, current_block, current_fragment;
150 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
151 void *snap_addr_block1, *snap_addr_block2;
153 int nb_diff1 = 0, nb_diff2 = 0;
155 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
157 int equal, res_compare = 0;
159 /* Check busy blocks*/
163 while(i1 <= heaplimit){
167 if(heapinfo1[i1].type == -1){ /* Free block */
172 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
173 snap_addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)heapbase1));
175 if(heapinfo1[i1].type == 0){ /* Large block */
177 if((xbt_dynar_length(*stack1) < xbt_dynar_length(stacks_areas)) && ((stack_name = is_stack(addr_block1)) != NULL)){
178 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
179 stack->address = snap_addr_block1;
180 stack->process_name = strdup(stack_name);
181 stack->size = heapinfo1[i1].busy_block.busy_size;
182 xbt_dynar_push(*stack1, &stack);
186 if(heapinfo1[i1].busy_block.equal_to != NULL){
195 /* Try first to associate to same block in the other heap */
196 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
198 if(heapinfo2[current_block].busy_block.equal_to == NULL){
200 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
201 snap_addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
203 if((xbt_dynar_length(*stack2) < xbt_dynar_length(stacks_areas)) && ((stack_name = is_stack(addr_block2)) != NULL)){
204 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
205 stack->address = snap_addr_block2;
206 stack->process_name = strdup(stack_name);
207 stack->size = heapinfo2[current_block].busy_block.busy_size;
208 xbt_dynar_push(*stack2, &stack);
212 res_compare = compare_area(addr_block1, addr_block2, previous);
214 if(res_compare == 0 || res_compare == -1){
215 for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
216 heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
217 for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
218 heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
220 match_equals(previous, equals);
221 i1 = i1 + heapinfo1[current_block].busy_block.size;
224 xbt_dynar_reset(previous);
230 while(i2 <= heaplimit && !equal){
232 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
233 snap_addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
235 if((xbt_dynar_length(*stack2) < xbt_dynar_length(stacks_areas)) && ((stack_name = is_stack(addr_block2)) != NULL)){
236 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
237 stack->address = snap_addr_block2;
238 stack->process_name = strdup(stack_name);
239 stack->size = heapinfo2[i2].busy_block.busy_size;
240 xbt_dynar_push(*stack2, &stack);
244 if(i2 == current_block){
249 if(heapinfo2[i2].type != 0){
254 if(heapinfo2[i2].busy_block.equal_to != NULL){
259 res_compare = compare_area(addr_block1, addr_block2, previous);
261 if(res_compare == 0 || res_compare == -1){
262 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
263 heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
264 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
265 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
267 match_equals(previous, equals);
268 i1 = i1 + heapinfo1[i1].busy_block.size;
271 xbt_dynar_reset(previous);
278 XBT_DEBUG("Block %zu not found (size_used = %zu)", i1, heapinfo1[i1].busy_block.busy_size);
283 }else{ /* Fragmented block */
285 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
287 current_fragment = j1;
289 if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
292 if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
295 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
300 /* Try first to associate to same fragment in the other heap */
301 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
303 if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){
305 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
306 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << ((xbt_mheap_t)s_heap)->heapinfo[current_block].type));
308 res_compare = compare_area(addr_frag1, addr_frag2, previous);
310 if(res_compare == 0){
312 match_equals(previous, equals);
315 xbt_dynar_reset(previous);
320 while(i2 <= heaplimit && !equal){
323 if(heapinfo2[i2].type <= 0){
328 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
330 if(heapinfo2[i2].type == heapinfo1[i1].type && i2 == current_block && j2 == current_fragment)
333 if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)
336 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
337 addr_frag2 = (void*) ((char *)addr_block2 + (j2 << ((xbt_mheap_t)s_heap)->heapinfo[i2].type));
339 res_compare = compare_area(addr_frag1, addr_frag2, previous);
341 if(res_compare == 0){
343 match_equals(previous, equals);
344 xbt_dynar_reset(previous);
348 xbt_dynar_reset(previous);
356 if(heapinfo1[i1].busy_frag.equal_to[j1] == NULL){
357 XBT_DEBUG("Block %zu, fragment %zu not found (size_used = %d)", i1, j1, heapinfo1[i1].busy_frag.frag_size[j1]);
372 /* All blocks/fragments are equal to another block/fragment ? */
376 if(heapinfo1[i].type == 0){
377 if(current_block == heaplimit){
378 if(heapinfo1[i].busy_block.busy_size > 0){
379 if(heapinfo1[i].busy_block.equal_to == NULL){
380 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
381 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
382 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
383 //mmalloc_backtrace_block_display((void*)heapinfo1, i);
389 xbt_free(heapinfo1[i].busy_block.equal_to);
390 heapinfo1[i].busy_block.equal_to = NULL;
392 if(heapinfo1[i].type > 0){
393 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
394 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
395 if(current_block == heaplimit){
396 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
397 if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
398 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
399 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
400 XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
401 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
407 xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
408 heapinfo1[i].busy_frag.equal_to[j] = NULL;
417 if(heapinfo2[i].type == 0){
418 if(current_block == heaplimit){
419 if(heapinfo2[i].busy_block.busy_size > 0){
420 if(heapinfo2[i].busy_block.equal_to == NULL){
421 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
422 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
423 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
424 //mmalloc_backtrace_block_display((void*)heapinfo2, i);
430 xbt_free(heapinfo2[i].busy_block.equal_to);
431 heapinfo2[i].busy_block.equal_to = NULL;
433 if(heapinfo2[i].type > 0){
434 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
435 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
436 if(current_block == heaplimit){
437 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
438 if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
439 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
440 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
441 XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
442 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
448 xbt_free(heapinfo2[i].busy_frag.equal_to[j]);
449 heapinfo2[i].busy_frag.equal_to[j] = NULL;
455 xbt_dynar_free(&previous);
457 s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
458 heapinfo1 = NULL, heapinfo2 = NULL;
459 heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
461 return ((nb_diff1 > 0) || (nb_diff2 > 0));
464 static heap_area_t new_heap_area(int block, int fragment){
465 heap_area_t area = NULL;
466 area = xbt_new0(s_heap_area_t, 1);
468 area->fragment = fragment;
472 static int in_mc_comparison_ignore(int block, int fragment){
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->block == block){
483 if(region->fragment == fragment)
485 if(region->fragment < fragment)
487 if(region->fragment > fragment)
490 if(region->block < block)
492 if(region->block > block)
499 static size_t heap_comparison_ignore_size(void *address){
500 unsigned int cursor = 0;
502 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
503 mc_heap_ignore_region_t region;
506 cursor = (start + end) / 2;
507 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
508 if(region->address == address)
510 if(region->address < address)
512 if(region->address > address)
520 int compare_area(void *area1, void* area2, xbt_dynar_t previous){
522 size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
523 void *address_pointed1, *address_pointed2;
525 size_t block1, frag1, block2, frag2, size;
526 int check_ignore = 0;
528 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
529 void *area1_to_compare, *area2_to_compare;
531 block1 = ((char*)area1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
532 block2 = ((char*)area2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
534 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))
537 addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)heapbase1));
538 addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)heapbase2));
540 if(heapinfo1[block1].type == heapinfo2[block2].type){
542 if(heapinfo1[block1].type == -1){
544 }else if(heapinfo1[block1].type == 0){
545 if(heapinfo1[block1].busy_block.size != heapinfo2[block2].busy_block.size)
547 if(heapinfo1[block1].busy_block.busy_size != heapinfo2[block2].busy_block.busy_size)
549 if(!add_heap_area_pair(previous, block1, -1, block2, -1))
552 size = heapinfo1[block1].busy_block.busy_size;
556 area1_to_compare = addr_block1;
557 area2_to_compare = addr_block2;
559 if((ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)) && heapinfo1[block1].busy_block.ignore == 1)
562 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
563 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
565 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2])
567 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2))
570 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
571 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
573 area1_to_compare = addr_frag1;
574 area2_to_compare = addr_frag2;
576 size = heapinfo1[block1].busy_frag.frag_size[frag1];
578 if((ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)) && heapinfo1[block1].busy_frag.ignore[frag1] == 1)
581 }else if((heapinfo1[block1].type > 0) && (heapinfo2[block2].type > 0)){
582 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
583 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
585 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2])
587 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2))
590 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
591 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
593 area1_to_compare = addr_frag1;
594 area2_to_compare = addr_frag2;
596 size = heapinfo1[block1].busy_frag.frag_size[frag1];
598 if((ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)) && heapinfo1[block1].busy_frag.ignore[frag1] == 1)
608 if((ignore1 = heap_comparison_ignore_size((char *)area1 + i)) > 0){
609 if((ignore2 = heap_comparison_ignore_size((char *)area2 + i)) == ignore1){
617 if(memcmp(((char *)area1_to_compare) + i, ((char *)area2_to_compare) + i, 1) != 0){
619 /* Check pointer difference */
620 pointer_align = (i / sizeof(void*)) * sizeof(void*);
621 address_pointed1 = *((void **)((char *)area1_to_compare + pointer_align));
622 address_pointed2 = *((void **)((char *)area2_to_compare + pointer_align));
624 res_compare = compare_area(address_pointed1, address_pointed2, previous);
629 i = pointer_align + sizeof(void *);
643 static void heap_area_pair_free(heap_area_pair_t pair){
650 static void heap_area_pair_free_voidp(void *d)
652 heap_area_pair_free((heap_area_pair_t) * (void **) d);
655 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
657 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
658 heap_area_pair_t pair = NULL;
659 pair = xbt_new0(s_heap_area_pair_t, 1);
660 pair->block1 = block1;
661 pair->fragment1 = fragment1;
662 pair->block2 = block2;
663 pair->fragment2 = fragment2;
665 xbt_dynar_push(list, &pair);
673 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
675 unsigned int cursor = 0;
676 heap_area_pair_t current_pair;
678 xbt_dynar_foreach(list, cursor, current_pair){
679 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
686 void match_equals(xbt_dynar_t list, xbt_dynar_t *equals){
688 unsigned int cursor = 0;
689 heap_area_pair_t current_pair;
690 heap_area_t previous_area;
692 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
694 xbt_dynar_foreach(list, cursor, current_pair){
696 if(current_pair->fragment1 != -1){
698 real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
699 real_addr_frag1 = (void*) ((char *)real_addr_block1 + (current_pair->fragment1 << heapinfo1[current_pair->block1].type));
700 real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
701 real_addr_frag2 = (void*) ((char *)real_addr_block2 + (current_pair->fragment2 << heapinfo2[current_pair->block2].type));
703 if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){
704 remove_heap_equality(equals, 1, real_addr_frag1);
705 previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
706 xbt_free(heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
707 heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
708 xbt_free(previous_area);
710 if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){
711 remove_heap_equality(equals, 2, real_addr_frag2);
712 previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
713 xbt_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
714 heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
715 xbt_free(previous_area);
718 if(real_addr_frag1 != real_addr_frag2)
719 add_heap_equality(equals, real_addr_frag1, real_addr_frag2);
721 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
722 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
726 real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
727 real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
729 if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
730 remove_heap_equality(equals, 1, real_addr_block1);
731 previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
732 xbt_free(heapinfo2[previous_area->block].busy_block.equal_to);
733 heapinfo2[previous_area->block].busy_block.equal_to = NULL;
734 xbt_free(previous_area);
736 if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
737 remove_heap_equality(equals, 2, real_addr_block2);
738 previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
739 xbt_free(heapinfo1[previous_area->block].busy_block.equal_to);
740 heapinfo1[previous_area->block].busy_block.equal_to = NULL;
741 xbt_free(previous_area);
744 if(real_addr_block1 != real_addr_block2)
745 add_heap_equality(equals, real_addr_block1, real_addr_block2);
747 heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
748 heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
757 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
760 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
762 if(heap1 == NULL && heap1 == NULL){
763 XBT_DEBUG("Malloc descriptors null");
767 if(heap1->heaplimit != heap2->heaplimit){
768 XBT_DEBUG("Different limit of valid info table indices");
772 /* Heap information */
773 heaplimit = ((struct mdesc *)heap1)->heaplimit;
775 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
777 heapbase1 = (char *)heap1 + BLOCKSIZE;
778 heapbase2 = (char *)heap2 + BLOCKSIZE;
780 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
781 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
783 heapsize1 = heap1->heapsize;
784 heapsize2 = heap2->heapsize;
786 /* Start comparison */
788 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
792 /* Check busy blocks*/
796 while(i <= heaplimit){
798 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
799 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
801 if(heapinfo1[i].type != heapinfo2[i].type){
803 distance += BLOCKSIZE;
804 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
809 if(heapinfo1[i].type == -1){ /* Free block */
814 if(heapinfo1[i].type == 0){ /* Large block */
816 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
817 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
818 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
819 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);
823 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
824 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
825 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
826 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);
832 //while(k < (heapinfo1[i].busy_block.busy_size)){
833 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
834 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
842 }else { /* Fragmented block */
844 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
846 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
847 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
849 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
854 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
855 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
856 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);
862 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
863 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
864 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
884 static char* is_stack(void *address){
885 unsigned int cursor = 0;
886 stack_region_t stack;
888 xbt_dynar_foreach(stacks_areas, cursor, stack){
889 if(address == stack->address)
890 return stack->process_name;
896 static void add_heap_equality(xbt_dynar_t *equals, void *a1, void *a2){
898 if(xbt_dynar_is_empty(*equals)){
900 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
904 xbt_dynar_insert_at(*equals, 0, &he);
908 unsigned int cursor = 0;
910 int end = xbt_dynar_length(*equals) - 1;
911 heap_equality_t current_equality = NULL;
914 cursor = (start + end) / 2;
915 current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
916 if(current_equality->address1 == a1){
917 if(current_equality->address2 == a2)
919 if(current_equality->address2 < a2)
921 if(current_equality->address2 > a2)
924 if(current_equality->address1 < a1)
926 if(current_equality->address1 > a1)
930 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
934 if(current_equality->address1 < a1)
935 xbt_dynar_insert_at(*equals, cursor + 1 , &he);
937 xbt_dynar_insert_at(*equals, cursor, &he);
943 static void remove_heap_equality(xbt_dynar_t *equals, int address, void *a){
945 unsigned int cursor = 0;
946 heap_equality_t current_equality;
952 int end = xbt_dynar_length(*equals) - 1;
955 while(start <= end && found == 0){
956 cursor = (start + end) / 2;
957 current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
958 if(current_equality->address1 == a)
960 if(current_equality->address1 < a)
962 if(current_equality->address1 > a)
967 xbt_dynar_remove_at(*equals, cursor, NULL);
971 xbt_dynar_foreach(*equals, cursor, current_equality){
972 if(current_equality->address2 == a){
979 xbt_dynar_remove_at(*equals, cursor, NULL);
985 int is_free_area(void *area, xbt_mheap_t heap){
987 void *sheap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
988 malloc_info *heapinfo = (malloc_info *)((char *)heap + ((uintptr_t)((char *)heap->heapinfo - (char *)sheap)));
989 size_t heapsize = heap->heapsize;
991 /* Get block number */
992 size_t block = ((char*)area - (char*)((xbt_mheap_t)sheap)->heapbase) / BLOCKSIZE + 1;
995 /* Check if valid block number */
996 if((char *)area < (char*)((xbt_mheap_t)sheap)->heapbase || block > heapsize || block < 1)
999 if(heapinfo[block].type < 0)
1002 if(heapinfo[block].type == 0)
1005 if(heapinfo[block].type > 0){
1006 fragment = ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1007 if(heapinfo[block].busy_frag.frag_size[fragment] == 0)