Logo AND Algorithmique Numérique Distribuée

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