Logo AND Algorithmique Numérique Distribuée

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