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;
18 void *maestro_stack_start, *maestro_stack_end;
20 static void heap_area_pair_free(heap_area_pair_t pair);
21 static void heap_area_pair_free_voidp(void *d);
22 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
23 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
24 static heap_area_t new_heap_area(int block, int fragment);
26 static size_t heap_comparison_ignore_size(xbt_dynar_t list, 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 int is_stack(void *address);
31 static int is_block_stack(int block);
32 static int equal_blocks(int b1, int b2);
33 static int equal_fragments(int b1, int f1, int b2, int f2);
35 void mmalloc_backtrace_block_display(void* heapinfo, int block){
39 /* if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) { */
40 /* fprintf(stderr, "No backtrace available for that block, sorry.\n"); */
44 /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE); */
45 /* e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size; */
47 /* xbt_ex_setup_backtrace(&e); */
48 /* if (e.used == 0) { */
49 /* fprintf(stderr, "(backtrace not set)\n"); */
50 /* } else if (e.bt_strings == NULL) { */
51 /* fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
55 /* fprintf(stderr, "Backtrace of where the block %d was malloced (%d frames):\n", block ,e.used); */
56 /* for (i = 0; i < e.used; i++) /\* no need to display "xbt_backtrace_display" *\/{ */
57 /* fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
62 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
66 /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE); */
67 /* e.used = XBT_BACKTRACE_SIZE; */
69 /* xbt_ex_setup_backtrace(&e); */
70 /* if (e.used == 0) { */
71 /* fprintf(stderr, "(backtrace not set)\n"); */
72 /* } else if (e.bt_strings == NULL) { */
73 /* fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
77 /* fprintf(stderr, "Backtrace of where the fragment %d in block %d was malloced (%d frames):\n", frag, block ,e.used); */
78 /* for (i = 0; i < e.used; i++) /\* no need to display "xbt_backtrace_display" *\/{ */
79 /* fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
85 void mmalloc_backtrace_display(void *addr){
87 /* size_t block, frag_nb; */
90 /* xbt_mheap_t heap = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit(); */
92 /* block = (((char*) (addr) - (char*) heap -> heapbase) / BLOCKSIZE + 1); */
94 /* type = heap->heapinfo[block].type; */
97 /* case -1 : /\* Free block *\/ */
98 /* fprintf(stderr, "Asked to display the backtrace of a block that is free. I'm puzzled\n"); */
101 /* case 0: /\* Large block *\/ */
102 /* mmalloc_backtrace_block_display(heap->heapinfo, block); */
104 /* default: /\* Fragmented block *\/ */
105 /* frag_nb = RESIDUAL(addr, BLOCKSIZE) >> type; */
106 /* if(heap->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){ */
107 /* fprintf(stderr , "Asked to display the backtrace of a fragment that is free. I'm puzzled\n"); */
110 /* mmalloc_backtrace_fragment_display(heap->heapinfo, block, frag_nb); */
116 void *s_heap = NULL, *heapbase1 = NULL, *heapbase2 = NULL;
117 malloc_info *heapinfo1 = NULL, *heapinfo2 = NULL;
118 size_t heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
119 xbt_dynar_t to_ignore1 = NULL, to_ignore2 = NULL;
121 void init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t i1, xbt_dynar_t i2){
123 heaplimit = ((struct mdesc *)heap1)->heaplimit;
125 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
127 heapbase1 = (char *)heap1 + BLOCKSIZE;
128 heapbase2 = (char *)heap2 + BLOCKSIZE;
130 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)((struct mdesc *)heap1)->heapinfo - (char *)s_heap)));
131 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)((struct mdesc *)heap2)->heapinfo - (char *)s_heap)));
133 heapsize1 = heap1->heapsize;
134 heapsize2 = heap2->heapsize;
140 MC_ignore_data_bss(&heaplimit, sizeof(heaplimit));
141 MC_ignore_data_bss(&s_heap, sizeof(s_heap));
142 MC_ignore_data_bss(&heapbase1, sizeof(heapbase1));
143 MC_ignore_data_bss(&heapbase2, sizeof(heapbase2));
144 MC_ignore_data_bss(&heapinfo1, sizeof(heapinfo1));
145 MC_ignore_data_bss(&heapinfo2, sizeof(heapinfo2));
146 MC_ignore_data_bss(&heapsize1, sizeof(heapsize1));
147 MC_ignore_data_bss(&heapsize2, sizeof(heapsize2));
148 MC_ignore_data_bss(&to_ignore1, sizeof(to_ignore1));
149 MC_ignore_data_bss(&to_ignore2, sizeof(to_ignore2));
153 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
155 if(heap1 == NULL && heap1 == NULL){
156 XBT_DEBUG("Malloc descriptors null");
160 if(heap1->heaplimit != heap2->heaplimit){
161 XBT_DEBUG("Different limit of valid info table indices");
165 /* Start comparison */
166 size_t i1, i2, j1, j2, k;
167 size_t current_block = -1; /* avoid "maybe uninitialized" warning */
168 size_t current_fragment;
169 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
170 int nb_diff1 = 0, nb_diff2 = 0;
172 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
174 int equal, res_compare = 0;
176 /* Check busy blocks*/
180 while(i1 <= heaplimit){
184 if(heapinfo1[i1].type == -1){ /* Free block */
189 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
191 if(heapinfo1[i1].type == 0){ /* Large block */
193 if(is_stack(addr_block1)){
194 for(k=0; k < heapinfo1[i1].busy_block.size; k++)
195 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i1, -1);
196 for(k=0; k < heapinfo2[i1].busy_block.size; k++)
197 heapinfo2[i1+k].busy_block.equal_to = new_heap_area(i1, -1);
198 i1 = i1 + heapinfo1[current_block].busy_block.size;
202 if(heapinfo1[i1].busy_block.equal_to != NULL){
211 /* Try first to associate to same block in the other heap */
212 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
214 if(heapinfo2[current_block].busy_block.equal_to == NULL){
216 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
218 res_compare = compare_area(addr_block1, addr_block2, NULL);
220 if(res_compare == 0){
221 for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
222 heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
223 for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
224 heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
226 i1 = i1 + heapinfo1[current_block].busy_block.size;
229 xbt_dynar_reset(previous);
235 while(i2 <= heaplimit && !equal){
237 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
239 if(i2 == current_block){
244 if(heapinfo2[i2].type != 0){
249 if(heapinfo2[i2].busy_block.equal_to != NULL){
254 res_compare = compare_area(addr_block1, addr_block2, NULL);
256 if(res_compare == 0){
257 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
258 heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
259 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
260 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
262 i1 = i1 + heapinfo1[i1].busy_block.size;
265 xbt_dynar_reset(previous);
272 /*XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1[i1].busy_block.busy_size, addr_block1);
278 }else{ /* Fragmented block */
280 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
282 current_fragment = j1;
284 if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
287 if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
290 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
295 /* Try first to associate to same fragment in the other heap */
296 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
298 if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){
300 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
301 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << ((xbt_mheap_t)s_heap)->heapinfo[current_block].type));
303 res_compare = compare_area(addr_frag1, addr_frag2, NULL);
308 xbt_dynar_reset(previous);
314 while(i2 <= heaplimit && !equal){
317 if(heapinfo2[i2].type <= 0){
322 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
324 if(i2 == current_block && j2 == current_fragment)
327 if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)
330 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
331 addr_frag2 = (void*) ((char *)addr_block2 + (j2 << ((xbt_mheap_t)s_heap)->heapinfo[i2].type));
333 res_compare = compare_area(addr_frag1, addr_frag2, NULL);
335 if(res_compare == 0){
337 xbt_dynar_reset(previous);
341 xbt_dynar_reset(previous);
349 /*if(heapinfo1[i1].busy_frag.equal_to[j1] == NULL){
350 fprintf(stderr,"Block %zu, fragment %zu not found (size_used = %zd, address = %p, ignore %d)\n", i1, j1, heapinfo1[i1].busy_frag.frag_size[j1], addr_frag1, heapinfo1[i1].busy_frag.ignore[j1]);
365 /* All blocks/fragments are equal to another block/fragment ? */
367 void *real_addr_frag1 = NULL, *real_addr_block1 = NULL, *real_addr_block2 = NULL, *real_addr_frag2 = NULL;
370 if(heapinfo1[i].type == 0){
371 if(current_block == heaplimit){
372 if(heapinfo1[i].busy_block.busy_size > 0){
373 if(heapinfo1[i].busy_block.equal_to == NULL){
374 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
375 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
376 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
377 //mmalloc_backtrace_block_display((void*)heapinfo1, i);
384 if(heapinfo1[i].type > 0){
385 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
386 real_addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)s_heap)->heapbase));
387 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
388 if(current_block == heaplimit){
389 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
390 if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
391 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
392 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
393 real_addr_frag1 = (void*) ((char *)real_addr_block1 + (j << ((struct mdesc *)s_heap)->heapinfo[i].type));
394 XBT_DEBUG("Block %zu, Fragment %zu (%p - %p) not found (size used = %zd)", i, j, addr_frag1, real_addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
395 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
406 if(current_block == heaplimit)
407 XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
412 if(heapinfo2[i].type == 0){
413 if(current_block == heaplimit){
414 if(heapinfo2[i].busy_block.busy_size > 0){
415 if(heapinfo2[i].busy_block.equal_to == NULL){
416 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
417 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
418 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
419 //mmalloc_backtrace_block_display((void*)heapinfo2, i);
426 if(heapinfo2[i].type > 0){
427 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
428 real_addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)s_heap)->heapbase));
429 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
430 if(current_block == heaplimit){
431 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
432 if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
433 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
434 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
435 real_addr_frag2 = (void*) ((char *)real_addr_block2 + (j << ((struct mdesc *)s_heap)->heapinfo[i].type));
436 XBT_DEBUG( "Block %zu, Fragment %zu (%p - %p) not found (size used = %zd)", i, j, addr_frag2, real_addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
437 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
448 if(current_block == heaplimit)
449 XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
451 xbt_dynar_free(&previous);
452 real_addr_frag1 = NULL, real_addr_block1 = NULL, real_addr_block2 = NULL, real_addr_frag2 = NULL;
454 return ((nb_diff1 > 0) || (nb_diff2 > 0));
457 void reset_heap_information(){
462 if(heapinfo1[i].type == 0){
463 xbt_free(heapinfo1[i].busy_block.equal_to);
464 heapinfo1[i].busy_block.equal_to = NULL;
466 if(heapinfo1[i].type > 0){
467 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
468 xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
469 heapinfo1[i].busy_frag.equal_to[j] = NULL;
478 if(heapinfo2[i].type == 0){
479 xbt_free(heapinfo2[i].busy_block.equal_to);
480 heapinfo2[i].busy_block.equal_to = NULL;
482 if(heapinfo2[i].type > 0){
483 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
484 xbt_free(heapinfo2[i].busy_frag.equal_to[j]);
485 heapinfo2[i].busy_frag.equal_to[j] = NULL;
491 s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
492 heapinfo1 = NULL, heapinfo2 = NULL;
493 heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
494 to_ignore1 = NULL, to_ignore2 = NULL;
498 static heap_area_t new_heap_area(int block, int fragment){
499 heap_area_t area = NULL;
500 area = xbt_new0(s_heap_area_t, 1);
502 area->fragment = fragment;
507 static size_t heap_comparison_ignore_size(xbt_dynar_t ignore_list, void *address){
509 unsigned int cursor = 0;
511 int end = xbt_dynar_length(ignore_list) - 1;
512 mc_heap_ignore_region_t region;
515 cursor = (start + end) / 2;
516 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(ignore_list, cursor, mc_heap_ignore_region_t);
517 if(region->address == address)
519 if(region->address < address)
521 if(region->address > address)
529 int compare_area(void *area1, void* area2, xbt_dynar_t previous){
531 size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
532 void *addr_pointed1, *addr_pointed2;
534 ssize_t block1, frag1, block2, frag2;
536 int check_ignore = 0;
538 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
539 void *area1_to_compare, *area2_to_compare;
543 if(previous == NULL){
544 previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
548 block1 = ((char*)area1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
549 block2 = ((char*)area2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
551 if(is_block_stack((int)block1) && is_block_stack((int)block2)){
552 add_heap_area_pair(previous, block1, -1, block2, -1);
554 match_equals(previous);
555 xbt_dynar_free(&previous);
560 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)){
562 xbt_dynar_free(&previous);
567 addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)heapbase1));
568 addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)heapbase2));
570 if(heapinfo1[block1].type == heapinfo2[block2].type){
572 if(heapinfo1[block1].type == -1){ /* Free block */
574 match_equals(previous);
575 xbt_dynar_free(&previous);
579 }else if(heapinfo1[block1].type == 0){ /* Complete block */
581 if(heapinfo1[block1].busy_block.equal_to != NULL && heapinfo2[block2].busy_block.equal_to != NULL){
582 if(equal_blocks(block1, block2)){
584 match_equals(previous);
585 xbt_dynar_free(&previous);
591 if(heapinfo1[block1].busy_block.size != heapinfo2[block2].busy_block.size){
593 xbt_dynar_free(&previous);
598 if(heapinfo1[block1].busy_block.busy_size != heapinfo2[block2].busy_block.busy_size){
600 xbt_dynar_free(&previous);
605 if(!add_heap_area_pair(previous, block1, -1, block2, -1)){
607 match_equals(previous);
608 xbt_dynar_free(&previous);
613 size = heapinfo1[block1].busy_block.busy_size;
617 match_equals(previous);
618 xbt_dynar_free(&previous);
626 area1_to_compare = addr_block1;
627 area2_to_compare = addr_block2;
629 if((heapinfo1[block1].busy_block.ignore > 0) && (heapinfo2[block2].busy_block.ignore == heapinfo1[block1].busy_block.ignore))
630 check_ignore = heapinfo1[block1].busy_block.ignore;
633 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
634 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
636 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
637 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
639 area1_to_compare = addr_frag1;
640 area2_to_compare = addr_frag2;
642 if(heapinfo1[block1].busy_frag.equal_to[frag1] != NULL && heapinfo2[block2].busy_frag.equal_to[frag2] != NULL){
643 if(equal_fragments(block1, frag1, block2, frag2)){
645 match_equals(previous);
646 xbt_dynar_free(&previous);
652 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2]){
654 xbt_dynar_free(&previous);
659 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
661 match_equals(previous);
662 xbt_dynar_free(&previous);
667 size = heapinfo1[block1].busy_frag.frag_size[frag1];
671 match_equals(previous);
672 xbt_dynar_free(&previous);
677 if((heapinfo1[block1].busy_frag.ignore[frag1] > 0) && ( heapinfo2[block2].busy_frag.ignore[frag2] == heapinfo1[block1].busy_frag.ignore[frag1]))
678 check_ignore = heapinfo1[block1].busy_frag.ignore[frag1];
682 }else if((heapinfo1[block1].type > 0) && (heapinfo2[block2].type > 0)){
684 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
685 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
687 if(heapinfo1[block1].busy_frag.equal_to[frag1] != NULL || heapinfo2[block2].busy_frag.equal_to[frag2] != NULL){
688 if(equal_fragments(block1, frag1, block2, frag2)){
690 match_equals(previous);
691 xbt_dynar_free(&previous);
697 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2]){
699 xbt_dynar_free(&previous);
704 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
706 match_equals(previous);
707 xbt_dynar_free(&previous);
712 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
713 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
715 area1_to_compare = addr_frag1;
716 area2_to_compare = addr_frag2;
718 size = heapinfo1[block1].busy_frag.frag_size[frag1];
722 match_equals(previous);
723 xbt_dynar_free(&previous);
728 if((heapinfo1[block1].busy_frag.ignore[frag1] > 0) && (heapinfo2[block2].busy_frag.ignore[frag2] == heapinfo1[block1].busy_frag.ignore[frag1]))
729 check_ignore = heapinfo1[block1].busy_frag.ignore[frag1];
733 xbt_dynar_free(&previous);
740 if(check_ignore > 0){
741 if((ignore1 = heap_comparison_ignore_size(to_ignore1, (char *)area1 + i)) > 0){
742 if((ignore2 = heap_comparison_ignore_size(to_ignore2, (char *)area2 + i)) == ignore1){
750 if(memcmp(((char *)area1_to_compare) + i, ((char *)area2_to_compare) + i, 1) != 0){
752 pointer_align = (i / sizeof(void*)) * sizeof(void*);
753 addr_pointed1 = *((void **)((char *)area1_to_compare + pointer_align));
754 addr_pointed2 = *((void **)((char *)area2_to_compare + pointer_align));
756 if(addr_pointed1 > maestro_stack_start && addr_pointed1 < maestro_stack_end && addr_pointed2 > maestro_stack_start && addr_pointed2 < maestro_stack_end){
757 i = pointer_align + sizeof(void *);
759 }else if((addr_pointed1 > s_heap) && ((char *)addr_pointed1 < (char *)s_heap + STD_HEAP_SIZE)
760 && (addr_pointed2 > s_heap) && ((char *)addr_pointed2 < (char *)s_heap + STD_HEAP_SIZE)){
761 res_compare = compare_area(addr_pointed1, addr_pointed2, previous);
762 if(res_compare != 0){
764 xbt_dynar_free(&previous);
767 i = pointer_align + sizeof(void *);
771 xbt_dynar_free(&previous);
782 match_equals(previous);
783 xbt_dynar_free(&previous);
791 static void heap_area_pair_free(heap_area_pair_t pair){
796 static void heap_area_pair_free_voidp(void *d)
798 heap_area_pair_free((heap_area_pair_t) * (void **) d);
801 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
803 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
804 heap_area_pair_t pair = NULL;
805 pair = xbt_new0(s_heap_area_pair_t, 1);
806 pair->block1 = block1;
807 pair->fragment1 = fragment1;
808 pair->block2 = block2;
809 pair->fragment2 = fragment2;
811 xbt_dynar_push(list, &pair);
819 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
821 unsigned int cursor = 0;
822 heap_area_pair_t current_pair;
824 xbt_dynar_foreach(list, cursor, current_pair){
825 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
833 void match_equals(xbt_dynar_t list){
835 unsigned int cursor = 0;
836 heap_area_pair_t current_pair;
837 heap_area_t previous_area;
839 xbt_dynar_foreach(list, cursor, current_pair){
841 if(current_pair->fragment1 != -1){
843 if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){
844 previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
845 xbt_free(heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
846 heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
847 xbt_free(previous_area);
849 if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){
850 previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
851 xbt_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
852 heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
853 xbt_free(previous_area);
856 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
857 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
861 if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
862 previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
863 xbt_free(heapinfo2[previous_area->block].busy_block.equal_to);
864 heapinfo2[previous_area->block].busy_block.equal_to = NULL;
865 xbt_free(previous_area);
867 if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
868 previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
869 xbt_free(heapinfo1[previous_area->block].busy_block.equal_to);
870 heapinfo1[previous_area->block].busy_block.equal_to = NULL;
871 xbt_free(previous_area);
874 heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
875 heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
883 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
886 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
888 if(heap1 == NULL && heap1 == NULL){
889 XBT_DEBUG("Malloc descriptors null");
893 if(heap1->heaplimit != heap2->heaplimit){
894 XBT_DEBUG("Different limit of valid info table indices");
898 /* Heap information */
899 heaplimit = ((struct mdesc *)heap1)->heaplimit;
901 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
903 heapbase1 = (char *)heap1 + BLOCKSIZE;
904 heapbase2 = (char *)heap2 + BLOCKSIZE;
906 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
907 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
909 heapsize1 = heap1->heapsize;
910 heapsize2 = heap2->heapsize;
912 /* Start comparison */
914 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
918 /* Check busy blocks*/
922 while(i <= heaplimit){
924 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
925 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
927 if(heapinfo1[i].type != heapinfo2[i].type){
929 distance += BLOCKSIZE;
930 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
935 if(heapinfo1[i].type == -1){ /* Free block */
940 if(heapinfo1[i].type == 0){ /* Large block */
942 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
943 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
944 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
945 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);
949 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
950 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
951 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
952 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);
958 //while(k < (heapinfo1[i].busy_block.busy_size)){
959 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
960 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
968 }else { /* Fragmented block */
970 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
972 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
973 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
975 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
980 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
981 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
982 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);
988 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
989 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
990 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
1010 static int is_stack(void *address){
1011 unsigned int cursor = 0;
1012 stack_region_t stack;
1014 xbt_dynar_foreach(stacks_areas, cursor, stack){
1015 if(address == stack->address)
1022 static int is_block_stack(int block){
1023 unsigned int cursor = 0;
1024 stack_region_t stack;
1026 xbt_dynar_foreach(stacks_areas, cursor, stack){
1027 if(block == stack->block)
1034 static void add_heap_equality(xbt_dynar_t equals, void *a1, void *a2){
1036 if(xbt_dynar_is_empty(equals)){
1038 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
1042 xbt_dynar_insert_at(equals, 0, &he);
1046 unsigned int cursor = 0;
1048 int end = xbt_dynar_length(equals) - 1;
1049 heap_equality_t current_equality = NULL;
1051 while(start <= end){
1052 cursor = (start + end) / 2;
1053 current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
1054 if(current_equality->address1 == a1){
1055 if(current_equality->address2 == a2)
1057 if(current_equality->address2 < a2)
1059 if(current_equality->address2 > a2)
1062 if(current_equality->address1 < a1)
1064 if(current_equality->address1 > a1)
1068 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
1072 if(current_equality->address1 < a1)
1073 xbt_dynar_insert_at(equals, cursor + 1 , &he);
1075 xbt_dynar_insert_at(equals, cursor, &he);
1081 static void remove_heap_equality(xbt_dynar_t equals, int address, void *a){
1083 unsigned int cursor = 0;
1084 heap_equality_t current_equality;
1090 int end = xbt_dynar_length(equals) - 1;
1093 while(start <= end && found == 0){
1094 cursor = (start + end) / 2;
1095 current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
1096 if(current_equality->address1 == a)
1098 if(current_equality->address1 < a)
1100 if(current_equality->address1 > a)
1105 xbt_dynar_remove_at(equals, cursor, NULL);
1109 xbt_dynar_foreach(equals, cursor, current_equality){
1110 if(current_equality->address2 == a){
1117 xbt_dynar_remove_at(equals, cursor, NULL);
1123 int is_free_area(void *area, xbt_mheap_t heap){
1125 void *sheap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1126 malloc_info *heapinfo = (malloc_info *)((char *)heap + ((uintptr_t)((char *)heap->heapinfo - (char *)sheap)));
1127 size_t heapsize = heap->heapsize;
1129 /* Get block number */
1130 size_t block = ((char*)area - (char*)((xbt_mheap_t)sheap)->heapbase) / BLOCKSIZE + 1;
1133 /* Check if valid block number */
1134 if((char *)area < (char*)((xbt_mheap_t)sheap)->heapbase || block > heapsize || block < 1)
1137 if(heapinfo[block].type < 0)
1140 if(heapinfo[block].type == 0)
1143 if(heapinfo[block].type > 0){
1144 fragment = ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1145 if(heapinfo[block].busy_frag.frag_size[fragment] == 0)
1153 static int equal_blocks(int b1, int b2){
1154 if(heapinfo1[b1].busy_block.equal_to != NULL){
1155 if(heapinfo2[b2].busy_block.equal_to != NULL){
1156 if(((heap_area_t)(heapinfo1[b1].busy_block.equal_to))->block == b2 && ((heap_area_t)(heapinfo2[b2].busy_block.equal_to))->block == b1)
1163 static int equal_fragments(int b1, int f1, int b2, int f2){
1164 if(heapinfo1[b1].busy_frag.equal_to[f1] != NULL){
1165 if(heapinfo2[b2].busy_frag.equal_to[f2] != NULL){
1166 if(((heap_area_t)(heapinfo1[b1].busy_frag.equal_to[f1]))->block == b2 && ((heap_area_t)(heapinfo2[b2].busy_frag.equal_to[f2]))->block == b1 && ((heap_area_t)(heapinfo1[b1].busy_frag.equal_to[f1]))->fragment == f2 && ((heap_area_t)(heapinfo2[b2].busy_frag.equal_to[f2]))->fragment == f1)