1 /* mm_diff - Memory snapshooting and comparison */
3 /* Copyright (c) 2008-2012. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "xbt/ex_interface.h" /* internals of backtrace setup */
11 #include "xbt/mmalloc.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
14 "Logging specific to mm_diff in mmalloc");
16 xbt_dynar_t mc_heap_comparison_ignore;
17 xbt_dynar_t stacks_areas;
19 static void heap_area_pair_free(heap_area_pair_t pair);
20 static void heap_area_pair_free_voidp(void *d);
21 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
22 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
23 static heap_area_t new_heap_area(int block, int fragment);
25 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore);
26 static void match_equals(xbt_dynar_t list, xbt_dynar_t *equals);
28 static int in_mc_comparison_ignore(int block, int fragment);
29 static size_t heap_comparison_ignore_size(void *address);
30 static void add_heap_equality(xbt_dynar_t *equals, void *a1, void *a2);
31 static void remove_heap_equality(xbt_dynar_t *equals, int address, void *a);
33 static char* is_stack(void *address);
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);
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);
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;
122 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t *stack1, xbt_dynar_t *stack2, xbt_dynar_t *equals){
124 if(heap1 == NULL && heap1 == NULL){
125 XBT_DEBUG("Malloc descriptors null");
129 if(heap1->heaplimit != heap2->heaplimit){
130 XBT_DEBUG("Different limit of valid info table indices");
134 /* Heap information */
135 heaplimit = ((struct mdesc *)heap1)->heaplimit;
137 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
139 heapbase1 = (char *)heap1 + BLOCKSIZE;
140 heapbase2 = (char *)heap2 + BLOCKSIZE;
142 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
143 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
145 heapsize1 = heap1->heapsize;
146 heapsize2 = heap2->heapsize;
148 /* Start comparison */
149 size_t i1, i2, j1, j2, k, current_block, current_fragment;
150 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
151 void *real_addr_block1, *real_addr_block2;
154 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
156 int equal, res_compare = 0;
158 /* Init equal information */
161 while(i1<=heaplimit){
162 if(heapinfo1[i1].type == 0){
163 heapinfo1[i1].busy_block.equal_to = NULL;
165 if(heapinfo1[i1].type > 0){
166 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
167 heapinfo1[i1].busy_frag.equal_to[j1] = NULL;
175 while(i2<=heaplimit){
176 if(heapinfo2[i2].type == 0){
177 heapinfo2[i2].busy_block.equal_to = NULL;
179 if(heapinfo2[i2].type > 0){
180 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
181 heapinfo2[i2].busy_frag.equal_to[j2] = NULL;
187 /* Check busy blocks*/
191 while(i1 <= heaplimit){
195 if(heapinfo1[i1].type == -1){ /* Free block */
200 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)heapbase1));
201 real_addr_block1 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block1) - (char *)heapbase1);
203 if(heapinfo1[i1].type == 0){ /* Large block */
205 if((stack_name = is_stack(real_addr_block1)) != NULL){
206 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
207 stack->address = addr_block1;
208 stack->process_name = strdup(stack_name);
209 stack->size = heapinfo1[i1].busy_block.busy_size;
210 xbt_dynar_push(*stack1, &stack);
214 if(heapinfo1[i1].busy_block.busy_size == 0){
219 if(heapinfo1[i1].busy_block.equal_to != NULL){
228 /* Try first to associate to same block in the other heap */
229 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
231 if(heapinfo2[current_block].busy_block.equal_to == NULL){
233 if(heapinfo1[current_block].busy_block.busy_size == heapinfo2[current_block].busy_block.busy_size){
235 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
236 real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
238 if((stack_name = is_stack(real_addr_block2)) != NULL){
239 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
240 stack->address = addr_block2;
241 stack->process_name = strdup(stack_name);
242 stack->size = heapinfo2[current_block].busy_block.busy_size;
243 xbt_dynar_push(*stack2, &stack);
247 add_heap_area_pair(previous, current_block, -1, current_block, -1);
249 if(res_compare != -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);
260 if(res_compare == 0 || res_compare == -1){
261 for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
262 heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
263 for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
264 heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
266 match_equals(previous, equals);
267 i1 = i1 + heapinfo1[current_block].busy_block.size;
270 xbt_dynar_reset(previous);
278 while(i2 <= heaplimit && !equal){
280 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
281 real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
283 if((stack_name = is_stack(real_addr_block2)) != NULL){
284 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
285 stack->address = addr_block2;
286 stack->process_name = strdup(stack_name);
287 stack->size = heapinfo2[i2].busy_block.busy_size;
288 xbt_dynar_push(*stack2, &stack);
292 if(i2 == current_block){
297 if(heapinfo2[i2].type != 0){
302 if(heapinfo2[i2].busy_block.equal_to != NULL){
307 if(heapinfo1[i1].busy_block.size != heapinfo2[i2].busy_block.size){
312 if(heapinfo1[i1].busy_block.busy_size != heapinfo2[i2].busy_block.busy_size){
318 add_heap_area_pair(previous, i1, -1, i2, -1);
320 if(res_compare != -1){
321 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
322 if(in_mc_comparison_ignore((int)i1, -1))
323 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 1);
325 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
327 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
331 if(res_compare == 0 || res_compare == -1){
332 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
333 heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
334 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
335 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
337 match_equals(previous, equals);
338 i1 = i1 + heapinfo1[i1].busy_block.size;
341 xbt_dynar_reset(previous);
350 }else{ /* Fragmented block */
352 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
354 current_fragment = j1;
356 if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
359 if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
362 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
367 /* Try first to associate to same fragment in the other heap */
368 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
370 if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){
372 if(heapinfo1[current_block].busy_frag.frag_size[current_fragment] == heapinfo2[current_block].busy_frag.frag_size[current_fragment]){
374 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
375 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << heapinfo2[current_block].type));
377 add_heap_area_pair(previous, current_block, current_fragment, current_block, current_fragment);
379 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
380 if(in_mc_comparison_ignore((int)current_block, (int)current_fragment))
381 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 1);
383 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
385 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
388 if(res_compare == 0){
390 match_equals(previous, equals);
393 xbt_dynar_reset(previous);
400 while(i2 <= heaplimit && !equal){
403 if(heapinfo2[i2].type <= 0){
408 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
410 if(heapinfo2[i2].type == heapinfo1[i1].type && i2 == current_block && j2 == current_fragment)
413 if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)
416 if(heapinfo1[i1].busy_frag.frag_size[j1] != heapinfo2[i2].busy_frag.frag_size[j2]) /* Different size_used */
419 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
420 addr_frag2 = (void*) ((char *)addr_block2 + (j2 << heapinfo2[i2].type));
423 add_heap_area_pair(previous, i1, j1, i2, j2);
425 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
426 if(in_mc_comparison_ignore((int)i1, (int)j1))
427 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 1);
429 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
431 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
434 if(res_compare == 0){
436 match_equals(previous, equals);
440 xbt_dynar_reset(previous);
456 /* All blocks/fragments are equal to another block/fragment ? */
458 int nb_diff1 = 0, nb_diff2 = 0;
461 if(heapinfo1[i].type == 0){
462 if(heapinfo1[i].busy_block.busy_size > 0){
463 if(heapinfo1[i].busy_block.equal_to == NULL){
464 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
465 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
466 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
467 mmalloc_backtrace_block_display((void*)heapinfo1, i);
471 xbt_free(heapinfo1[i].busy_block.equal_to);
475 if(heapinfo1[i].type > 0){
476 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
477 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
478 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
479 if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
480 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
481 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
482 XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
483 mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
487 xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
496 XBT_DEBUG("Different blocks or fragments in heap1 : %d", nb_diff1);
501 if(heapinfo2[i].type == 0){
502 if(heapinfo2[i].busy_block.busy_size > 0){
503 if(heapinfo2[i].busy_block.equal_to == NULL){
504 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
505 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
506 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
507 mmalloc_backtrace_block_display((void*)heapinfo2, i);
511 xbt_free(heapinfo2[i].busy_block.equal_to);
515 if(heapinfo2[i].type > 0){
516 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
517 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
518 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
519 if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
520 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
521 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
522 XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
523 mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
527 xbt_free(heapinfo2[i].busy_frag.equal_to[j]);
535 XBT_DEBUG("Different blocks or fragments in heap2 : %d", nb_diff2);
537 xbt_dynar_free(&previous);
539 s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
540 heapinfo1 = NULL, heapinfo2 = NULL;
541 heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
543 return ((nb_diff1 > 0) || (nb_diff2 > 0));
546 static heap_area_t new_heap_area(int block, int fragment){
547 heap_area_t area = NULL;
548 area = xbt_new0(s_heap_area_t, 1);
550 area->fragment = fragment;
554 static int in_mc_comparison_ignore(int block, int fragment){
556 unsigned int cursor = 0;
558 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
559 mc_heap_ignore_region_t region;
562 cursor = (start + end) / 2;
563 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
564 if(region->block == block){
565 if(region->fragment == fragment)
567 if(region->fragment < fragment)
569 if(region->fragment > fragment)
572 if(region->block < block)
574 if(region->block > block)
581 static size_t heap_comparison_ignore_size(void *address){
582 unsigned int cursor = 0;
584 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
585 mc_heap_ignore_region_t region;
588 cursor = (start + end) / 2;
589 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
590 if(region->address == address)
592 if(region->address < address)
594 if(region->address > address)
602 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore){
604 size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
605 void *address_pointed1, *address_pointed2, *addr_block_pointed1, *addr_block_pointed2, *addr_frag_pointed1, *addr_frag_pointed2;
606 size_t block_pointed1, block_pointed2, frag_pointed1, frag_pointed2;
608 void *current_area1, *current_area2;
614 current_area1 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area1) + i) - (char *)heapbase1);
615 if((ignore1 = heap_comparison_ignore_size(current_area1)) > 0){
616 current_area2 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area2) + i) - (char *)heapbase2);
617 if((ignore2 = heap_comparison_ignore_size(current_area2)) == ignore1){
626 if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
628 /* Check pointer difference */
629 pointer_align = (i / sizeof(void*)) * sizeof(void*);
630 address_pointed1 = *((void **)((char *)area1 + pointer_align));
631 address_pointed2 = *((void **)((char *)area2 + pointer_align));
633 /* Get pointed blocks number */
634 block_pointed1 = ((char*)address_pointed1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
635 block_pointed2 = ((char*)address_pointed2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
637 /* Check if valid blocks number */
638 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)
641 if(heapinfo1[block_pointed1].type == heapinfo2[block_pointed2].type){ /* Same type of block (large or fragmented) */
643 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
644 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
646 if(heapinfo1[block_pointed1].type == 0){ /* Large block */
648 if(heapinfo1[block_pointed1].busy_block.size != heapinfo2[block_pointed2].busy_block.size){
652 if(heapinfo1[block_pointed1].busy_block.busy_size != heapinfo2[block_pointed2].busy_block.busy_size){
656 if(add_heap_area_pair(previous, block_pointed1, -1, block_pointed2, -1)){
658 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
659 if(in_mc_comparison_ignore(block_pointed1, -1))
660 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 1);
662 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
664 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
672 }else{ /* Fragmented block */
674 /* Get pointed fragments number */
675 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
676 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
678 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
681 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
682 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
684 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
686 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
687 if(in_mc_comparison_ignore(block_pointed1, frag_pointed1))
688 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
690 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
692 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
704 if((heapinfo1[block_pointed1].type > 0) && (heapinfo2[block_pointed2].type > 0)){
706 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
707 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
709 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
710 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
712 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
715 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
716 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
718 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
720 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
721 if(in_mc_comparison_ignore(block_pointed1, frag_pointed1))
722 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
724 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
726 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
740 i = pointer_align + sizeof(void *);
754 static void heap_area_pair_free(heap_area_pair_t pair){
761 static void heap_area_pair_free_voidp(void *d)
763 heap_area_pair_free((heap_area_pair_t) * (void **) d);
766 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
768 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
769 heap_area_pair_t pair = NULL;
770 pair = xbt_new0(s_heap_area_pair_t, 1);
771 pair->block1 = block1;
772 pair->fragment1 = fragment1;
773 pair->block2 = block2;
774 pair->fragment2 = fragment2;
776 xbt_dynar_push(list, &pair);
784 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
786 unsigned int cursor = 0;
787 heap_area_pair_t current_pair;
789 xbt_dynar_foreach(list, cursor, current_pair){
790 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
797 static void match_equals(xbt_dynar_t list, xbt_dynar_t *equals){
799 unsigned int cursor = 0;
800 heap_area_pair_t current_pair;
801 heap_area_t previous_area;
803 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
805 xbt_dynar_foreach(list, cursor, current_pair){
807 if(current_pair->fragment1 != -1){
809 real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
810 real_addr_frag1 = (void*) ((char *)real_addr_block1 + (current_pair->fragment1 << heapinfo1[current_pair->block1].type));
811 real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
812 real_addr_frag2 = (void*) ((char *)real_addr_block2 + (current_pair->fragment2 << heapinfo2[current_pair->block2].type));
814 if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){
815 remove_heap_equality(equals, 1, real_addr_frag1);
816 previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
817 xbt_free( heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
818 heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
819 xbt_free(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1]);
821 if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){
822 remove_heap_equality(equals, 2, real_addr_frag2);
823 previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
824 xbt_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
825 heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
826 xbt_free(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2]);
829 if(real_addr_frag1 != real_addr_frag2)
830 add_heap_equality(equals, real_addr_frag1, real_addr_frag2);
832 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
833 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
837 real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
838 real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
840 if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
841 remove_heap_equality(equals, 1, real_addr_block1);
842 previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
843 xbt_free(heapinfo2[previous_area->block].busy_block.equal_to);
844 heapinfo2[previous_area->block].busy_block.equal_to = NULL;
845 xbt_free(heapinfo1[current_pair->block1].busy_block.equal_to);
847 if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
848 remove_heap_equality(equals, 2, real_addr_block2);
849 previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
850 xbt_free(heapinfo1[previous_area->block].busy_block.equal_to);
851 heapinfo1[previous_area->block].busy_block.equal_to = NULL;
852 xbt_free(heapinfo2[current_pair->block2].busy_block.equal_to);
855 if(real_addr_block1 != real_addr_block2)
856 add_heap_equality(equals, real_addr_block1, real_addr_block2);
858 heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
859 heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
868 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
871 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
873 if(heap1 == NULL && heap1 == NULL){
874 XBT_DEBUG("Malloc descriptors null");
878 if(heap1->heaplimit != heap2->heaplimit){
879 XBT_DEBUG("Different limit of valid info table indices");
883 /* Heap information */
884 heaplimit = ((struct mdesc *)heap1)->heaplimit;
886 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
888 heapbase1 = (char *)heap1 + BLOCKSIZE;
889 heapbase2 = (char *)heap2 + BLOCKSIZE;
891 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
892 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
894 heapsize1 = heap1->heapsize;
895 heapsize2 = heap2->heapsize;
897 /* Start comparison */
899 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
903 /* Check busy blocks*/
907 while(i <= heaplimit){
909 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
910 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
912 if(heapinfo1[i].type != heapinfo2[i].type){
914 distance += BLOCKSIZE;
915 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
920 if(heapinfo1[i].type == -1){ /* Free block */
925 if(heapinfo1[i].type == 0){ /* Large block */
927 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
928 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
929 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
930 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);
934 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
935 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
936 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
937 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);
943 //while(k < (heapinfo1[i].busy_block.busy_size)){
944 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
945 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
953 }else { /* Fragmented block */
955 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
957 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
958 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
960 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
965 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
966 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
967 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);
973 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
974 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
975 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
995 static char * is_stack(void *address){
996 unsigned int cursor = 0;
997 stack_region_t stack;
999 xbt_dynar_foreach(stacks_areas, cursor, stack){
1000 if(address == stack->address)
1001 return stack->process_name;
1007 static void add_heap_equality(xbt_dynar_t *equals, void *a1, void *a2){
1009 if(xbt_dynar_is_empty(*equals)){
1011 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
1015 xbt_dynar_insert_at(*equals, 0, &he);
1019 unsigned int cursor = 0;
1021 int end = xbt_dynar_length(*equals) - 1;
1022 heap_equality_t current_equality = NULL;
1024 while(start <= end){
1025 cursor = (start + end) / 2;
1026 current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
1027 if(current_equality->address1 == a1){
1028 if(current_equality->address2 == a2)
1030 if(current_equality->address2 < a2)
1032 if(current_equality->address2 > a2)
1035 if(current_equality->address1 < a1)
1037 if(current_equality->address1 > a1)
1041 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
1045 if(current_equality->address1 < a1)
1046 xbt_dynar_insert_at(*equals, cursor + 1 , &he);
1048 xbt_dynar_insert_at(*equals, cursor, &he);
1054 static void remove_heap_equality(xbt_dynar_t *equals, int address, void *a){
1056 unsigned int cursor = 0;
1057 heap_equality_t current_equality;
1063 int end = xbt_dynar_length(*equals) - 1;
1066 while(start <= end && found == 0){
1067 cursor = (start + end) / 2;
1068 current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
1069 if(current_equality->address1 == a)
1071 if(current_equality->address1 < a)
1073 if(current_equality->address1 > a)
1078 xbt_dynar_remove_at(*equals, cursor, NULL);
1082 xbt_dynar_foreach(*equals, cursor, current_equality){
1083 if(current_equality->address2 == a){
1090 xbt_dynar_remove_at(*equals, cursor, NULL);
1097 int is_free_area(void *area, xbt_mheap_t heap){
1099 void *sheap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1100 malloc_info *heapinfo = (malloc_info *)((char *)heap + ((uintptr_t)((char *)heap->heapinfo - (char *)sheap)));
1101 size_t heapsize = heap->heapsize;
1103 /* Get block number */
1104 size_t block = ((char*)area - (char*)((xbt_mheap_t)sheap)->heapbase) / BLOCKSIZE + 1;
1107 /* Check if valid block number */
1108 if((char *)area < (char*)((xbt_mheap_t)sheap)->heapbase || block > heapsize || block < 1)
1111 if(heapinfo[block].type < 0)
1114 if(heapinfo[block].type == 0)
1117 if(heapinfo[block].type > 0){
1118 fragment = ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1119 if(heapinfo[block].busy_frag.frag_size[fragment] == 0)