Logo AND Algorithmique Numérique Distribuée

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