Logo AND Algorithmique Numérique Distribuée

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