Logo AND Algorithmique Numérique Distribuée

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