Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b019d5b95c2ff8514e7bf92e6fe4c81dd0dc9ce9
[simgrid.git] / src / xbt / mmalloc / mm_diff.c
1 /* mm_diff - Memory snapshooting and comparison                             */
2
3 /* Copyright (c) 2008-2012. The SimGrid Team. All rights reserved.          */
4
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. */
7
8 #include "xbt/ex_interface.h" /* internals of backtrace setup */
9 #include "xbt/str.h"
10 #include "mc/mc.h"
11 #include "xbt/mmalloc.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
14                                 "Logging specific to mm_diff in mmalloc");
15
16 extern char *xbt_binary_name;
17
18 xbt_dynar_t mc_heap_comparison_ignore;
19 xbt_dynar_t stacks_areas;
20
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);
26
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);
29
30 static int in_mc_comparison_ignore(int block, int fragment);
31 static size_t heap_comparison_ignore_size(void *address);
32 static void add_heap_equality(xbt_dynar_t *equals, void *a1, void *a2);
33 static void remove_heap_equality(xbt_dynar_t *equals, int address, void *a);
34
35 static char* is_stack(void *address);
36
37 void mmalloc_backtrace_block_display(void* heapinfo, int block){
38
39   xbt_ex_t e;
40
41   if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) {
42     fprintf(stderr, "No backtrace available for that block, sorry.\n");
43     return;
44   }
45
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;
48
49   xbt_ex_setup_backtrace(&e);
50   if (e.used == 0) {
51     fprintf(stderr, "(backtrace not set)\n");
52   } else if (e.bt_strings == NULL) {
53     fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
54   } else {
55     int i;
56
57     fprintf(stderr, "Backtrace of where the block %d was malloced (%d frames):\n", block ,e.used);
58     for (i = 0; i < e.used; i++)       /* no need to display "xbt_backtrace_display" */{
59       fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4);
60     }
61   }
62
63 }
64
65 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
66
67   xbt_ex_t e;
68
69   memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE);
70   e.used = XBT_BACKTRACE_SIZE;
71
72   xbt_ex_setup_backtrace(&e);
73   if (e.used == 0) {
74     fprintf(stderr, "(backtrace not set)\n");
75   } else if (e.bt_strings == NULL) {
76     fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
77   } else {
78     int i;
79
80     fprintf(stderr, "Backtrace of where the fragment %d in block %d was malloced (%d frames):\n", frag, block ,e.used);
81     for (i = 0; i < e.used; i++)       /* no need to display "xbt_backtrace_display" */{
82       fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4);
83     }
84   }
85
86 }
87
88 void mmalloc_backtrace_display(void *addr){
89
90   size_t block, frag_nb;
91   int type;
92   
93   xbt_mheap_t heap = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
94
95   block = (((char*) (addr) - (char*) heap -> heapbase) / BLOCKSIZE + 1);
96
97   type = heap->heapinfo[block].type;
98
99   switch(type){
100   case -1 : /* Free block */
101     fprintf(stderr, "Asked to display the backtrace of a block that is free. I'm puzzled\n");
102     xbt_abort();
103     break; 
104   case 0: /* Large block */
105     mmalloc_backtrace_block_display(heap->heapinfo, block);
106     break;
107   default: /* Fragmented block */
108     frag_nb = RESIDUAL(addr, BLOCKSIZE) >> type;
109     if(heap->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){
110       fprintf(stderr , "Asked to display the backtrace of a fragment that is free. I'm puzzled\n");
111       xbt_abort();
112     }
113     mmalloc_backtrace_fragment_display(heap->heapinfo, block, frag_nb);
114     break;
115   }
116
117 }
118
119
120 void *s_heap = NULL, *heapbase1 = NULL, *heapbase2 = NULL;
121 malloc_info *heapinfo1 = NULL, *heapinfo2 = NULL;
122 size_t heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
123
124 int ignore_done = 0;
125
126 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t *stack1, xbt_dynar_t *stack2, xbt_dynar_t *equals){
127
128   if(heap1 == NULL && heap1 == NULL){
129     XBT_DEBUG("Malloc descriptors null");
130     return 0;
131   }
132
133   if(heap1->heaplimit != heap2->heaplimit){
134     XBT_DEBUG("Different limit of valid info table indices");
135     return 1;
136   }
137
138   if(heap1->heapstats.chunks_used != heap2->heapstats.chunks_used){
139     XBT_DEBUG("Different number of chunks used in heap : %zu - %zu", heap1->heapstats.chunks_used, heap2->heapstats.chunks_used);
140     return 1;
141   }
142
143   /* Heap information */
144   heaplimit = ((struct mdesc *)heap1)->heaplimit;
145
146   s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
147
148   heapbase1 = (char *)heap1 + BLOCKSIZE;
149   heapbase2 = (char *)heap2 + BLOCKSIZE;
150
151   heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
152   heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
153
154   heapsize1 = heap1->heapsize;
155   heapsize2 = heap2->heapsize;
156
157   /* Start comparison */
158   size_t i1, i2, j1, j2, k, current_block, current_fragment;
159   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
160   void *real_addr_block1, *real_addr_block2;
161   char *stack_name;
162
163   xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
164
165   int equal, res_compare;
166
167   /* Init equal information */
168   i1 = 1;
169
170   while(i1<=heaplimit){
171     if(heapinfo1[i1].type == 0){
172       heapinfo1[i1].busy_block.equal_to = NULL;
173     }
174     if(heapinfo1[i1].type > 0){
175       for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
176         heapinfo1[i1].busy_frag.equal_to[j1] = NULL;
177       }
178     }
179     i1++; 
180   }
181
182   i2 = 1;
183
184   while(i2<=heaplimit){
185     if(heapinfo2[i2].type == 0){
186       heapinfo2[i2].busy_block.equal_to = NULL;
187     }
188     if(heapinfo2[i2].type > 0){
189       for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
190         heapinfo2[i2].busy_frag.equal_to[j2] = NULL;
191       }
192     }
193     i2++; 
194   }
195
196   /* Check busy blocks*/
197
198   i1 = 1;
199
200   while(i1 <= heaplimit){
201
202     current_block = i1;
203
204     if(heapinfo1[i1].type == -1){ /* Free block */
205       i1++;
206       continue;
207     }
208
209     addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)heapbase1));
210     real_addr_block1 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block1) - (char *)heapbase1);
211
212     if(heapinfo1[i1].type == 0){  /* Large block */
213       
214       if((stack_name = is_stack(real_addr_block1)) != NULL){
215         stack_region_t stack = xbt_new0(s_stack_region_t, 1);
216         stack->address = addr_block1;
217         stack->process_name = strdup(stack_name);
218         stack->size = heapinfo1[i1].busy_block.busy_size;
219         xbt_dynar_push(*stack1, &stack);
220       }
221
222       if(heapinfo1[i1].busy_block.busy_size == 0){
223         i1++;
224         continue;
225       }
226
227       if(heapinfo1[i1].busy_block.equal_to != NULL){
228         i1++;
229         continue;
230       }
231     
232       i2 = 1;
233       equal = 0;
234   
235       /* Try first to associate to same block in the other heap */
236       if(heapinfo2[current_block].type == heapinfo1[current_block].type){
237
238         if(heapinfo2[current_block].busy_block.equal_to == NULL){  
239         
240           if(heapinfo1[current_block].busy_block.busy_size == heapinfo2[current_block].busy_block.busy_size){
241
242             addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
243             real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
244           
245             if((stack_name = is_stack(real_addr_block2)) != NULL){
246               stack_region_t stack = xbt_new0(s_stack_region_t, 1);
247               stack->address = addr_block2;
248               stack->process_name = strdup(stack_name);
249               stack->size = heapinfo2[current_block].busy_block.busy_size;
250               xbt_dynar_push(*stack2, &stack);
251             }
252         
253             add_heap_area_pair(previous, current_block, -1, current_block, -1);
254         
255             if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
256               if(in_mc_comparison_ignore((int)current_block, -1))
257                 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 1);
258               else
259                 res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
260             }else{
261               res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
262             }
263         
264             if(res_compare == 0){
265               for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
266                 heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
267               for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
268                 heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
269               equal = 1;
270               match_equals(previous, equals);
271               i1 = i1 + heapinfo1[current_block].busy_block.size;
272             }
273         
274             xbt_dynar_reset(previous);
275         
276           }
277
278         }
279         
280       }
281
282       while(i2 <= heaplimit && !equal){
283
284         addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));        
285         real_addr_block2 = (char*)((xbt_mheap_t)s_heap)->heapbase + (((char *)addr_block2) - (char *)heapbase2);
286         
287         if((stack_name = is_stack(real_addr_block2)) != NULL){
288           stack_region_t stack = xbt_new0(s_stack_region_t, 1);
289           stack->address = addr_block2;
290           stack->process_name = strdup(stack_name);
291           stack->size = heapinfo2[i2].busy_block.busy_size;
292           xbt_dynar_push(*stack2, &stack);
293         }
294            
295         if(i2 == current_block){
296           i2++;
297           continue;
298         }
299
300         if(heapinfo2[i2].type != 0){
301           i2++;
302           continue;
303         }
304
305         if(heapinfo2[i2].busy_block.equal_to != NULL){         
306           i2++;
307           continue;
308         }
309         
310         if(heapinfo1[i1].busy_block.size != heapinfo2[i2].busy_block.size){
311           i2++;
312           continue;
313         }
314         
315         if(heapinfo1[i1].busy_block.busy_size != heapinfo2[i2].busy_block.busy_size){
316           i2++;
317           continue;
318         }
319
320         /* Comparison */
321         add_heap_area_pair(previous, i1, -1, i2, -1);
322         
323         if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
324           if(in_mc_comparison_ignore((int)i1, -1))
325             res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 1);
326           else
327             res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
328         }else{
329           res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
330         }
331         
332         if(res_compare == 0){
333           for(k=1; k < heapinfo2[i2].busy_block.size; k++)
334             heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
335           for(k=1; k < heapinfo1[i1].busy_block.size; k++)
336             heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
337           equal = 1;
338           match_equals(previous, equals);
339           i1 = i1 + heapinfo1[i1].busy_block.size;
340         }
341
342         xbt_dynar_reset(previous);
343
344         i2++;
345
346       }
347
348       if(!equal)
349         i1++;
350       
351     }else{ /* Fragmented block */
352
353       for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
354
355         current_fragment = j1;
356
357         if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
358           continue;
359
360         if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
361           continue;
362
363         addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
364
365         i2 = 1;
366         equal = 0;
367         
368         /* Try first to associate to same fragment in the other heap */
369         if(heapinfo2[current_block].type == heapinfo1[current_block].type){
370
371           if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){  
372           
373               if(heapinfo1[current_block].busy_frag.frag_size[current_fragment] == heapinfo2[current_block].busy_frag.frag_size[current_fragment]){
374
375                 addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
376                 addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << heapinfo2[current_block].type));
377                
378                 add_heap_area_pair(previous, current_block, current_fragment, current_block, current_fragment);
379             
380                 if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
381                   if(in_mc_comparison_ignore((int)current_block, (int)current_fragment))
382                     res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 1);
383                   else
384                     res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
385                 }else{
386                   res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
387                 }
388
389                 if(res_compare == 0){
390                   equal = 1;
391                   match_equals(previous, equals);
392                 }
393             
394                 xbt_dynar_reset(previous);
395             
396               }
397
398             }
399
400         }
401
402         while(i2 <= heaplimit && !equal){
403
404           
405           if(heapinfo2[i2].type <= 0){
406             i2++;
407             continue;
408           }
409
410           for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
411
412             if(heapinfo2[i2].type == heapinfo1[i1].type && i2 == current_block && j2 == current_fragment)
413               continue;
414
415             if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)                
416               continue;              
417              
418             if(heapinfo1[i1].busy_frag.frag_size[j1] != heapinfo2[i2].busy_frag.frag_size[j2]) /* Different size_used */    
419               continue;
420              
421             addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
422             addr_frag2 = (void*) ((char *)addr_block2 + (j2 << heapinfo2[i2].type));
423              
424             /* Comparison */
425             add_heap_area_pair(previous, i1, j1, i2, j2);
426             
427             if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
428               if(in_mc_comparison_ignore((int)i1, (int)j1))
429                 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 1);
430               else
431                 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
432             }else{
433               res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
434             }
435
436             if(res_compare == 0){
437               equal = 1;
438               match_equals(previous, equals);
439               break;
440             }
441
442             xbt_dynar_reset(previous);
443
444           }
445
446           i2++;
447
448         }
449
450       }
451
452       i1++;
453       
454     }
455
456   }
457
458   /* All blocks/fragments are equal to another block/fragment ? */
459   size_t i = 1, j = 0;
460   int nb_diff1 = 0, nb_diff2 = 0;
461  
462   while(i<heaplimit){
463     if(heapinfo1[i].type == 0){
464       if(heapinfo1[i].busy_block.busy_size > 0){
465         if(heapinfo1[i].busy_block.equal_to == NULL){
466           if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
467             addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
468             XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
469             mmalloc_backtrace_block_display((void*)heapinfo1, i);
470           }
471           nb_diff1++;
472         }else{
473           xbt_free(heapinfo1[i].busy_block.equal_to);
474         }
475       }
476     }
477     if(heapinfo1[i].type > 0){
478       addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
479       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
480         if(heapinfo1[i].busy_frag.frag_size[j] > 0){
481           if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
482             if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
483               addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
484               XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
485               mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
486             }
487             nb_diff1++;
488           }else{
489             xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
490           }
491         }
492       }
493     }
494     
495     i++; 
496   }
497
498   XBT_DEBUG("Different blocks or fragments in heap1 : %d", nb_diff1);
499
500   i = 1;
501
502   while(i<heaplimit){
503     if(heapinfo2[i].type == 0){
504       if(heapinfo2[i].busy_block.busy_size > 0){
505         if(heapinfo2[i].busy_block.equal_to == NULL){
506           if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
507             addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
508             XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
509             mmalloc_backtrace_block_display((void*)heapinfo2, i);
510           }
511           nb_diff2++;
512         }else{
513           xbt_free(heapinfo2[i].busy_block.equal_to);
514         }
515       }
516     }
517     if(heapinfo2[i].type > 0){
518       addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
519       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
520         if(heapinfo2[i].busy_frag.frag_size[j] > 0){
521           if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
522             if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
523               addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
524               XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
525               mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
526             }
527             nb_diff2++;
528           }else{
529             xbt_free(heapinfo2[i].busy_frag.equal_to[j]);
530           }
531         }
532       }
533     }
534     i++; 
535   }
536
537   XBT_DEBUG("Different blocks or fragments in heap2 : %d", nb_diff2);
538
539   xbt_dynar_free(&previous);
540   ignore_done = 0;
541   s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
542   heapinfo1 = NULL, heapinfo2 = NULL;
543   heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
544
545   return ((nb_diff1 > 0) || (nb_diff2 > 0));
546
547 }
548
549 static heap_area_t new_heap_area(int block, int fragment){
550   heap_area_t area = NULL;
551   area = xbt_new0(s_heap_area_t, 1);
552   area->block = block;
553   area->fragment = fragment;
554   return area;
555 }
556
557 static int in_mc_comparison_ignore(int block, int fragment){
558
559   unsigned int cursor = 0;
560   int start = 0;
561   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
562   mc_heap_ignore_region_t region;
563
564   while(start <= end){
565     cursor = (start + end) / 2;
566     region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
567     if(region->block == block){
568       if(region->fragment == fragment)
569         return 1;
570       if(region->fragment < fragment)
571         start = cursor + 1;
572       if(region->fragment > fragment)
573         end = cursor - 1;
574     }
575     if(region->block < block)
576       start = cursor + 1;
577     if(region->block > block)
578       end = cursor - 1; 
579   }
580
581   return 0;
582 }
583
584 static size_t heap_comparison_ignore_size(void *address){
585   unsigned int cursor = 0;
586   int start = 0;
587   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
588   mc_heap_ignore_region_t region;
589
590   while(start <= end){
591     cursor = (start + end) / 2;
592     region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
593     if(region->address == address)
594       return region->size;
595     if(region->address < address)
596       start = cursor + 1;
597     if(region->address > address)
598       end = cursor - 1;   
599   }
600
601   return 0;
602 }
603
604
605 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore){
606
607   size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
608   void *address_pointed1, *address_pointed2, *addr_block_pointed1, *addr_block_pointed2, *addr_frag_pointed1, *addr_frag_pointed2;
609   size_t block_pointed1, block_pointed2, frag_pointed1, frag_pointed2;
610   int res_compare;
611   void *current_area1, *current_area2;
612  
613   while(i<size){
614
615     if(check_ignore){
616
617       current_area1 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area1) + i) - (char *)heapbase1);
618       if((ignore1 = heap_comparison_ignore_size(current_area1)) > 0){
619         current_area2 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area2) + i) - (char *)heapbase2);
620         if((ignore2 = heap_comparison_ignore_size(current_area2))  == ignore1){
621           i = i + ignore2;
622           ignore_done++;
623           continue;
624         }
625       }
626
627     }
628    
629     if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
630
631       /* Check pointer difference */
632       pointer_align = (i / sizeof(void*)) * sizeof(void*);
633       address_pointed1 = *((void **)((char *)area1 + pointer_align));
634       address_pointed2 = *((void **)((char *)area2 + pointer_align));
635
636       /* Get pointed blocks number */ 
637       block_pointed1 = ((char*)address_pointed1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
638       block_pointed2 = ((char*)address_pointed2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
639
640       /* Check if valid blocks number */
641       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)
642         return 1;
643
644       if(heapinfo1[block_pointed1].type == heapinfo2[block_pointed2].type){ /* Same type of block (large or fragmented) */
645
646         addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
647         addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
648         
649         if(heapinfo1[block_pointed1].type == 0){ /* Large block */
650
651           if(heapinfo1[block_pointed1].busy_block.size != heapinfo2[block_pointed2].busy_block.size){
652             return 1;
653           }
654
655           if(heapinfo1[block_pointed1].busy_block.busy_size != heapinfo2[block_pointed2].busy_block.busy_size){
656             return 1;
657           }
658
659           if(add_heap_area_pair(previous, block_pointed1, -1, block_pointed2, -1)){
660
661             if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
662               if(in_mc_comparison_ignore(block_pointed1, -1))
663                 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 1);
664               else
665                 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
666             }else{
667               res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
668             }
669             
670             if(res_compare == 1)    
671               return 1;
672            
673           }
674           
675         }else{ /* Fragmented block */
676
677           /* Get pointed fragments number */ 
678           frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
679           frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
680          
681           if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */    
682             return 1;
683             
684           addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
685           addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
686
687           if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
688
689             if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
690               if(in_mc_comparison_ignore(block_pointed1, frag_pointed1))
691                 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
692               else
693                 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
694             }else{
695               res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
696             }
697
698             if(res_compare == 1)
699               return 1;
700            
701           }
702           
703         }
704           
705       }else{
706
707         if((heapinfo1[block_pointed1].type > 0) && (heapinfo2[block_pointed2].type > 0)){
708           
709           addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
710           addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
711        
712           frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
713           frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
714
715           if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */  
716             return 1;
717
718           addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
719           addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
720
721           if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
722
723             if(ignore_done < xbt_dynar_length(mc_heap_comparison_ignore)){
724               if(in_mc_comparison_ignore(block_pointed1, frag_pointed1))
725                 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
726               else
727                 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
728             }else{
729               res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
730             }
731             
732             if(res_compare == 1)
733               return 1;
734            
735           }
736
737         }else{
738           return 1;
739         }
740
741       }
742
743       i = pointer_align + sizeof(void *);
744       
745     }else{
746
747       i++;
748
749     }
750   }
751
752   return 0;
753   
754
755 }
756
757 static void heap_area_pair_free(heap_area_pair_t pair){
758   if (pair){
759     free(pair);
760     pair = NULL;
761   }
762 }
763
764 static void heap_area_pair_free_voidp(void *d)
765 {
766   heap_area_pair_free((heap_area_pair_t) * (void **) d);
767 }
768
769 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
770
771   if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
772     heap_area_pair_t pair = NULL;
773     pair = xbt_new0(s_heap_area_pair_t, 1);
774     pair->block1 = block1;
775     pair->fragment1 = fragment1;
776     pair->block2 = block2;
777     pair->fragment2 = fragment2;
778     
779     xbt_dynar_push(list, &pair); 
780
781     return 1;
782   }
783
784   return 0;
785 }
786  
787 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
788   
789   unsigned int cursor = 0;
790   heap_area_pair_t current_pair;
791
792   xbt_dynar_foreach(list, cursor, current_pair){
793     if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
794       return 0; 
795   }
796   
797   return 1;
798 }
799
800 static void match_equals(xbt_dynar_t list, xbt_dynar_t *equals){
801
802   unsigned int cursor = 0;
803   heap_area_pair_t current_pair;
804   heap_area_t previous_area;
805
806   void *real_addr_block1, *real_addr_block2, *real_addr_frag1, *real_addr_frag2;
807
808   xbt_dynar_foreach(list, cursor, current_pair){
809
810     if(current_pair->fragment1 != -1){
811
812       real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
813       real_addr_frag1 = (void*) ((char *)real_addr_block1 + (current_pair->fragment1 << heapinfo1[current_pair->block1].type));
814       real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
815       real_addr_frag2 = (void*) ((char *)real_addr_block2 + (current_pair->fragment2 << heapinfo2[current_pair->block2].type));
816
817       if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){    
818         remove_heap_equality(equals, 1, real_addr_frag1);
819         previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
820         xbt_free( heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
821         heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
822         xbt_free(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1]); 
823       }
824       if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){        
825         remove_heap_equality(equals, 2, real_addr_frag2); 
826         previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
827         xbt_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
828         heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
829         xbt_free(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2]);
830       }
831       
832       if(real_addr_frag1 != real_addr_frag2)
833         add_heap_equality(equals, real_addr_frag1, real_addr_frag2);
834
835       heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
836       heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
837
838     }else{
839
840       real_addr_block1 = ((void*) (((ADDR2UINT((size_t)current_pair->block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
841       real_addr_block2 = ((void*) (((ADDR2UINT((size_t)current_pair->block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
842
843       if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
844         remove_heap_equality(equals, 1, real_addr_block1);
845         previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
846         xbt_free(heapinfo2[previous_area->block].busy_block.equal_to);
847         heapinfo2[previous_area->block].busy_block.equal_to = NULL; 
848         xbt_free(heapinfo1[current_pair->block1].busy_block.equal_to);
849       }
850       if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
851         remove_heap_equality(equals, 2, real_addr_block2);
852         previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
853         xbt_free(heapinfo1[previous_area->block].busy_block.equal_to);
854         heapinfo1[previous_area->block].busy_block.equal_to = NULL;
855         xbt_free(heapinfo2[current_pair->block2].busy_block.equal_to);
856       }
857       
858       if(real_addr_block1 != real_addr_block2)
859         add_heap_equality(equals, real_addr_block1, real_addr_block2);
860
861       heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
862       heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
863
864     }
865   }
866
867
868 }
869
870 #ifndef max
871 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
872 #endif
873
874 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
875
876   if(heap1 == NULL && heap1 == NULL){
877     XBT_DEBUG("Malloc descriptors null");
878     return 0;
879   }
880
881   if(heap1->heaplimit != heap2->heaplimit){
882     XBT_DEBUG("Different limit of valid info table indices");
883     return 1;
884   }
885
886   /* Heap information */
887   heaplimit = ((struct mdesc *)heap1)->heaplimit;
888
889   s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
890
891   heapbase1 = (char *)heap1 + BLOCKSIZE;
892   heapbase2 = (char *)heap2 + BLOCKSIZE;
893
894   heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
895   heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
896
897   heapsize1 = heap1->heapsize;
898   heapsize2 = heap2->heapsize;
899
900   /* Start comparison */
901   size_t i, j, k;
902   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
903
904   int distance = 0;
905
906   /* Check busy blocks*/
907
908   i = 1;
909
910   while(i <= heaplimit){
911
912     addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
913     addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
914
915     if(heapinfo1[i].type != heapinfo2[i].type){
916   
917       distance += BLOCKSIZE;
918       XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
919       i++;
920     
921     }else{
922
923       if(heapinfo1[i].type == -1){ /* Free block */
924         i++;
925         continue;
926       }
927
928       if(heapinfo1[i].type == 0){ /* Large block */
929        
930         if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
931           distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
932           i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
933           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           continue;
935         }
936
937         /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
938           distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
939           i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
940           XBT_DEBUG("Different size used oin large cluster at block %zu : %zu - %zu -> distance = %d", i, heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size, distance);
941           continue;
942           }*/
943
944         k = 0;
945
946         //while(k < (heapinfo1[i].busy_block.busy_size)){
947         while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
948           if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
949             distance ++;
950           }
951           k++;
952         } 
953
954         i++;
955
956       }else { /* Fragmented block */
957
958         for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
959
960           addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
961           addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
962
963           if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
964             continue;
965           }
966           
967           
968           /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
969             distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
970             XBT_DEBUG("Different size used in fragment %zu in block %zu : %d - %d -> distance = %d", j, i, heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j], distance); 
971             continue;
972             }*/
973    
974           k=0;
975
976           //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
977           while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
978             if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
979               distance ++;
980             }
981             k++;
982           }
983
984         }
985
986         i++;
987
988       }
989       
990     }
991
992   }
993
994   return distance;
995   
996 }
997
998 static char * is_stack(void *address){
999   unsigned int cursor = 0;
1000   stack_region_t stack;
1001
1002   xbt_dynar_foreach(stacks_areas, cursor, stack){
1003     if(address == stack->address)
1004       return stack->process_name;
1005   }
1006
1007   return NULL;
1008 }
1009
1010 static void add_heap_equality(xbt_dynar_t *equals, void *a1, void *a2){
1011   
1012   if(xbt_dynar_is_empty(*equals)){
1013
1014     heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
1015     he->address1 = a1;
1016     he->address2 = a2;
1017
1018     xbt_dynar_insert_at(*equals, 0, &he);
1019   
1020   }else{
1021
1022     unsigned int cursor = 0;
1023     int start = 0;
1024     int end = xbt_dynar_length(*equals) - 1;
1025     heap_equality_t current_equality = NULL;
1026
1027     while(start <= end){
1028       cursor = (start + end) / 2;
1029       current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
1030       if(current_equality->address1 == a1){
1031         if(current_equality->address2 == a2)
1032           return;
1033         if(current_equality->address2 < a2)
1034           start = cursor + 1;
1035         if(current_equality->address2 > a2)
1036           end = cursor - 1;
1037       }
1038       if(current_equality->address1 < a1)
1039         start = cursor + 1;
1040       if(current_equality->address1 > a1)
1041         end = cursor - 1; 
1042     }
1043
1044     heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
1045     he->address1 = a1;
1046     he->address2 = a2;
1047   
1048     if(current_equality->address1 < a1)
1049       xbt_dynar_insert_at(*equals, cursor + 1 , &he);
1050     else
1051        xbt_dynar_insert_at(*equals, cursor, &he); 
1052
1053   }
1054
1055 }
1056
1057 static void remove_heap_equality(xbt_dynar_t *equals, int address, void *a){
1058   
1059   unsigned int cursor = 0;
1060   heap_equality_t current_equality;
1061   int found = 0;
1062
1063   if(address == 1){
1064
1065     int start = 0;
1066     int end = xbt_dynar_length(*equals) - 1;
1067
1068
1069     while(start <= end && found == 0){
1070       cursor = (start + end) / 2;
1071       current_equality = (heap_equality_t)xbt_dynar_get_as(*equals, cursor, heap_equality_t);
1072       if(current_equality->address1 == a)
1073         found = 1;
1074       if(current_equality->address1 < a)
1075         start = cursor + 1;
1076       if(current_equality->address1 > a)
1077         end = cursor - 1; 
1078     }
1079
1080     if(found == 1)
1081       xbt_dynar_remove_at(*equals, cursor, NULL);
1082   
1083   }else{
1084
1085     xbt_dynar_foreach(*equals, cursor, current_equality){
1086       if(current_equality->address2 == a){
1087         found = 1;
1088         break;
1089       }
1090     }
1091
1092     if(found == 1)
1093       xbt_dynar_remove_at(*equals, cursor, NULL);
1094
1095   }
1096
1097   
1098 }
1099
1100 int is_free_area(void *area, xbt_mheap_t heap){
1101
1102   void *sheap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1103   malloc_info *heapinfo = (malloc_info *)((char *)heap + ((uintptr_t)((char *)heap->heapinfo - (char *)sheap)));
1104   size_t heapsize = heap->heapsize;
1105
1106   /* Get block number */ 
1107   size_t block = ((char*)area - (char*)((xbt_mheap_t)sheap)->heapbase) / BLOCKSIZE + 1;
1108   size_t fragment;
1109
1110   /* Check if valid block number */
1111   if((char *)area < (char*)((xbt_mheap_t)sheap)->heapbase || block > heapsize || block < 1)
1112     return 0;
1113
1114   if(heapinfo[block].type < 0)
1115     return 1;
1116
1117   if(heapinfo[block].type == 0)
1118     return 0;
1119
1120   if(heapinfo[block].type > 0){
1121     fragment = ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1122     if(heapinfo[block].busy_frag.frag_size[fragment] == 0)
1123       return 1;  
1124   }
1125
1126   return 0;
1127   
1128
1129
1130 }