Logo AND Algorithmique Numérique Distribuée

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