Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7618035b3b59e1e4cefa356745d5493d5b200a6b
[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   }
376
377   free(equals_to1);
378   free(equals_to2);
379
380   s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
381   heapinfo1 = NULL, heapinfo2 = NULL;
382   heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
383   to_ignore1 = NULL, to_ignore2 = NULL;
384   equals_to1 = NULL, equals_to2 = NULL;
385
386 }
387
388 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
389
390   if(heap1 == NULL && heap2 == NULL){
391     XBT_DEBUG("Malloc descriptors null");
392     return 0;
393   }
394
395   /* Start comparison */
396   size_t i1, i2, j1, j2, k;
397   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
398   int nb_diff1 = 0, nb_diff2 = 0;
399
400   xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
401
402   int equal, res_compare = 0;
403
404   /* Check busy blocks*/
405
406   i1 = 1;
407
408   while(i1 <= heaplimit){
409
410     if(heapinfo1[i1].type == -1){ /* Free block */
411       i1++;
412       continue;
413     }
414
415     addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
416
417     if(heapinfo1[i1].type == 0){  /* Large block */
418       
419       if(is_stack(addr_block1)){
420         for(k=0; k < heapinfo1[i1].busy_block.size; k++)
421           equals_to1[i1+k][0] = new_heap_area(i1, -1);
422         for(k=0; k < heapinfo2[i1].busy_block.size; k++)
423           equals_to2[i1+k][0] = new_heap_area(i1, -1);
424         i1 += heapinfo1[i1].busy_block.size;
425         continue;
426       }
427
428       if(equals_to1[i1][0] != NULL){
429         i1++;
430         continue;
431       }
432     
433       i2 = 1;
434       equal = 0;
435       res_compare = 0;
436   
437       /* Try first to associate to same block in the other heap */
438       if(heapinfo2[i1].type == heapinfo1[i1].type){
439
440         if(equals_to2[i1][0] == NULL){
441
442           addr_block2 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
443         
444           res_compare = compare_heap_area(addr_block1, addr_block2, NULL, NULL, NULL, NULL, 0);
445         
446           if(res_compare == 0){
447             for(k=1; k < heapinfo2[i1].busy_block.size; k++)
448               equals_to2[i1+k][0] = new_heap_area(i1, -1);
449             for(k=1; k < heapinfo1[i1].busy_block.size; k++)
450               equals_to1[i1+k][0] = new_heap_area(i1, -1);
451             equal = 1;
452             i1 += heapinfo1[i1].busy_block.size;
453           }
454         
455           xbt_dynar_reset(previous);
456         
457         }
458         
459       }
460
461       while(i2 <= heaplimit && !equal){
462
463         addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));        
464            
465         if(i2 == i1){
466           i2++;
467           continue;
468         }
469
470         if(heapinfo2[i2].type != 0){
471           i2++;
472           continue;
473         }
474     
475         if(equals_to2[i2][0] != NULL){  
476           i2++;
477           continue;
478         }
479           
480         res_compare = compare_heap_area(addr_block1, addr_block2, NULL, NULL, NULL, NULL, 0);
481         
482         if(res_compare == 0){
483           for(k=1; k < heapinfo2[i2].busy_block.size; k++)
484             equals_to2[i2+k][0] = new_heap_area(i1, -1);
485           for(k=1; k < heapinfo1[i1].busy_block.size; k++)
486             equals_to1[i1+k][0] = new_heap_area(i2, -1);
487           equal = 1;
488           i1 += heapinfo1[i1].busy_block.size;
489         }
490
491         xbt_dynar_reset(previous);
492
493         i2++;
494
495       }
496
497       if(!equal){
498         XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1[i1].busy_block.busy_size, addr_block1);
499         i1 = heaplimit + 1;
500         nb_diff1++;
501           //i1++;
502       }
503       
504     }else{ /* Fragmented block */
505
506       for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
507
508         if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
509           continue;
510
511         if(equals_to1[i1][j1] != NULL)
512           continue;
513
514         addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
515
516         i2 = 1;
517         equal = 0;
518         
519         /* Try first to associate to same fragment in the other heap */
520         if(heapinfo2[i1].type == heapinfo1[i1].type){
521
522           if(equals_to2[i1][j1] == NULL){
523
524             addr_block2 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
525             addr_frag2 = (void*) ((char *)addr_block2 + (j1 << ((xbt_mheap_t)s_heap)->heapinfo[i1].type));
526
527             res_compare = compare_heap_area(addr_frag1, addr_frag2, NULL, NULL, NULL, NULL, 0);
528
529             if(res_compare == 0)
530               equal = 1;
531         
532             xbt_dynar_reset(previous);
533
534           }
535
536         }
537
538         while(i2 <= heaplimit && !equal){
539
540           if(heapinfo2[i2].type <= 0){
541             i2++;
542             continue;
543           }
544
545           for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
546
547             if(i2 == i1 && j2 == j1)
548               continue;
549            
550             if(equals_to2[i2][j2] != NULL)
551               continue;
552                           
553             addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
554             addr_frag2 = (void*) ((char *)addr_block2 + (j2 <<((xbt_mheap_t)s_heap)->heapinfo[i2].type));
555
556             res_compare = compare_heap_area(addr_frag1, addr_frag2, NULL, NULL, NULL, NULL, 0);
557             
558             if(res_compare == 0){
559               equal = 1;
560               xbt_dynar_reset(previous);
561               break;
562             }
563
564             xbt_dynar_reset(previous);
565
566           }
567
568           i2++;
569
570         }
571
572         if(!equal){
573           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);
574           i2 = heaplimit + 1;
575           i1 = heaplimit + 1;
576           nb_diff1++;
577           break;
578         }
579
580       }
581
582       i1++;
583       
584     }
585
586   }
587
588   /* All blocks/fragments are equal to another block/fragment ? */
589   size_t i = 1, j = 0;
590   void *real_addr_frag1 = NULL, *real_addr_block1 = NULL, *real_addr_block2 = NULL, *real_addr_frag2 = NULL;
591  
592   while(i<=heaplimit){
593     if(heapinfo1[i].type == 0){
594       if(i1 == heaplimit){
595         if(heapinfo1[i].busy_block.busy_size > 0){
596           if(equals_to1[i][0] == NULL){            
597             if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
598               addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
599               XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
600               //mmalloc_backtrace_block_display((void*)heapinfo1, i);
601             }
602             nb_diff1++;
603           }
604         }
605       }
606     }
607     if(heapinfo1[i].type > 0){
608       addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
609       real_addr_block1 =  ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)s_heap)->heapbase));
610       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
611         if(i1== heaplimit){
612           if(heapinfo1[i].busy_frag.frag_size[j] > 0){
613             if(equals_to1[i][j] == NULL){
614               if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
615                 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
616                 real_addr_frag1 = (void*) ((char *)real_addr_block1 + (j << ((struct mdesc *)s_heap)->heapinfo[i].type));
617                 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]);
618                 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
619               }
620               nb_diff1++;
621             }
622           }
623         }
624       }
625     }
626     i++; 
627   }
628
629   if(i1 == heaplimit)
630     XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
631
632   i = 1;
633
634   while(i<=heaplimit){
635     if(heapinfo2[i].type == 0){
636       if(i1 == heaplimit){
637         if(heapinfo2[i].busy_block.busy_size > 0){
638           if(equals_to2[i][0] == NULL){
639             if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
640               addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
641               XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
642               //mmalloc_backtrace_block_display((void*)heapinfo2, i);
643             }
644             nb_diff2++;
645           }
646         }
647       }
648     }
649     if(heapinfo2[i].type > 0){
650       addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
651       real_addr_block2 =  ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)s_heap)->heapbase));
652       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
653         if(i1 == heaplimit){
654           if(heapinfo2[i].busy_frag.frag_size[j] > 0){
655             if(equals_to2[i][j] == NULL){
656               if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
657                 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
658                 real_addr_frag2 = (void*) ((char *)real_addr_block2 + (j << ((struct mdesc *)s_heap)->heapinfo[i].type));
659                 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]);
660                 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
661               }
662               nb_diff2++;
663             }
664           }
665         }
666       }
667     }
668     i++; 
669   }
670
671   if(i1 == heaplimit)
672     XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
673
674   xbt_dynar_free(&previous);
675   real_addr_frag1 = NULL, real_addr_block1 = NULL, real_addr_block2 = NULL, real_addr_frag2 = NULL;
676
677   return ((nb_diff1 > 0) || (nb_diff2 > 0));
678 }
679
680 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){
681
682   int i = 0;
683   void *addr_pointed1, *addr_pointed2;
684   int pointer_align, ignore1, ignore2, res_compare;
685
686   while(i<size){
687
688     if(check_ignore > 0){
689       if((ignore1 = heap_comparison_ignore_size(to_ignore1, (char *)real_area1 + i)) > 0){
690         if((ignore2 = heap_comparison_ignore_size(to_ignore2, (char *)real_area2 + i))  == ignore1){
691           i = i + ignore2;
692           check_ignore--;
693           continue;
694         }
695       }
696     }
697
698     if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
699
700       pointer_align = (i / sizeof(void*)) * sizeof(void*);
701       addr_pointed1 = *((void **)((char *)area1 + pointer_align));
702       addr_pointed2 = *((void **)((char *)area2 + pointer_align));
703       
704       if(addr_pointed1 > maestro_stack_start && addr_pointed1 < maestro_stack_end && addr_pointed2 > maestro_stack_start && addr_pointed2 < maestro_stack_end){
705         i = pointer_align + sizeof(void *);
706         continue;
707       }else if((addr_pointed1 > s_heap) && ((char *)addr_pointed1 < (char *)s_heap + STD_HEAP_SIZE) 
708                && (addr_pointed2 > s_heap) && ((char *)addr_pointed2 < (char *)s_heap + STD_HEAP_SIZE)){
709         res_compare = compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, NULL, 0); 
710         if(res_compare == 1)
711           return res_compare;
712         i = pointer_align + sizeof(void *);
713         continue;
714       }else{
715         return 1;
716       }
717       
718     }
719     
720     i++;
721
722   }
723
724   return 0;
725  
726 }
727
728
729 static int compare_heap_area_with_type(void *real_area1, void *real_area2, void *area1, void *area2, 
730                                        xbt_dynar_t previous, xbt_dict_t all_types, xbt_dict_t other_types, char *type_id, 
731                                        int area_size, int check_ignore, int pointer_level){
732
733   if(is_stack(real_area1) && is_stack(real_area2))
734     return 0;
735
736   size_t ignore1, ignore2;
737
738   if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2))  == ignore1))
739     return 0;
740   
741   dw_type_t type = xbt_dict_get_or_null(all_types, type_id);
742   dw_type_t subtype, subsubtype;
743   int res, elm_size, i, switch_types = 0;
744   unsigned int cursor = 0;
745   dw_type_t member;
746   void *addr_pointed1, *addr_pointed2;;
747   char *type_desc;
748
749   switch(type->type){
750   case e_dw_base_type:
751     if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2))  == ignore1))
752       return 0;
753     if(strcmp(type->name, "char") == 0){ /* String, hence random (arbitrary ?) size */
754       if(real_area1 == real_area2)
755         return -1;
756       else
757         return (memcmp(area1, area2, area_size) != 0);
758     }else{
759       if(area_size != -1 && type->size != area_size)
760         return -1;
761       else
762         return (memcmp(area1, area2, type->size) != 0);
763     }
764     break;
765   case e_dw_enumeration_type:
766     if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2))  == ignore1))
767       return 0;
768     if(area_size != -1 && type->size != area_size)
769       return -1;
770     else
771       return (memcmp(area1, area2, type->size) != 0);
772     break;
773   case e_dw_typedef:
774     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);
775     break;
776   case e_dw_const_type:
777     return 0;
778     break;
779   case e_dw_array_type:
780     subtype = xbt_dict_get_or_null(all_types, type->dw_type_id);
781     switch(subtype->type){
782     case e_dw_base_type:
783     case e_dw_enumeration_type:
784     case e_dw_pointer_type:
785     case e_dw_structure_type:
786     case e_dw_union_type:
787       if(subtype->size == 0){ /*declaration of the type, need the complete description */
788         type_desc = get_type_description(all_types, subtype->name);
789         if(type_desc){
790           subtype = xbt_dict_get_or_null(all_types, type_desc);
791         }else{
792           subtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subtype->name));
793           switch_types = 1;
794         }
795       }
796       elm_size = subtype->size;
797       break;
798     case e_dw_typedef:
799     case e_dw_volatile_type:
800       subsubtype = xbt_dict_get_or_null(all_types, subtype->dw_type_id);
801       if(subsubtype->size == 0){ /*declaration of the type, need the complete description */
802         type_desc = get_type_description(all_types, subsubtype->name);
803         if(type_desc){
804           subsubtype = xbt_dict_get_or_null(all_types, type_desc);
805         }else{
806           subsubtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subtype->name));
807           switch_types = 1;
808         }
809       }
810       elm_size = subsubtype->size;
811       break;
812     default : 
813       return 0;
814       break;
815     }
816     for(i=0; i<type->size; i++){
817       if(switch_types)
818         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);
819       else
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, all_types, other_types, type->dw_type_id, type->size, check_ignore, pointer_level);
821       if(res == 1)
822         return res;
823     }
824     break;
825   case e_dw_pointer_type:
826     if(type->dw_type_id && ((dw_type_t)xbt_dict_get_or_null(all_types, type->dw_type_id))->type == e_dw_subroutine_type){
827       addr_pointed1 = *((void **)(area1)); 
828       addr_pointed2 = *((void **)(area2));
829       return (addr_pointed1 != addr_pointed2);;
830     }else{
831       pointer_level++;
832       if(pointer_level > 1){ /* Array of pointers */
833         for(i=0; i<(area_size/sizeof(void *)); i++){
834           addr_pointed1 = *((void **)((char *)area1 + (i*sizeof(void *)))); 
835           addr_pointed2 = *((void **)((char *)area2 + (i*sizeof(void *)))); 
836           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)
837             res =  compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, type->dw_type_id, pointer_level); 
838           else
839             res =  (addr_pointed1 != addr_pointed2);
840           if(res == 1)
841             return res;
842         }
843       }else{
844         addr_pointed1 = *((void **)(area1)); 
845         addr_pointed2 = *((void **)(area2));
846         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)
847           return compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, type->dw_type_id, pointer_level); 
848         else
849           return  (addr_pointed1 != addr_pointed2);
850       }
851     }
852     break;
853   case e_dw_structure_type:
854     if(type->size == 0){ /*declaration of the structure, need the complete description */
855       type_desc = get_type_description(all_types, type->name);
856       if(type_desc){
857         type = xbt_dict_get_or_null(all_types, type_desc);
858       }else{
859         type = xbt_dict_get_or_null(other_types, get_type_description(other_types, type->name));
860         switch_types = 1;
861       }
862     }
863     if(area_size != -1 && type->size != area_size){
864       if(area_size>type->size && area_size%type->size == 0){
865         for(i=0; i<(area_size/type->size); i++){
866           if(switch_types)
867             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); 
868           else
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, all_types, other_types, type_id, -1, check_ignore, 0); 
870           if(res == 1)
871             return res;
872         }
873       }else{
874         return -1;
875       }
876     }else{
877       cursor = 0;
878       xbt_dynar_foreach(type->members, cursor, member){
879         if(switch_types)
880           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);
881         else
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, all_types, other_types, member->dw_type_id, -1, check_ignore, 0);  
883         if(res == 1)
884           return res;
885       }
886     }
887     break;
888   case e_dw_union_type:
889     if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2))  == ignore1))
890       return 0;
891     else
892       return compare_heap_area_without_type(real_area1, real_area2, area1, area2, previous, all_types, other_types, type->size, check_ignore);
893     break;
894   case e_dw_volatile_type:
895     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);
896     break;
897   default:
898     break;
899   }
900
901   return 0;
902
903 }
904
905 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){
906
907   int res_compare;
908   ssize_t block1, frag1, block2, frag2;
909   ssize_t size;
910   int check_ignore = 0;
911
912   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
913   void *area1_to_compare, *area2_to_compare;
914   dw_type_t type = NULL;
915   char *type_desc;
916   int type_size = -1;
917
918   int match_pairs = 0;
919
920   if(previous == NULL){
921     previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
922     match_pairs = 1;
923   }
924
925   block1 = ((char*)area1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
926   block2 = ((char*)area2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
927
928   if(is_block_stack((int)block1) && is_block_stack((int)block2)){
929     add_heap_area_pair(previous, block1, -1, block2, -1);
930     if(match_pairs){
931       match_equals(previous);
932       xbt_dynar_free(&previous);
933     }
934     return 0;
935   }
936
937   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)){
938     if(match_pairs){
939       xbt_dynar_free(&previous);
940     }
941     return 1;
942   }
943
944   addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)heapbase1));
945   addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)heapbase2));
946
947   if(type_id){
948     type = xbt_dict_get_or_null(all_types, type_id);
949     if(type->size == 0){
950       if(type->dw_type_id == NULL){
951         type_desc = get_type_description(all_types, type->name);
952         if(type_desc)
953           type = xbt_dict_get_or_null(all_types, type_desc);
954         else
955           type = xbt_dict_get_or_null(other_types, get_type_description(other_types, type->name));
956       }else{
957         type = xbt_dict_get_or_null(all_types, type->dw_type_id);
958       }
959     }
960     if((type->type == e_dw_pointer_type) || ((type->type == e_dw_base_type) && (!strcmp(type->name, "char"))))
961       type_size = -1;
962     else
963       type_size = type->size;
964   }
965   
966   if((heapinfo1[block1].type == -1) && (heapinfo2[block2].type == -1)){  /* Free block */
967
968     if(match_pairs){
969       match_equals(previous);
970       xbt_dynar_free(&previous);
971     }
972     return 0;
973
974   }else if((heapinfo1[block1].type == 0) && (heapinfo2[block2].type == 0)){ /* Complete block */
975
976     if(equals_to1[block1][0] != NULL && equals_to2[block2][0] != NULL){
977       if(equal_blocks(block1, block2)){
978         if(match_pairs){
979           match_equals(previous);
980           xbt_dynar_free(&previous);
981         }
982         return 0;
983       }
984     }
985
986     if(type_size != -1){
987       if(type_size != heapinfo1[block1].busy_block.busy_size && type_size != heapinfo2[block2].busy_block.busy_size && strcmp(type->name, "s_smx_context") != 0)
988         return -1;
989     }
990
991     if(heapinfo1[block1].busy_block.size != heapinfo2[block2].busy_block.size){
992       if(match_pairs){
993         xbt_dynar_free(&previous);
994       }
995       return 1;
996     }
997
998     if(heapinfo1[block1].busy_block.busy_size != heapinfo2[block2].busy_block.busy_size){
999       if(match_pairs){
1000         xbt_dynar_free(&previous);
1001       }
1002       return 1;
1003     }
1004
1005     if(!add_heap_area_pair(previous, block1, -1, block2, -1)){
1006       if(match_pairs){
1007         match_equals(previous);
1008         xbt_dynar_free(&previous);
1009       }
1010       return 0;
1011     }
1012  
1013     size = heapinfo1[block1].busy_block.busy_size;
1014
1015     if(size <= 0){
1016       if(match_pairs){
1017         match_equals(previous);
1018         xbt_dynar_free(&previous);
1019       }
1020       return 0;
1021     }
1022
1023     frag1 = -1;
1024     frag2 = -1;
1025
1026     area1_to_compare = addr_block1;
1027     area2_to_compare = addr_block2;
1028
1029     if((heapinfo1[block1].busy_block.ignore > 0) && (heapinfo2[block2].busy_block.ignore == heapinfo1[block1].busy_block.ignore))
1030       check_ignore = heapinfo1[block1].busy_block.ignore;
1031       
1032   }else if((heapinfo1[block1].type > 0) && (heapinfo2[block2].type > 0)){ /* Fragmented block */
1033
1034     frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
1035     frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
1036       
1037     addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
1038     addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
1039       
1040     area1_to_compare = addr_frag1;
1041     area2_to_compare = addr_frag2;
1042
1043     if(type_size != -1){
1044       if(heapinfo1[block1].busy_frag.frag_size[frag1] == -1 || heapinfo2[block2].busy_frag.frag_size[frag2] == -1)
1045         return -1;
1046       if(type_size != heapinfo1[block1].busy_frag.frag_size[frag1] && type_size !=  heapinfo2[block2].busy_frag.frag_size[frag2])
1047         return -1;
1048     }
1049
1050     if(equals_to1[block1][frag1] != NULL && equals_to2[block2][frag2] != NULL){
1051       if(equal_fragments(block1, frag1, block2, frag2)){
1052         if(match_pairs){
1053           match_equals(previous);
1054           xbt_dynar_free(&previous);
1055         }
1056         return 0;
1057       }
1058     }
1059
1060     if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2]){
1061       if(type_size == -1){
1062         return -1;
1063       }else{
1064         if(match_pairs){
1065           xbt_dynar_free(&previous);
1066         }
1067         return 1;
1068       }
1069     }
1070       
1071     if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
1072       if(match_pairs){
1073         match_equals(previous);
1074         xbt_dynar_free(&previous);
1075       }
1076       return 0;
1077     }
1078
1079     size = heapinfo1[block1].busy_frag.frag_size[frag1];
1080
1081     if(size <= 0){
1082       if(match_pairs){
1083         match_equals(previous);
1084         xbt_dynar_free(&previous);
1085       }
1086       return 0;
1087     }
1088       
1089     if((heapinfo1[block1].busy_frag.ignore[frag1] > 0) && ( heapinfo2[block2].busy_frag.ignore[frag2] == heapinfo1[block1].busy_frag.ignore[frag1]))
1090       check_ignore = heapinfo1[block1].busy_frag.ignore[frag1];
1091     
1092   }else{
1093
1094     if(match_pairs){
1095       xbt_dynar_free(&previous);
1096     }
1097     return 1;
1098
1099   }
1100   
1101
1102   /* Start comparison*/
1103   if(type_id != NULL){
1104     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);
1105     if(res_compare != 0){
1106       if(match_pairs)
1107         xbt_dynar_free(&previous);
1108       return res_compare;
1109     }
1110   }else{
1111     res_compare = compare_heap_area_without_type(area1, area2, area1_to_compare, area2_to_compare, previous, all_types, other_types, size, check_ignore);
1112     if(res_compare != 0){
1113       if(match_pairs)
1114         xbt_dynar_free(&previous);
1115       return res_compare;
1116     }
1117   }
1118
1119   if(match_pairs){
1120     match_equals(previous);
1121     xbt_dynar_free(&previous);
1122   }
1123
1124   return 0;
1125 }
1126
1127 /*********************************************** Miscellaneous ***************************************************/
1128 /****************************************************************************************************************/
1129
1130
1131 int get_pointed_area_size(void *area, int heap){
1132
1133   int block, frag;
1134   malloc_info *heapinfo;
1135
1136   if(heap == 1)
1137     heapinfo = heapinfo1;
1138   else
1139     heapinfo = heapinfo2;
1140
1141   block = ((char*)area - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
1142
1143   if(((char *)area < (char*)((xbt_mheap_t)s_heap)->heapbase)  || (block > heapsize1) || (block < 1))
1144     return -1;
1145
1146   if(heapinfo[block].type == -1){ /* Free block */
1147     return -1;  
1148   }else if(heapinfo[block].type == 0){ /* Complete block */
1149     return (int)heapinfo[block].busy_block.busy_size;
1150   }else{
1151     frag = ((uintptr_t) (ADDR2UINT (area) % (BLOCKSIZE))) >> heapinfo[block].type;
1152     return (int)heapinfo[block].busy_frag.frag_size[frag];
1153   }
1154
1155 }
1156
1157 char *get_type_description(xbt_dict_t types, char *type_name){
1158
1159   xbt_dict_cursor_t dict_cursor;
1160   char *type_origin;
1161   dw_type_t type;
1162
1163   xbt_dict_foreach(types, dict_cursor, type_origin, type){
1164     if(type->name && (strcmp(type->name, type_name) == 0) && type->size > 0){
1165       xbt_dict_cursor_free(&dict_cursor);
1166       return type_origin;
1167     }
1168   }
1169
1170   xbt_dict_cursor_free(&dict_cursor);
1171   return NULL;
1172 }
1173
1174
1175 #ifndef max
1176 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
1177 #endif
1178
1179 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
1180
1181   if(heap1 == NULL && heap1 == NULL){
1182     XBT_DEBUG("Malloc descriptors null");
1183     return 0;
1184   }
1185
1186   if(heap1->heaplimit != heap2->heaplimit){
1187     XBT_DEBUG("Different limit of valid info table indices");
1188     return 1;
1189   }
1190
1191   /* Heap information */
1192   heaplimit = ((struct mdesc *)heap1)->heaplimit;
1193
1194   s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1195
1196   heapbase1 = (char *)heap1 + BLOCKSIZE;
1197   heapbase2 = (char *)heap2 + BLOCKSIZE;
1198
1199   heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
1200   heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
1201
1202   heapsize1 = heap1->heapsize;
1203   heapsize2 = heap2->heapsize;
1204
1205   /* Start comparison */
1206   size_t i, j, k;
1207   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
1208
1209   int distance = 0;
1210
1211   /* Check busy blocks*/
1212
1213   i = 1;
1214
1215   while(i <= heaplimit){
1216
1217     addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
1218     addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
1219
1220     if(heapinfo1[i].type != heapinfo2[i].type){
1221   
1222       distance += BLOCKSIZE;
1223       XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
1224       i++;
1225     
1226     }else{
1227
1228       if(heapinfo1[i].type == -1){ /* Free block */
1229         i++;
1230         continue;
1231       }
1232
1233       if(heapinfo1[i].type == 0){ /* Large block */
1234        
1235         if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
1236           distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1237           i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1238           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);
1239           continue;
1240         }
1241
1242         /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
1243           distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
1244           i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1245           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);
1246           continue;
1247           }*/
1248
1249         k = 0;
1250
1251         //while(k < (heapinfo1[i].busy_block.busy_size)){
1252         while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
1253           if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
1254             distance ++;
1255           }
1256           k++;
1257         } 
1258
1259         i++;
1260
1261       }else { /* Fragmented block */
1262
1263         for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
1264
1265           addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
1266           addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
1267
1268           if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
1269             continue;
1270           }
1271           
1272           
1273           /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
1274             distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
1275             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); 
1276             continue;
1277             }*/
1278    
1279           k=0;
1280
1281           //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
1282           while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
1283             if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
1284               distance ++;
1285             }
1286             k++;
1287           }
1288
1289         }
1290
1291         i++;
1292
1293       }
1294       
1295     }
1296
1297   }
1298
1299   return distance;
1300   
1301 }
1302