Logo AND Algorithmique Numérique Distribuée

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