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 extern char *xbt_binary_name;
18 xbt_dynar_t mmalloc_ignore;
19 xbt_dynar_t stacks_areas;
21 typedef struct s_heap_area_pair{
26 }s_heap_area_pair_t, *heap_area_pair_t;
28 static void heap_area_pair_free(heap_area_pair_t pair);
29 static void heap_area_pair_free_voidp(void *d);
30 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
31 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
33 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore);
34 static void match_equals(xbt_dynar_t list);
36 static int in_mmalloc_ignore(int block, int fragment);
37 static size_t heap_comparison_ignore(void *address);
39 static char* is_stack(void *address);
41 void mmalloc_backtrace_block_display(void* heapinfo, int block){
45 if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) {
46 XBT_DEBUG("No backtrace available for that block, sorry.");
50 memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
51 e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size;
53 xbt_ex_setup_backtrace(&e);
55 XBT_DEBUG("(backtrace not set)");
56 } else if (e.bt_strings == NULL) {
57 XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
61 XBT_DEBUG("Backtrace of where the block %d was malloced (%d frames):", block ,e.used);
62 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
63 XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
69 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
73 memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE);
74 e.used = XBT_BACKTRACE_SIZE;
76 xbt_ex_setup_backtrace(&e);
78 XBT_DEBUG("(backtrace not set)");
79 } else if (e.bt_strings == NULL) {
80 XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
84 XBT_DEBUG("Backtrace of where the fragment %d in block %d was malloced (%d frames):", frag, block ,e.used);
85 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
86 XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
92 void *s_heap, *heapbase1, *heapbase2;
93 malloc_info *heapinfo1, *heapinfo2;
94 size_t heaplimit, heapsize1, heapsize2;
98 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t *stack1, xbt_dynar_t *stack2){
100 if(heap1 == NULL && heap1 == NULL){
101 XBT_DEBUG("Malloc descriptors null");
105 if(heap1->heaplimit != heap2->heaplimit){
106 XBT_DEBUG("Different limit of valid info table indices");
110 /* Heap information */
111 heaplimit = ((struct mdesc *)heap1)->heaplimit;
113 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
115 heapbase1 = (char *)heap1 + BLOCKSIZE;
116 heapbase2 = (char *)heap2 + BLOCKSIZE;
118 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
119 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
121 heapsize1 = heap1->heapsize;
122 heapsize2 = heap2->heapsize;
124 /* Start comparison */
125 size_t i1, i2, j1, j2, k, current_block, current_fragment;
126 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
127 void *real_addr_block1, *real_addr_block2;
130 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
132 int equal, res_compare;
136 /* Init equal information */
139 while(i1<=heaplimit){
140 if(heapinfo1[i1].type == 0){
141 heapinfo1[i1].busy_block.equal_to = -1;
143 if(heapinfo1[i1].type > 0){
144 for(j1=0; j1 < MAX_FRAGMENT_PER_BLOCK; j1++){
145 heapinfo1[i1].busy_frag.equal_to[j1] = -1;
153 while(i2<=heaplimit){
154 if(heapinfo2[i2].type == 0){
155 heapinfo2[i2].busy_block.equal_to = -1;
157 if(heapinfo2[i2].type > 0){
158 for(j2=0; j2 < MAX_FRAGMENT_PER_BLOCK; j2++){
159 heapinfo2[i2].busy_frag.equal_to[j2] = -1;
165 /* Check busy blocks*/
169 while(i1 <= heaplimit){
173 if(heapinfo1[i1].type == -1){ /* Free block */
178 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)heapbase1));
180 if(heapinfo1[i1].type == 0){ /* Large block */
182 real_addr_block1 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block1) - (char *)heapbase1);
184 if((stack_name = is_stack(real_addr_block1)) != NULL){
185 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
186 stack->address = addr_block1;
187 stack->process_name = strdup(stack_name);
188 xbt_dynar_push(*stack1, &stack);
191 if(heapinfo1[i1].busy_block.busy_size == 0){
199 /* Try first to associate to same block in the other heap */
200 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
202 if(heapinfo1[current_block].busy_block.busy_size == heapinfo2[current_block].busy_block.busy_size){
204 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
206 real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
208 if((stack_name = is_stack(real_addr_block2)) != NULL){
209 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
210 stack->address = addr_block2;
211 stack->process_name = strdup(stack_name);
212 xbt_dynar_push(*stack2, &stack);
216 add_heap_area_pair(previous, current_block, -1, current_block, -1);
218 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
219 if(in_mmalloc_ignore((int)current_block, -1))
220 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 1);
222 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
224 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
227 if(res_compare == 0){
228 for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
229 heapinfo2[current_block+k].busy_block.equal_to = 1 ;
230 for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
231 heapinfo1[current_block+k].busy_block.equal_to = 1 ;
233 match_equals(previous);
234 i1 = i1 + heapinfo1[i1].busy_block.size;
237 xbt_dynar_reset(previous);
243 while(i2 <= heaplimit && !equal){
245 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
247 real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
249 if((stack_name = is_stack(real_addr_block2)) != NULL){
250 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
251 stack->address = addr_block2;
252 stack->process_name = strdup(stack_name);
253 xbt_dynar_push(*stack2, &stack);
256 if(i2 == current_block){
261 if(heapinfo2[i2].type != 0){
266 if(heapinfo2[i2].busy_block.equal_to == 1){
271 if(heapinfo1[i1].busy_block.size != heapinfo2[i2].busy_block.size){
276 if(heapinfo1[i1].busy_block.busy_size != heapinfo2[i2].busy_block.busy_size){
282 add_heap_area_pair(previous, i1, -1, i2, -1);
284 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
285 if(in_mmalloc_ignore((int)i1, -1))
286 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 1);
288 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
290 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
294 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
295 heapinfo2[i2+k].busy_block.equal_to = 1;
296 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
297 heapinfo1[i1+k].busy_block.equal_to = 1;
299 match_equals(previous);
302 xbt_dynar_reset(previous);
311 }else{ /* Fragmented block */
313 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
315 current_fragment = j1;
317 if(heapinfo1[i1].busy_frag.frag_size[j1] == 0) /* Free fragment */
320 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
325 /* Try first to associate to same fragment in the other heap */
326 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
328 if(heapinfo1[current_block].busy_frag.frag_size[current_fragment] == heapinfo2[current_block].busy_frag.frag_size[current_fragment]){
330 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
331 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << heapinfo2[current_block].type));
333 add_heap_area_pair(previous, current_block, current_fragment, current_block, current_fragment);
335 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
336 if(in_mmalloc_ignore((int)current_block, (int)current_fragment))
337 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 1);
339 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
341 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
344 if(res_compare == 0){
346 match_equals(previous);
349 xbt_dynar_reset(previous);
356 while(i2 <= heaplimit && !equal){
358 if(heapinfo2[i2].type <= 0){
363 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
365 if(heapinfo2[i2].type == heapinfo1[i1].type && i2 == current_block && j2 == current_fragment)
368 if(heapinfo2[i2].busy_frag.equal_to[j2] == 1)
371 if(heapinfo1[i1].busy_frag.frag_size[j1] != heapinfo2[i2].busy_frag.frag_size[j2]) /* Different size_used */
374 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
375 addr_frag2 = (void*) ((char *)addr_block2 + (j2 << heapinfo2[i2].type));
378 add_heap_area_pair(previous, i1, j1, i2, j2);
380 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
381 if(in_mmalloc_ignore((int)i1, (int)j1))
382 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 1);
384 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
386 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
391 match_equals(previous);
392 xbt_dynar_reset(previous);
396 xbt_dynar_reset(previous);
412 /* All blocks/fragments are equal to another block/fragment ? */
414 int nb_diff1 = 0, nb_diff2 = 0;
417 if(heapinfo1[i].type == 0){
418 if(heapinfo1[i].busy_block.busy_size > 0){
419 if(heapinfo1[i].busy_block.equal_to == -1){
420 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
421 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
422 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
423 mmalloc_backtrace_block_display((void*)heapinfo1, i);
429 if(heapinfo1[i].type > 0){
430 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
431 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
432 if(heapinfo1[i].busy_frag.equal_to[j] == -1){
433 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
434 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
435 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
436 XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
437 mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
448 XBT_DEBUG("Different blocks or fragments in heap1 : %d\n", nb_diff1);
453 if(heapinfo2[i].type == 0){
454 if(heapinfo2[i].busy_block.busy_size > 0){
455 if(heapinfo2[i].busy_block.equal_to == -1){
456 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
457 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
458 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
459 mmalloc_backtrace_block_display((void*)heapinfo2, i);
465 if(heapinfo2[i].type > 0){
466 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
467 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
468 if(heapinfo2[i].busy_frag.equal_to[j] == -1){
469 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
470 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
471 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
472 XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
473 mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
483 XBT_DEBUG("Different blocks or fragments in heap2 : %d\n", nb_diff2);
485 xbt_dynar_free(&previous);
487 return ((nb_diff1 > 0) || (nb_diff2 > 0));
491 static int in_mmalloc_ignore(int block, int fragment){
493 unsigned int cursor = 0;
495 int end = xbt_dynar_length(mmalloc_ignore) - 1;
496 mc_ignore_region_t region;
499 cursor = (start + end) / 2;
500 region = (mc_ignore_region_t)xbt_dynar_get_as(mmalloc_ignore, cursor, mc_ignore_region_t);
501 if(region->block == block){
502 if(region->fragment == fragment)
504 if(region->fragment < fragment)
506 if(region->fragment > fragment)
509 if(region->block < block)
511 if(region->block > block)
518 static size_t heap_comparison_ignore(void *address){
519 unsigned int cursor = 0;
521 int end = xbt_dynar_length(mmalloc_ignore) - 1;
522 mc_ignore_region_t region;
525 cursor = (start + end) / 2;
526 region = (mc_ignore_region_t)xbt_dynar_get_as(mmalloc_ignore, cursor, mc_ignore_region_t);
527 if(region->address == address)
529 if(region->address < address)
531 if(region->address > address)
539 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore){
541 size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
542 void *address_pointed1, *address_pointed2, *addr_block_pointed1, *addr_block_pointed2, *addr_frag_pointed1, *addr_frag_pointed2;
543 size_t block_pointed1, block_pointed2, frag_pointed1, frag_pointed2;
545 void *current_area1, *current_area2;
551 current_area1 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area1) + i) - (char *)heapbase1);
552 if((ignore1 = heap_comparison_ignore(current_area1)) > 0){
553 current_area2 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area2) + i) - (char *)heapbase2);
554 if((ignore2 = heap_comparison_ignore(current_area2)) == ignore1){
563 if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
565 /* Check pointer difference */
566 pointer_align = (i / sizeof(void*)) * sizeof(void*);
567 address_pointed1 = *((void **)((char *)area1 + pointer_align));
568 address_pointed2 = *((void **)((char *)area2 + pointer_align));
570 /* Get pointed blocks number */
571 block_pointed1 = ((char*)address_pointed1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
572 block_pointed2 = ((char*)address_pointed2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
574 /* Check if valid blocks number */
575 if((char *)address_pointed1 < (char*)((xbt_mheap_t)s_heap)->heapbase || block_pointed1 > heapsize1 || block_pointed1 < 1 || (char *)address_pointed2 < (char*)((xbt_mheap_t)s_heap)->heapbase || block_pointed2 > heapsize2 || block_pointed2 < 1)
578 if(heapinfo1[block_pointed1].type == heapinfo2[block_pointed2].type){ /* Same type of block (large or fragmented) */
580 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
581 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
583 if(heapinfo1[block_pointed1].type == 0){ /* Large block */
585 if(heapinfo1[block_pointed1].busy_block.size != heapinfo2[block_pointed2].busy_block.size){
589 if(heapinfo1[block_pointed1].busy_block.busy_size != heapinfo2[block_pointed2].busy_block.busy_size){
593 if(add_heap_area_pair(previous, block_pointed1, -1, block_pointed2, -1)){
595 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
596 if(in_mmalloc_ignore(block_pointed1, -1))
597 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 1);
599 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
601 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
609 }else{ /* Fragmented block */
611 /* Get pointed fragments number */
612 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
613 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
615 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
618 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
619 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
621 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
623 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
624 if(in_mmalloc_ignore(block_pointed1, frag_pointed1))
625 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
627 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
629 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
641 if((heapinfo1[block_pointed1].type > 0) && (heapinfo2[block_pointed2].type > 0)){
643 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
644 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
646 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
647 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
649 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
652 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
653 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
655 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
657 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
658 if(in_mmalloc_ignore(block_pointed1, frag_pointed1))
659 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
661 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
663 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
677 i = pointer_align + sizeof(void *);
691 static void heap_area_pair_free(heap_area_pair_t pair){
698 static void heap_area_pair_free_voidp(void *d)
700 heap_area_pair_free((heap_area_pair_t) * (void **) d);
703 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
705 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
706 heap_area_pair_t pair = NULL;
707 pair = xbt_new0(s_heap_area_pair_t, 1);
708 pair->block1 = block1;
709 pair->fragment1 = fragment1;
710 pair->block2 = block2;
711 pair->fragment2 = fragment2;
713 xbt_dynar_push(list, &pair);
721 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
723 unsigned int cursor = 0;
724 heap_area_pair_t current_pair;
726 xbt_dynar_foreach(list, cursor, current_pair){
727 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
734 static void match_equals(xbt_dynar_t list){
736 unsigned int cursor = 0;
737 heap_area_pair_t current_pair;
739 xbt_dynar_foreach(list, cursor, current_pair){
740 if(current_pair->fragment1 != -1){
741 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = 1;
742 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = 1;
744 heapinfo1[current_pair->block1].busy_block.equal_to = 1;
745 heapinfo2[current_pair->block2].busy_block.equal_to = 1;
752 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
755 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
757 if(heap1 == NULL && heap1 == NULL){
758 XBT_DEBUG("Malloc descriptors null");
762 if(heap1->heaplimit != heap2->heaplimit){
763 XBT_DEBUG("Different limit of valid info table indices");
767 /* Heap information */
768 heaplimit = ((struct mdesc *)heap1)->heaplimit;
770 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
772 heapbase1 = (char *)heap1 + BLOCKSIZE;
773 heapbase2 = (char *)heap2 + BLOCKSIZE;
775 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
776 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
778 heapsize1 = heap1->heapsize;
779 heapsize2 = heap2->heapsize;
781 /* Start comparison */
783 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
787 /* Check busy blocks*/
791 while(i <= heaplimit){
793 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
794 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
796 if(heapinfo1[i].type != heapinfo2[i].type){
798 distance += BLOCKSIZE;
799 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
804 if(heapinfo1[i].type == -1){ /* Free block */
809 if(heapinfo1[i].type == 0){ /* Large block */
811 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
812 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
813 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
814 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);
818 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
819 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
820 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
821 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);
827 //while(k < (heapinfo1[i].busy_block.busy_size)){
828 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
829 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
837 }else { /* Fragmented block */
839 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
841 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
842 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
844 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
849 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
850 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
851 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);
857 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
858 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
859 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
879 static char * is_stack(void *address){
880 unsigned int cursor = 0;
881 stack_region_t stack;
883 xbt_dynar_foreach(stacks_areas, cursor, stack){
884 if(address == stack->address)
885 return stack->process_name;