Logo AND Algorithmique Numérique Distribuée

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