Logo AND Algorithmique Numérique Distribuée

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