Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Comments/documentation of memory related code
[simgrid.git] / src / xbt / mmalloc / mm_diff.c
1 /* mm_diff - Memory snapshooting and comparison                             */
2
3 /* Copyright (c) 2008-2014. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "xbt/ex_interface.h" /* internals of backtrace setup */
10 #include "xbt/str.h"
11 #include "mc/mc.h"
12 #include "xbt/mmalloc.h"
13 #include "mc/datatypes.h"
14 #include "mc/mc_private.h"
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
17                                 "Logging specific to mm_diff in mmalloc");
18
19 xbt_dynar_t mc_heap_comparison_ignore;
20 xbt_dynar_t stacks_areas;
21 void *maestro_stack_start, *maestro_stack_end;
22
23
24 /********************************* Backtrace ***********************************/
25 /******************************************************************************/
26
27 static void mmalloc_backtrace_block_display(void* heapinfo, int block){
28
29   /* xbt_ex_t e; */
30
31   /* if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) { */
32   /*   fprintf(stderr, "No backtrace available for that block, sorry.\n"); */
33   /*   return; */
34   /* } */
35
36   /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE); */
37   /* e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size; */
38
39   /* xbt_ex_setup_backtrace(&e); */
40   /* if (e.used == 0) { */
41   /*   fprintf(stderr, "(backtrace not set)\n"); */
42   /* } else if (e.bt_strings == NULL) { */
43   /*   fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
44   /* } else { */
45   /*   int i; */
46
47   /*   fprintf(stderr, "Backtrace of where the block %d was malloced (%d frames):\n", block ,e.used); */
48   /*   for (i = 0; i < e.used; i++)       /\* no need to display "xbt_backtrace_display" *\/{ */
49   /*     fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
50   /*   } */
51   /* } */
52 }
53
54 static void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
55
56   /* xbt_ex_t e; */
57
58   /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE); */
59   /* e.used = XBT_BACKTRACE_SIZE; */
60
61   /* xbt_ex_setup_backtrace(&e); */
62   /* if (e.used == 0) { */
63   /*   fprintf(stderr, "(backtrace not set)\n"); */
64   /* } else if (e.bt_strings == NULL) { */
65   /*   fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
66   /* } else { */
67   /*   int i; */
68
69   /*   fprintf(stderr, "Backtrace of where the fragment %d in block %d was malloced (%d frames):\n", frag, block ,e.used); */
70   /*   for (i = 0; i < e.used; i++)       /\* no need to display "xbt_backtrace_display" *\/{ */
71   /*     fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
72   /*   } */
73   /* } */
74
75 }
76
77 static void mmalloc_backtrace_display(void *addr){
78
79   /* size_t block, frag_nb; */
80   /* int type; */
81   
82   /* xbt_mheap_t heap = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit(); */
83
84   /* block = (((char*) (addr) - (char*) heap -> heapbase) / BLOCKSIZE + 1); */
85
86   /* type = heap->heapinfo[block].type; */
87
88   /* switch(type){ */
89   /* case -1 : /\* Free block *\/ */
90   /*   fprintf(stderr, "Asked to display the backtrace of a block that is free. I'm puzzled\n"); */
91   /*   xbt_abort(); */
92   /*   break;  */
93   /* case 0: /\* Large block *\/ */
94   /*   mmalloc_backtrace_block_display(heap->heapinfo, block); */
95   /*   break; */
96   /* default: /\* Fragmented block *\/ */
97   /*   frag_nb = RESIDUAL(addr, BLOCKSIZE) >> type; */
98   /*   if(heap->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){ */
99   /*     fprintf(stderr , "Asked to display the backtrace of a fragment that is free. I'm puzzled\n"); */
100   /*     xbt_abort(); */
101   /*   } */
102   /*   mmalloc_backtrace_fragment_display(heap->heapinfo, block, frag_nb); */
103   /*   break; */
104   /* } */
105 }
106
107
108 static int compare_backtrace(int b1, int f1, int b2, int f2){
109   /*int i = 0;
110   if(f1 != -1){
111     for(i=0; i< XBT_BACKTRACE_SIZE; i++){
112       if(heapinfo1[b1].busy_frag.bt[f1][i] != heapinfo2[b2].busy_frag.bt[f2][i]){
113         //mmalloc_backtrace_fragment_display((void*)heapinfo1, b1, f1);
114         //mmalloc_backtrace_fragment_display((void*)heapinfo2, b2, f2);
115         return 1;
116       }
117     }
118   }else{
119     for(i=0; i< heapinfo1[b1].busy_block.bt_size; i++){
120       if(heapinfo1[b1].busy_block.bt[i] != heapinfo2[b2].busy_block.bt[i]){
121         //mmalloc_backtrace_block_display((void*)heapinfo1, b1);
122         //mmalloc_backtrace_block_display((void*)heapinfo2, b2);
123         return 1;
124       }
125     }
126     }*/
127   return 0;
128 }
129
130
131 /*********************************** Heap comparison ***********************************/
132 /***************************************************************************************/
133
134 typedef char* type_name;
135
136 struct s_mm_diff {
137   /** \brief Base address of the real heap */
138   void *s_heap;
139   /** \brief Base address of the first heap snapshot */
140   void *heapbase1;
141   /** \brief Base address of the second heap snapshot */
142   void *heapbase2;
143   malloc_info *heapinfo1, *heapinfo2;
144   size_t heaplimit;
145   // Number of blocks in the heaps:
146   size_t heapsize1, heapsize2;
147   xbt_dynar_t to_ignore1, to_ignore2;
148   s_heap_area_t *equals_to1, *equals_to2;
149   dw_type_t *types1, *types2;
150   size_t available;
151 };
152
153 #define equals_to1_(i,j) equals_to1[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
154 #define equals_to2_(i,j) equals_to2[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
155 #define types1_(i,j) types1[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
156 #define types2_(i,j) types2[ MAX_FRAGMENT_PER_BLOCK*(i) + (j)]
157
158 __thread struct s_mm_diff* mm_diff_info = NULL;
159
160 /*********************************** Free functions ************************************/
161
162 static void heap_area_pair_free(heap_area_pair_t pair){
163   xbt_free(pair);
164   pair = NULL;
165 }
166
167 static void heap_area_pair_free_voidp(void *d){
168   heap_area_pair_free((heap_area_pair_t) * (void **) d);
169 }
170
171 static void heap_area_free(heap_area_t area){
172   xbt_free(area);
173   area = NULL;
174 }
175
176 /************************************************************************************/
177
178 static s_heap_area_t make_heap_area(int block, int fragment){
179   s_heap_area_t area;
180   area.valid = 1;
181   area.block = block;
182   area.fragment = fragment;
183   return area;
184 }
185
186  
187 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
188   
189   unsigned int cursor = 0;
190   heap_area_pair_t current_pair;
191
192   xbt_dynar_foreach(list, cursor, current_pair){
193     if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
194       return 0; 
195   }
196   
197   return 1;
198 }
199
200 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
201
202   if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
203     heap_area_pair_t pair = NULL;
204     pair = xbt_new0(s_heap_area_pair_t, 1);
205     pair->block1 = block1;
206     pair->fragment1 = fragment1;
207     pair->block2 = block2;
208     pair->fragment2 = fragment2;
209     
210     xbt_dynar_push(list, &pair); 
211
212     return 1;
213   }
214
215   return 0;
216 }
217
218 static ssize_t heap_comparison_ignore_size(xbt_dynar_t ignore_list, void *address){
219
220   unsigned int cursor = 0;
221   int start = 0;
222   int end = xbt_dynar_length(ignore_list) - 1;
223   mc_heap_ignore_region_t region;
224
225   while(start <= end){
226     cursor = (start + end) / 2;
227     region = (mc_heap_ignore_region_t)xbt_dynar_get_as(ignore_list, cursor, mc_heap_ignore_region_t);
228     if(region->address == address)
229       return region->size;
230     if(region->address < address)
231       start = cursor + 1;
232     if(region->address > address)
233       end = cursor - 1;   
234   }
235
236   return -1;
237 }
238
239 static int is_stack(void *address){
240   unsigned int cursor = 0;
241   stack_region_t stack;
242
243   xbt_dynar_foreach(stacks_areas, cursor, stack){
244     if(address == stack->address)
245       return 1;
246   }
247
248   return 0;
249 }
250
251 static int is_block_stack(int block){
252   unsigned int cursor = 0;
253   stack_region_t stack;
254
255   xbt_dynar_foreach(stacks_areas, cursor, stack){
256     if(block == stack->block)
257       return 1;
258   }
259
260   return 0;
261 }
262
263 static void match_equals(struct s_mm_diff *state, xbt_dynar_t list){
264
265   unsigned int cursor = 0;
266   heap_area_pair_t current_pair;
267
268   xbt_dynar_foreach(list, cursor, current_pair){
269
270     if(current_pair->fragment1 != -1){
271
272       state->equals_to1_(current_pair->block1,current_pair->fragment1) = make_heap_area(current_pair->block2, current_pair->fragment2);
273       state->equals_to2_(current_pair->block2,current_pair->fragment2) = make_heap_area(current_pair->block1, current_pair->fragment1);
274       
275     }else{
276
277       state->equals_to1_(current_pair->block1,0) = make_heap_area(current_pair->block2, current_pair->fragment2);
278       state->equals_to2_(current_pair->block2,0) = make_heap_area(current_pair->block1, current_pair->fragment1);
279
280     }
281
282   }
283 }
284
285 /** Check whether two blocks are known to be matching
286  *
287  *  @param state  State used
288  *  @param b1     Block of state 1
289  *  @param b2     Block of state 2
290  *  @return       if the blocks are known to be matching
291  */
292 static int equal_blocks(struct s_mm_diff *state, int b1, int b2){
293   
294   if(state->equals_to1_(b1,0).block == b2 && state->equals_to2_(b2,0).block == b1)
295     return 1;
296
297   return 0;
298 }
299
300 /** Check whether two fragments are known to be matching
301  *
302  *  @param state  State used
303  *  @param b1     Block of state 1
304  *  @param f1     Fragment of state 1
305  *  @param b2     Block of state 2
306  *  @param f2     Fragment of state 2
307  *  @return       if the fragments are known to be matching
308  */
309 static int equal_fragments(struct s_mm_diff *state, int b1, int f1, int b2, int f2){
310   
311   if(state->equals_to1_(b1,f1).block == b2
312     && state->equals_to1_(b1,f1).fragment == f2
313     && state->equals_to2_(b2,f2).block == b1
314     && state->equals_to2_(b2,f2).fragment == f1)
315     return 1;
316
317   return 0;
318 }
319
320 int init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t i1, xbt_dynar_t i2){
321   if(mm_diff_info==NULL) {
322     mm_diff_info = xbt_new0(struct s_mm_diff, 1);
323     mm_diff_info->equals_to1 = NULL;
324     mm_diff_info->equals_to2 = NULL;
325     mm_diff_info->types1 = NULL;
326     mm_diff_info->types2 = NULL;
327   }
328   struct s_mm_diff *state = mm_diff_info;
329
330   if((((struct mdesc *)heap1)->heaplimit != ((struct mdesc *)heap2)->heaplimit)
331     || ((((struct mdesc *)heap1)->heapsize != ((struct mdesc *)heap2)->heapsize) ))
332     return -1;
333
334   state->heaplimit = ((struct mdesc *)heap1)->heaplimit;
335
336   // Mamailloute in order to find the base address of the main heap:
337   // This heavily depends on the structure of MC: we need to move this code into MC.
338   // (STD_HEAP_SIZE comes from mc.h anyway)
339   state->s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - xbt_pagesize;
340
341   state->heapbase1 = (char *)heap1 + BLOCKSIZE;
342   state->heapbase2 = (char *)heap2 + BLOCKSIZE;
343
344   state->heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)((struct mdesc *)heap1)->heapinfo - (char *)state->s_heap)));
345   state->heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)((struct mdesc *)heap2)->heapinfo - (char *)state->s_heap)));
346
347   state->heapsize1 = heap1->heapsize;
348   state->heapsize2 = heap2->heapsize;
349
350   state->to_ignore1 = i1;
351   state-> to_ignore2 = i2;
352
353   if(state->heaplimit > state->available) {
354     state->equals_to1 = realloc(state->equals_to1, state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(s_heap_area_t));
355     state->types1 = realloc(state->types1, state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(type_name *));
356     state->equals_to2 = realloc(state->equals_to2, state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(s_heap_area_t));
357     state->types2 = realloc(state->types2, state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(type_name *));
358     state->available = state->heaplimit;
359   }
360
361   memset(state->equals_to1, 0, state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(s_heap_area_t));
362   memset(state->equals_to2, 0, state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(s_heap_area_t));
363   memset(state->types1, 0, state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(type_name *));
364   memset(state->types2, 0, state->heaplimit * MAX_FRAGMENT_PER_BLOCK * sizeof(type_name *));
365
366   if(MC_is_active()){
367     MC_ignore_global_variable("mm_diff_info");
368   }
369
370   return 0;
371
372 }
373
374 void reset_heap_information(){
375
376 }
377
378 int mmalloc_compare_heap(mc_snapshot_t snapshot1, mc_snapshot_t snapshot2, xbt_mheap_t heap1, xbt_mheap_t heap2){
379
380   struct s_mm_diff *state = mm_diff_info;
381
382   if(heap1 == NULL && heap2 == NULL){
383     XBT_DEBUG("Malloc descriptors null");
384     return 0;
385   }
386
387   /* Start comparison */
388   size_t i1, i2, j1, j2, k;
389   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
390   int nb_diff1 = 0, nb_diff2 = 0;
391
392   xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
393
394   int equal, res_compare = 0;
395
396   /* Check busy blocks*/
397
398   i1 = 1;
399
400   while(i1 <= state->heaplimit){
401
402     if(state->heapinfo1[i1].type == -1){ /* Free block */
403       i1++;
404       continue;
405     }
406
407     addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)state->s_heap)->heapbase));
408
409     if(state->heapinfo1[i1].type == 0){  /* Large block */
410       
411       if(is_stack(addr_block1)){
412         for(k=0; k < state->heapinfo1[i1].busy_block.size; k++)
413           state->equals_to1_(i1+k,0) = make_heap_area(i1, -1);
414         for(k=0; k < state->heapinfo2[i1].busy_block.size; k++)
415           state->equals_to2_(i1+k,0) = make_heap_area(i1, -1);
416         i1 += state->heapinfo1[i1].busy_block.size;
417         continue;
418       }
419
420       if(state->equals_to1_(i1,0).valid){
421         i1++;
422         continue;
423       }
424     
425       i2 = 1;
426       equal = 0;
427       res_compare = 0;
428   
429       /* Try first to associate to same block in the other heap */
430       if(state->heapinfo2[i1].type == state->heapinfo1[i1].type){
431
432         if(state->equals_to2_(i1,0).valid == 0){
433
434           addr_block2 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)state->s_heap)->heapbase));
435         
436           res_compare = compare_heap_area(addr_block1, addr_block2, snapshot1, snapshot2, NULL, NULL, 0);
437         
438           if(res_compare != 1){
439             for(k=1; k < state->heapinfo2[i1].busy_block.size; k++)
440               state->equals_to2_(i1+k,0) = make_heap_area(i1, -1);
441             for(k=1; k < state->heapinfo1[i1].busy_block.size; k++)
442               state->equals_to1_(i1+k,0) = make_heap_area(i1, -1);
443             equal = 1;
444             i1 += state->heapinfo1[i1].busy_block.size;
445           }
446         
447           xbt_dynar_reset(previous);
448         
449         }
450         
451       }
452
453       while(i2 <= state->heaplimit && !equal){
454
455         addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)state->s_heap)->heapbase));
456            
457         if(i2 == i1){
458           i2++;
459           continue;
460         }
461
462         if(state->heapinfo2[i2].type != 0){
463           i2++;
464           continue;
465         }
466     
467         if(state->equals_to2_(i2,0).valid){
468           i2++;
469           continue;
470         }
471           
472         res_compare = compare_heap_area(addr_block1, addr_block2, snapshot1, snapshot2, NULL, NULL, 0);
473         
474         if(res_compare != 1 ){
475           for(k=1; k < state->heapinfo2[i2].busy_block.size; k++)
476             state->equals_to2_(i2+k,0) = make_heap_area(i1, -1);
477           for(k=1; k < state->heapinfo1[i1].busy_block.size; k++)
478             state->equals_to1_(i1+k,0) = make_heap_area(i2, -1);
479           equal = 1;
480           i1 += state->heapinfo1[i1].busy_block.size;
481         }
482
483         xbt_dynar_reset(previous);
484
485         i2++;
486
487       }
488
489       if(!equal){
490         XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, state->heapinfo1[i1].busy_block.busy_size, addr_block1);
491         i1 = state->heaplimit + 1;
492         nb_diff1++;
493           //i1++;
494       }
495       
496     }else{ /* Fragmented block */
497
498       for(j1=0; j1 < (size_t) (BLOCKSIZE >> state->heapinfo1[i1].type); j1++){
499
500         if(state->heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
501           continue;
502
503         if(state->equals_to1_(i1,j1).valid)
504           continue;
505
506         addr_frag1 = (void*) ((char *)addr_block1 + (j1 << state->heapinfo1[i1].type));
507
508         i2 = 1;
509         equal = 0;
510         
511         /* Try first to associate to same fragment in the other heap */
512         if(state->heapinfo2[i1].type == state->heapinfo1[i1].type){
513
514           if(state->equals_to2_(i1,j1).valid == 0){
515
516             addr_block2 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)state->s_heap)->heapbase));
517             addr_frag2 = (void*) ((char *)addr_block2 + (j1 << ((xbt_mheap_t)state->s_heap)->heapinfo[i1].type));
518
519             res_compare = compare_heap_area(addr_frag1, addr_frag2, snapshot1, snapshot2, NULL, NULL, 0);
520
521             if(res_compare !=  1)
522               equal = 1;
523         
524             xbt_dynar_reset(previous);
525
526           }
527
528         }
529
530         while(i2 <= state->heaplimit && !equal){
531
532           if(state->heapinfo2[i2].type <= 0){
533             i2++;
534             continue;
535           }
536
537           for(j2=0; j2 < (size_t) (BLOCKSIZE >> state->heapinfo2[i2].type); j2++){
538
539             if(i2 == i1 && j2 == j1)
540               continue;
541            
542             if(state->equals_to2_(i2,j2).valid)
543               continue;
544                           
545             addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)state->s_heap)->heapbase));
546             addr_frag2 = (void*) ((char *)addr_block2 + (j2 <<((xbt_mheap_t)state->s_heap)->heapinfo[i2].type));
547
548             res_compare = compare_heap_area(addr_frag1, addr_frag2, snapshot2, snapshot2, NULL, NULL, 0);
549             
550             if(res_compare != 1){
551               equal = 1;
552               xbt_dynar_reset(previous);
553               break;
554             }
555
556             xbt_dynar_reset(previous);
557
558           }
559
560           i2++;
561
562         }
563
564         if(!equal){
565           XBT_DEBUG("Block %zu, fragment %zu not found (size_used = %zd, address = %p)\n", i1, j1, state->heapinfo1[i1].busy_frag.frag_size[j1], addr_frag1);
566           i2 = state->heaplimit + 1;
567           i1 = state->heaplimit + 1;
568           nb_diff1++;
569           break;
570         }
571
572       }
573
574       i1++;
575       
576     }
577
578   }
579
580   /* All blocks/fragments are equal to another block/fragment ? */
581   size_t i = 1, j = 0;
582   void *real_addr_frag1 = NULL, *real_addr_block1 = NULL, *real_addr_block2 = NULL, *real_addr_frag2 = NULL;
583  
584   while(i<=state->heaplimit){
585     if(state->heapinfo1[i].type == 0){
586       if(i1 == state->heaplimit){
587         if(state->heapinfo1[i].busy_block.busy_size > 0){
588           if(state->equals_to1_(i,0).valid == 0){
589             if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
590               addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)state->heapbase1));
591               XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, state->heapinfo1[i].busy_block.busy_size);
592               //mmalloc_backtrace_block_display((void*)heapinfo1, i);
593             }
594             nb_diff1++;
595           }
596         }
597       }
598     }
599     if(state->heapinfo1[i].type > 0){
600       addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)state->heapbase1));
601       real_addr_block1 =  ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)state->s_heap)->heapbase));
602       for(j=0; j < (size_t) (BLOCKSIZE >> state->heapinfo1[i].type); j++){
603         if(i1== state->heaplimit){
604           if(state->heapinfo1[i].busy_frag.frag_size[j] > 0){
605             if(state->equals_to1_(i,j).valid == 0){
606               if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
607                 addr_frag1 = (void*) ((char *)addr_block1 + (j << state->heapinfo1[i].type));
608                 real_addr_frag1 = (void*) ((char *)real_addr_block1 + (j << ((struct mdesc *)state->s_heap)->heapinfo[i].type));
609                 XBT_DEBUG("Block %zu, Fragment %zu (%p - %p) not found (size used = %zd)", i, j, addr_frag1, real_addr_frag1, state->heapinfo1[i].busy_frag.frag_size[j]);
610                 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
611               }
612               nb_diff1++;
613             }
614           }
615         }
616       }
617     }
618     i++; 
619   }
620
621   if(i1 == state->heaplimit)
622     XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
623
624   i = 1;
625
626   while(i<=state->heaplimit){
627     if(state->heapinfo2[i].type == 0){
628       if(i1 == state->heaplimit){
629         if(state->heapinfo2[i].busy_block.busy_size > 0){
630           if(state->equals_to2_(i,0).valid == 0){
631             if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
632               addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)state->heapbase2));
633               XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, state->heapinfo2[i].busy_block.busy_size);
634               //mmalloc_backtrace_block_display((void*)heapinfo2, i);
635             }
636             nb_diff2++;
637           }
638         }
639       }
640     }
641     if(state->heapinfo2[i].type > 0){
642       addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)state->heapbase2));
643       real_addr_block2 =  ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)state->s_heap)->heapbase));
644       for(j=0; j < (size_t) (BLOCKSIZE >> state->heapinfo2[i].type); j++){
645         if(i1 == state->heaplimit){
646           if(state->heapinfo2[i].busy_frag.frag_size[j] > 0){
647             if(state->equals_to2_(i,j).valid == 0){
648               if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
649                 addr_frag2 = (void*) ((char *)addr_block2 + (j << state->heapinfo2[i].type));
650                 real_addr_frag2 = (void*) ((char *)real_addr_block2 + (j << ((struct mdesc *)state->s_heap)->heapinfo[i].type));
651                 XBT_DEBUG( "Block %zu, Fragment %zu (%p - %p) not found (size used = %zd)", i, j, addr_frag2, real_addr_frag2, state->heapinfo2[i].busy_frag.frag_size[j]);
652                 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
653               }
654               nb_diff2++;
655             }
656           }
657         }
658       }
659     }
660     i++; 
661   }
662
663   if(i1 == state->heaplimit)
664     XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
665
666   xbt_dynar_free(&previous);
667   real_addr_frag1 = NULL, real_addr_block1 = NULL, real_addr_block2 = NULL, real_addr_frag2 = NULL;
668
669   return ((nb_diff1 > 0) || (nb_diff2 > 0));
670 }
671
672 /**
673  *
674  * @param state
675  * @param real_area1     Process address for state 1
676  * @param real_area2     Process address for state 2
677  * @param area1          Snapshot address for state 1
678  * @param area2          Snapshot address for state 2
679  * @param snapshot1      Snapshot of state 1
680  * @param snapshot2      Snapshot of state 2
681  * @param previous
682  * @param size
683  * @param check_ignore
684  */
685 static int compare_heap_area_without_type(struct s_mm_diff *state, void *real_area1, void *real_area2, void *area1, void *area2, mc_snapshot_t snapshot1, mc_snapshot_t snapshot2, xbt_dynar_t previous, int size, int check_ignore){
686
687   int i = 0;
688   void *addr_pointed1, *addr_pointed2;
689   int pointer_align, res_compare;
690   ssize_t ignore1, ignore2;
691
692   while(i<size){
693
694     if(check_ignore > 0){
695       if((ignore1 = heap_comparison_ignore_size(state->to_ignore1, (char *)real_area1 + i)) != -1){
696         if((ignore2 = heap_comparison_ignore_size(state->to_ignore2, (char *)real_area2 + i))  == ignore1){
697           if(ignore1 == 0){
698             check_ignore--;
699             return 0;
700           }else{
701             i = i + ignore2;
702             check_ignore--;
703             continue;
704           }
705         }
706       }
707     }
708
709     if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
710
711       pointer_align = (i / sizeof(void*)) * sizeof(void*);
712       addr_pointed1 = *((void **)((char *)area1 + pointer_align));
713       addr_pointed2 = *((void **)((char *)area2 + pointer_align));
714       
715       if(addr_pointed1 > maestro_stack_start && addr_pointed1 < maestro_stack_end && addr_pointed2 > maestro_stack_start && addr_pointed2 < maestro_stack_end){
716         i = pointer_align + sizeof(void *);
717         continue;
718       }else if((addr_pointed1 > state->s_heap) && ((char *)addr_pointed1 < (char *)state->s_heap + STD_HEAP_SIZE)
719                && (addr_pointed2 > state->s_heap) && ((char *)addr_pointed2 < (char *)state->s_heap + STD_HEAP_SIZE)){
720         res_compare = compare_heap_area(addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous, NULL, 0);
721         if(res_compare == 1){
722           return res_compare;
723         }
724         i = pointer_align + sizeof(void *);
725         continue;
726       }else{
727         return 1;
728       }
729       
730     }
731     
732     i++;
733
734   }
735
736   return 0;
737  
738 }
739
740 /**
741  *
742  * @param state
743  * @param real_area1     Process address for state 1
744  * @param real_area2     Process address for state 2
745  * @param area1          Snapshot address for state 1
746  * @param area2          Snapshot address for state 2
747  * @param snapshot1      Snapshot of state 1
748  * @param snapshot2      Snapshot of state 2
749  * @param previous
750  * @param type_id
751  * @param area_size      either a byte_size or an elements_count (?)
752  * @param check_ignore
753  * @param pointer_level
754  * @return               0 (same), 1 (different), -1 (unknown)
755  */
756 static int compare_heap_area_with_type(struct s_mm_diff *state, void *real_area1, void *real_area2, void *area1, void *area2,
757                                        mc_snapshot_t snapshot1, mc_snapshot_t snapshot2,
758                                        xbt_dynar_t previous, dw_type_t type,
759                                        int area_size, int check_ignore, int pointer_level){
760
761   if(is_stack(real_area1) && is_stack(real_area2))
762     return 0;
763
764   ssize_t ignore1, ignore2;
765
766   if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(state->to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(state->to_ignore2, real_area2))  == ignore1)){
767     return 0;
768   }
769   
770   dw_type_t subtype, subsubtype;
771   int res, elm_size, i;
772   unsigned int cursor = 0;
773   dw_type_t member;
774   void *addr_pointed1, *addr_pointed2;;
775
776   switch(type->type){
777   case DW_TAG_unspecified_type:
778     return 1;
779
780   case DW_TAG_base_type:
781     if(type->name!=NULL && strcmp(type->name, "char") == 0){ /* String, hence random (arbitrary ?) size */
782       if(real_area1 == real_area2)
783         return -1;
784       else
785         return (memcmp(area1, area2, area_size) != 0);
786     }else{
787       if(area_size != -1 && type->byte_size != area_size)
788         return -1;
789       else{
790         return  (memcmp(area1, area2, type->byte_size) != 0);
791       }
792     }
793     break;
794   case DW_TAG_enumeration_type:
795     if(area_size != -1 && type->byte_size != area_size)
796       return -1;
797     else
798       return (memcmp(area1, area2, type->byte_size) != 0);
799     break;
800   case DW_TAG_typedef:
801   case DW_TAG_const_type:
802   case DW_TAG_volatile_type:
803     return compare_heap_area_with_type(state, real_area1, real_area2, area1, area2, snapshot1, snapshot2, previous, type->subtype, area_size, check_ignore, pointer_level);
804     break;
805   case DW_TAG_array_type:
806     subtype = type->subtype;
807     switch(subtype->type){
808     case DW_TAG_unspecified_type:
809       return 1;
810
811     case DW_TAG_base_type:
812     case DW_TAG_enumeration_type:
813     case DW_TAG_pointer_type:
814     case DW_TAG_reference_type:
815     case DW_TAG_rvalue_reference_type:
816     case DW_TAG_structure_type:
817     case DW_TAG_class_type:
818     case DW_TAG_union_type:
819       if(subtype->full_type)
820         subtype = subtype->full_type;
821       elm_size = subtype->byte_size;
822       break;
823     // TODO, just remove the type indirection?
824     case DW_TAG_const_type:
825     case DW_TAG_typedef:
826     case DW_TAG_volatile_type:
827       subsubtype = subtype->subtype;
828       if(subsubtype->full_type)
829         subsubtype = subsubtype->full_type;
830       elm_size = subsubtype->byte_size;
831       break;
832     default : 
833       return 0;
834       break;
835     }
836     for(i=0; i<type->element_count; i++){
837       // TODO, add support for variable stride (DW_AT_byte_stride)
838       res = compare_heap_area_with_type(state, (char *)real_area1 + (i*elm_size), (char *)real_area2 + (i*elm_size), (char *)area1 + (i*elm_size), (char *)area2 + (i*elm_size), snapshot1, snapshot2, previous, type->subtype, subtype->byte_size, check_ignore, pointer_level);
839       if(res == 1)
840         return res;
841     }
842     break;
843   case DW_TAG_reference_type:
844   case DW_TAG_rvalue_reference_type:
845   case DW_TAG_pointer_type:
846     if(type->subtype && type->subtype->type == DW_TAG_subroutine_type){
847       addr_pointed1 = *((void **)(area1)); 
848       addr_pointed2 = *((void **)(area2));
849       return (addr_pointed1 != addr_pointed2);;
850     }else{
851       pointer_level++;
852       if(pointer_level > 1){ /* Array of pointers */
853         for(i=0; i<(area_size/sizeof(void *)); i++){ 
854           addr_pointed1 = *((void **)((char *)area1 + (i*sizeof(void *)))); 
855           addr_pointed2 = *((void **)((char *)area2 + (i*sizeof(void *)))); 
856           if(addr_pointed1 > state->s_heap && (char *)addr_pointed1 < (char*) state->s_heap + STD_HEAP_SIZE && addr_pointed2 > state->s_heap && (char *)addr_pointed2 < (char*) state->s_heap + STD_HEAP_SIZE)
857             res =  compare_heap_area(addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous, type->subtype, pointer_level);
858           else
859             res =  (addr_pointed1 != addr_pointed2);
860           if(res == 1)
861             return res;
862         }
863       }else{
864         addr_pointed1 = *((void **)(area1)); 
865         addr_pointed2 = *((void **)(area2));
866         if(addr_pointed1 > state->s_heap && (char *)addr_pointed1 < (char*) state->s_heap + STD_HEAP_SIZE && addr_pointed2 > state->s_heap && (char *)addr_pointed2 < (char*) state->s_heap + STD_HEAP_SIZE)
867           return compare_heap_area(addr_pointed1, addr_pointed2, snapshot1, snapshot2, previous, type->subtype, pointer_level);
868         else
869           return  (addr_pointed1 != addr_pointed2);
870       }
871     }
872     break;
873   case DW_TAG_structure_type:
874   case DW_TAG_class_type:
875     if(type->full_type)
876       type = type->full_type;
877     if(area_size != -1 && type->byte_size != area_size){
878       if(area_size>type->byte_size && area_size%type->byte_size == 0){
879         for(i=0; i<(area_size/type->byte_size); i++){
880           res = compare_heap_area_with_type(state, (char *)real_area1 + (i*type->byte_size), (char *)real_area2 + (i*type->byte_size), (char *)area1 + (i*type->byte_size), (char *)area2 + (i*type->byte_size), snapshot1, snapshot2, previous, type, -1, check_ignore, 0);
881           if(res == 1)
882             return res;
883         }
884       }else{
885         return -1;
886       }
887     }else{
888       cursor = 0;
889       xbt_dynar_foreach(type->members, cursor, member){
890         // TODO, optimize this? (for the offset case)
891         char* real_member1 = mc_member_resolve(real_area1, type, member, snapshot1);
892         char* real_member2 = mc_member_resolve(real_area2, type, member, snapshot2);
893         char* member1 = mc_translate_address((uintptr_t)real_member1, snapshot1);
894         char* member2 = mc_translate_address((uintptr_t)real_member2, snapshot2);
895         res = compare_heap_area_with_type(state, real_member1, real_member2, member1, member2, snapshot1, snapshot2, previous, member->subtype, -1, check_ignore, 0);
896         if(res == 1){
897           return res;
898         }
899       }
900     }
901     break;
902   case DW_TAG_union_type:
903     return compare_heap_area_without_type(state, real_area1, real_area2, area1, area2, snapshot1, snapshot2, previous, type->byte_size, check_ignore);
904     break;
905   default:
906     break;
907   }
908
909   return 0;
910
911 }
912
913 /** Infer the type of a part of the block from the type of the block
914  *
915  * TODO, handle DW_TAG_array_type as well as arrays of the object ((*p)[5], p[5])
916  *
917  * TODO, handle subfields ((*p).bar.foo, (*p)[5].bar…)
918  *
919  * @param  type_id            DWARF type ID of the root address
920  * @param  area_size
921  * @return                    DWARF type ID for given offset
922  */
923 static dw_type_t get_offset_type(void* real_base_address, dw_type_t type, int offset, int area_size, mc_snapshot_t snapshot){
924
925   // Beginning of the block, the infered variable type if the type of the block:
926   if(offset==0)
927     return type;
928
929   switch(type->type){
930   case DW_TAG_structure_type :
931   case DW_TAG_class_type:
932     if(type->full_type)
933       type = type->full_type;
934     
935     if(area_size != -1 && type->byte_size != area_size){
936       if(area_size>type->byte_size && area_size%type->byte_size == 0)
937         return type;
938       else
939         return NULL;
940     }else{
941       unsigned int cursor = 0;
942       dw_type_t member;
943       xbt_dynar_foreach(type->members, cursor, member){ 
944
945         if(!member->location.size) {
946           // We have the offset, use it directly (shortcut):
947           if(member->offset == offset)
948             return member->subtype;
949         } else {
950           char* real_member = mc_member_resolve(real_base_address, type, member, snapshot);
951           if(real_member - (char*)real_base_address == offset)
952             return member->subtype;
953         }
954
955       }
956       return NULL;
957     }
958     break;
959   default:
960     /* FIXME : other cases ? */
961     return NULL;
962     break;
963   }
964 }
965
966 /**
967  *
968  * @param area1          Process address for state 1
969  * @param area2          Process address for state 2
970  * @param snapshot1      Snapshot of state 1
971  * @param snapshot2      Snapshot of state 2
972  * @param previous       Pairs of blocks already compared on the current path (or NULL)
973  * @param type_id        Type of variable
974  * @param pointer_level
975  * @return 0 (same), 1 (different), -1
976  */
977 int compare_heap_area(void *area1, void* area2, mc_snapshot_t snapshot1, mc_snapshot_t snapshot2, xbt_dynar_t previous, dw_type_t type, int pointer_level){
978
979   struct s_mm_diff* state = mm_diff_info;
980
981   int res_compare;
982   ssize_t block1, frag1, block2, frag2;
983   ssize_t size;
984   int check_ignore = 0;
985
986   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2, *real_addr_block1, *real_addr_block2,  *real_addr_frag1, *real_addr_frag2;
987   void *area1_to_compare, *area2_to_compare;
988   int type_size = -1;
989   int offset1 =0, offset2 = 0;
990   int new_size1 = -1, new_size2 = -1;
991   dw_type_t new_type1 = NULL, new_type2 = NULL;
992
993   int match_pairs = 0;
994
995   if(previous == NULL){
996     previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
997     match_pairs = 1;
998   }
999
1000   // Get block number:
1001   block1 = ((char*)area1 - (char*)((xbt_mheap_t)state->s_heap)->heapbase) / BLOCKSIZE + 1;
1002   block2 = ((char*)area2 - (char*)((xbt_mheap_t)state->s_heap)->heapbase) / BLOCKSIZE + 1;
1003
1004   // If either block is a stack block:
1005   if(is_block_stack((int)block1) && is_block_stack((int)block2)){
1006     add_heap_area_pair(previous, block1, -1, block2, -1);
1007     if(match_pairs){
1008       match_equals(state, previous);
1009       xbt_dynar_free(&previous);
1010     }
1011     return 0;
1012   }
1013
1014   // If either block is not in the expected area of memory:
1015   if(((char *)area1 < (char*)((xbt_mheap_t)state->s_heap)->heapbase)  || (block1 > state->heapsize1) || (block1 < 1)
1016     || ((char *)area2 < (char*)((xbt_mheap_t)state->s_heap)->heapbase) || (block2 > state->heapsize2) || (block2 < 1)){
1017     if(match_pairs){
1018       xbt_dynar_free(&previous);
1019     }
1020     return 1;
1021   }
1022
1023   // Snapshot address of the block:
1024   addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)state->heapbase1));
1025   addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)state->heapbase2));
1026
1027   // Process address of the block:
1028   real_addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)state->s_heap)->heapbase));
1029   real_addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)state->s_heap)->heapbase));
1030
1031   if(type){
1032
1033     if(type->full_type)
1034       type = type->full_type;
1035
1036     // This assume that for "boring" types (volatile ...) byte_size is absent:
1037     while(type->byte_size == 0 && type->subtype!=NULL)
1038       type = type->subtype;
1039
1040     // Find type_size:
1041     if((type->type == DW_TAG_pointer_type) || ((type->type == DW_TAG_base_type) && type->name!=NULL && (!strcmp(type->name, "char"))))
1042       type_size = -1;
1043     else
1044       type_size = type->byte_size;
1045
1046   }
1047   
1048   if((state->heapinfo1[block1].type == -1) && (state->heapinfo2[block2].type == -1)){  /* Free block */
1049
1050     if(match_pairs){
1051       match_equals(state, previous);
1052       xbt_dynar_free(&previous);
1053     }
1054     return 0;
1055
1056   }else if((state->heapinfo1[block1].type == 0) && (state->heapinfo2[block2].type == 0)){ /* Complete block */
1057     
1058     // TODO, lookup variable type from block type as done for fragmented blocks
1059
1060     if(state->equals_to1_(block1,0).valid && state->equals_to2_(block2,0).valid){
1061       if(equal_blocks(state, block1, block2)){
1062         if(match_pairs){
1063           match_equals(state, previous);
1064           xbt_dynar_free(&previous);
1065         }
1066         return 0;
1067       }
1068     }
1069
1070     if(type_size != -1){
1071       if(type_size != state->heapinfo1[block1].busy_block.busy_size
1072         && type_size != state->heapinfo2[block2].busy_block.busy_size
1073         && type->name!=NULL && !strcmp(type->name, "s_smx_context")){
1074         if(match_pairs){
1075           match_equals(state, previous);
1076           xbt_dynar_free(&previous);
1077         }
1078         return -1;
1079       }
1080     }
1081
1082     if(state->heapinfo1[block1].busy_block.size != state->heapinfo2[block2].busy_block.size){
1083       if(match_pairs){
1084         xbt_dynar_free(&previous);
1085       }
1086       return 1;
1087     }
1088
1089     if(state->heapinfo1[block1].busy_block.busy_size != state->heapinfo2[block2].busy_block.busy_size){
1090       if(match_pairs){
1091         xbt_dynar_free(&previous);
1092       }
1093       return 1;
1094     }
1095
1096     if(!add_heap_area_pair(previous, block1, -1, block2, -1)){
1097       if(match_pairs){
1098         match_equals(state, previous);
1099         xbt_dynar_free(&previous);
1100       }
1101       return 0;
1102     }
1103  
1104     size = state->heapinfo1[block1].busy_block.busy_size;
1105     
1106     // Remember (basic) type inference.
1107     // The current data structure only allows us to do this for the whole block.
1108     if (type != NULL && area1==real_addr_block1) {
1109       state->types1_(block1,0) = type;
1110     }
1111     if (type != NULL && area2==real_addr_block2) {
1112       state->types2_(block2,0) = type;
1113     }
1114
1115     if(size <= 0){
1116       if(match_pairs){
1117         match_equals(state, previous);
1118         xbt_dynar_free(&previous);
1119       }
1120       return 0;
1121     }
1122
1123     frag1 = -1;
1124     frag2 = -1;
1125
1126     area1_to_compare = addr_block1;
1127     area2_to_compare = addr_block2;
1128
1129     if((state->heapinfo1[block1].busy_block.ignore > 0) && (state->heapinfo2[block2].busy_block.ignore == state->heapinfo1[block1].busy_block.ignore))
1130       check_ignore = state->heapinfo1[block1].busy_block.ignore;
1131       
1132   }else if((state->heapinfo1[block1].type > 0) && (state->heapinfo2[block2].type > 0)){ /* Fragmented block */
1133
1134     // Fragment number:
1135     frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> state->heapinfo1[block1].type;
1136     frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> state->heapinfo2[block2].type;
1137
1138     // Snapshot address of the fragment:
1139     addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << state->heapinfo1[block1].type));
1140     addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << state->heapinfo2[block2].type));
1141
1142     // Process address of the fragment:
1143     real_addr_frag1 = (void*) ((char *)real_addr_block1 + (frag1 << ((xbt_mheap_t)state->s_heap)->heapinfo[block1].type));
1144     real_addr_frag2 = (void*) ((char *)real_addr_block2 + (frag2 << ((xbt_mheap_t)state->s_heap)->heapinfo[block2].type));
1145
1146     // Check the size of the fragments against the size of the type:
1147     if(type_size != -1){
1148       if(state->heapinfo1[block1].busy_frag.frag_size[frag1] == -1 || state->heapinfo2[block2].busy_frag.frag_size[frag2] == -1){
1149         if(match_pairs){
1150           match_equals(state, previous);
1151           xbt_dynar_free(&previous);
1152         }
1153         return -1;
1154       }
1155       if(type_size != state->heapinfo1[block1].busy_frag.frag_size[frag1]|| type_size !=  state->heapinfo2[block2].busy_frag.frag_size[frag2]){
1156         if(match_pairs){
1157           match_equals(state, previous);
1158           xbt_dynar_free(&previous);
1159         }
1160         return -1;
1161       }
1162     }
1163
1164     // Check if the blocks are already matched together:
1165     if(state->equals_to1_(block1,frag1).valid && state->equals_to2_(block2,frag2).valid){
1166       if(equal_fragments(state, block1, frag1, block2, frag2)){
1167         if(match_pairs){
1168           match_equals(state, previous);
1169           xbt_dynar_free(&previous);
1170         }
1171         return 0;
1172       }
1173     }
1174
1175     // Compare the size of both fragments:
1176     if(state->heapinfo1[block1].busy_frag.frag_size[frag1] != state->heapinfo2[block2].busy_frag.frag_size[frag2]){
1177       if(type_size == -1){
1178          if(match_pairs){
1179           match_equals(state, previous);
1180           xbt_dynar_free(&previous);
1181         }
1182         return -1;
1183       }else{
1184         if(match_pairs){
1185           xbt_dynar_free(&previous);
1186         }
1187         return 1;
1188       }
1189     }
1190       
1191     // Size of the fragment:
1192     size = state->heapinfo1[block1].busy_frag.frag_size[frag1];
1193
1194     // Remember (basic) type inference.
1195     // The current data structure only allows us to do this for the whole block.
1196     if(type != NULL && area1==real_addr_frag1){
1197       state->types1_(block1,frag1) = type;
1198     }
1199     if(type != NULL && area2==real_addr_frag2) {
1200       state->types2_(block2,frag2) = type;
1201     }
1202
1203     // The type of the variable is already known:
1204     if(type) {
1205       new_type1 = type;
1206       new_type2 = type;
1207     }
1208
1209     // Type inference from the block type.
1210     else if(state->types1_(block1,frag1) != NULL || state->types2_(block2,frag2) != NULL) {
1211
1212       offset1 = (char *)area1 - (char *)real_addr_frag1;
1213       offset2 = (char *)area2 - (char *)real_addr_frag2;
1214
1215       if(state->types1_(block1,frag1) != NULL && state->types2_(block2,frag2) != NULL){
1216         new_type1 = get_offset_type(real_addr_frag1, state->types1_(block1,frag1), offset1, size, snapshot1);
1217         new_type2 = get_offset_type(real_addr_frag2, state->types2_(block2,frag2), offset1, size, snapshot2);
1218       }else if(state->types1_(block1,frag1) != NULL){
1219         new_type1 = get_offset_type(real_addr_frag1, state->types1_(block1,frag1), offset1, size, snapshot1);
1220         new_type2 = get_offset_type(real_addr_frag2, state->types1_(block1,frag1), offset2, size, snapshot2);
1221       }else if(state->types2_(block2,frag2) != NULL){
1222         new_type1 = get_offset_type(real_addr_frag1, state->types2_(block2,frag2), offset1, size, snapshot1);
1223         new_type2 = get_offset_type(real_addr_frag2, state->types2_(block2,frag2), offset2, size, snapshot2);
1224       }else{
1225         if(match_pairs){
1226           match_equals(state, previous);
1227           xbt_dynar_free(&previous);
1228         }
1229         return -1;
1230       }   
1231
1232       if(new_type1 !=  NULL && new_type2 !=  NULL && new_type1!=new_type2){
1233
1234           type = new_type1;
1235           while(type->byte_size == 0 && type->subtype != NULL)
1236             type = type->subtype;
1237           new_size1 = type->byte_size;
1238
1239           type = new_type2;
1240           while(type->byte_size == 0 && type->subtype != NULL)
1241             type = type->subtype;
1242           new_size2 = type->byte_size;
1243
1244       }else{
1245         if(match_pairs){
1246           match_equals(state, previous);
1247           xbt_dynar_free(&previous);
1248         }
1249         return -1;
1250       }
1251     }
1252
1253     area1_to_compare = (char *)addr_frag1 + offset1;
1254     area2_to_compare = (char *)addr_frag2 + offset2;
1255     
1256     if(new_size1 > 0 && new_size1 == new_size2){
1257       type = new_type1;
1258       size = new_size1;
1259     }
1260
1261     if(offset1 == 0 && offset2 == 0){
1262       if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
1263         if(match_pairs){
1264           match_equals(state, previous);
1265           xbt_dynar_free(&previous);
1266         }
1267         return 0;
1268       }
1269     }
1270
1271     if(size <= 0){
1272       if(match_pairs){
1273         match_equals(state, previous);
1274         xbt_dynar_free(&previous);
1275       }
1276       return 0;
1277     }
1278       
1279     if((state->heapinfo1[block1].busy_frag.ignore[frag1] > 0) && ( state->heapinfo2[block2].busy_frag.ignore[frag2] == state->heapinfo1[block1].busy_frag.ignore[frag1]))
1280       check_ignore = state->heapinfo1[block1].busy_frag.ignore[frag1];
1281     
1282   }else{
1283
1284     if(match_pairs){
1285       xbt_dynar_free(&previous);
1286     }
1287     return 1;
1288
1289   }
1290   
1291
1292   /* Start comparison*/
1293   if(type){
1294     res_compare = compare_heap_area_with_type(state, area1, area2, area1_to_compare, area2_to_compare, snapshot1, snapshot2, previous, type, size, check_ignore, pointer_level);
1295   }else{
1296     res_compare = compare_heap_area_without_type(state, area1, area2, area1_to_compare, area2_to_compare, snapshot1, snapshot2, previous, size, check_ignore);
1297   }
1298   if(res_compare == 1){
1299     if(match_pairs)
1300       xbt_dynar_free(&previous);
1301     return res_compare;
1302   }
1303
1304   if(match_pairs){
1305     match_equals(state, previous);
1306     xbt_dynar_free(&previous);
1307   }
1308
1309   return 0;
1310 }
1311
1312 /*********************************************** Miscellaneous ***************************************************/
1313 /****************************************************************************************************************/
1314
1315 // Not used:
1316 static int get_pointed_area_size(void *area, int heap){
1317
1318   struct s_mm_diff *state = mm_diff_info;
1319
1320   int block, frag;
1321   malloc_info *heapinfo;
1322
1323   if(heap == 1)
1324     heapinfo = state->heapinfo1;
1325   else
1326     heapinfo = state->heapinfo2;
1327
1328   block = ((char*)area - (char*)((xbt_mheap_t)state->s_heap)->heapbase) / BLOCKSIZE + 1;
1329
1330   if(((char *)area < (char*)((xbt_mheap_t)state->s_heap)->heapbase)  || (block > state->heapsize1) || (block < 1))
1331     return -1;
1332
1333   if(heapinfo[block].type == -1){ /* Free block */
1334     return -1;  
1335   }else if(heapinfo[block].type == 0){ /* Complete block */
1336     return (int)heapinfo[block].busy_block.busy_size;
1337   }else{
1338     frag = ((uintptr_t) (ADDR2UINT (area) % (BLOCKSIZE))) >> heapinfo[block].type;
1339     return (int)heapinfo[block].busy_frag.frag_size[frag];
1340   }
1341
1342 }
1343
1344 // Not used:
1345 char *get_type_description(mc_object_info_t info, char *type_name){
1346
1347   xbt_dict_cursor_t dict_cursor;
1348   char *type_origin;
1349   dw_type_t type;
1350
1351   xbt_dict_foreach(info->types, dict_cursor, type_origin, type){
1352     if(type->name && (strcmp(type->name, type_name) == 0) && type->byte_size > 0){
1353       xbt_dict_cursor_free(&dict_cursor);
1354       return type_origin;
1355     }
1356   }
1357
1358   xbt_dict_cursor_free(&dict_cursor);
1359   return NULL;
1360 }
1361
1362
1363 #ifndef max
1364 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
1365 #endif
1366
1367 // Not used:
1368 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
1369
1370   struct s_mm_diff *state = mm_diff_info;
1371
1372   if(heap1 == NULL && heap1 == NULL){
1373     XBT_DEBUG("Malloc descriptors null");
1374     return 0;
1375   }
1376
1377   if(heap1->heaplimit != heap2->heaplimit){
1378     XBT_DEBUG("Different limit of valid info table indices");
1379     return 1;
1380   }
1381
1382   /* Heap information */
1383   state->heaplimit = ((struct mdesc *)heap1)->heaplimit;
1384
1385   state->s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - xbt_pagesize;
1386
1387   state->heapbase1 = (char *)heap1 + BLOCKSIZE;
1388   state->heapbase2 = (char *)heap2 + BLOCKSIZE;
1389
1390   state->heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)state->s_heap)));
1391   state->heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)state->s_heap)));
1392
1393   state->heapsize1 = heap1->heapsize;
1394   state->heapsize2 = heap2->heapsize;
1395
1396   /* Start comparison */
1397   size_t i, j, k;
1398   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
1399
1400   int distance = 0;
1401
1402   /* Check busy blocks*/
1403
1404   i = 1;
1405
1406   while(i <= state->heaplimit){
1407
1408     addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)state->heapbase1));
1409     addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)state->heapbase2));
1410
1411     if(state->heapinfo1[i].type != state->heapinfo2[i].type){
1412   
1413       distance += BLOCKSIZE;
1414       XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, state->heapinfo1[i].type, state->heapinfo2[i].type, distance);
1415       i++;
1416     
1417     }else{
1418
1419       if(state->heapinfo1[i].type == -1){ /* Free block */
1420         i++;
1421         continue;
1422       }
1423
1424       if(state->heapinfo1[i].type == 0){ /* Large block */
1425        
1426         if(state->heapinfo1[i].busy_block.size != state->heapinfo2[i].busy_block.size){
1427           distance += BLOCKSIZE * max(state->heapinfo1[i].busy_block.size, state->heapinfo2[i].busy_block.size);
1428           i += max(state->heapinfo1[i].busy_block.size, state->heapinfo2[i].busy_block.size);
1429           XBT_DEBUG("Different larger of cluster at block %zu : %zu - %zu -> distance = %d", i, state->heapinfo1[i].busy_block.size, state->heapinfo2[i].busy_block.size, distance);
1430           continue;
1431         }
1432
1433         /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
1434           distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
1435           i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1436           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);
1437           continue;
1438           }*/
1439
1440         k = 0;
1441
1442         //while(k < (heapinfo1[i].busy_block.busy_size)){
1443         while(k < state->heapinfo1[i].busy_block.size * BLOCKSIZE){
1444           if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
1445             distance ++;
1446           }
1447           k++;
1448         } 
1449
1450         i++;
1451
1452       }else { /* Fragmented block */
1453
1454         for(j=0; j < (size_t) (BLOCKSIZE >> state->heapinfo1[i].type); j++){
1455
1456           addr_frag1 = (void*) ((char *)addr_block1 + (j << state->heapinfo1[i].type));
1457           addr_frag2 = (void*) ((char *)addr_block2 + (j << state->heapinfo2[i].type));
1458
1459           if(state->heapinfo1[i].busy_frag.frag_size[j] == 0 && state->heapinfo2[i].busy_frag.frag_size[j] == 0){
1460             continue;
1461           }
1462           
1463           
1464           /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
1465             distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
1466             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); 
1467             continue;
1468             }*/
1469    
1470           k=0;
1471
1472           //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
1473           while(k < (BLOCKSIZE / (BLOCKSIZE >> state->heapinfo1[i].type))){
1474             if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
1475               distance ++;
1476             }
1477             k++;
1478           }
1479
1480         }
1481
1482         i++;
1483
1484       }
1485       
1486     }
1487
1488   }
1489
1490   return distance;
1491   
1492 }
1493