1 /* mm_diff - Memory snapshooting and comparison */
3 /* Copyright (c) 2008-2012. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "xbt/ex_interface.h" /* internals of backtrace setup */
11 #include "xbt/mmalloc.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
14 "Logging specific to mm_diff in mmalloc");
16 xbt_dynar_t mc_heap_comparison_ignore;
17 xbt_dynar_t stacks_areas;
18 void *maestro_stack_start, *maestro_stack_end;
20 static void heap_area_pair_free(heap_area_pair_t pair);
21 static void heap_area_pair_free_voidp(void *d);
22 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
23 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
24 static heap_area_t new_heap_area(int block, int fragment);
26 static size_t heap_comparison_ignore_size(xbt_dynar_t list, void *address);
27 static void add_heap_equality(xbt_dynar_t equals, void *a1, void *a2);
28 static void remove_heap_equality(xbt_dynar_t equals, int address, void *a);
30 static int is_stack(void *address);
31 static int is_block_stack(int block);
32 static int equal_blocks(int b1, int b2);
34 void mmalloc_backtrace_block_display(void* heapinfo, int block){
38 /* if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) { */
39 /* fprintf(stderr, "No backtrace available for that block, sorry.\n"); */
43 /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE); */
44 /* e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size; */
46 /* xbt_ex_setup_backtrace(&e); */
47 /* if (e.used == 0) { */
48 /* fprintf(stderr, "(backtrace not set)\n"); */
49 /* } else if (e.bt_strings == NULL) { */
50 /* fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
54 /* fprintf(stderr, "Backtrace of where the block %d was malloced (%d frames):\n", block ,e.used); */
55 /* for (i = 0; i < e.used; i++) /\* no need to display "xbt_backtrace_display" *\/{ */
56 /* fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
61 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
65 /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE); */
66 /* e.used = XBT_BACKTRACE_SIZE; */
68 /* xbt_ex_setup_backtrace(&e); */
69 /* if (e.used == 0) { */
70 /* fprintf(stderr, "(backtrace not set)\n"); */
71 /* } else if (e.bt_strings == NULL) { */
72 /* fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
76 /* fprintf(stderr, "Backtrace of where the fragment %d in block %d was malloced (%d frames):\n", frag, block ,e.used); */
77 /* for (i = 0; i < e.used; i++) /\* no need to display "xbt_backtrace_display" *\/{ */
78 /* fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
84 void mmalloc_backtrace_display(void *addr){
86 /* size_t block, frag_nb; */
89 /* xbt_mheap_t heap = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit(); */
91 /* block = (((char*) (addr) - (char*) heap -> heapbase) / BLOCKSIZE + 1); */
93 /* type = heap->heapinfo[block].type; */
96 /* case -1 : /\* Free block *\/ */
97 /* fprintf(stderr, "Asked to display the backtrace of a block that is free. I'm puzzled\n"); */
100 /* case 0: /\* Large block *\/ */
101 /* mmalloc_backtrace_block_display(heap->heapinfo, block); */
103 /* default: /\* Fragmented block *\/ */
104 /* frag_nb = RESIDUAL(addr, BLOCKSIZE) >> type; */
105 /* if(heap->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){ */
106 /* fprintf(stderr , "Asked to display the backtrace of a fragment that is free. I'm puzzled\n"); */
109 /* mmalloc_backtrace_fragment_display(heap->heapinfo, block, frag_nb); */
115 void *s_heap = NULL, *heapbase1 = NULL, *heapbase2 = NULL;
116 malloc_info *heapinfo1 = NULL, *heapinfo2 = NULL;
117 size_t heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
118 xbt_dynar_t to_ignore1 = NULL, to_ignore2 = NULL;
120 int ignore_done1 = 0, ignore_done2 = 0;
122 void init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t i1, xbt_dynar_t i2){
124 heaplimit = ((struct mdesc *)heap1)->heaplimit;
126 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
128 heapbase1 = (char *)heap1 + BLOCKSIZE;
129 heapbase2 = (char *)heap2 + BLOCKSIZE;
131 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
132 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
134 heapsize1 = heap1->heapsize;
135 heapsize2 = heap2->heapsize;
142 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
144 if(heap1 == NULL && heap1 == NULL){
145 XBT_DEBUG("Malloc descriptors null");
149 if(heap1->heaplimit != heap2->heaplimit){
150 XBT_DEBUG("Different limit of valid info table indices");
154 /* Start comparison */
155 size_t i1, i2, j1, j2, k;
156 size_t current_block = -1; /* avoid "maybe uninitialized" warning */
157 size_t current_fragment;
158 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
159 int nb_diff1 = 0, nb_diff2 = 0;
161 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
163 int equal, res_compare = 0;
165 /* Check busy blocks*/
169 while(i1 <= heaplimit){
173 if(heapinfo1[i1].type == -1){ /* Free block */
178 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
180 if(heapinfo1[i1].type == 0){ /* Large block */
182 if(is_stack(addr_block1)){
183 for(k=0; k < heapinfo1[i1].busy_block.size; k++)
184 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i1, -1);
185 for(k=0; k < heapinfo2[i1].busy_block.size; k++)
186 heapinfo2[i1+k].busy_block.equal_to = new_heap_area(i1, -1);
187 i1 = i1 + heapinfo1[current_block].busy_block.size;
191 if(heapinfo1[i1].busy_block.equal_to != NULL){
200 /* Try first to associate to same block in the other heap */
201 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
203 if(heapinfo2[current_block].busy_block.equal_to == NULL){
205 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
207 res_compare = compare_area(addr_block1, addr_block2, previous);
209 if(res_compare == 0){
210 for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
211 heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
212 for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
213 heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
215 match_equals(previous);
216 i1 = i1 + heapinfo1[current_block].busy_block.size;
219 xbt_dynar_reset(previous);
225 while(i2 <= heaplimit && !equal){
227 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
229 if(i2 == current_block){
234 if(heapinfo2[i2].type != 0){
239 if(heapinfo2[i2].busy_block.equal_to != NULL){
244 res_compare = compare_area(addr_block1, addr_block2, previous);
246 if(res_compare == 0){
247 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
248 heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
249 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
250 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
252 match_equals(previous);
253 i1 = i1 + heapinfo1[i1].busy_block.size;
256 xbt_dynar_reset(previous);
263 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1[i1].busy_block.busy_size, addr_block1);
268 }else{ /* Fragmented block */
270 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
272 current_fragment = j1;
274 if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
277 if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
280 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
285 /* Try first to associate to same fragment in the other heap */
286 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
288 if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){
290 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
291 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << ((xbt_mheap_t)s_heap)->heapinfo[current_block].type));
293 res_compare = compare_area(addr_frag1, addr_frag2, previous);
295 if(res_compare == 0){
297 match_equals(previous);
300 xbt_dynar_reset(previous);
305 while(i2 <= heaplimit && !equal){
308 if(heapinfo2[i2].type <= 0){
313 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
315 if(heapinfo2[i2].type == heapinfo1[i1].type && i2 == current_block && j2 == current_fragment)
318 if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)
321 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
322 addr_frag2 = (void*) ((char *)addr_block2 + (j2 << ((xbt_mheap_t)s_heap)->heapinfo[i2].type));
324 res_compare = compare_area(addr_frag1, addr_frag2, previous);
326 if(res_compare == 0){
328 match_equals(previous);
329 xbt_dynar_reset(previous);
333 xbt_dynar_reset(previous);
341 if(heapinfo1[i1].busy_frag.equal_to[j1] == NULL){
342 XBT_DEBUG("Block %zu, fragment %zu not found (size_used = %d, address = %p)", i1, j1, heapinfo1[i1].busy_frag.frag_size[j1], addr_frag1);
357 /* All blocks/fragments are equal to another block/fragment ? */
361 if(heapinfo1[i].type == 0){
362 if(current_block == heaplimit){
363 if(heapinfo1[i].busy_block.busy_size > 0){
364 if(heapinfo1[i].busy_block.equal_to == NULL){
365 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
366 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
367 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
368 //mmalloc_backtrace_block_display((void*)heapinfo1, i);
374 xbt_free(heapinfo1[i].busy_block.equal_to);
375 heapinfo1[i].busy_block.equal_to = NULL;
377 if(heapinfo1[i].type > 0){
378 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
379 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
380 if(current_block == heaplimit){
381 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
382 if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
383 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
384 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
385 XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
386 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
392 xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
393 heapinfo1[i].busy_frag.equal_to[j] = NULL;
399 if(current_block == heaplimit)
400 XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
405 if(heapinfo2[i].type == 0){
406 if(current_block == heaplimit){
407 if(heapinfo2[i].busy_block.busy_size > 0){
408 if(heapinfo2[i].busy_block.equal_to == NULL){
409 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
410 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
411 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
412 //mmalloc_backtrace_block_display((void*)heapinfo2, i);
419 if(heapinfo2[i].type > 0){
420 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
421 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
422 if(current_block == heaplimit){
423 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
424 if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
425 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
426 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
427 XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
428 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
439 if(current_block == heaplimit)
440 XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
442 xbt_dynar_free(&previous);
444 return ((nb_diff1 > 0) || (nb_diff2 > 0));
447 void reset_heap_information(){
452 if(heapinfo1[i].type == 0){
453 xbt_free(heapinfo1[i].busy_block.equal_to);
454 heapinfo1[i].busy_block.equal_to = NULL;
456 if(heapinfo1[i].type > 0){
457 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
458 xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
459 heapinfo1[i].busy_frag.equal_to[j] = NULL;
468 if(heapinfo2[i].type == 0){
469 xbt_free(heapinfo2[i].busy_block.equal_to);
470 heapinfo2[i].busy_block.equal_to = NULL;
472 if(heapinfo2[i].type > 0){
473 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
474 xbt_free(heapinfo2[i].busy_frag.equal_to[j]);
475 heapinfo2[i].busy_frag.equal_to[j] = NULL;
481 ignore_done1 = 0, ignore_done2 = 0;
482 s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
483 heapinfo1 = NULL, heapinfo2 = NULL;
484 heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
485 to_ignore1 = NULL, to_ignore2 = NULL;
489 static heap_area_t new_heap_area(int block, int fragment){
490 heap_area_t area = NULL;
491 area = xbt_new0(s_heap_area_t, 1);
493 area->fragment = fragment;
498 static size_t heap_comparison_ignore_size(xbt_dynar_t ignore_list, void *address){
500 unsigned int cursor = 0;
502 int end = xbt_dynar_length(ignore_list) - 1;
503 mc_heap_ignore_region_t region;
506 cursor = (start + end) / 2;
507 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(ignore_list, cursor, mc_heap_ignore_region_t);
508 if(region->address == address)
510 if(region->address < address)
512 if(region->address > address)
520 int compare_area(void *area1, void* area2, xbt_dynar_t previous){
522 size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
523 void *addr_pointed1, *addr_pointed2;
525 size_t block1, frag1, block2, frag2, size;
526 int check_ignore = 0;
528 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
529 void *area1_to_compare, *area2_to_compare;
533 if(previous == NULL){
534 previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
538 block1 = ((char*)area1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
539 block2 = ((char*)area2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
541 if(is_block_stack((int)block1) && is_block_stack((int)block2))
544 if(((char *)area1 < (char*)((xbt_mheap_t)s_heap)->heapbase) || (block1 > heapsize1) || (block1 < 1) || ((char *)area2 < (char*)((xbt_mheap_t)s_heap)->heapbase) || (block2 > heapsize2) || (block2 < 1))
547 addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)heapbase1));
548 addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)heapbase2));
550 if(heapinfo1[block1].type == heapinfo2[block2].type){
552 if(heapinfo1[block1].type == -1){
554 }else if(heapinfo1[block1].type == 0){
555 if(heapinfo1[block1].busy_block.equal_to != NULL){
556 if(equal_blocks(block1, block2)){
560 if(heapinfo1[block1].busy_block.size != heapinfo2[block2].busy_block.size)
562 if(heapinfo1[block1].busy_block.busy_size != heapinfo2[block2].busy_block.busy_size)
564 if(!add_heap_area_pair(previous, block1, -1, block2, -1))
567 size = heapinfo1[block1].busy_block.busy_size;
571 area1_to_compare = addr_block1;
572 area2_to_compare = addr_block2;
574 if(heapinfo1[block1].busy_block.ignore == 1 || heapinfo2[block2].busy_block.ignore == 1)
577 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
578 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
580 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2])
582 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2))
585 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
586 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
588 area1_to_compare = addr_frag1;
589 area2_to_compare = addr_frag2;
591 size = heapinfo1[block1].busy_frag.frag_size[frag1];
596 if(heapinfo1[block1].busy_frag.ignore[frag1] == 1 || heapinfo2[block2].busy_frag.ignore[frag2] == 1)
599 }else if((heapinfo1[block1].type > 0) && (heapinfo2[block2].type > 0)){
600 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
601 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
603 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2])
605 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2))
608 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
609 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
611 area1_to_compare = addr_frag1;
612 area2_to_compare = addr_frag2;
614 size = heapinfo1[block1].busy_frag.frag_size[frag1];
619 if(heapinfo1[block1].busy_frag.ignore[frag1] == 1 || heapinfo2[block2].busy_frag.ignore[frag2] == 1)
628 if((ignore_done1 < xbt_dynar_length(to_ignore1)) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, (char *)area1 + i)) > 0)){
629 if((ignore_done2 < xbt_dynar_length(to_ignore2)) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, (char *)area2 + i)) == ignore1)){
638 if(memcmp(((char *)area1_to_compare) + i, ((char *)area2_to_compare) + i, 1) != 0){
640 /* Check pointer difference */
641 pointer_align = (i / sizeof(void*)) * sizeof(void*);
642 addr_pointed1 = *((void **)((char *)area1_to_compare + pointer_align));
643 addr_pointed2 = *((void **)((char *)area2_to_compare + pointer_align));
645 if(addr_pointed1 > maestro_stack_start && addr_pointed1 < maestro_stack_end && addr_pointed2 > maestro_stack_start && addr_pointed2 < maestro_stack_end){
646 i = pointer_align + sizeof(void *);
650 res_compare = compare_area(addr_pointed1, addr_pointed2, previous);
655 i = pointer_align + sizeof(void *);
665 match_equals(previous);
672 static void heap_area_pair_free(heap_area_pair_t pair){
679 static void heap_area_pair_free_voidp(void *d)
681 heap_area_pair_free((heap_area_pair_t) * (void **) d);
684 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
686 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
687 heap_area_pair_t pair = NULL;
688 pair = xbt_new0(s_heap_area_pair_t, 1);
689 pair->block1 = block1;
690 pair->fragment1 = fragment1;
691 pair->block2 = block2;
692 pair->fragment2 = fragment2;
694 xbt_dynar_push(list, &pair);
702 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
704 unsigned int cursor = 0;
705 heap_area_pair_t current_pair;
707 xbt_dynar_foreach(list, cursor, current_pair){
708 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
715 void match_equals(xbt_dynar_t list){
717 unsigned int cursor = 0;
718 heap_area_pair_t current_pair;
719 heap_area_t previous_area;
721 xbt_dynar_foreach(list, cursor, current_pair){
723 if(current_pair->fragment1 != -1){
725 if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){
726 previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
727 xbt_free(heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
728 heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
729 xbt_free(previous_area);
731 if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){
732 previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
733 xbt_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
734 heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
735 xbt_free(previous_area);
738 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
739 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
743 if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
744 previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
745 xbt_free(heapinfo2[previous_area->block].busy_block.equal_to);
746 heapinfo2[previous_area->block].busy_block.equal_to = NULL;
747 xbt_free(previous_area);
749 if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
750 previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
751 xbt_free(heapinfo1[previous_area->block].busy_block.equal_to);
752 heapinfo1[previous_area->block].busy_block.equal_to = NULL;
753 xbt_free(previous_area);
756 heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
757 heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
766 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
769 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
771 if(heap1 == NULL && heap1 == NULL){
772 XBT_DEBUG("Malloc descriptors null");
776 if(heap1->heaplimit != heap2->heaplimit){
777 XBT_DEBUG("Different limit of valid info table indices");
781 /* Heap information */
782 heaplimit = ((struct mdesc *)heap1)->heaplimit;
784 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
786 heapbase1 = (char *)heap1 + BLOCKSIZE;
787 heapbase2 = (char *)heap2 + BLOCKSIZE;
789 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
790 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
792 heapsize1 = heap1->heapsize;
793 heapsize2 = heap2->heapsize;
795 /* Start comparison */
797 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
801 /* Check busy blocks*/
805 while(i <= heaplimit){
807 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
808 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
810 if(heapinfo1[i].type != heapinfo2[i].type){
812 distance += BLOCKSIZE;
813 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
818 if(heapinfo1[i].type == -1){ /* Free block */
823 if(heapinfo1[i].type == 0){ /* Large block */
825 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
826 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
827 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
828 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);
832 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
833 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
834 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
835 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);
841 //while(k < (heapinfo1[i].busy_block.busy_size)){
842 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
843 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
851 }else { /* Fragmented block */
853 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
855 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
856 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
858 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
863 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
864 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
865 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);
871 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
872 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
873 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
893 static int is_stack(void *address){
894 unsigned int cursor = 0;
895 stack_region_t stack;
897 xbt_dynar_foreach(stacks_areas, cursor, stack){
898 if(address == stack->address)
905 static int is_block_stack(int block){
906 unsigned int cursor = 0;
907 stack_region_t stack;
909 xbt_dynar_foreach(stacks_areas, cursor, stack){
910 if(block == stack->block)
917 static void add_heap_equality(xbt_dynar_t equals, void *a1, void *a2){
919 if(xbt_dynar_is_empty(equals)){
921 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
925 xbt_dynar_insert_at(equals, 0, &he);
929 unsigned int cursor = 0;
931 int end = xbt_dynar_length(equals) - 1;
932 heap_equality_t current_equality = NULL;
935 cursor = (start + end) / 2;
936 current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
937 if(current_equality->address1 == a1){
938 if(current_equality->address2 == a2)
940 if(current_equality->address2 < a2)
942 if(current_equality->address2 > a2)
945 if(current_equality->address1 < a1)
947 if(current_equality->address1 > a1)
951 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
955 if(current_equality->address1 < a1)
956 xbt_dynar_insert_at(equals, cursor + 1 , &he);
958 xbt_dynar_insert_at(equals, cursor, &he);
964 static void remove_heap_equality(xbt_dynar_t equals, int address, void *a){
966 unsigned int cursor = 0;
967 heap_equality_t current_equality;
973 int end = xbt_dynar_length(equals) - 1;
976 while(start <= end && found == 0){
977 cursor = (start + end) / 2;
978 current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
979 if(current_equality->address1 == a)
981 if(current_equality->address1 < a)
983 if(current_equality->address1 > a)
988 xbt_dynar_remove_at(equals, cursor, NULL);
992 xbt_dynar_foreach(equals, cursor, current_equality){
993 if(current_equality->address2 == a){
1000 xbt_dynar_remove_at(equals, cursor, NULL);
1006 int is_free_area(void *area, xbt_mheap_t heap){
1008 void *sheap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1009 malloc_info *heapinfo = (malloc_info *)((char *)heap + ((uintptr_t)((char *)heap->heapinfo - (char *)sheap)));
1010 size_t heapsize = heap->heapsize;
1012 /* Get block number */
1013 size_t block = ((char*)area - (char*)((xbt_mheap_t)sheap)->heapbase) / BLOCKSIZE + 1;
1016 /* Check if valid block number */
1017 if((char *)area < (char*)((xbt_mheap_t)sheap)->heapbase || block > heapsize || block < 1)
1020 if(heapinfo[block].type < 0)
1023 if(heapinfo[block].type == 0)
1026 if(heapinfo[block].type > 0){
1027 fragment = ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1028 if(heapinfo[block].busy_frag.frag_size[fragment] == 0)
1036 static int equal_blocks(b1, b2){
1037 if(heapinfo1[b1].busy_block.equal_to != NULL){
1038 if(heapinfo2[b2].busy_block.equal_to != NULL){
1039 if(((heap_area_t)(heapinfo1[b1].busy_block.equal_to))->block == b2 && ((heap_area_t)(heapinfo2[b2].busy_block.equal_to))->block == b1)