Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
02f333df67c7295e46a772f5f9ac8d93ecb9676b
[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
780   switch(type->type){
781   case DW_TAG_base_type:
782     if(type->name!=NULL && strcmp(type->name, "char") == 0){ /* String, hence random (arbitrary ?) size */
783       if(real_area1 == real_area2)
784         return -1;
785       else
786         return (memcmp(area1, area2, area_size) != 0);
787     }else{
788       if(area_size != -1 && type->byte_size != area_size)
789         return -1;
790       else{
791         return  (memcmp(area1, area2, type->byte_size) != 0);
792       }
793     }
794     break;
795   case DW_TAG_enumeration_type:
796     if(area_size != -1 && type->byte_size != area_size)
797       return -1;
798     else
799       return (memcmp(area1, area2, type->byte_size) != 0);
800     break;
801   case DW_TAG_typedef:
802   case DW_TAG_const_type:
803   case DW_TAG_volatile_type:
804     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);
805     break;
806   case DW_TAG_array_type:
807     subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
808     switch(subtype->type){
809     case DW_TAG_base_type:
810     case DW_TAG_enumeration_type:
811     case DW_TAG_pointer_type:
812     case DW_TAG_structure_type:
813     case DW_TAG_union_type:
814       if(subtype->byte_size == 0){ /*declaration of the type, need the complete description */
815           subtype = xbt_dict_get_or_null(other_info->types_by_name, subtype->name);
816           switch_types = 1;
817       }
818       elm_size = subtype->byte_size;
819       break;
820     // TODO, just remove the type indirection?
821     case DW_TAG_const_type:
822     case DW_TAG_typedef:
823     case DW_TAG_volatile_type:
824       subsubtype = subtype->subtype;
825       if(subsubtype->byte_size == 0){ /*declaration of the type, need the complete description */
826           subsubtype = xbt_dict_get_or_null(other_info->types_by_name, subtype->name);
827           switch_types = 1;
828       }
829       elm_size = subsubtype->byte_size;
830       break;
831     default : 
832       return 0;
833       break;
834     }
835     for(i=0; i<type->element_count; i++){
836       // TODO, add support for variable stride (DW_AT_byte_stride)
837       if(switch_types)
838         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);
839       else
840         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);
841       if(res == 1)
842         return res;
843     }
844     break;
845   case DW_TAG_pointer_type:
846     if(type->dw_type_id && ((dw_type_t)xbt_dict_get_or_null(info->types, type->dw_type_id))->type == DW_TAG_subroutine_type){
847       addr_pointed1 = *((void **)(area1)); 
848       addr_pointed2 = *((void **)(area2));
849       return (addr_pointed1 != addr_pointed2);;
850     }else{
851       pointer_level++;
852       if(pointer_level > 1){ /* Array of pointers */
853         for(i=0; i<(area_size/sizeof(void *)); i++){ 
854           addr_pointed1 = *((void **)((char *)area1 + (i*sizeof(void *)))); 
855           addr_pointed2 = *((void **)((char *)area2 + (i*sizeof(void *)))); 
856           if(addr_pointed1 > s_heap && (char *)addr_pointed1 < (char*) s_heap + STD_HEAP_SIZE && addr_pointed2 > s_heap && (char *)addr_pointed2 < (char*) s_heap + STD_HEAP_SIZE)
857             res =  compare_heap_area(addr_pointed1, addr_pointed2, previous, info, other_info, type->dw_type_id, pointer_level);
858           else
859             res =  (addr_pointed1 != addr_pointed2);
860           if(res == 1)
861             return res;
862         }
863       }else{
864         addr_pointed1 = *((void **)(area1)); 
865         addr_pointed2 = *((void **)(area2));
866         if(addr_pointed1 > 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           return compare_heap_area(addr_pointed1, addr_pointed2, previous, info, other_info, type->dw_type_id, pointer_level);
868         else
869           return  (addr_pointed1 != addr_pointed2);
870       }
871     }
872     break;
873   case DW_TAG_structure_type:
874     if(type->byte_size == 0){ /*declaration of the structure, need the complete description */
875       dw_type_t full_type = xbt_dict_get_or_null(info->types_by_name, type->name);
876       if(full_type){
877         type = full_type;
878       }else{
879         type = xbt_dict_get_or_null(other_info->types_by_name, type->name);
880         switch_types = 1;
881       }
882     }
883     if(area_size != -1 && type->byte_size != area_size){
884       if(area_size>type->byte_size && area_size%type->byte_size == 0){
885         for(i=0; i<(area_size/type->byte_size); i++){
886           if(switch_types)
887             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);
888           else
889             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);
890           if(res == 1)
891             return res;
892         }
893       }else{
894         return -1;
895       }
896     }else{
897       cursor = 0;
898       xbt_dynar_foreach(type->members, cursor, member){ 
899         if(switch_types)
900           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);
901         else
902           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);
903         if(res == 1){
904           return res;
905         }
906       }
907     }
908     break;
909   case DW_TAG_union_type:
910     return compare_heap_area_without_type(real_area1, real_area2, area1, area2, previous, info, other_info, type->byte_size, check_ignore);
911     break;
912   default:
913     break;
914   }
915
916   return 0;
917
918 }
919
920 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){
921   dw_type_t type = xbt_dict_get_or_null(info->types, type_id);
922   if(type == NULL){
923     type = xbt_dict_get_or_null(other_info->types, type_id);
924     *switch_type = 1;
925   }
926   switch(type->type){
927   case DW_TAG_structure_type :
928     if(type->byte_size == 0){ /*declaration of the structure, need the complete description */
929       if(*switch_type == 0){
930         dw_type_t full_type = xbt_dict_get_or_null(info->types_by_name, type->name);
931         if(full_type){
932           type = full_type;
933         }else{
934           type = xbt_dict_get_or_null(other_info->types_by_name, type->name);
935           *switch_type = 1;
936         }
937       }else{
938         dw_type_t full_type = xbt_dict_get_or_null(other_info->types_by_name, type->name);
939         if(full_type){
940           type = full_type;
941         }else{
942           type = xbt_dict_get_or_null(info->types_by_name, type->name);
943           *switch_type = 0;
944         }
945       }
946     
947     }
948     if(area_size != -1 && type->byte_size != area_size){
949       if(area_size>type->byte_size && area_size%type->byte_size == 0)
950         return type_id;
951       else
952         return NULL;
953     }else{
954       unsigned int cursor = 0;
955       dw_type_t member;
956       xbt_dynar_foreach(type->members, cursor, member){ 
957         if(member->offset == offset)
958           return member->dw_type_id;
959       }
960       return NULL;
961     }
962     break;
963   default:
964     /* FIXME : other cases ? */
965     return NULL;
966     break;
967   }
968 }
969
970 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){
971
972   int res_compare;
973   ssize_t block1, frag1, block2, frag2;
974   ssize_t size;
975   int check_ignore = 0;
976
977   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2, *real_addr_block1, *real_addr_block2,  *real_addr_frag1, *real_addr_frag2;
978   void *area1_to_compare, *area2_to_compare;
979   dw_type_t type = NULL;
980   int type_size = -1;
981   int offset1 =0, offset2 = 0;
982   int new_size1 = -1, new_size2 = -1;
983   char *new_type_id1 = NULL, *new_type_id2 = NULL;
984   int switch_type = 0;
985
986   int match_pairs = 0;
987
988   if(previous == NULL){
989     previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
990     match_pairs = 1;
991   }
992
993   block1 = ((char*)area1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
994   block2 = ((char*)area2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
995
996   if(is_block_stack((int)block1) && is_block_stack((int)block2)){
997     add_heap_area_pair(previous, block1, -1, block2, -1);
998     if(match_pairs){
999       match_equals(previous);
1000       xbt_dynar_free(&previous);
1001     }
1002     return 0;
1003   }
1004
1005   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)){
1006     if(match_pairs){
1007       xbt_dynar_free(&previous);
1008     }
1009     return 1;
1010   }
1011
1012   addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)heapbase1));
1013   addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)heapbase2));
1014
1015   real_addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
1016   real_addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
1017
1018   if(type_id){
1019     type = xbt_dict_get_or_null(info->types, type_id);
1020     if(type->byte_size == 0){
1021       if(type->subtype == NULL){
1022         dw_type_t full_type = xbt_dict_get_or_null(info->types_by_name, type->name);
1023         if(full_type)
1024           type = full_type;
1025         else
1026           type = xbt_dict_get_or_null(other_info->types_by_name, type->name);
1027       }else{
1028         type = type->subtype;
1029       }
1030     }
1031     if((type->byte_size == DW_TAG_pointer_type) || ((type->type == DW_TAG_base_type) && type->name!=NULL && (!strcmp(type->name, "char"))))
1032       type_size = -1;
1033     else
1034       type_size = type->byte_size;
1035   }
1036   
1037   if((heapinfo1[block1].type == -1) && (heapinfo2[block2].type == -1)){  /* Free block */
1038
1039     if(match_pairs){
1040       match_equals(previous);
1041       xbt_dynar_free(&previous);
1042     }
1043     return 0;
1044
1045   }else if((heapinfo1[block1].type == 0) && (heapinfo2[block2].type == 0)){ /* Complete block */ 
1046     
1047     if(equals_to1[block1][0] != NULL && equals_to2[block2][0] != NULL){
1048       if(equal_blocks(block1, block2)){
1049         if(match_pairs){
1050           match_equals(previous);
1051           xbt_dynar_free(&previous);
1052         }
1053         return 0;
1054       }
1055     }
1056
1057     if(type_size != -1){
1058       if(type_size != heapinfo1[block1].busy_block.busy_size && type_size != heapinfo2[block2].busy_block.busy_size && type->name!=NULL && !strcmp(type->name, "s_smx_context")){
1059         if(match_pairs){
1060           match_equals(previous);
1061           xbt_dynar_free(&previous);
1062         }
1063         return -1;
1064       }
1065     }
1066
1067     if(heapinfo1[block1].busy_block.size != heapinfo2[block2].busy_block.size){
1068       if(match_pairs){
1069         xbt_dynar_free(&previous);
1070       }
1071       return 1;
1072     }
1073
1074     if(heapinfo1[block1].busy_block.busy_size != heapinfo2[block2].busy_block.busy_size){
1075       if(match_pairs){
1076         xbt_dynar_free(&previous);
1077       }
1078       return 1;
1079     }
1080
1081     if(!add_heap_area_pair(previous, block1, -1, block2, -1)){
1082       if(match_pairs){
1083         match_equals(previous);
1084         xbt_dynar_free(&previous);
1085       }
1086       return 0;
1087     }
1088  
1089     size = heapinfo1[block1].busy_block.busy_size;
1090     
1091     if(type_id != NULL){
1092       xbt_free(types1[block1][0]);
1093       xbt_free(types2[block2][0]);
1094       types1[block1][0] = strdup(type_id);
1095       types2[block2][0] = strdup(type_id);
1096     }
1097
1098     if(size <= 0){
1099       if(match_pairs){
1100         match_equals(previous);
1101         xbt_dynar_free(&previous);
1102       }
1103       return 0;
1104     }
1105
1106     frag1 = -1;
1107     frag2 = -1;
1108
1109     area1_to_compare = addr_block1;
1110     area2_to_compare = addr_block2;
1111
1112     if((heapinfo1[block1].busy_block.ignore > 0) && (heapinfo2[block2].busy_block.ignore == heapinfo1[block1].busy_block.ignore))
1113       check_ignore = heapinfo1[block1].busy_block.ignore;
1114       
1115   }else if((heapinfo1[block1].type > 0) && (heapinfo2[block2].type > 0)){ /* Fragmented block */
1116
1117     frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
1118     frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
1119
1120     addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
1121     addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
1122
1123     real_addr_frag1 = (void*) ((char *)real_addr_block1 + (frag1 << ((xbt_mheap_t)s_heap)->heapinfo[block1].type));
1124     real_addr_frag2 = (void*) ((char *)real_addr_block2 + (frag2 << ((xbt_mheap_t)s_heap)->heapinfo[block2].type));
1125
1126     if(type_size != -1){
1127       if(heapinfo1[block1].busy_frag.frag_size[frag1] == -1 || heapinfo2[block2].busy_frag.frag_size[frag2] == -1){
1128         if(match_pairs){
1129           match_equals(previous);
1130           xbt_dynar_free(&previous);
1131         }
1132         return -1;
1133       }
1134       if(type_size != heapinfo1[block1].busy_frag.frag_size[frag1] || type_size !=  heapinfo2[block2].busy_frag.frag_size[frag2]){
1135         if(match_pairs){
1136           match_equals(previous);
1137           xbt_dynar_free(&previous);
1138         }
1139         return -1;
1140       }
1141     }
1142
1143     if(equals_to1[block1][frag1] != NULL && equals_to2[block2][frag2] != NULL){
1144       if(equal_fragments(block1, frag1, block2, frag2)){
1145         if(match_pairs){
1146           match_equals(previous);
1147           xbt_dynar_free(&previous);
1148         }
1149         return 0;
1150       }
1151     }
1152
1153     if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2]){
1154       if(type_size == -1){
1155          if(match_pairs){
1156           match_equals(previous);
1157           xbt_dynar_free(&previous);
1158         }
1159         return -1;
1160       }else{
1161         if(match_pairs){
1162           xbt_dynar_free(&previous);
1163         }
1164         return 1;
1165       }
1166     }
1167       
1168     size = heapinfo1[block1].busy_frag.frag_size[frag1];
1169
1170     if(type_id != NULL){
1171       xbt_free(types1[block1][frag1]);
1172       xbt_free(types2[block2][frag2]);
1173       types1[block1][frag1] = strdup(type_id);
1174       types2[block2][frag2] = strdup(type_id);
1175     }
1176
1177     if(real_addr_frag1 != area1 || real_addr_frag2 != area2){
1178       offset1 = (char *)area1 - (char *)real_addr_frag1;
1179       offset2 = (char *)area2 - (char *)real_addr_frag2;
1180       if(types1[block1][frag1] != NULL && types2[block2][frag2] != NULL){
1181         new_type_id1 = get_offset_type(types1[block1][frag1], offset1, info, other_info, size, &switch_type);
1182         new_type_id2 = get_offset_type(types2[block2][frag2], offset1, info, other_info, size, &switch_type);
1183       }else if(types1[block1][frag1] != NULL){
1184         new_type_id1 = get_offset_type(types1[block1][frag1], offset1, info, other_info, size, &switch_type);
1185         new_type_id2 = get_offset_type(types1[block1][frag1], offset2, info, other_info, size, &switch_type);
1186       }else if(types2[block2][frag2] != NULL){
1187         new_type_id1 = get_offset_type(types2[block2][frag2], offset1, info, other_info, size, &switch_type);
1188         new_type_id2 = get_offset_type(types2[block2][frag2], offset2, info, other_info, size, &switch_type);
1189       }else{
1190         if(match_pairs){
1191           match_equals(previous);
1192           xbt_dynar_free(&previous);
1193         }
1194         return -1;
1195       }   
1196
1197       if(new_type_id1 !=  NULL && new_type_id2 !=  NULL && !strcmp(new_type_id1, new_type_id2)){
1198         if(switch_type){
1199           type = xbt_dict_get_or_null(other_info->types, new_type_id1);
1200           while(type->byte_size == 0 && type->dw_type_id != NULL)
1201             type = xbt_dict_get_or_null(other_info->types, type->dw_type_id);
1202           new_size1 = type->byte_size;
1203           type = xbt_dict_get_or_null(other_info->types, new_type_id2);
1204           while(type->byte_size == 0 && type->dw_type_id != NULL)
1205             type = xbt_dict_get_or_null(other_info->types, type->dw_type_id);
1206           new_size2 = type->byte_size;
1207         }else{
1208           type = xbt_dict_get_or_null(info->types, new_type_id1);
1209           while(type->byte_size == 0 && type->dw_type_id != NULL)
1210             type = xbt_dict_get_or_null(info->types, type->dw_type_id);
1211           new_size1 = type->byte_size;
1212           type = xbt_dict_get_or_null(info->types, new_type_id2);
1213           while(type->byte_size == 0 && type->dw_type_id != NULL)
1214             type = xbt_dict_get_or_null(info->types, type->dw_type_id);
1215           new_size2 = type->byte_size;
1216         }
1217       }else{
1218         if(match_pairs){
1219           match_equals(previous);
1220           xbt_dynar_free(&previous);
1221         }
1222         return -1;
1223       }
1224     }
1225
1226     area1_to_compare = (char *)addr_frag1 + offset1;
1227     area2_to_compare = (char *)addr_frag2 + offset2;
1228     
1229     if(new_size1 > 0 && new_size1 == new_size2){
1230       type_id = new_type_id1;
1231       size = new_size1;
1232     }
1233
1234     if(offset1 == 0 && offset2 == 0){
1235       if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
1236         if(match_pairs){
1237           match_equals(previous);
1238           xbt_dynar_free(&previous);
1239         }
1240         return 0;
1241       }
1242     }
1243
1244     if(size <= 0){
1245       if(match_pairs){
1246         match_equals(previous);
1247         xbt_dynar_free(&previous);
1248       }
1249       return 0;
1250     }
1251       
1252     if((heapinfo1[block1].busy_frag.ignore[frag1] > 0) && ( heapinfo2[block2].busy_frag.ignore[frag2] == heapinfo1[block1].busy_frag.ignore[frag1]))
1253       check_ignore = heapinfo1[block1].busy_frag.ignore[frag1];
1254     
1255   }else{
1256
1257     if(match_pairs){
1258       xbt_dynar_free(&previous);
1259     }
1260     return 1;
1261
1262   }
1263   
1264
1265   /* Start comparison*/
1266   if(type_id != NULL){
1267     if(switch_type)
1268       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);
1269     else
1270       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);
1271     if(res_compare == 1){
1272       if(match_pairs)
1273         xbt_dynar_free(&previous);
1274       return res_compare;
1275     }
1276   }else{
1277     if(switch_type)
1278       res_compare = compare_heap_area_without_type(area1, area2, area1_to_compare, area2_to_compare, previous, other_info, info, size, check_ignore);
1279     else
1280       res_compare = compare_heap_area_without_type(area1, area2, area1_to_compare, area2_to_compare, previous, info, other_info, size, check_ignore);
1281     if(res_compare == 1){
1282       if(match_pairs)
1283         xbt_dynar_free(&previous);
1284       return res_compare;
1285     }
1286   }
1287
1288   if(match_pairs){
1289     match_equals(previous);
1290     xbt_dynar_free(&previous);
1291   }
1292
1293   return 0;
1294 }
1295
1296 /*********************************************** Miscellaneous ***************************************************/
1297 /****************************************************************************************************************/
1298
1299
1300 int get_pointed_area_size(void *area, int heap){
1301
1302   int block, frag;
1303   malloc_info *heapinfo;
1304
1305   if(heap == 1)
1306     heapinfo = heapinfo1;
1307   else
1308     heapinfo = heapinfo2;
1309
1310   block = ((char*)area - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
1311
1312   if(((char *)area < (char*)((xbt_mheap_t)s_heap)->heapbase)  || (block > heapsize1) || (block < 1))
1313     return -1;
1314
1315   if(heapinfo[block].type == -1){ /* Free block */
1316     return -1;  
1317   }else if(heapinfo[block].type == 0){ /* Complete block */
1318     return (int)heapinfo[block].busy_block.busy_size;
1319   }else{
1320     frag = ((uintptr_t) (ADDR2UINT (area) % (BLOCKSIZE))) >> heapinfo[block].type;
1321     return (int)heapinfo[block].busy_frag.frag_size[frag];
1322   }
1323
1324 }
1325
1326 char *get_type_description(mc_object_info_t info, char *type_name){
1327
1328   xbt_dict_cursor_t dict_cursor;
1329   char *type_origin;
1330   dw_type_t type;
1331
1332   xbt_dict_foreach(info->types, dict_cursor, type_origin, type){
1333     if(type->name && (strcmp(type->name, type_name) == 0) && type->byte_size > 0){
1334       xbt_dict_cursor_free(&dict_cursor);
1335       return type_origin;
1336     }
1337   }
1338
1339   xbt_dict_cursor_free(&dict_cursor);
1340   return NULL;
1341 }
1342
1343
1344 #ifndef max
1345 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
1346 #endif
1347
1348 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
1349
1350   if(heap1 == NULL && heap1 == NULL){
1351     XBT_DEBUG("Malloc descriptors null");
1352     return 0;
1353   }
1354
1355   if(heap1->heaplimit != heap2->heaplimit){
1356     XBT_DEBUG("Different limit of valid info table indices");
1357     return 1;
1358   }
1359
1360   /* Heap information */
1361   heaplimit = ((struct mdesc *)heap1)->heaplimit;
1362
1363   s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1364
1365   heapbase1 = (char *)heap1 + BLOCKSIZE;
1366   heapbase2 = (char *)heap2 + BLOCKSIZE;
1367
1368   heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
1369   heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
1370
1371   heapsize1 = heap1->heapsize;
1372   heapsize2 = heap2->heapsize;
1373
1374   /* Start comparison */
1375   size_t i, j, k;
1376   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
1377
1378   int distance = 0;
1379
1380   /* Check busy blocks*/
1381
1382   i = 1;
1383
1384   while(i <= heaplimit){
1385
1386     addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
1387     addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
1388
1389     if(heapinfo1[i].type != heapinfo2[i].type){
1390   
1391       distance += BLOCKSIZE;
1392       XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
1393       i++;
1394     
1395     }else{
1396
1397       if(heapinfo1[i].type == -1){ /* Free block */
1398         i++;
1399         continue;
1400       }
1401
1402       if(heapinfo1[i].type == 0){ /* Large block */
1403        
1404         if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
1405           distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1406           i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1407           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);
1408           continue;
1409         }
1410
1411         /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
1412           distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
1413           i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1414           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);
1415           continue;
1416           }*/
1417
1418         k = 0;
1419
1420         //while(k < (heapinfo1[i].busy_block.busy_size)){
1421         while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
1422           if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
1423             distance ++;
1424           }
1425           k++;
1426         } 
1427
1428         i++;
1429
1430       }else { /* Fragmented block */
1431
1432         for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
1433
1434           addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
1435           addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
1436
1437           if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
1438             continue;
1439           }
1440           
1441           
1442           /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
1443             distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
1444             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); 
1445             continue;
1446             }*/
1447    
1448           k=0;
1449
1450           //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
1451           while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
1452             if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
1453               distance ++;
1454             }
1455             k++;
1456           }
1457
1458         }
1459
1460         i++;
1461
1462       }
1463       
1464     }
1465
1466   }
1467
1468   return distance;
1469   
1470 }
1471