1 /* mm_diff - Memory snapshooting and comparison */
3 /* Copyright (c) 2008-2012. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "xbt/ex_interface.h" /* internals of backtrace setup */
11 #include "xbt/mmalloc.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
14 "Logging specific to mm_diff in mmalloc");
16 xbt_dynar_t mc_heap_comparison_ignore;
17 xbt_dynar_t stacks_areas;
18 void *maestro_stack_start, *maestro_stack_end;
20 static void heap_area_pair_free(heap_area_pair_t pair);
21 static void heap_area_pair_free_voidp(void *d);
22 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
23 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
24 static heap_area_t new_heap_area(int block, int fragment);
26 static size_t heap_comparison_ignore_size(xbt_dynar_t list, void *address);
27 static void add_heap_equality(xbt_dynar_t equals, void *a1, void *a2);
28 static void remove_heap_equality(xbt_dynar_t equals, int address, void *a);
30 static int is_stack(void *address);
31 static int is_block_stack(int block);
32 static int equal_blocks(int b1, int b2);
33 static int equal_fragments(int b1, int f1, int b2, int f2);
35 void mmalloc_backtrace_block_display(void* heapinfo, int block){
39 /* if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) { */
40 /* fprintf(stderr, "No backtrace available for that block, sorry.\n"); */
44 /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE); */
45 /* e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size; */
47 /* xbt_ex_setup_backtrace(&e); */
48 /* if (e.used == 0) { */
49 /* fprintf(stderr, "(backtrace not set)\n"); */
50 /* } else if (e.bt_strings == NULL) { */
51 /* fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
55 /* fprintf(stderr, "Backtrace of where the block %d was malloced (%d frames):\n", block ,e.used); */
56 /* for (i = 0; i < e.used; i++) /\* no need to display "xbt_backtrace_display" *\/{ */
57 /* fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
62 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
66 /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE); */
67 /* e.used = XBT_BACKTRACE_SIZE; */
69 /* xbt_ex_setup_backtrace(&e); */
70 /* if (e.used == 0) { */
71 /* fprintf(stderr, "(backtrace not set)\n"); */
72 /* } else if (e.bt_strings == NULL) { */
73 /* fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
77 /* fprintf(stderr, "Backtrace of where the fragment %d in block %d was malloced (%d frames):\n", frag, block ,e.used); */
78 /* for (i = 0; i < e.used; i++) /\* no need to display "xbt_backtrace_display" *\/{ */
79 /* fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
85 void mmalloc_backtrace_display(void *addr){
87 /* size_t block, frag_nb; */
90 /* xbt_mheap_t heap = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit(); */
92 /* block = (((char*) (addr) - (char*) heap -> heapbase) / BLOCKSIZE + 1); */
94 /* type = heap->heapinfo[block].type; */
97 /* case -1 : /\* Free block *\/ */
98 /* fprintf(stderr, "Asked to display the backtrace of a block that is free. I'm puzzled\n"); */
101 /* case 0: /\* Large block *\/ */
102 /* mmalloc_backtrace_block_display(heap->heapinfo, block); */
104 /* default: /\* Fragmented block *\/ */
105 /* frag_nb = RESIDUAL(addr, BLOCKSIZE) >> type; */
106 /* if(heap->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){ */
107 /* fprintf(stderr , "Asked to display the backtrace of a fragment that is free. I'm puzzled\n"); */
110 /* mmalloc_backtrace_fragment_display(heap->heapinfo, block, frag_nb); */
116 void *s_heap = NULL, *heapbase1 = NULL, *heapbase2 = NULL;
117 malloc_info *heapinfo1 = NULL, *heapinfo2 = NULL;
118 size_t heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
119 xbt_dynar_t to_ignore1 = NULL, to_ignore2 = NULL;
121 int ignore_done1 = 0, ignore_done2 = 0;
123 void init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t i1, xbt_dynar_t i2){
125 heaplimit = ((struct mdesc *)heap1)->heaplimit;
127 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
129 heapbase1 = (char *)heap1 + BLOCKSIZE;
130 heapbase2 = (char *)heap2 + BLOCKSIZE;
132 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
133 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
135 heapsize1 = heap1->heapsize;
136 heapsize2 = heap2->heapsize;
142 MC_ignore_data_bss(&heaplimit, sizeof(heaplimit));
143 MC_ignore_data_bss(&s_heap, sizeof(s_heap));
144 MC_ignore_data_bss(&heapbase1, sizeof(heapbase1));
145 MC_ignore_data_bss(&heapbase2, sizeof(heapbase2));
146 MC_ignore_data_bss(&heapinfo1, sizeof(heapinfo1));
147 MC_ignore_data_bss(&heapinfo2, sizeof(heapinfo2));
148 MC_ignore_data_bss(&heapsize1, sizeof(heapsize1));
149 MC_ignore_data_bss(&heapsize2, sizeof(heapsize2));
150 MC_ignore_data_bss(&to_ignore1, sizeof(to_ignore1));
151 MC_ignore_data_bss(&to_ignore2, sizeof(to_ignore2));
156 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
158 if(heap1 == NULL && heap1 == NULL){
159 XBT_DEBUG("Malloc descriptors null");
163 if(heap1->heaplimit != heap2->heaplimit){
164 XBT_DEBUG("Different limit of valid info table indices");
168 /* Start comparison */
169 size_t i1, i2, j1, j2, k;
170 size_t current_block = -1; /* avoid "maybe uninitialized" warning */
171 size_t current_fragment;
172 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
173 int nb_diff1 = 0, nb_diff2 = 0;
175 xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
177 int equal, res_compare = 0;
179 /* Check busy blocks*/
183 while(i1 <= heaplimit){
187 if(heapinfo1[i1].type == -1){ /* Free block */
192 addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
194 if(heapinfo1[i1].type == 0){ /* Large block */
196 if(is_stack(addr_block1)){
197 for(k=0; k < heapinfo1[i1].busy_block.size; k++)
198 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i1, -1);
199 for(k=0; k < heapinfo2[i1].busy_block.size; k++)
200 heapinfo2[i1+k].busy_block.equal_to = new_heap_area(i1, -1);
201 i1 = i1 + heapinfo1[current_block].busy_block.size;
205 if(heapinfo1[i1].busy_block.equal_to != NULL){
214 /* Try first to associate to same block in the other heap */
215 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
217 if(heapinfo2[current_block].busy_block.equal_to == NULL){
219 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
221 res_compare = compare_area(addr_block1, addr_block2, previous);
223 if(res_compare == 0){
224 for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
225 heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
226 for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
227 heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
229 match_equals(previous);
230 i1 = i1 + heapinfo1[current_block].busy_block.size;
233 xbt_dynar_reset(previous);
239 while(i2 <= heaplimit && !equal){
241 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
243 if(i2 == current_block){
248 if(heapinfo2[i2].type != 0){
253 if(heapinfo2[i2].busy_block.equal_to != NULL){
258 res_compare = compare_area(addr_block1, addr_block2, previous);
260 if(res_compare == 0){
261 for(k=1; k < heapinfo2[i2].busy_block.size; k++)
262 heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
263 for(k=1; k < heapinfo1[i1].busy_block.size; k++)
264 heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
266 match_equals(previous);
267 i1 = i1 + heapinfo1[i1].busy_block.size;
270 xbt_dynar_reset(previous);
277 XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1[i1].busy_block.busy_size, addr_block1);
282 }else{ /* Fragmented block */
284 for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
286 current_fragment = j1;
288 if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
291 if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
294 addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
299 /* Try first to associate to same fragment in the other heap */
300 if(heapinfo2[current_block].type == heapinfo1[current_block].type){
302 if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){
304 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
305 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << ((xbt_mheap_t)s_heap)->heapinfo[current_block].type));
307 res_compare = compare_area(addr_frag1, addr_frag2, previous);
309 if(res_compare == 0){
311 match_equals(previous);
314 xbt_dynar_reset(previous);
319 while(i2 <= heaplimit && !equal){
322 if(heapinfo2[i2].type <= 0){
327 for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
329 if(heapinfo2[i2].type == heapinfo1[i1].type && i2 == current_block && j2 == current_fragment)
332 if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)
335 addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
336 addr_frag2 = (void*) ((char *)addr_block2 + (j2 << ((xbt_mheap_t)s_heap)->heapinfo[i2].type));
338 res_compare = compare_area(addr_frag1, addr_frag2, previous);
340 if(res_compare == 0){
342 match_equals(previous);
343 xbt_dynar_reset(previous);
347 xbt_dynar_reset(previous);
355 if(heapinfo1[i1].busy_frag.equal_to[j1] == NULL){
356 XBT_DEBUG("Block %zu, fragment %zu not found (size_used = %d, address = %p)", i1, j1, heapinfo1[i1].busy_frag.frag_size[j1], addr_frag1);
371 /* All blocks/fragments are equal to another block/fragment ? */
375 if(heapinfo1[i].type == 0){
376 if(current_block == heaplimit){
377 if(heapinfo1[i].busy_block.busy_size > 0){
378 if(heapinfo1[i].busy_block.equal_to == NULL){
379 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
380 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
381 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
382 //mmalloc_backtrace_block_display((void*)heapinfo1, i);
388 xbt_free(heapinfo1[i].busy_block.equal_to);
389 heapinfo1[i].busy_block.equal_to = NULL;
391 if(heapinfo1[i].type > 0){
392 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
393 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
394 if(current_block == heaplimit){
395 if(heapinfo1[i].busy_frag.frag_size[j] > 0){
396 if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
397 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
398 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
399 XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
400 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
406 xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
407 heapinfo1[i].busy_frag.equal_to[j] = NULL;
413 if(current_block == heaplimit)
414 XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
419 if(heapinfo2[i].type == 0){
420 if(current_block == heaplimit){
421 if(heapinfo2[i].busy_block.busy_size > 0){
422 if(heapinfo2[i].busy_block.equal_to == NULL){
423 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
424 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
425 XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
426 //mmalloc_backtrace_block_display((void*)heapinfo2, i);
433 if(heapinfo2[i].type > 0){
434 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
435 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
436 if(current_block == heaplimit){
437 if(heapinfo2[i].busy_frag.frag_size[j] > 0){
438 if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
439 if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
440 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
441 XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
442 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
453 if(current_block == heaplimit)
454 XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
456 xbt_dynar_free(&previous);
458 return ((nb_diff1 > 0) || (nb_diff2 > 0));
461 void reset_heap_information(){
466 if(heapinfo1[i].type == 0){
467 xbt_free(heapinfo1[i].busy_block.equal_to);
468 heapinfo1[i].busy_block.equal_to = NULL;
470 if(heapinfo1[i].type > 0){
471 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
472 xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
473 heapinfo1[i].busy_frag.equal_to[j] = NULL;
482 if(heapinfo2[i].type == 0){
483 xbt_free(heapinfo2[i].busy_block.equal_to);
484 heapinfo2[i].busy_block.equal_to = NULL;
486 if(heapinfo2[i].type > 0){
487 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
488 xbt_free(heapinfo2[i].busy_frag.equal_to[j]);
489 heapinfo2[i].busy_frag.equal_to[j] = NULL;
495 ignore_done1 = 0, ignore_done2 = 0;
496 s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
497 heapinfo1 = NULL, heapinfo2 = NULL;
498 heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
499 to_ignore1 = NULL, to_ignore2 = NULL;
503 static heap_area_t new_heap_area(int block, int fragment){
504 heap_area_t area = NULL;
505 area = xbt_new0(s_heap_area_t, 1);
507 area->fragment = fragment;
512 static size_t heap_comparison_ignore_size(xbt_dynar_t ignore_list, void *address){
514 unsigned int cursor = 0;
516 int end = xbt_dynar_length(ignore_list) - 1;
517 mc_heap_ignore_region_t region;
520 cursor = (start + end) / 2;
521 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(ignore_list, cursor, mc_heap_ignore_region_t);
522 if(region->address == address)
524 if(region->address < address)
526 if(region->address > address)
534 int compare_area(void *area1, void* area2, xbt_dynar_t previous){
536 size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
537 void *addr_pointed1, *addr_pointed2;
539 size_t block1, frag1, block2, frag2, size;
540 int check_ignore = 0;
542 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
543 void *area1_to_compare, *area2_to_compare;
547 if(previous == NULL){
548 previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
552 block1 = ((char*)area1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
553 block2 = ((char*)area2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
555 if(is_block_stack((int)block1) && is_block_stack((int)block2))
558 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))
561 addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)heapbase1));
562 addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)heapbase2));
564 if(heapinfo1[block1].type == heapinfo2[block2].type){
566 if(heapinfo1[block1].type == -1){
568 }else if(heapinfo1[block1].type == 0){
569 if(heapinfo1[block1].busy_block.equal_to != NULL){
570 if(equal_blocks(block1, block2)){
574 if(heapinfo1[block1].busy_block.size != heapinfo2[block2].busy_block.size)
576 if(heapinfo1[block1].busy_block.busy_size != heapinfo2[block2].busy_block.busy_size)
578 if(!add_heap_area_pair(previous, block1, -1, block2, -1))
581 size = heapinfo1[block1].busy_block.busy_size;
585 area1_to_compare = addr_block1;
586 area2_to_compare = addr_block2;
588 if(heapinfo1[block1].busy_block.ignore == 1 || heapinfo2[block2].busy_block.ignore == 1)
591 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
592 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
594 if(heapinfo1[block1].busy_frag.equal_to[frag1] != NULL){
595 if(equal_fragments(block1, frag1, block2, frag2)){
600 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2])
602 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2))
605 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
606 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
608 area1_to_compare = addr_frag1;
609 area2_to_compare = addr_frag2;
611 size = heapinfo1[block1].busy_frag.frag_size[frag1];
616 if(heapinfo1[block1].busy_frag.ignore[frag1] == 1 || heapinfo2[block2].busy_frag.ignore[frag2] == 1)
619 }else if((heapinfo1[block1].type > 0) && (heapinfo2[block2].type > 0)){
620 frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
621 frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
623 if(heapinfo1[block1].busy_frag.equal_to[frag1] != NULL){
624 if(equal_fragments(block1, frag1, block2, frag2)){
629 if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2])
631 if(!add_heap_area_pair(previous, block1, frag1, block2, frag2))
634 addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
635 addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
637 area1_to_compare = addr_frag1;
638 area2_to_compare = addr_frag2;
640 size = heapinfo1[block1].busy_frag.frag_size[frag1];
645 if(heapinfo1[block1].busy_frag.ignore[frag1] == 1 || heapinfo2[block2].busy_frag.ignore[frag2] == 1)
654 if((ignore_done1 < xbt_dynar_length(to_ignore1)) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, (char *)area1 + i)) > 0)){
655 if((ignore_done2 < xbt_dynar_length(to_ignore2)) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, (char *)area2 + i)) == ignore1)){
664 if(memcmp(((char *)area1_to_compare) + i, ((char *)area2_to_compare) + i, 1) != 0){
666 /* Check pointer difference */
667 pointer_align = (i / sizeof(void*)) * sizeof(void*);
668 addr_pointed1 = *((void **)((char *)area1_to_compare + pointer_align));
669 addr_pointed2 = *((void **)((char *)area2_to_compare + pointer_align));
671 if(addr_pointed1 > maestro_stack_start && addr_pointed1 < maestro_stack_end && addr_pointed2 > maestro_stack_start && addr_pointed2 < maestro_stack_end){
672 i = pointer_align + sizeof(void *);
676 res_compare = compare_area(addr_pointed1, addr_pointed2, previous);
681 i = pointer_align + sizeof(void *);
691 match_equals(previous);
698 static void heap_area_pair_free(heap_area_pair_t pair){
703 static void heap_area_pair_free_voidp(void *d)
705 heap_area_pair_free((heap_area_pair_t) * (void **) d);
708 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
710 if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
711 heap_area_pair_t pair = NULL;
712 pair = xbt_new0(s_heap_area_pair_t, 1);
713 pair->block1 = block1;
714 pair->fragment1 = fragment1;
715 pair->block2 = block2;
716 pair->fragment2 = fragment2;
718 xbt_dynar_push(list, &pair);
726 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
728 unsigned int cursor = 0;
729 heap_area_pair_t current_pair;
731 xbt_dynar_foreach(list, cursor, current_pair){
732 if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
739 void match_equals(xbt_dynar_t list){
741 unsigned int cursor = 0;
742 heap_area_pair_t current_pair;
743 heap_area_t previous_area;
745 xbt_dynar_foreach(list, cursor, current_pair){
747 if(current_pair->fragment1 != -1){
749 if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){
750 previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
751 xbt_free(heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
752 heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
753 xbt_free(previous_area);
755 if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){
756 previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
757 xbt_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
758 heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
759 xbt_free(previous_area);
762 heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
763 heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
767 if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
768 previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
769 xbt_free(heapinfo2[previous_area->block].busy_block.equal_to);
770 heapinfo2[previous_area->block].busy_block.equal_to = NULL;
771 xbt_free(previous_area);
773 if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
774 previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
775 xbt_free(heapinfo1[previous_area->block].busy_block.equal_to);
776 heapinfo1[previous_area->block].busy_block.equal_to = NULL;
777 xbt_free(previous_area);
780 heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
781 heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
790 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
793 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
795 if(heap1 == NULL && heap1 == NULL){
796 XBT_DEBUG("Malloc descriptors null");
800 if(heap1->heaplimit != heap2->heaplimit){
801 XBT_DEBUG("Different limit of valid info table indices");
805 /* Heap information */
806 heaplimit = ((struct mdesc *)heap1)->heaplimit;
808 s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
810 heapbase1 = (char *)heap1 + BLOCKSIZE;
811 heapbase2 = (char *)heap2 + BLOCKSIZE;
813 heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
814 heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
816 heapsize1 = heap1->heapsize;
817 heapsize2 = heap2->heapsize;
819 /* Start comparison */
821 void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
825 /* Check busy blocks*/
829 while(i <= heaplimit){
831 addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
832 addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
834 if(heapinfo1[i].type != heapinfo2[i].type){
836 distance += BLOCKSIZE;
837 XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
842 if(heapinfo1[i].type == -1){ /* Free block */
847 if(heapinfo1[i].type == 0){ /* Large block */
849 if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
850 distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
851 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
852 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);
856 /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
857 distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
858 i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
859 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);
865 //while(k < (heapinfo1[i].busy_block.busy_size)){
866 while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
867 if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
875 }else { /* Fragmented block */
877 for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
879 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
880 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
882 if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
887 /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
888 distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
889 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);
895 //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
896 while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
897 if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
917 static int is_stack(void *address){
918 unsigned int cursor = 0;
919 stack_region_t stack;
921 xbt_dynar_foreach(stacks_areas, cursor, stack){
922 if(address == stack->address)
929 static int is_block_stack(int block){
930 unsigned int cursor = 0;
931 stack_region_t stack;
933 xbt_dynar_foreach(stacks_areas, cursor, stack){
934 if(block == stack->block)
941 static void add_heap_equality(xbt_dynar_t equals, void *a1, void *a2){
943 if(xbt_dynar_is_empty(equals)){
945 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
949 xbt_dynar_insert_at(equals, 0, &he);
953 unsigned int cursor = 0;
955 int end = xbt_dynar_length(equals) - 1;
956 heap_equality_t current_equality = NULL;
959 cursor = (start + end) / 2;
960 current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
961 if(current_equality->address1 == a1){
962 if(current_equality->address2 == a2)
964 if(current_equality->address2 < a2)
966 if(current_equality->address2 > a2)
969 if(current_equality->address1 < a1)
971 if(current_equality->address1 > a1)
975 heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
979 if(current_equality->address1 < a1)
980 xbt_dynar_insert_at(equals, cursor + 1 , &he);
982 xbt_dynar_insert_at(equals, cursor, &he);
988 static void remove_heap_equality(xbt_dynar_t equals, int address, void *a){
990 unsigned int cursor = 0;
991 heap_equality_t current_equality;
997 int end = xbt_dynar_length(equals) - 1;
1000 while(start <= end && found == 0){
1001 cursor = (start + end) / 2;
1002 current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
1003 if(current_equality->address1 == a)
1005 if(current_equality->address1 < a)
1007 if(current_equality->address1 > a)
1012 xbt_dynar_remove_at(equals, cursor, NULL);
1016 xbt_dynar_foreach(equals, cursor, current_equality){
1017 if(current_equality->address2 == a){
1024 xbt_dynar_remove_at(equals, cursor, NULL);
1030 int is_free_area(void *area, xbt_mheap_t heap){
1032 void *sheap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1033 malloc_info *heapinfo = (malloc_info *)((char *)heap + ((uintptr_t)((char *)heap->heapinfo - (char *)sheap)));
1034 size_t heapsize = heap->heapsize;
1036 /* Get block number */
1037 size_t block = ((char*)area - (char*)((xbt_mheap_t)sheap)->heapbase) / BLOCKSIZE + 1;
1040 /* Check if valid block number */
1041 if((char *)area < (char*)((xbt_mheap_t)sheap)->heapbase || block > heapsize || block < 1)
1044 if(heapinfo[block].type < 0)
1047 if(heapinfo[block].type == 0)
1050 if(heapinfo[block].type > 0){
1051 fragment = ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1052 if(heapinfo[block].busy_frag.frag_size[fragment] == 0)
1060 static int equal_blocks(int b1, int b2){
1061 if(heapinfo1[b1].busy_block.equal_to != NULL){
1062 if(heapinfo2[b2].busy_block.equal_to != NULL){
1063 if(((heap_area_t)(heapinfo1[b1].busy_block.equal_to))->block == b2 && ((heap_area_t)(heapinfo2[b2].busy_block.equal_to))->block == b1)
1070 static int equal_fragments(int b1, int f1, int b2, int f2){
1071 if(heapinfo1[b1].busy_frag.equal_to[f1] != NULL){
1072 if(heapinfo2[b2].busy_frag.equal_to[f2] != NULL){
1073 if(((heap_area_t)(heapinfo1[b1].busy_frag.equal_to[f1]))->block == b2 && ((heap_area_t)(heapinfo2[b2].busy_frag.equal_to[f2]))->block == b1 && ((heap_area_t)(heapinfo1[b1].busy_frag.equal_to[f1]))->fragment == f2 && ((heap_area_t)(heapinfo2[b2].busy_frag.equal_to[f2]))->fragment == f1)