Logo AND Algorithmique Numérique Distribuée

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