Logo AND Algorithmique Numérique Distribuée

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