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 mc_heap_comparison_ignore;
19 xbt_dynar_t stacks_areas;
21 static void heap_area_pair_free(heap_area_pair_t pair);
22 static void heap_area_pair_free_voidp(void *d);
23 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
24 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
25 static heap_area_t new_heap_area(int block, int fragment);
27 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore);
28 static void match_equals(xbt_dynar_t list, xbt_dynar_t *equals);
30 static int in_mc_comparison_ignore(int block, int fragment);
31 static size_t heap_comparison_ignore_size(void *address);
32 static void add_heap_equality(xbt_dynar_t *equals, void *a1, void *a2);
33 static void remove_heap_equality(xbt_dynar_t *equals, int address, void *a);
35 static char* is_stack(void *address);
37 void mmalloc_backtrace_block_display(void* heapinfo, int block){
41 if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) {
42 fprintf(stderr, "No backtrace available for that block, sorry.\n");
46 memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
47 e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size;
49 xbt_ex_setup_backtrace(&e);
51 fprintf(stderr, "(backtrace not set)\n");
52 } else if (e.bt_strings == NULL) {
53 fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
57 fprintf(stderr, "Backtrace of where the block %d was malloced (%d frames):\n", block ,e.used);
58 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
59 fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4);
65 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
69 memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE);
70 e.used = XBT_BACKTRACE_SIZE;
72 xbt_ex_setup_backtrace(&e);
74 fprintf(stderr, "(backtrace not set)\n");
75 } else if (e.bt_strings == NULL) {
76 fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
80 fprintf(stderr, "Backtrace of where the fragment %d in block %d was malloced (%d frames):\n", frag, block ,e.used);
81 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
82 fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4);
88 void mmalloc_backtrace_display(void *addr){
90 size_t block, frag_nb;
93 xbt_mheap_t heap = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
95 block = (((char*) (addr) - (char*) heap -> heapbase) / BLOCKSIZE + 1);
97 type = heap->heapinfo[block].type;
100 case -1 : /* Free block */
101 fprintf(stderr, "Asked to display the backtrace of a block that is free. I'm puzzled\n");
104 case 0: /* Large block */
105 mmalloc_backtrace_block_display(heap->heapinfo, block);
107 default: /* Fragmented block */
108 frag_nb = RESIDUAL(addr, BLOCKSIZE) >> type;
109 if(heap->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){
110 fprintf(stderr , "Asked to display the backtrace of a fragment that is free. I'm puzzled\n");
113 mmalloc_backtrace_fragment_display(heap->heapinfo, block, frag_nb);
120 void *s_heap = NULL, *heapbase1 = NULL, *heapbase2 = NULL;
121 malloc_info *heapinfo1 = NULL, *heapinfo2 = NULL;
122 size_t heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
126 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t *stack1, xbt_dynar_t *stack2, xbt_dynar_t *equals){
128 if(heap1 == NULL && heap1 == NULL){
129 XBT_DEBUG("Malloc descriptors null");
133 if(heap1->heaplimit != heap2->heaplimit){
134 XBT_DEBUG("Different limit of valid info table indices");
138 /* Heap information */
139 heaplimit = ((struct mdesc *)heap1)->heaplimit;
141 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
143 heapbase1 = (char *)heap1 + BLOCKSIZE;
144 heapbase2 = (char *)heap2 + BLOCKSIZE;
146 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
147 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
149 heapsize1 = heap1->heapsize;
150 heapsize2 = heap2->heapsize;
152 /* Start comparison */
153 size_t i1, i2, j1, j2, k, current_block, current_fragment;
154 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
155 void *real_addr_block1, *real_addr_block2;
158 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
160 int equal, res_compare;
162 /* Init equal information */
165 while(i1<=heaplimit){
166 if(heapinfo1[i1].type == 0){
167 heapinfo1[i1].busy_block.equal_to = NULL;
169 if(heapinfo1[i1].type > 0){
170 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
171 heapinfo1[i1].busy_frag.equal_to[j1] = NULL;
179 while(i2<=heaplimit){
180 if(heapinfo2[i2].type == 0){
181 heapinfo2[i2].busy_block.equal_to = NULL;
183 if(heapinfo2[i2].type > 0){
184 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
185 heapinfo2[i2].busy_frag.equal_to[j2] = NULL;
191 /* Check busy blocks*/
195 while(i1 <= heaplimit){
199 if(heapinfo1[i1].type == -1){ /* Free block */
204 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)heapbase1));
205 real_addr_block1 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block1) - (char *)heapbase1);
207 if(heapinfo1[i1].type == 0){ /* Large block */
209 if((stack_name = is_stack(real_addr_block1)) != NULL){
210 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
211 stack->address = addr_block1;
212 stack->process_name = strdup(stack_name);
213 stack->size = heapinfo1[i1].busy_block.busy_size;
214 xbt_dynar_push(*stack1, &stack);
217 if(heapinfo1[i1].busy_block.busy_size == 0){
222 if(heapinfo1[i1].busy_block.equal_to != NULL){
230 /* Try first to associate to same block in the other heap */
231 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
233 if(heapinfo2[current_block].busy_block.equal_to == NULL){
235 if(heapinfo1[current_block].busy_block.busy_size == heapinfo2[current_block].busy_block.busy_size){
237 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
238 real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
240 if((stack_name = is_stack(real_addr_block2)) != NULL){
241 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
242 stack->address = addr_block2;
243 stack->process_name = strdup(stack_name);
244 stack->size = heapinfo2[current_block].busy_block.busy_size;
245 xbt_dynar_push(*stack2, &stack);
248 add_heap_area_pair(previous, current_block, -1, current_block, -1);
250 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
251 if(in_mc_comparison_ignore((int)current_block, -1))
252 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 1);
254 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
256 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
259 if(res_compare == 0){
260 for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
261 heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
262 for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
263 heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
265 match_equals(previous, equals);
266 i1 = i1 + heapinfo1[current_block].busy_block.size;
269 xbt_dynar_reset(previous);
277 while(i2 <= heaplimit && !equal){
279 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
280 real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
282 if((stack_name = is_stack(real_addr_block2)) != NULL){
283 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
284 stack->address = addr_block2;
285 stack->process_name = strdup(stack_name);
286 stack->size = heapinfo2[i2].busy_block.busy_size;
287 xbt_dynar_push(*stack2, &stack);
290 if(i2 == current_block){
295 if(heapinfo2[i2].type != 0){
300 if(heapinfo2[i2].busy_block.equal_to != NULL){
305 if(heapinfo1[i1].busy_block.size != heapinfo2[i2].busy_block.size){
310 if(heapinfo1[i1].busy_block.busy_size != heapinfo2[i2].busy_block.busy_size){
316 add_heap_area_pair(previous, i1, -1, i2, -1);
318 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
319 if(in_mc_comparison_ignore((int)i1, -1))
320 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 1);
322 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
324 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
327 if(res_compare == 0){
328 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
329 heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
330 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
331 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
333 match_equals(previous, equals);
334 i1 = i1 + heapinfo1[i1].busy_block.size;
337 xbt_dynar_reset(previous);
346 }else{ /* Fragmented block */
348 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
350 current_fragment = j1;
352 if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
355 if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
358 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
363 /* Try first to associate to same fragment in the other heap */
364 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
366 if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){
368 if(heapinfo1[current_block].busy_frag.frag_size[current_fragment] == heapinfo2[current_block].busy_frag.frag_size[current_fragment]){
370 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
371 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << heapinfo2[current_block].type));
373 add_heap_area_pair(previous, current_block, current_fragment, current_block, current_fragment);
375 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
376 if(in_mc_comparison_ignore((int)current_block, (int)current_fragment))
377 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 1);
379 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
381 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
384 if(res_compare == 0){
386 match_equals(previous, equals);
389 xbt_dynar_reset(previous);
397 while(i2 <= heaplimit && !equal){
400 if(heapinfo2[i2].type <= 0){
405 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
407 if(heapinfo2[i2].type == heapinfo1[i1].type && i2 == current_block && j2 == current_fragment)
410 if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)
413 if(heapinfo1[i1].busy_frag.frag_size[j1] != heapinfo2[i2].busy_frag.frag_size[j2]) /* Different size_used */
416 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
417 addr_frag2 = (void*) ((char *)addr_block2 + (j2 << heapinfo2[i2].type));
420 add_heap_area_pair(previous, i1, j1, i2, j2);
422 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
423 if(in_mc_comparison_ignore((int)i1, (int)j1))
424 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 1);
426 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
428 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
431 if(res_compare == 0){
433 match_equals(previous, equals);
437 xbt_dynar_reset(previous);
453 /* All blocks/fragments are equal to another block/fragment ? */
455 int nb_diff1 = 0, nb_diff2 = 0;
458 if(heapinfo1[i].type == 0){
459 if(heapinfo1[i].busy_block.busy_size > 0){
460 if(heapinfo1[i].busy_block.equal_to == NULL){
461 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
462 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
463 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
464 mmalloc_backtrace_block_display((void*)heapinfo1, i);
468 xbt_free(heapinfo1[i].busy_block.equal_to);
472 if(heapinfo1[i].type > 0){
473 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
474 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
475 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
476 if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
477 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
478 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
479 XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
480 mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
484 xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
493 XBT_DEBUG("Different blocks or fragments in heap1 : %d", nb_diff1);
498 if(heapinfo2[i].type == 0){
499 if(heapinfo2[i].busy_block.busy_size > 0){
500 if(heapinfo2[i].busy_block.equal_to == NULL){
501 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
502 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
503 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
504 mmalloc_backtrace_block_display((void*)heapinfo2, i);
508 xbt_free(heapinfo2[i].busy_block.equal_to);
512 if(heapinfo2[i].type > 0){
513 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
514 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
515 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
516 if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
517 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
518 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
519 XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
520 mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
524 xbt_free(heapinfo2[i].busy_frag.equal_to[j]);
532 XBT_DEBUG("Different blocks or fragments in heap2 : %d", nb_diff2);
534 xbt_dynar_free(&previous);
536 s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
537 heapinfo1 = NULL, heapinfo2 = NULL;
538 heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
540 return ((nb_diff1 > 0) || (nb_diff2 > 0));
544 static heap_area_t new_heap_area(int block, int fragment){
545 heap_area_t area = NULL;
546 area = xbt_new0(s_heap_area_t, 1);
548 area->fragment = fragment;
552 static int in_mc_comparison_ignore(int block, int fragment){
554 unsigned int cursor = 0;
556 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
557 mc_heap_ignore_region_t region;
560 cursor = (start + end) / 2;
561 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
562 if(region->block == block){
563 if(region->fragment == fragment)
565 if(region->fragment < fragment)
567 if(region->fragment > fragment)
570 if(region->block < block)
572 if(region->block > block)
579 static size_t heap_comparison_ignore_size(void *address){
580 unsigned int cursor = 0;
582 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
583 mc_heap_ignore_region_t region;
586 cursor = (start + end) / 2;
587 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
588 if(region->address == address)
590 if(region->address < address)
592 if(region->address > address)
600 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore){
602 size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
603 void *address_pointed1, *address_pointed2, *addr_block_pointed1, *addr_block_pointed2, *addr_frag_pointed1, *addr_frag_pointed2;
604 size_t block_pointed1, block_pointed2, frag_pointed1, frag_pointed2;
606 void *current_area1, *current_area2;
612 current_area1 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area1) + i) - (char *)heapbase1);
613 if((ignore1 = heap_comparison_ignore_size(current_area1)) > 0){
614 current_area2 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area2) + i) - (char *)heapbase2);
615 if((ignore2 = heap_comparison_ignore_size(current_area2)) == ignore1){
624 if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
626 /* Check pointer difference */
627 pointer_align = (i / sizeof(void*)) * sizeof(void*);
628 address_pointed1 = *((void **)((char *)area1 + pointer_align));
629 address_pointed2 = *((void **)((char *)area2 + pointer_align));
631 /* Get pointed blocks number */
632 block_pointed1 = ((char*)address_pointed1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
633 block_pointed2 = ((char*)address_pointed2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
635 /* Check if valid blocks number */
636 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)
639 if(heapinfo1[block_pointed1].type == heapinfo2[block_pointed2].type){ /* Same type of block (large or fragmented) */
641 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
642 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
644 if(heapinfo1[block_pointed1].type == 0){ /* Large block */
646 if(heapinfo1[block_pointed1].busy_block.size != heapinfo2[block_pointed2].busy_block.size){
650 if(heapinfo1[block_pointed1].busy_block.busy_size != heapinfo2[block_pointed2].busy_block.busy_size){
654 if(add_heap_area_pair(previous, block_pointed1, -1, block_pointed2, -1)){
656 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
657 if(in_mc_comparison_ignore(block_pointed1, -1))
658 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 1);
660 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
662 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
670 }else{ /* Fragmented block */
672 /* Get pointed fragments number */
673 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
674 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
676 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
679 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
680 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
682 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
684 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
685 if(in_mc_comparison_ignore(block_pointed1, frag_pointed1))
686 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
688 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
690 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
702 if((heapinfo1[block_pointed1].type > 0) && (heapinfo2[block_pointed2].type > 0)){
704 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
705 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
707 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
708 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
710 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
713 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
714 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
716 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
718 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
719 if(in_mc_comparison_ignore(block_pointed1, frag_pointed1))
720 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
722 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
724 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
738 i = pointer_align + sizeof(void *);
752 static void heap_area_pair_free(heap_area_pair_t pair){
759 static void heap_area_pair_free_voidp(void *d)
761 heap_area_pair_free((heap_area_pair_t) * (void **) d);
764 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
766 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
767 heap_area_pair_t pair = NULL;
768 pair = xbt_new0(s_heap_area_pair_t, 1);
769 pair->block1 = block1;
770 pair->fragment1 = fragment1;
771 pair->block2 = block2;
772 pair->fragment2 = fragment2;
774 xbt_dynar_push(list, &pair);
782 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
784 unsigned int cursor = 0;
785 heap_area_pair_t current_pair;
787 xbt_dynar_foreach(list, cursor, current_pair){
788 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
795 static void match_equals(xbt_dynar_t list, xbt_dynar_t *equals){
797 unsigned int cursor = 0;
798 heap_area_pair_t current_pair;
799 heap_area_t previous_area;
801 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
803 xbt_dynar_foreach(list, cursor, current_pair){
805 if(current_pair->fragment1 != -1){
807 real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
808 real_addr_frag1 = (void*) ((char *)real_addr_block1 + (current_pair->fragment1 << heapinfo1[current_pair->block1].type));
809 real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
810 real_addr_frag2 = (void*) ((char *)real_addr_block2 + (current_pair->fragment2 << heapinfo2[current_pair->block2].type));
812 if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){
813 remove_heap_equality(equals, 1, real_addr_frag1);
814 previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
815 xbt_free( heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
816 heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
817 xbt_free(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1]);
819 if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){
820 remove_heap_equality(equals, 2, real_addr_frag2);
821 previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
822 xbt_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
823 heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
824 xbt_free(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2]);
827 if(real_addr_frag1 != real_addr_frag2)
828 add_heap_equality(equals, real_addr_frag1, real_addr_frag2);
830 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
831 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
835 real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
836 real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
838 if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
839 remove_heap_equality(equals, 1, real_addr_block1);
840 previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
841 xbt_free(heapinfo2[previous_area->block].busy_block.equal_to);
842 heapinfo2[previous_area->block].busy_block.equal_to = NULL;
843 xbt_free(heapinfo1[current_pair->block1].busy_block.equal_to);
845 if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
846 remove_heap_equality(equals, 2, real_addr_block2);
847 previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
848 xbt_free(heapinfo1[previous_area->block].busy_block.equal_to);
849 heapinfo1[previous_area->block].busy_block.equal_to = NULL;
850 xbt_free(heapinfo2[current_pair->block2].busy_block.equal_to);
853 if(real_addr_block1 != real_addr_block2)
854 add_heap_equality(equals, real_addr_block1, real_addr_block2);
856 heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
857 heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
866 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
869 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
871 if(heap1 == NULL && heap1 == NULL){
872 XBT_DEBUG("Malloc descriptors null");
876 if(heap1->heaplimit != heap2->heaplimit){
877 XBT_DEBUG("Different limit of valid info table indices");
881 /* Heap information */
882 heaplimit = ((struct mdesc *)heap1)->heaplimit;
884 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
886 heapbase1 = (char *)heap1 + BLOCKSIZE;
887 heapbase2 = (char *)heap2 + BLOCKSIZE;
889 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
890 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
892 heapsize1 = heap1->heapsize;
893 heapsize2 = heap2->heapsize;
895 /* Start comparison */
897 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
901 /* Check busy blocks*/
905 while(i <= heaplimit){
907 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
908 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
910 if(heapinfo1[i].type != heapinfo2[i].type){
912 distance += BLOCKSIZE;
913 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
918 if(heapinfo1[i].type == -1){ /* Free block */
923 if(heapinfo1[i].type == 0){ /* Large block */
925 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
926 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
927 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
928 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);
932 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
933 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
934 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
935 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);
941 //while(k < (heapinfo1[i].busy_block.busy_size)){
942 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
943 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
951 }else { /* Fragmented block */
953 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
955 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
956 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
958 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
963 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
964 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
965 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);
971 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
972 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
973 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
993 static char * is_stack(void *address){
994 unsigned int cursor = 0;
995 stack_region_t stack;
997 xbt_dynar_foreach(stacks_areas, cursor, stack){
998 if(address == stack->address)
999 return stack->process_name;
1005 static void add_heap_equality(xbt_dynar_t *equals, void *a1, void *a2){
1007 if(xbt_dynar_is_empty(*equals)){
1009 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
1013 xbt_dynar_insert_at(*equals, 0, &he);
1017 unsigned int cursor = 0;
1019 int end = xbt_dynar_length(*equals) - 1;
1020 heap_equality_t current_equality = NULL;
1022 while(start <= end){
1023 cursor = (start + end) / 2;
1024 current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
1025 if(current_equality->address1 == a1){
1026 if(current_equality->address2 == a2)
1028 if(current_equality->address2 < a2)
1030 if(current_equality->address2 > a2)
1033 if(current_equality->address1 < a1)
1035 if(current_equality->address1 > a1)
1039 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
1043 if(current_equality->address1 < a1)
1044 xbt_dynar_insert_at(*equals, cursor + 1 , &he);
1046 xbt_dynar_insert_at(*equals, cursor, &he);
1052 static void remove_heap_equality(xbt_dynar_t *equals, int address, void *a){
1054 unsigned int cursor = 0;
1055 heap_equality_t current_equality;
1061 int end = xbt_dynar_length(*equals) - 1;
1064 while(start <= end && found == 0){
1065 cursor = (start + end) / 2;
1066 current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
1067 if(current_equality->address1 == a)
1069 if(current_equality->address1 < a)
1071 if(current_equality->address1 > a)
1076 xbt_dynar_remove_at(*equals, cursor, NULL);
1080 xbt_dynar_foreach(*equals, cursor, current_equality){
1081 if(current_equality->address2 == a){
1088 xbt_dynar_remove_at(*equals, cursor, NULL);
1095 int is_free_area(void *area, xbt_mheap_t heap){
1097 void *sheap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1098 malloc_info *heapinfo = (malloc_info *)((char *)heap + ((uintptr_t)((char *)heap->heapinfo - (char *)sheap)));
1099 size_t heapsize = heap->heapsize;
1101 /* Get block number */
1102 size_t block = ((char*)area - (char*)((xbt_mheap_t)sheap)->heapbase) / BLOCKSIZE + 1;
1105 /* Check if valid block number */
1106 if((char *)area < (char*)((xbt_mheap_t)sheap)->heapbase || block > heapsize || block < 1)
1109 if(heapinfo[block].type < 0)
1112 if(heapinfo[block].type == 0)
1115 if(heapinfo[block].type > 0){
1116 fragment = ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1117 if(heapinfo[block].busy_frag.frag_size[fragment] == 0)