1 /* mm_diff - Memory snapshooting and comparison */
3 /* Copyright (c) 2008-2012. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "xbt/ex_interface.h" /* internals of backtrace setup */
11 #include "xbt/mmalloc.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
14 "Logging specific to mm_diff in mmalloc");
16 extern char *xbt_binary_name;
18 xbt_dynar_t mmalloc_ignore;
19 xbt_dynar_t stacks_areas;
21 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_mmalloc_ignore(int block, int fragment);
31 static size_t heap_comparison_ignore(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 XBT_DEBUG("No backtrace available for that block, sorry.");
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 XBT_DEBUG("(backtrace not set)");
52 } else if (e.bt_strings == NULL) {
53 XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
57 XBT_DEBUG("Backtrace of where the block %d was malloced (%d frames):", block ,e.used);
58 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
59 XBT_DEBUG("%d ---> %s",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 XBT_DEBUG("(backtrace not set)");
75 } else if (e.bt_strings == NULL) {
76 XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
80 XBT_DEBUG("Backtrace of where the fragment %d in block %d was malloced (%d frames):", frag, block ,e.used);
81 for (i = 0; i < e.used; i++) /* no need to display "xbt_backtrace_display" */{
82 XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
89 void *s_heap, *heapbase1, *heapbase2;
90 malloc_info *heapinfo1, *heapinfo2;
91 size_t heaplimit, heapsize1, heapsize2;
95 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t *stack1, xbt_dynar_t *stack2, xbt_dynar_t *equals){
97 if(heap1 == NULL && heap1 == NULL){
98 XBT_DEBUG("Malloc descriptors null");
102 if(heap1->heaplimit != heap2->heaplimit){
103 XBT_DEBUG("Different limit of valid info table indices");
107 /* Heap information */
108 heaplimit = ((struct mdesc *)heap1)->heaplimit;
110 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
112 heapbase1 = (char *)heap1 + BLOCKSIZE;
113 heapbase2 = (char *)heap2 + BLOCKSIZE;
115 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
116 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
118 heapsize1 = heap1->heapsize;
119 heapsize2 = heap2->heapsize;
121 /* Start comparison */
122 size_t i1, i2, j1, j2, k, current_block, current_fragment;
123 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
124 void *real_addr_block1, *real_addr_block2;
126 int nb_block1=0, nb_frag1=0, nb_block2=0, nb_frag2=0;
128 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
130 int equal, res_compare;
134 /* Init equal information */
137 while(i1<=heaplimit){
138 if(heapinfo1[i1].type == 0){
139 if(heapinfo1[i1].busy_block.busy_size > 0)
141 heapinfo1[i1].busy_block.equal_to = NULL;
143 if(heapinfo1[i1].type > 0){
144 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
145 if(heapinfo1[i1].busy_frag.frag_size[j1] > 0)
147 heapinfo1[i1].busy_frag.equal_to[j1] = NULL;
155 while(i2<=heaplimit){
156 if(heapinfo2[i2].type == 0){
157 if(heapinfo2[i2].busy_block.busy_size > 0)
159 heapinfo2[i2].busy_block.equal_to = NULL;
161 if(heapinfo2[i2].type > 0){
162 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
163 if(heapinfo2[i2].busy_frag.frag_size[j2] > 0)
165 heapinfo2[i2].busy_frag.equal_to[j2] = NULL;
171 if(nb_block1 != nb_block2 || nb_frag1 != nb_frag2){
172 XBT_DEBUG("Different number of busy blocks (%d - %d) or busy fragments (%d - %d)", nb_block1, nb_block2, nb_frag1, nb_frag2);
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*)heapbase1));
190 real_addr_block1 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block1) - (char *)heapbase1);
192 if(heapinfo1[i1].type == 0){ /* Large block */
194 if((stack_name = is_stack(real_addr_block1)) != NULL){
195 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
196 stack->address = addr_block1;
197 stack->process_name = strdup(stack_name);
198 stack->size = heapinfo1[i1].busy_block.busy_size;
199 xbt_dynar_push(*stack1, &stack);
202 if(heapinfo1[i1].busy_block.busy_size == 0){
207 if(heapinfo1[i1].busy_block.equal_to != NULL){
215 /* Try first to associate to same block in the other heap */
216 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
218 if(heapinfo2[current_block].busy_block.equal_to == NULL){
220 if(heapinfo1[current_block].busy_block.busy_size == heapinfo2[current_block].busy_block.busy_size){
222 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
224 real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
226 if((stack_name = is_stack(real_addr_block2)) != NULL){
227 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
228 stack->address = addr_block2;
229 stack->process_name = strdup(stack_name);
230 stack->size = heapinfo2[current_block].busy_block.busy_size;
231 xbt_dynar_push(*stack2, &stack);
234 add_heap_area_pair(previous, current_block, -1, current_block, -1);
236 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
237 if(in_mmalloc_ignore((int)current_block, -1))
238 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 1);
240 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
242 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
245 if(res_compare == 0){
246 for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
247 heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
248 for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
249 heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
251 match_equals(previous, equals);
252 i1 = i1 + heapinfo1[current_block].busy_block.size;
255 xbt_dynar_reset(previous);
263 while(i2 <= heaplimit && !equal){
265 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
267 real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
269 if((stack_name = is_stack(real_addr_block2)) != NULL){
270 stack_region_t stack = xbt_new0(s_stack_region_t, 1);
271 stack->address = addr_block2;
272 stack->process_name = strdup(stack_name);
273 stack->size = heapinfo2[i2].busy_block.busy_size;
274 xbt_dynar_push(*stack2, &stack);
277 if(i2 == current_block){
282 if(heapinfo2[i2].type != 0){
287 if(heapinfo2[i2].busy_block.equal_to != NULL){
292 if(heapinfo1[i1].busy_block.size != heapinfo2[i2].busy_block.size){
297 if(heapinfo1[i1].busy_block.busy_size != heapinfo2[i2].busy_block.busy_size){
303 add_heap_area_pair(previous, i1, -1, i2, -1);
305 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
306 if(in_mmalloc_ignore((int)i1, -1))
307 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 1);
309 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
311 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
314 if(res_compare == 0){
315 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
316 heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
317 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
318 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
320 match_equals(previous, equals);
321 i1 = i1 + heapinfo1[i1].busy_block.size;
324 xbt_dynar_reset(previous);
333 }else{ /* Fragmented block */
335 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
337 current_fragment = j1;
339 if(heapinfo1[i1].busy_frag.frag_size[j1] == 0) /* Free fragment */
342 if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
345 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
350 /* Try first to associate to same fragment in the other heap */
351 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
353 if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){
355 if(heapinfo1[current_block].busy_frag.frag_size[current_fragment] == heapinfo2[current_block].busy_frag.frag_size[current_fragment]){
357 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
358 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << heapinfo2[current_block].type));
360 add_heap_area_pair(previous, current_block, current_fragment, current_block, current_fragment);
362 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
363 if(in_mmalloc_ignore((int)current_block, (int)current_fragment))
364 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 1);
366 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
368 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
371 if(res_compare == 0){
373 match_equals(previous, equals);
376 xbt_dynar_reset(previous);
384 while(i2 <= heaplimit && !equal){
387 if(heapinfo2[i2].type <= 0){
392 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
394 if(heapinfo2[i2].type == heapinfo1[i1].type && i2 == current_block && j2 == current_fragment)
397 if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)
400 if(heapinfo1[i1].busy_frag.frag_size[j1] != heapinfo2[i2].busy_frag.frag_size[j2]) /* Different size_used */
403 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
404 addr_frag2 = (void*) ((char *)addr_block2 + (j2 << heapinfo2[i2].type));
407 add_heap_area_pair(previous, i1, j1, i2, j2);
409 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
410 if(in_mmalloc_ignore((int)i1, (int)j1))
411 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 1);
413 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
415 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
418 if(res_compare == 0){
420 match_equals(previous, equals);
424 xbt_dynar_reset(previous);
440 /* All blocks/fragments are equal to another block/fragment ? */
442 int nb_diff1 = 0, nb_diff2 = 0;
445 if(heapinfo1[i].type == 0){
446 if(heapinfo1[i].busy_block.busy_size > 0){
447 if(heapinfo1[i].busy_block.equal_to == NULL){
448 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
449 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
450 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
451 mmalloc_backtrace_block_display((void*)heapinfo1, i);
457 if(heapinfo1[i].type > 0){
458 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
459 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
460 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
461 if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
462 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
463 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
464 XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
465 mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
476 XBT_DEBUG("Different blocks or fragments in heap1 : %d", nb_diff1);
481 if(heapinfo2[i].type == 0){
482 if(heapinfo2[i].busy_block.busy_size > 0){
483 if(heapinfo2[i].busy_block.equal_to == NULL){
484 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
485 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
486 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
487 mmalloc_backtrace_block_display((void*)heapinfo2, i);
493 if(heapinfo2[i].type > 0){
494 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
495 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
496 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
497 if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
498 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
499 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
500 XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
501 mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
511 XBT_DEBUG("Different blocks or fragments in heap2 : %d", nb_diff2);
513 xbt_dynar_free(&previous);
515 return ((nb_diff1 > 0) || (nb_diff2 > 0));
519 static heap_area_t new_heap_area(int block, int fragment){
520 heap_area_t area = NULL;
521 area = xbt_new0(s_heap_area_t, 1);
523 area->fragment = fragment;
527 static int in_mmalloc_ignore(int block, int fragment){
529 unsigned int cursor = 0;
531 int end = xbt_dynar_length(mmalloc_ignore) - 1;
532 mc_ignore_region_t region;
535 cursor = (start + end) / 2;
536 region = (mc_ignore_region_t)xbt_dynar_get_as(mmalloc_ignore, cursor, mc_ignore_region_t);
537 if(region->block == block){
538 if(region->fragment == fragment)
540 if(region->fragment < fragment)
542 if(region->fragment > fragment)
545 if(region->block < block)
547 if(region->block > block)
554 static size_t heap_comparison_ignore(void *address){
555 unsigned int cursor = 0;
557 int end = xbt_dynar_length(mmalloc_ignore) - 1;
558 mc_ignore_region_t region;
561 cursor = (start + end) / 2;
562 region = (mc_ignore_region_t)xbt_dynar_get_as(mmalloc_ignore, cursor, mc_ignore_region_t);
563 if(region->address == address)
565 if(region->address < address)
567 if(region->address > address)
575 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore){
577 size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
578 void *address_pointed1, *address_pointed2, *addr_block_pointed1, *addr_block_pointed2, *addr_frag_pointed1, *addr_frag_pointed2;
579 size_t block_pointed1, block_pointed2, frag_pointed1, frag_pointed2;
581 void *current_area1, *current_area2;
587 current_area1 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area1) + i) - (char *)heapbase1);
588 if((ignore1 = heap_comparison_ignore(current_area1)) > 0){
589 current_area2 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area2) + i) - (char *)heapbase2);
590 if((ignore2 = heap_comparison_ignore(current_area2)) == ignore1){
599 if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
601 /* Check pointer difference */
602 pointer_align = (i / sizeof(void*)) * sizeof(void*);
603 address_pointed1 = *((void **)((char *)area1 + pointer_align));
604 address_pointed2 = *((void **)((char *)area2 + pointer_align));
606 /* Get pointed blocks number */
607 block_pointed1 = ((char*)address_pointed1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
608 block_pointed2 = ((char*)address_pointed2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
610 /* Check if valid blocks number */
611 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)
614 if(heapinfo1[block_pointed1].type == heapinfo2[block_pointed2].type){ /* Same type of block (large or fragmented) */
616 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
617 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
619 if(heapinfo1[block_pointed1].type == 0){ /* Large block */
621 if(heapinfo1[block_pointed1].busy_block.size != heapinfo2[block_pointed2].busy_block.size){
625 if(heapinfo1[block_pointed1].busy_block.busy_size != heapinfo2[block_pointed2].busy_block.busy_size){
629 if(add_heap_area_pair(previous, block_pointed1, -1, block_pointed2, -1)){
631 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
632 if(in_mmalloc_ignore(block_pointed1, -1))
633 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 1);
635 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
637 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
645 }else{ /* Fragmented block */
647 /* Get pointed fragments number */
648 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
649 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
651 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
654 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
655 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
657 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
659 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
660 if(in_mmalloc_ignore(block_pointed1, frag_pointed1))
661 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
663 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
665 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
677 if((heapinfo1[block_pointed1].type > 0) && (heapinfo2[block_pointed2].type > 0)){
679 addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
680 addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
682 frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
683 frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
685 if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */
688 addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
689 addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
691 if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
693 if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
694 if(in_mmalloc_ignore(block_pointed1, frag_pointed1))
695 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
697 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
699 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
713 i = pointer_align + sizeof(void *);
727 static void heap_area_pair_free(heap_area_pair_t pair){
734 static void heap_area_pair_free_voidp(void *d)
736 heap_area_pair_free((heap_area_pair_t) * (void **) d);
739 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
741 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
742 heap_area_pair_t pair = NULL;
743 pair = xbt_new0(s_heap_area_pair_t, 1);
744 pair->block1 = block1;
745 pair->fragment1 = fragment1;
746 pair->block2 = block2;
747 pair->fragment2 = fragment2;
749 xbt_dynar_push(list, &pair);
757 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
759 unsigned int cursor = 0;
760 heap_area_pair_t current_pair;
762 xbt_dynar_foreach(list, cursor, current_pair){
763 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
770 static void match_equals(xbt_dynar_t list, xbt_dynar_t *equals){
772 unsigned int cursor = 0;
773 heap_area_pair_t current_pair;
774 heap_area_t previous_area;
776 void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
778 xbt_dynar_foreach(list, cursor, current_pair){
780 if(current_pair->fragment1 != -1){
782 real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
783 real_addr_frag1 = (void*) ((char *)real_addr_block1 + (current_pair->fragment1 << heapinfo1[current_pair->block1].type));
784 real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
785 real_addr_frag2 = (void*) ((char *)real_addr_block2 + (current_pair->fragment2 << heapinfo2[current_pair->block2].type));
787 if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){
788 remove_heap_equality(equals, 1, real_addr_frag1);
789 previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
790 xbt_free( heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
791 heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
792 xbt_free(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1]);
794 if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){
795 remove_heap_equality(equals, 2, real_addr_frag2);
796 previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
797 xbt_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
798 heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
799 xbt_free(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2]);
802 if(real_addr_frag1 != real_addr_frag2)
803 add_heap_equality(equals, real_addr_frag1, real_addr_frag2);
805 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
806 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
810 real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
811 real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
813 if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
814 remove_heap_equality(equals, 1, real_addr_block1);
815 previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
816 xbt_free(heapinfo2[previous_area->block].busy_block.equal_to);
817 heapinfo2[previous_area->block].busy_block.equal_to = NULL;
818 xbt_free(heapinfo1[current_pair->block1].busy_block.equal_to);
820 if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
821 remove_heap_equality(equals, 2, real_addr_block2);
822 previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
823 xbt_free(heapinfo1[previous_area->block].busy_block.equal_to);
824 heapinfo1[previous_area->block].busy_block.equal_to = NULL;
825 xbt_free(heapinfo2[current_pair->block2].busy_block.equal_to);
828 if(real_addr_block1 != real_addr_block2)
829 add_heap_equality(equals, real_addr_block1, real_addr_block2);
831 heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
832 heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
841 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
844 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
846 if(heap1 == NULL && heap1 == NULL){
847 XBT_DEBUG("Malloc descriptors null");
851 if(heap1->heaplimit != heap2->heaplimit){
852 XBT_DEBUG("Different limit of valid info table indices");
856 /* Heap information */
857 heaplimit = ((struct mdesc *)heap1)->heaplimit;
859 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
861 heapbase1 = (char *)heap1 + BLOCKSIZE;
862 heapbase2 = (char *)heap2 + BLOCKSIZE;
864 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
865 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
867 heapsize1 = heap1->heapsize;
868 heapsize2 = heap2->heapsize;
870 /* Start comparison */
872 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
876 /* Check busy blocks*/
880 while(i <= heaplimit){
882 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
883 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
885 if(heapinfo1[i].type != heapinfo2[i].type){
887 distance += BLOCKSIZE;
888 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
893 if(heapinfo1[i].type == -1){ /* Free block */
898 if(heapinfo1[i].type == 0){ /* Large block */
900 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
901 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
902 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
903 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);
907 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
908 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
909 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
910 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);
916 //while(k < (heapinfo1[i].busy_block.busy_size)){
917 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
918 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
926 }else { /* Fragmented block */
928 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
930 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
931 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
933 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
938 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
939 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
940 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);
946 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
947 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
948 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
968 static char * is_stack(void *address){
969 unsigned int cursor = 0;
970 stack_region_t stack;
972 xbt_dynar_foreach(stacks_areas, cursor, stack){
973 if(address == stack->address)
974 return stack->process_name;
980 static void add_heap_equality(xbt_dynar_t *equals, void *a1, void *a2){
982 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
986 if(xbt_dynar_is_empty(*equals)){
988 xbt_dynar_insert_at(*equals, 0, &he);
992 unsigned int cursor = 0;
994 int end = xbt_dynar_length(*equals) - 1;
995 heap_equality_t current_equality = NULL;
998 cursor = (start + end) / 2;
999 current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
1000 if(current_equality->address1 == a1){
1001 if(current_equality->address2 == a2)
1003 if(current_equality->address2 < a2)
1005 if(current_equality->address2 > a2)
1008 if(current_equality->address1 < a1)
1010 if(current_equality->address1 > a1)
1014 if(current_equality->address1 < a1)
1015 xbt_dynar_insert_at(*equals, cursor + 1 , &he);
1017 xbt_dynar_insert_at(*equals, cursor, &he);
1023 static void remove_heap_equality(xbt_dynar_t *equals, int address, void *a){
1025 unsigned int cursor = 0;
1026 heap_equality_t current_equality;
1032 int end = xbt_dynar_length(*equals) - 1;
1035 while(start <= end && found == 0){
1036 cursor = (start + end) / 2;
1037 current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
1038 if(current_equality->address1 == a)
1040 if(current_equality->address1 < a)
1042 if(current_equality->address1 > a)
1047 xbt_dynar_remove_at(*equals, cursor, NULL);
1051 xbt_dynar_foreach(*equals, cursor, current_equality){
1052 if(current_equality->address2 == a){
1059 xbt_dynar_remove_at(*equals, cursor, NULL);
1066 int is_free_area(void *area, xbt_mheap_t heap){
1068 void *sheap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1069 malloc_info *heapinfo = (malloc_info *)((char *)heap + ((uintptr_t)((char *)heap->heapinfo - (char *)sheap)));
1070 size_t heapsize = heap->heapsize;
1072 /* Get block number */
1073 size_t block = ((char*)area - (char*)((xbt_mheap_t)sheap)->heapbase) / BLOCKSIZE + 1;
1076 /* Check if valid block number */
1077 if((char *)area < (char*)((xbt_mheap_t)sheap)->heapbase || block > heapsize || block < 1)
1080 if(heapinfo[block].type < 0)
1083 if(heapinfo[block].type == 0)
1086 if(heapinfo[block].type > 0){
1087 fragment = ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1088 if(heapinfo[block].busy_frag.frag_size[fragment] == 0)