Logo AND Algorithmique Numérique Distribuée

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