Logo AND Algorithmique Numérique Distribuée

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