Logo AND Algorithmique Numérique Distribuée

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