Logo AND Algorithmique Numérique Distribuée

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