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, previous);
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 match_equals(previous);
227 i1 = i1 + heapinfo1[current_block].busy_block.size;
230 xbt_dynar_reset(previous);
236 while(i2 <= heaplimit && !equal){
238 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
240 if(i2 == current_block){
245 if(heapinfo2[i2].type != 0){
250 if(heapinfo2[i2].busy_block.equal_to != NULL){
255 res_compare = compare_area(addr_block1, addr_block2, previous);
257 if(res_compare == 0){
258 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
259 heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
260 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
261 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
263 match_equals(previous);
264 i1 = i1 + heapinfo1[i1].busy_block.size;
267 xbt_dynar_reset(previous);
274 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1[i1].busy_block.busy_size, addr_block1);
279 }else{ /* Fragmented block */
281 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
283 current_fragment = j1;
285 if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
288 if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
291 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
296 /* Try first to associate to same fragment in the other heap */
297 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
299 if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){
301 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
302 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << ((xbt_mheap_t)s_heap)->heapinfo[current_block].type));
304 res_compare = compare_area(addr_frag1, addr_frag2, previous);
306 if(res_compare == 0){
308 match_equals(previous);
311 xbt_dynar_reset(previous);
316 while(i2 <= heaplimit && !equal){
319 if(heapinfo2[i2].type <= 0){
324 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
326 if(heapinfo2[i2].type == heapinfo1[i1].type && i2 == current_block && j2 == current_fragment)
329 if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)
332 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
333 addr_frag2 = (void*) ((char *)addr_block2 + (j2 << ((xbt_mheap_t)s_heap)->heapinfo[i2].type));
335 res_compare = compare_area(addr_frag1, addr_frag2, previous);
337 if(res_compare == 0){
339 match_equals(previous);
340 xbt_dynar_reset(previous);
344 xbt_dynar_reset(previous);
352 if(heapinfo1[i1].busy_frag.equal_to[j1] == NULL){
353 XBT_DEBUG("Block %zu, fragment %zu not found (size_used = %zd, address = %p, ignore %d)", i1, j1, heapinfo1[i1].busy_frag.frag_size[j1], addr_frag1, heapinfo1[i1].busy_frag.ignore[j1]);
368 /* All blocks/fragments are equal to another block/fragment ? */
370 void *real_addr_frag1 = NULL, *real_addr_block1 = NULL, *real_addr_block2 = NULL, *real_addr_frag2 = NULL;
373 if(heapinfo1[i].type == 0){
374 if(current_block == heaplimit){
375 if(heapinfo1[i].busy_block.busy_size > 0){
376 if(heapinfo1[i].busy_block.equal_to == NULL){
377 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
378 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
379 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
380 //mmalloc_backtrace_block_display((void*)heapinfo1, i);
387 if(heapinfo1[i].type > 0){
388 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
389 real_addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)s_heap)->heapbase));
390 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
391 if(current_block == heaplimit){
392 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
393 if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
394 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
395 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
396 real_addr_frag1 = (void*) ((char *)real_addr_block1 + (j << ((struct mdesc *)s_heap)->heapinfo[i].type));
397 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]);
398 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
409 if(current_block == heaplimit)
410 XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
415 if(heapinfo2[i].type == 0){
416 if(current_block == heaplimit){
417 if(heapinfo2[i].busy_block.busy_size > 0){
418 if(heapinfo2[i].busy_block.equal_to == NULL){
419 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
420 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
421 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
422 //mmalloc_backtrace_block_display((void*)heapinfo2, i);
429 if(heapinfo2[i].type > 0){
430 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
431 real_addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)s_heap)->heapbase));
432 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
433 if(current_block == heaplimit){
434 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
435 if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
436 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
437 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
438 real_addr_frag2 = (void*) ((char *)real_addr_block2 + (j << ((struct mdesc *)s_heap)->heapinfo[i].type));
439 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]);
440 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
451 if(current_block == heaplimit)
452 XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
454 xbt_dynar_free(&previous);
455 real_addr_frag1 = NULL, real_addr_block1 = NULL, real_addr_block2 = NULL, real_addr_frag2 = NULL;
457 return ((nb_diff1 > 0) || (nb_diff2 > 0));
460 void reset_heap_information(){
465 if(heapinfo1[i].type == 0){
466 xbt_free(heapinfo1[i].busy_block.equal_to);
467 heapinfo1[i].busy_block.equal_to = NULL;
469 if(heapinfo1[i].type > 0){
470 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
471 xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
472 heapinfo1[i].busy_frag.equal_to[j] = NULL;
481 if(heapinfo2[i].type == 0){
482 xbt_free(heapinfo2[i].busy_block.equal_to);
483 heapinfo2[i].busy_block.equal_to = NULL;
485 if(heapinfo2[i].type > 0){
486 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
487 xbt_free(heapinfo2[i].busy_frag.equal_to[j]);
488 heapinfo2[i].busy_frag.equal_to[j] = NULL;
494 s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
495 heapinfo1 = NULL, heapinfo2 = NULL;
496 heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
497 to_ignore1 = NULL, to_ignore2 = NULL;
501 static heap_area_t new_heap_area(int block, int fragment){
502 heap_area_t area = NULL;
503 area = xbt_new0(s_heap_area_t, 1);
505 area->fragment = fragment;
510 static size_t heap_comparison_ignore_size(xbt_dynar_t ignore_list, void *address){
512 unsigned int cursor = 0;
514 int end = xbt_dynar_length(ignore_list) - 1;
515 mc_heap_ignore_region_t region;
518 cursor = (start + end) / 2;
519 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(ignore_list, cursor, mc_heap_ignore_region_t);
520 if(region->address == address)
522 if(region->address < address)
524 if(region->address > address)
532 int compare_area(void *area1, void* area2, xbt_dynar_t previous){ /* Return code : 0 = equal, 1 = same size but different bytes, 2 = different size used */
534 size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
535 void *addr_pointed1, *addr_pointed2;
537 ssize_t block1, frag1, block2, frag2;
539 int check_ignore = 0;
542 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
543 void *area1_to_compare, *area2_to_compare;
547 if(previous == NULL){
548 previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
552 block1 = ((char*)area1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
553 block2 = ((char*)area2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
555 if(is_block_stack((int)block1) && is_block_stack((int)block2)){
556 add_heap_area_pair(previous, block1, -1, block2, -1);
558 match_equals(previous);
559 xbt_dynar_free(&previous);
564 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)){
566 xbt_dynar_free(&previous);
571 addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)heapbase1));
572 addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)heapbase2));
574 if(heapinfo1[block1].type == heapinfo2[block2].type){
576 if(heapinfo1[block1].type == -1){
578 match_equals(previous);
579 xbt_dynar_free(&previous);
582 }else if(heapinfo1[block1].type == 0){
584 if(heapinfo1[block1].busy_block.equal_to != NULL || heapinfo2[block2].busy_block.equal_to != NULL){
585 if(equal_blocks(block1, block2)){
587 match_equals(previous);
588 xbt_dynar_free(&previous);
593 xbt_dynar_free(&previous);
599 if(heapinfo1[block1].busy_block.size != heapinfo2[block2].busy_block.size){
601 xbt_dynar_free(&previous);
606 if(heapinfo1[block1].busy_block.busy_size != heapinfo2[block2].busy_block.busy_size){
608 xbt_dynar_free(&previous);
613 if(!add_heap_area_pair(previous, block1, -1, block2, -1)){
615 match_equals(previous);
616 xbt_dynar_free(&previous);
621 size = heapinfo1[block1].busy_block.busy_size;
625 match_equals(previous);
626 xbt_dynar_free(&previous);
634 area1_to_compare = addr_block1;
635 area2_to_compare = addr_block2;
637 if(heapinfo1[block1].busy_block.ignore == 1 && heapinfo2[block2].busy_block.ignore == 1)
641 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
642 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
644 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
645 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
647 area1_to_compare = addr_frag1;
648 area2_to_compare = addr_frag2;
650 if(heapinfo1[block1].busy_frag.equal_to[frag1] != NULL || heapinfo2[block2].busy_frag.equal_to[frag2] != NULL){
651 if(equal_fragments(block1, frag1, block2, frag2)){
653 match_equals(previous);
654 xbt_dynar_free(&previous);
659 xbt_dynar_free(&previous);
665 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2]){
667 xbt_dynar_free(&previous);
672 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
674 match_equals(previous);
675 xbt_dynar_free(&previous);
680 size = heapinfo1[block1].busy_frag.frag_size[frag1];
684 match_equals(previous);
685 xbt_dynar_free(&previous);
690 if(heapinfo1[block1].busy_frag.ignore[frag1] == 1 && heapinfo2[block2].busy_frag.ignore[frag2] == 1)
695 }else if((heapinfo1[block1].type > 0) && (heapinfo2[block2].type > 0)){
697 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
698 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
700 if(heapinfo1[block1].busy_frag.equal_to[frag1] != NULL || heapinfo2[block2].busy_frag.equal_to[frag2] != NULL){
701 if(equal_fragments(block1, frag1, block2, frag2)){
703 match_equals(previous);
704 xbt_dynar_free(&previous);
709 xbt_dynar_free(&previous);
715 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2]){
717 xbt_dynar_free(&previous);
722 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
724 match_equals(previous);
725 xbt_dynar_free(&previous);
730 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
731 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
733 area1_to_compare = addr_frag1;
734 area2_to_compare = addr_frag2;
736 size = heapinfo1[block1].busy_frag.frag_size[frag1];
740 match_equals(previous);
741 xbt_dynar_free(&previous);
746 if(heapinfo1[block1].busy_frag.ignore[frag1] == 1 && heapinfo2[block2].busy_frag.ignore[frag2] == 1)
751 xbt_dynar_free(&previous);
759 if((ignore1 = heap_comparison_ignore_size(to_ignore1, (char *)area1 + i)) > 0){
760 if((ignore2 = heap_comparison_ignore_size(to_ignore2, (char *)area2 + i)) == ignore1){
767 pointer_align = (i / sizeof(void*)) * sizeof(void*);
768 addr_pointed1 = *((void **)((char *)area1_to_compare + pointer_align));
769 addr_pointed2 = *((void **)((char *)area2_to_compare + pointer_align));
771 if(addr_pointed1 > maestro_stack_start && addr_pointed1 < maestro_stack_end && addr_pointed2 > maestro_stack_start && addr_pointed2 < maestro_stack_end){
772 i = pointer_align + sizeof(void *);
774 }else if((addr_pointed1 > s_heap) && ((char *)addr_pointed1 < (char *)s_heap + STD_HEAP_SIZE)
775 && (addr_pointed2 > s_heap) && ((char *)addr_pointed2 < (char *)s_heap + STD_HEAP_SIZE)){
776 res_compare = compare_area(addr_pointed1, addr_pointed2, previous);
777 if(res_compare != 0){
779 xbt_dynar_free(&previous);
784 while(j<sizeof(void*) && (i + j) < size){
785 if(memcmp(((char *)area1_to_compare) + i + j, ((char *)area2_to_compare) + i + j , 1) != 0){
787 xbt_dynar_free(&previous);
794 i = pointer_align + sizeof(void *);
798 match_equals(previous);
799 xbt_dynar_free(&previous);
807 static void heap_area_pair_free(heap_area_pair_t pair){
812 static void heap_area_pair_free_voidp(void *d)
814 heap_area_pair_free((heap_area_pair_t) * (void **) d);
817 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
819 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
820 heap_area_pair_t pair = NULL;
821 pair = xbt_new0(s_heap_area_pair_t, 1);
822 pair->block1 = block1;
823 pair->fragment1 = fragment1;
824 pair->block2 = block2;
825 pair->fragment2 = fragment2;
827 xbt_dynar_push(list, &pair);
835 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
837 unsigned int cursor = 0;
838 heap_area_pair_t current_pair;
840 xbt_dynar_foreach(list, cursor, current_pair){
841 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
849 void match_equals(xbt_dynar_t list){
851 unsigned int cursor = 0;
852 heap_area_pair_t current_pair;
853 heap_area_t previous_area;
855 xbt_dynar_foreach(list, cursor, current_pair){
857 if(current_pair->fragment1 != -1){
859 if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){
860 previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
861 xbt_free(heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
862 heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
863 xbt_free(previous_area);
865 if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){
866 previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
867 xbt_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
868 heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
869 xbt_free(previous_area);
872 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
873 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
877 if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
878 previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
879 xbt_free(heapinfo2[previous_area->block].busy_block.equal_to);
880 heapinfo2[previous_area->block].busy_block.equal_to = NULL;
881 xbt_free(previous_area);
883 if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
884 previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
885 xbt_free(heapinfo1[previous_area->block].busy_block.equal_to);
886 heapinfo1[previous_area->block].busy_block.equal_to = NULL;
887 xbt_free(previous_area);
890 heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
891 heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
899 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
902 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
904 if(heap1 == NULL && heap1 == NULL){
905 XBT_DEBUG("Malloc descriptors null");
909 if(heap1->heaplimit != heap2->heaplimit){
910 XBT_DEBUG("Different limit of valid info table indices");
914 /* Heap information */
915 heaplimit = ((struct mdesc *)heap1)->heaplimit;
917 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
919 heapbase1 = (char *)heap1 + BLOCKSIZE;
920 heapbase2 = (char *)heap2 + BLOCKSIZE;
922 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
923 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
925 heapsize1 = heap1->heapsize;
926 heapsize2 = heap2->heapsize;
928 /* Start comparison */
930 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
934 /* Check busy blocks*/
938 while(i <= heaplimit){
940 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
941 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
943 if(heapinfo1[i].type != heapinfo2[i].type){
945 distance += BLOCKSIZE;
946 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
951 if(heapinfo1[i].type == -1){ /* Free block */
956 if(heapinfo1[i].type == 0){ /* Large block */
958 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
959 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
960 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
961 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);
965 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
966 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
967 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
968 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);
974 //while(k < (heapinfo1[i].busy_block.busy_size)){
975 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
976 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
984 }else { /* Fragmented block */
986 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
988 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
989 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
991 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
996 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
997 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
998 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);
1004 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
1005 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
1006 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
1026 static int is_stack(void *address){
1027 unsigned int cursor = 0;
1028 stack_region_t stack;
1030 xbt_dynar_foreach(stacks_areas, cursor, stack){
1031 if(address == stack->address)
1038 static int is_block_stack(int block){
1039 unsigned int cursor = 0;
1040 stack_region_t stack;
1042 xbt_dynar_foreach(stacks_areas, cursor, stack){
1043 if(block == stack->block)
1050 static void add_heap_equality(xbt_dynar_t equals, void *a1, void *a2){
1052 if(xbt_dynar_is_empty(equals)){
1054 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
1058 xbt_dynar_insert_at(equals, 0, &he);
1062 unsigned int cursor = 0;
1064 int end = xbt_dynar_length(equals) - 1;
1065 heap_equality_t current_equality = NULL;
1067 while(start <= end){
1068 cursor = (start + end) / 2;
1069 current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
1070 if(current_equality->address1 == a1){
1071 if(current_equality->address2 == a2)
1073 if(current_equality->address2 < a2)
1075 if(current_equality->address2 > a2)
1078 if(current_equality->address1 < a1)
1080 if(current_equality->address1 > a1)
1084 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
1088 if(current_equality->address1 < a1)
1089 xbt_dynar_insert_at(equals, cursor + 1 , &he);
1091 xbt_dynar_insert_at(equals, cursor, &he);
1097 static void remove_heap_equality(xbt_dynar_t equals, int address, void *a){
1099 unsigned int cursor = 0;
1100 heap_equality_t current_equality;
1106 int end = xbt_dynar_length(equals) - 1;
1109 while(start <= end && found == 0){
1110 cursor = (start + end) / 2;
1111 current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
1112 if(current_equality->address1 == a)
1114 if(current_equality->address1 < a)
1116 if(current_equality->address1 > a)
1121 xbt_dynar_remove_at(equals, cursor, NULL);
1125 xbt_dynar_foreach(equals, cursor, current_equality){
1126 if(current_equality->address2 == a){
1133 xbt_dynar_remove_at(equals, cursor, NULL);
1139 int is_free_area(void *area, xbt_mheap_t heap){
1141 void *sheap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1142 malloc_info *heapinfo = (malloc_info *)((char *)heap + ((uintptr_t)((char *)heap->heapinfo - (char *)sheap)));
1143 size_t heapsize = heap->heapsize;
1145 /* Get block number */
1146 size_t block = ((char*)area - (char*)((xbt_mheap_t)sheap)->heapbase) / BLOCKSIZE + 1;
1149 /* Check if valid block number */
1150 if((char *)area < (char*)((xbt_mheap_t)sheap)->heapbase || block > heapsize || block < 1)
1153 if(heapinfo[block].type < 0)
1156 if(heapinfo[block].type == 0)
1159 if(heapinfo[block].type > 0){
1160 fragment = ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1161 if(heapinfo[block].busy_frag.frag_size[fragment] == 0)
1169 static int equal_blocks(int b1, int b2){
1170 if(heapinfo1[b1].busy_block.equal_to != NULL){
1171 if(heapinfo2[b2].busy_block.equal_to != NULL){
1172 if(((heap_area_t)(heapinfo1[b1].busy_block.equal_to))->block == b2 && ((heap_area_t)(heapinfo2[b2].busy_block.equal_to))->block == b1)
1179 static int equal_fragments(int b1, int f1, int b2, int f2){
1180 if(heapinfo1[b1].busy_frag.equal_to[f1] != NULL){
1181 if(heapinfo2[b2].busy_frag.equal_to[f2] != NULL){
1182 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)