Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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 != 0){
710           return res_compare;
711         }
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       return  (memcmp(area1, area2, area_size) != 0);
755     }else{
756       if(area_size != -1 && type->size != area_size)
757         return -1;
758       else
759         return (memcmp(area1, area2, type->size) != 0);
760     }
761     break;
762   case e_dw_enumeration_type:
763     if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2))  == ignore1))
764       return 0;
765     if(area_size != -1 && type->size != area_size)
766       return -1;
767     else
768       return (memcmp(area1, area2, type->size) != 0);
769     break;
770   case e_dw_typedef:
771     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);
772     break;
773   case e_dw_const_type:
774     return 0;
775     break;
776   case e_dw_array_type:
777     subtype = xbt_dict_get_or_null(all_types, type->dw_type_id);
778     switch(subtype->type){
779     case e_dw_base_type:
780     case e_dw_enumeration_type:
781     case e_dw_pointer_type:
782     case e_dw_structure_type:
783     case e_dw_union_type:
784       if(subtype->size == 0){ /*declaration of the type, need the complete description */
785         type_desc = get_type_description(all_types, subtype->name);
786         if(type_desc){
787           subtype = xbt_dict_get_or_null(all_types, type_desc);
788         }else{
789           subtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subtype->name));
790           switch_types = 1;
791         }
792       }
793       elm_size = subtype->size;
794       break;
795     case e_dw_typedef:
796     case e_dw_volatile_type:
797       subsubtype = xbt_dict_get_or_null(all_types, subtype->dw_type_id);
798       if(subsubtype->size == 0){ /*declaration of the type, need the complete description */
799         type_desc = get_type_description(all_types, subsubtype->name);
800         if(type_desc){
801           subsubtype = xbt_dict_get_or_null(all_types, type_desc);
802         }else{
803           subsubtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subtype->name));
804           switch_types = 1;
805         }
806       }
807       elm_size = subsubtype->size;
808       break;
809     default : 
810       return 0;
811       break;
812     }
813     for(i=0; i<type->size; i++){
814       if(switch_types)
815         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);
816       else
817         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);
818       if(res == 1)
819         return res;
820     }
821     break;
822   case e_dw_pointer_type:
823     if(type->dw_type_id && ((dw_type_t)xbt_dict_get_or_null(all_types, type->dw_type_id))->type == e_dw_subroutine_type){
824       addr_pointed1 = *((void **)(area1)); 
825       addr_pointed2 = *((void **)(area2));
826       return (addr_pointed1 != addr_pointed2);;
827     }else{
828       pointer_level++;
829       if(pointer_level > 1){ /* Array of pointers */
830         for(i=0; i<(area_size/sizeof(void *)); i++){
831           addr_pointed1 = *((void **)((char *)area1 + (i*sizeof(void *)))); 
832           addr_pointed2 = *((void **)((char *)area2 + (i*sizeof(void *)))); 
833           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)
834             res =  compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, type->dw_type_id, pointer_level); 
835           else
836             res =  (addr_pointed1 != addr_pointed2);
837           if(res == 1)
838             return res;
839         }
840       }else{
841         addr_pointed1 = *((void **)(area1)); 
842         addr_pointed2 = *((void **)(area2));
843         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)
844           return compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, type->dw_type_id, pointer_level); 
845         else
846           return  (addr_pointed1 != addr_pointed2);
847       }
848     }
849     break;
850   case e_dw_structure_type:
851     if(type->size == 0){ /*declaration of the structure, need the complete description */
852       type_desc = get_type_description(all_types, type->name);
853       if(type_desc){
854         type = xbt_dict_get_or_null(all_types, type_desc);
855       }else{
856         type = xbt_dict_get_or_null(other_types, get_type_description(other_types, type->name));
857         switch_types = 1;
858       }
859     }
860     if(area_size != -1 && type->size != area_size){
861       if(area_size>type->size && area_size%type->size == 0){
862         for(i=0; i<(area_size/type->size); i++){
863           if(switch_types)
864             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); 
865           else
866             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); 
867           if(res == 1)
868             return res;
869         }
870       }else{
871         return -1;
872       }
873     }else{
874       cursor = 0;
875       xbt_dynar_foreach(type->members, cursor, member){
876         if(switch_types)
877           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);
878         else
879           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);  
880         if(res == 1)
881           return res;        
882       }
883     }
884     break;
885   case e_dw_union_type:
886     if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2))  == ignore1))
887       return 0;
888     return compare_heap_area_without_type(real_area1, real_area2, area1, area2, previous, all_types, other_types, type->size, check_ignore);
889     break;
890   case e_dw_volatile_type:
891     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);
892     break;
893   default:
894     break;
895   }
896
897   return 0;
898
899 }
900
901 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){
902
903   int res_compare;
904   ssize_t block1, frag1, block2, frag2;
905   ssize_t size;
906   int check_ignore = 0;
907
908   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
909   void *area1_to_compare, *area2_to_compare;
910   dw_type_t type = NULL;
911   char *type_desc;
912
913   int match_pairs = 0;
914
915   if(previous == NULL){
916     previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
917     match_pairs = 1;
918   }
919
920   block1 = ((char*)area1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
921   block2 = ((char*)area2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
922
923   if(is_block_stack((int)block1) && is_block_stack((int)block2)){
924     add_heap_area_pair(previous, block1, -1, block2, -1);
925     if(match_pairs){
926       match_equals(previous);
927       xbt_dynar_free(&previous);
928     }
929     return 0;
930   }
931
932   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)){
933     if(match_pairs){
934       xbt_dynar_free(&previous);
935     }
936     return 1;
937   }
938
939   addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)heapbase1));
940   addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)heapbase2));
941   
942   if((heapinfo1[block1].type == -1) && (heapinfo2[block2].type == -1)){  /* Free block */
943
944     if(match_pairs){
945       match_equals(previous);
946       xbt_dynar_free(&previous);
947     }
948     return 0;
949
950   }else if((heapinfo1[block1].type == 0) && (heapinfo2[block2].type == 0)){ /* Complete block */
951
952     if(equals_to1[block1][0] != NULL && equals_to2[block2][0] != NULL){
953       if(equal_blocks(block1, block2)){
954         if(match_pairs){
955           match_equals(previous);
956           xbt_dynar_free(&previous);
957         }
958         return 0;
959       }
960     }
961
962     if(type_id){
963       type = xbt_dict_get_or_null(all_types, type_id);
964       if(strcmp(type->name, "char") ==0){
965         if(area1 == area2)
966           return -1;
967       }
968       if(type->size == 0){
969         type_desc = get_type_description(all_types, type->name);
970         if(type_desc)
971           type = xbt_dict_get_or_null(all_types, type_desc);
972         else
973           type = xbt_dict_get_or_null(other_types, get_type_description(other_types, type->name));
974       }
975       if(strcmp(type->name, "s_smx_context") != 0){
976         if(type->size > 1){
977           if(heapinfo1[block1].busy_block.busy_size != type->size && heapinfo2[block2].busy_block.busy_size  != type->size)
978             return -1;
979         }
980       }
981     }
982
983     if(heapinfo1[block1].busy_block.size != heapinfo2[block2].busy_block.size){
984       if(match_pairs){
985         xbt_dynar_free(&previous);
986       }
987       return 1;
988     }
989
990     if(heapinfo1[block1].busy_block.busy_size != heapinfo2[block2].busy_block.busy_size){
991       if(match_pairs){
992         xbt_dynar_free(&previous);
993       }
994       return 1;
995     }
996
997     if(!add_heap_area_pair(previous, block1, -1, block2, -1)){
998       if(match_pairs){
999         match_equals(previous);
1000         xbt_dynar_free(&previous);
1001       }
1002       return 0;
1003     }
1004  
1005     size = heapinfo1[block1].busy_block.busy_size;
1006
1007     if(size <= 0){
1008       if(match_pairs){
1009         match_equals(previous);
1010         xbt_dynar_free(&previous);
1011       }
1012       return 0;
1013     }
1014
1015     frag1 = -1;
1016     frag2 = -1;
1017
1018     area1_to_compare = addr_block1;
1019     area2_to_compare = addr_block2;
1020
1021     if((heapinfo1[block1].busy_block.ignore > 0) && (heapinfo2[block2].busy_block.ignore == heapinfo1[block1].busy_block.ignore))
1022       check_ignore = heapinfo1[block1].busy_block.ignore;
1023       
1024   }else if((heapinfo1[block1].type > 0) && (heapinfo2[block2].type > 0)){ /* Fragmented block */
1025
1026     frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
1027     frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
1028       
1029     addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
1030     addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
1031       
1032     area1_to_compare = addr_frag1;
1033     area2_to_compare = addr_frag2;
1034
1035     if(type_id){
1036       type = xbt_dict_get_or_null(all_types, type_id);
1037       if(strcmp(type->name, "char") ==0){
1038         if(area1 == area2)
1039           return -1;
1040       }
1041       if(type->size == 0 || type->type == e_dw_pointer_type){
1042         if(!type->dw_type_id){
1043           type_desc = get_type_description(all_types, type->name);
1044           if(type_desc)
1045             type = xbt_dict_get_or_null(all_types, type_desc);
1046           else
1047             type = xbt_dict_get_or_null(other_types, get_type_description(other_types, type->name));
1048         }else{
1049           type = xbt_dict_get_or_null(all_types, type->dw_type_id);
1050         }
1051       }
1052       if(type->size > 1){
1053         if(heapinfo1[block1].busy_frag.frag_size[frag1] != type->size || heapinfo2[block2].busy_frag.frag_size[frag2]  != type->size)
1054           return -1;
1055       }
1056     }
1057
1058     if(equals_to1[block1][frag1] != NULL && equals_to2[block2][frag2] != NULL){
1059       if(equal_fragments(block1, frag1, block2, frag2)){
1060         if(match_pairs){
1061           match_equals(previous);
1062           xbt_dynar_free(&previous);
1063         }
1064         return 0;
1065       }
1066     }
1067
1068     if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2]){
1069       if(match_pairs){
1070         xbt_dynar_free(&previous);
1071       }
1072       return 1;  
1073     }
1074       
1075     if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
1076       if(match_pairs){
1077         match_equals(previous);
1078         xbt_dynar_free(&previous);
1079       }
1080       return 0;
1081     }
1082
1083     size = heapinfo1[block1].busy_frag.frag_size[frag1];
1084
1085     if(size <= 0){
1086       if(match_pairs){
1087         match_equals(previous);
1088         xbt_dynar_free(&previous);
1089       }
1090       return 0;
1091     }
1092       
1093     if((heapinfo1[block1].busy_frag.ignore[frag1] > 0) && ( heapinfo2[block2].busy_frag.ignore[frag2] == heapinfo1[block1].busy_frag.ignore[frag1]))
1094       check_ignore = heapinfo1[block1].busy_frag.ignore[frag1];
1095     
1096   }else{
1097
1098     if(match_pairs){
1099       xbt_dynar_free(&previous);
1100     }
1101     return 1;
1102
1103   }
1104   
1105
1106   /* Start comparison*/
1107   if(type_id != NULL){
1108     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);
1109     if(res_compare != 0){
1110       if(match_pairs)
1111         xbt_dynar_free(&previous);
1112       return res_compare;
1113     }
1114   }else{
1115     res_compare = compare_heap_area_without_type(area1, area2, area1_to_compare, area2_to_compare, previous, all_types, other_types, size, check_ignore);
1116     if(res_compare != 0){
1117       if(match_pairs)
1118         xbt_dynar_free(&previous);
1119       return res_compare;
1120     }
1121   }
1122
1123   if(match_pairs){
1124     match_equals(previous);
1125     xbt_dynar_free(&previous);
1126   }
1127
1128   return 0;
1129 }
1130
1131 /*********************************************** Miscellaneous ***************************************************/
1132 /****************************************************************************************************************/
1133
1134
1135 int get_pointed_area_size(void *area, int heap){
1136
1137   int block, frag;
1138   malloc_info *heapinfo;
1139
1140   if(heap == 1)
1141     heapinfo = heapinfo1;
1142   else
1143     heapinfo = heapinfo2;
1144
1145   block = ((char*)area - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
1146
1147   if(((char *)area < (char*)((xbt_mheap_t)s_heap)->heapbase)  || (block > heapsize1) || (block < 1))
1148     return -1;
1149
1150   if(heapinfo[block].type == -1){ /* Free block */
1151     return -1;  
1152   }else if(heapinfo[block].type == 0){ /* Complete block */
1153     return (int)heapinfo[block].busy_block.busy_size;
1154   }else{
1155     frag = ((uintptr_t) (ADDR2UINT (area) % (BLOCKSIZE))) >> heapinfo[block].type;
1156     return (int)heapinfo[block].busy_frag.frag_size[frag];
1157   }
1158
1159 }
1160
1161 char *get_type_description(xbt_dict_t types, char *type_name){
1162
1163   xbt_dict_cursor_t dict_cursor;
1164   char *type_origin;
1165   dw_type_t type;
1166
1167   xbt_dict_foreach(types, dict_cursor, type_origin, type){
1168     if(type->name && (strcmp(type->name, type_name) == 0) && type->size > 0){
1169       xbt_dict_cursor_free(&dict_cursor);
1170       return type_origin;
1171     }
1172   }
1173
1174   xbt_dict_cursor_free(&dict_cursor);
1175   return NULL;
1176 }
1177
1178
1179 #ifndef max
1180 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
1181 #endif
1182
1183 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
1184
1185   if(heap1 == NULL && heap1 == NULL){
1186     XBT_DEBUG("Malloc descriptors null");
1187     return 0;
1188   }
1189
1190   if(heap1->heaplimit != heap2->heaplimit){
1191     XBT_DEBUG("Different limit of valid info table indices");
1192     return 1;
1193   }
1194
1195   /* Heap information */
1196   heaplimit = ((struct mdesc *)heap1)->heaplimit;
1197
1198   s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1199
1200   heapbase1 = (char *)heap1 + BLOCKSIZE;
1201   heapbase2 = (char *)heap2 + BLOCKSIZE;
1202
1203   heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
1204   heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
1205
1206   heapsize1 = heap1->heapsize;
1207   heapsize2 = heap2->heapsize;
1208
1209   /* Start comparison */
1210   size_t i, j, k;
1211   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
1212
1213   int distance = 0;
1214
1215   /* Check busy blocks*/
1216
1217   i = 1;
1218
1219   while(i <= heaplimit){
1220
1221     addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
1222     addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
1223
1224     if(heapinfo1[i].type != heapinfo2[i].type){
1225   
1226       distance += BLOCKSIZE;
1227       XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
1228       i++;
1229     
1230     }else{
1231
1232       if(heapinfo1[i].type == -1){ /* Free block */
1233         i++;
1234         continue;
1235       }
1236
1237       if(heapinfo1[i].type == 0){ /* Large block */
1238        
1239         if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
1240           distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1241           i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1242           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);
1243           continue;
1244         }
1245
1246         /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
1247           distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
1248           i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1249           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);
1250           continue;
1251           }*/
1252
1253         k = 0;
1254
1255         //while(k < (heapinfo1[i].busy_block.busy_size)){
1256         while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
1257           if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
1258             distance ++;
1259           }
1260           k++;
1261         } 
1262
1263         i++;
1264
1265       }else { /* Fragmented block */
1266
1267         for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
1268
1269           addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
1270           addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
1271
1272           if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
1273             continue;
1274           }
1275           
1276           
1277           /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
1278             distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
1279             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); 
1280             continue;
1281             }*/
1282    
1283           k=0;
1284
1285           //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
1286           while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
1287             if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
1288               distance ++;
1289             }
1290             k++;
1291           }
1292
1293         }
1294
1295         i++;
1296
1297       }
1298       
1299     }
1300
1301   }
1302
1303   return distance;
1304   
1305 }
1306