Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : check if fragment or block has been already compared
[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
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
14                                 "Logging specific to mm_diff in mmalloc");
15
16 xbt_dynar_t mc_heap_comparison_ignore;
17 xbt_dynar_t stacks_areas;
18 void *maestro_stack_start, *maestro_stack_end;
19
20 static void heap_area_pair_free(heap_area_pair_t pair);
21 static void heap_area_pair_free_voidp(void *d);
22 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
23 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
24 static heap_area_t new_heap_area(int block, int fragment);
25
26 static size_t heap_comparison_ignore_size(xbt_dynar_t list, void *address);
27 static void add_heap_equality(xbt_dynar_t equals, void *a1, void *a2);
28 static void remove_heap_equality(xbt_dynar_t equals, int address, void *a);
29
30 static int is_stack(void *address);
31 static int is_block_stack(int block);
32 static int equal_blocks(int b1, int b2);
33 static int equal_fragments(int b1, int f1, int b2, int f2);
34
35 void mmalloc_backtrace_block_display(void* heapinfo, int block){
36
37   /* xbt_ex_t e; */
38
39   /* if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) { */
40   /*   fprintf(stderr, "No backtrace available for that block, sorry.\n"); */
41   /*   return; */
42   /* } */
43
44   /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE); */
45   /* e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size; */
46
47   /* xbt_ex_setup_backtrace(&e); */
48   /* if (e.used == 0) { */
49   /*   fprintf(stderr, "(backtrace not set)\n"); */
50   /* } else if (e.bt_strings == NULL) { */
51   /*   fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
52   /* } else { */
53   /*   int i; */
54
55   /*   fprintf(stderr, "Backtrace of where the block %d was malloced (%d frames):\n", block ,e.used); */
56   /*   for (i = 0; i < e.used; i++)       /\* no need to display "xbt_backtrace_display" *\/{ */
57   /*     fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
58   /*   } */
59   /* } */
60 }
61
62 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
63
64   /* xbt_ex_t e; */
65
66   /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE); */
67   /* e.used = XBT_BACKTRACE_SIZE; */
68
69   /* xbt_ex_setup_backtrace(&e); */
70   /* if (e.used == 0) { */
71   /*   fprintf(stderr, "(backtrace not set)\n"); */
72   /* } else if (e.bt_strings == NULL) { */
73   /*   fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
74   /* } else { */
75   /*   int i; */
76
77   /*   fprintf(stderr, "Backtrace of where the fragment %d in block %d was malloced (%d frames):\n", frag, block ,e.used); */
78   /*   for (i = 0; i < e.used; i++)       /\* no need to display "xbt_backtrace_display" *\/{ */
79   /*     fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
80   /*   } */
81   /* } */
82
83 }
84
85 void mmalloc_backtrace_display(void *addr){
86
87   /* size_t block, frag_nb; */
88   /* int type; */
89   
90   /* xbt_mheap_t heap = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit(); */
91
92   /* block = (((char*) (addr) - (char*) heap -> heapbase) / BLOCKSIZE + 1); */
93
94   /* type = heap->heapinfo[block].type; */
95
96   /* switch(type){ */
97   /* case -1 : /\* Free block *\/ */
98   /*   fprintf(stderr, "Asked to display the backtrace of a block that is free. I'm puzzled\n"); */
99   /*   xbt_abort(); */
100   /*   break;  */
101   /* case 0: /\* Large block *\/ */
102   /*   mmalloc_backtrace_block_display(heap->heapinfo, block); */
103   /*   break; */
104   /* default: /\* Fragmented block *\/ */
105   /*   frag_nb = RESIDUAL(addr, BLOCKSIZE) >> type; */
106   /*   if(heap->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){ */
107   /*     fprintf(stderr , "Asked to display the backtrace of a fragment that is free. I'm puzzled\n"); */
108   /*     xbt_abort(); */
109   /*   } */
110   /*   mmalloc_backtrace_fragment_display(heap->heapinfo, block, frag_nb); */
111   /*   break; */
112   /* } */
113 }
114
115
116 void *s_heap = NULL, *heapbase1 = NULL, *heapbase2 = NULL;
117 malloc_info *heapinfo1 = NULL, *heapinfo2 = NULL;
118 size_t heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
119 xbt_dynar_t to_ignore1 = NULL, to_ignore2 = NULL;
120
121 int ignore_done1 = 0, ignore_done2 = 0;
122
123 void init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t i1, xbt_dynar_t i2){
124
125   heaplimit = ((struct mdesc *)heap1)->heaplimit;
126
127   s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
128
129   heapbase1 = (char *)heap1 + BLOCKSIZE;
130   heapbase2 = (char *)heap2 + BLOCKSIZE;
131
132   heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
133   heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
134
135   heapsize1 = heap1->heapsize;
136   heapsize2 = heap2->heapsize;
137
138   to_ignore1 = i1;
139   to_ignore2 = i2;
140
141   if(MC_is_active()){
142     MC_ignore_data_bss(&heaplimit, sizeof(heaplimit));
143     MC_ignore_data_bss(&s_heap, sizeof(s_heap));
144     MC_ignore_data_bss(&heapbase1, sizeof(heapbase1));
145     MC_ignore_data_bss(&heapbase2, sizeof(heapbase2));
146     MC_ignore_data_bss(&heapinfo1, sizeof(heapinfo1));
147     MC_ignore_data_bss(&heapinfo2, sizeof(heapinfo2));
148     MC_ignore_data_bss(&heapsize1, sizeof(heapsize1));
149     MC_ignore_data_bss(&heapsize2, sizeof(heapsize2));
150     MC_ignore_data_bss(&to_ignore1, sizeof(to_ignore1));
151     MC_ignore_data_bss(&to_ignore2, sizeof(to_ignore2));
152   }
153   
154 }
155
156 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
157
158   if(heap1 == NULL && heap1 == NULL){
159     XBT_DEBUG("Malloc descriptors null");
160     return 0;
161   }
162
163   if(heap1->heaplimit != heap2->heaplimit){
164     XBT_DEBUG("Different limit of valid info table indices");
165     return 1;
166   }
167
168   /* Start comparison */
169   size_t i1, i2, j1, j2, k;
170   size_t current_block = -1;    /* avoid "maybe uninitialized" warning */
171   size_t current_fragment;
172   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
173   int nb_diff1 = 0, nb_diff2 = 0;
174
175   xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
176
177   int equal, res_compare = 0;
178   
179   /* Check busy blocks*/
180
181   i1 = 1;
182
183   while(i1 <= heaplimit){
184
185     current_block = i1;
186
187     if(heapinfo1[i1].type == -1){ /* Free block */
188       i1++;
189       continue;
190     }
191
192     addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
193
194     if(heapinfo1[i1].type == 0){  /* Large block */
195       
196       if(is_stack(addr_block1)){
197         for(k=0; k < heapinfo1[i1].busy_block.size; k++)
198           heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i1, -1);
199         for(k=0; k < heapinfo2[i1].busy_block.size; k++)
200           heapinfo2[i1+k].busy_block.equal_to = new_heap_area(i1, -1);
201         i1 = i1 + heapinfo1[current_block].busy_block.size;
202         continue;
203       }
204
205       if(heapinfo1[i1].busy_block.equal_to != NULL){
206         i1++;
207         continue;
208       }
209     
210       i2 = 1;
211       equal = 0;
212       res_compare = 0;
213   
214       /* Try first to associate to same block in the other heap */
215       if(heapinfo2[current_block].type == heapinfo1[current_block].type){
216
217         if(heapinfo2[current_block].busy_block.equal_to == NULL){  
218         
219           addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
220         
221           res_compare = compare_area(addr_block1, addr_block2, previous);
222         
223           if(res_compare == 0){
224             for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
225               heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
226             for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
227               heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
228             equal = 1;
229             match_equals(previous);
230             i1 = i1 + heapinfo1[current_block].busy_block.size;
231           }
232         
233           xbt_dynar_reset(previous);
234         
235         }
236         
237       }
238
239       while(i2 <= heaplimit && !equal){
240
241         addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));        
242            
243         if(i2 == current_block){
244           i2++;
245           continue;
246         }
247
248         if(heapinfo2[i2].type != 0){
249           i2++;
250           continue;
251         }
252
253         if(heapinfo2[i2].busy_block.equal_to != NULL){         
254           i2++;
255           continue;
256         }
257         
258         res_compare = compare_area(addr_block1, addr_block2, previous);
259         
260         if(res_compare == 0){
261           for(k=1; k < heapinfo2[i2].busy_block.size; k++)
262             heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
263           for(k=1; k < heapinfo1[i1].busy_block.size; k++)
264             heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
265           equal = 1;
266           match_equals(previous);
267           i1 = i1 + heapinfo1[i1].busy_block.size;
268         }
269
270         xbt_dynar_reset(previous);
271
272         i2++;
273
274       }
275
276       if(!equal){
277         XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1[i1].busy_block.busy_size, addr_block1);
278         i1 = heaplimit + 1;
279         nb_diff1++;
280       }
281       
282     }else{ /* Fragmented block */
283
284       for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
285
286         current_fragment = j1;
287
288         if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
289           continue;
290
291         if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
292           continue;
293
294         addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
295
296         i2 = 1;
297         equal = 0;
298         
299         /* Try first to associate to same fragment in the other heap */
300         if(heapinfo2[current_block].type == heapinfo1[current_block].type){
301
302           if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){  
303           
304             addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
305             addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << ((xbt_mheap_t)s_heap)->heapinfo[current_block].type));
306
307             res_compare = compare_area(addr_frag1, addr_frag2, previous);
308
309             if(res_compare == 0){
310               equal = 1;
311               match_equals(previous);
312             }
313         
314             xbt_dynar_reset(previous);
315
316             }
317         }
318
319         while(i2 <= heaplimit && !equal){
320
321           
322           if(heapinfo2[i2].type <= 0){
323             i2++;
324             continue;
325           }
326
327           for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
328
329             if(heapinfo2[i2].type == heapinfo1[i1].type && i2 == current_block && j2 == current_fragment)
330               continue;
331
332             if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)                
333               continue;            
334                           
335             addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
336             addr_frag2 = (void*) ((char *)addr_block2 + (j2 << ((xbt_mheap_t)s_heap)->heapinfo[i2].type));
337
338             res_compare = compare_area(addr_frag1, addr_frag2, previous);
339             
340             if(res_compare == 0){
341               equal = 1;
342               match_equals(previous);
343               xbt_dynar_reset(previous);
344               break;
345             }
346
347             xbt_dynar_reset(previous);
348
349           }
350
351           i2++;
352
353         }
354
355         if(heapinfo1[i1].busy_frag.equal_to[j1] == NULL){
356           XBT_DEBUG("Block %zu, fragment %zu not found (size_used = %d, address = %p)", i1, j1, heapinfo1[i1].busy_frag.frag_size[j1], addr_frag1);
357           i2 = heaplimit + 1;
358           i1 = heaplimit + 1;
359           nb_diff1++;
360           break;
361         }
362
363       }
364
365       i1++;
366       
367     }
368
369   }
370
371   /* All blocks/fragments are equal to another block/fragment ? */
372   size_t i = 1, j = 0;
373  
374   while(i<=heaplimit){
375     if(heapinfo1[i].type == 0){
376       if(current_block == heaplimit){
377         if(heapinfo1[i].busy_block.busy_size > 0){
378           if(heapinfo1[i].busy_block.equal_to == NULL){
379             if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
380               addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
381               XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
382               //mmalloc_backtrace_block_display((void*)heapinfo1, i);
383             }
384             nb_diff1++;
385           }
386         }
387       }
388       xbt_free(heapinfo1[i].busy_block.equal_to);
389       heapinfo1[i].busy_block.equal_to = NULL;
390     }
391     if(heapinfo1[i].type > 0){
392       addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
393       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
394         if(current_block == heaplimit){
395           if(heapinfo1[i].busy_frag.frag_size[j] > 0){
396             if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
397               if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
398                 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
399                 XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
400                 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
401               }
402               nb_diff1++;
403             }
404           }
405         }
406         xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
407         heapinfo1[i].busy_frag.equal_to[j] = NULL;
408       }
409     }
410     i++; 
411   }
412
413   if(current_block == heaplimit)
414     XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
415
416   i = 1;
417
418   while(i<=heaplimit){
419     if(heapinfo2[i].type == 0){
420       if(current_block == heaplimit){
421         if(heapinfo2[i].busy_block.busy_size > 0){
422           if(heapinfo2[i].busy_block.equal_to == NULL){
423             if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
424               addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
425               XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
426               //mmalloc_backtrace_block_display((void*)heapinfo2, i);
427             }
428             nb_diff2++;
429           }
430         }
431       }
432     }
433     if(heapinfo2[i].type > 0){
434       addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
435       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
436         if(current_block == heaplimit){
437           if(heapinfo2[i].busy_frag.frag_size[j] > 0){
438             if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
439               if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
440                 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
441                 XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
442                 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
443               }
444               nb_diff2++;
445             }
446           }
447         }
448       }
449     }
450     i++; 
451   }
452
453   if(current_block == heaplimit)
454     XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
455
456   xbt_dynar_free(&previous);
457
458   return ((nb_diff1 > 0) || (nb_diff2 > 0));
459 }
460
461 void reset_heap_information(){
462
463   size_t i = 0, j;
464
465   while(i<=heaplimit){
466     if(heapinfo1[i].type == 0){
467       xbt_free(heapinfo1[i].busy_block.equal_to);
468       heapinfo1[i].busy_block.equal_to = NULL;
469     }
470     if(heapinfo1[i].type > 0){
471       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
472         xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
473         heapinfo1[i].busy_frag.equal_to[j] = NULL;
474       }
475     }
476     i++; 
477   }
478
479   i = 0;
480
481   while(i<=heaplimit){
482     if(heapinfo2[i].type == 0){
483       xbt_free(heapinfo2[i].busy_block.equal_to);
484       heapinfo2[i].busy_block.equal_to = NULL;
485     }
486     if(heapinfo2[i].type > 0){
487       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
488         xbt_free(heapinfo2[i].busy_frag.equal_to[j]);
489         heapinfo2[i].busy_frag.equal_to[j] = NULL;
490       }
491     }
492     i++; 
493   }
494
495   ignore_done1 = 0, ignore_done2 = 0;
496   s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
497   heapinfo1 = NULL, heapinfo2 = NULL;
498   heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
499   to_ignore1 = NULL, to_ignore2 = NULL;
500
501 }
502
503 static heap_area_t new_heap_area(int block, int fragment){
504   heap_area_t area = NULL;
505   area = xbt_new0(s_heap_area_t, 1);
506   area->block = block;
507   area->fragment = fragment;
508   return area;
509 }
510
511
512 static size_t heap_comparison_ignore_size(xbt_dynar_t ignore_list, void *address){
513
514   unsigned int cursor = 0;
515   int start = 0;
516   int end = xbt_dynar_length(ignore_list) - 1;
517   mc_heap_ignore_region_t region;
518
519   while(start <= end){
520     cursor = (start + end) / 2;
521     region = (mc_heap_ignore_region_t)xbt_dynar_get_as(ignore_list, cursor, mc_heap_ignore_region_t);
522     if(region->address == address)
523       return region->size;
524     if(region->address < address)
525       start = cursor + 1;
526     if(region->address > address)
527       end = cursor - 1;   
528   }
529
530   return 0;
531 }
532
533
534 int compare_area(void *area1, void* area2, xbt_dynar_t previous){
535
536   size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
537   void *addr_pointed1, *addr_pointed2;
538   int res_compare;
539   size_t block1, frag1, block2, frag2, size;
540   int check_ignore = 0;
541
542   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
543   void *area1_to_compare, *area2_to_compare;
544
545   int match_pairs = 0;
546
547   if(previous == NULL){
548     previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
549     match_pairs = 1;
550   }
551
552   block1 = ((char*)area1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
553   block2 = ((char*)area2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
554
555   if(is_block_stack((int)block1) && is_block_stack((int)block2))
556     return 0;
557
558   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))
559     return 1;
560
561   addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)heapbase1));
562   addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)heapbase2));
563   
564   if(heapinfo1[block1].type == heapinfo2[block2].type){
565     
566     if(heapinfo1[block1].type == -1){
567       return 0;
568     }else if(heapinfo1[block1].type == 0){
569       if(heapinfo1[block1].busy_block.equal_to != NULL){
570         if(equal_blocks(block1, block2)){
571           return 0;
572         }
573       }
574       if(heapinfo1[block1].busy_block.size != heapinfo2[block2].busy_block.size)
575         return 1;
576       if(heapinfo1[block1].busy_block.busy_size != heapinfo2[block2].busy_block.busy_size)
577         return 1;
578       if(!add_heap_area_pair(previous, block1, -1, block2, -1))
579         return 0;
580
581       size = heapinfo1[block1].busy_block.busy_size;
582       frag1 = -1;
583       frag2 = -1;
584
585       area1_to_compare = addr_block1;
586       area2_to_compare = addr_block2;
587
588       if(heapinfo1[block1].busy_block.ignore == 1 || heapinfo2[block2].busy_block.ignore == 1)
589         check_ignore = 1;
590     }else{
591       frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
592       frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
593
594       if(heapinfo1[block1].busy_frag.equal_to[frag1] != NULL){
595         if(equal_fragments(block1, frag1, block2, frag2)){
596           return 0;
597         }
598       }
599
600       if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2])
601         return 1;  
602       if(!add_heap_area_pair(previous, block1, frag1, block2, frag2))
603         return 0;
604
605       addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
606       addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
607
608       area1_to_compare = addr_frag1;
609       area2_to_compare = addr_frag2;
610       
611       size = heapinfo1[block1].busy_frag.frag_size[frag1];
612
613       if(size == 0)
614         return 0;
615       
616       if(heapinfo1[block1].busy_frag.ignore[frag1] == 1 || heapinfo2[block2].busy_frag.ignore[frag2] == 1)
617         check_ignore = 1;
618     }
619   }else if((heapinfo1[block1].type > 0) && (heapinfo2[block2].type > 0)){
620     frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
621     frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
622
623     if(heapinfo1[block1].busy_frag.equal_to[frag1] != NULL){
624       if(equal_fragments(block1, frag1, block2, frag2)){
625         return 0;
626       }
627     }
628
629     if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2])
630       return 1;       
631     if(!add_heap_area_pair(previous, block1, frag1, block2, frag2))
632       return 0;
633
634     addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
635     addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
636
637     area1_to_compare = addr_frag1;
638     area2_to_compare = addr_frag2;
639       
640     size = heapinfo1[block1].busy_frag.frag_size[frag1];
641
642     if(size == 0)
643       return 0;
644
645     if(heapinfo1[block1].busy_frag.ignore[frag1] == 1 || heapinfo2[block2].busy_frag.ignore[frag2] == 1)
646       check_ignore = 1;   
647   }else{
648     return 1;
649   }
650   
651   while(i<size){
652
653     if(check_ignore){
654       if((ignore_done1 < xbt_dynar_length(to_ignore1)) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, (char *)area1 + i)) > 0)){
655         if((ignore_done2 < xbt_dynar_length(to_ignore2)) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, (char *)area2 + i))  == ignore1)){
656           i = i + ignore2;
657           ignore_done1++;
658           ignore_done2++;
659           continue;
660         }
661       }
662     }
663    
664     if(memcmp(((char *)area1_to_compare) + i, ((char *)area2_to_compare) + i, 1) != 0){
665
666       /* Check pointer difference */
667       pointer_align = (i / sizeof(void*)) * sizeof(void*);
668       addr_pointed1 = *((void **)((char *)area1_to_compare + pointer_align));
669       addr_pointed2 = *((void **)((char *)area2_to_compare + pointer_align));
670       
671       if(addr_pointed1 > maestro_stack_start && addr_pointed1 < maestro_stack_end && addr_pointed2 > maestro_stack_start && addr_pointed2 < maestro_stack_end){
672         i = pointer_align + sizeof(void *);
673         continue;
674       }
675
676       res_compare = compare_area(addr_pointed1, addr_pointed2, previous);
677       
678       if(res_compare == 1)
679         return 1; 
680       
681       i = pointer_align + sizeof(void *);
682       
683     }else{
684
685       i++;
686
687     }
688   }
689
690   if(match_pairs)
691     match_equals(previous);
692
693   return 0;
694   
695
696 }
697
698 static void heap_area_pair_free(heap_area_pair_t pair){
699   xbt_free(pair);
700   pair = NULL;
701 }
702
703 static void heap_area_pair_free_voidp(void *d)
704 {
705   heap_area_pair_free((heap_area_pair_t) * (void **) d);
706 }
707
708 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
709
710   if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
711     heap_area_pair_t pair = NULL;
712     pair = xbt_new0(s_heap_area_pair_t, 1);
713     pair->block1 = block1;
714     pair->fragment1 = fragment1;
715     pair->block2 = block2;
716     pair->fragment2 = fragment2;
717     
718     xbt_dynar_push(list, &pair); 
719
720     return 1;
721   }
722
723   return 0;
724 }
725  
726 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
727   
728   unsigned int cursor = 0;
729   heap_area_pair_t current_pair;
730
731   xbt_dynar_foreach(list, cursor, current_pair){
732     if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
733       return 0; 
734   }
735   
736   return 1;
737 }
738
739 void match_equals(xbt_dynar_t list){
740
741   unsigned int cursor = 0;
742   heap_area_pair_t current_pair;
743   heap_area_t previous_area;
744
745   xbt_dynar_foreach(list, cursor, current_pair){
746
747     if(current_pair->fragment1 != -1){
748       
749       if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){    
750         previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
751         xbt_free(heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
752         heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
753         xbt_free(previous_area); 
754       }
755       if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){        
756         previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
757         xbt_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
758         heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
759         xbt_free(previous_area);
760       }
761
762       heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
763       heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
764
765     }else{
766
767       if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
768         previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
769         xbt_free(heapinfo2[previous_area->block].busy_block.equal_to);
770         heapinfo2[previous_area->block].busy_block.equal_to = NULL; 
771         xbt_free(previous_area);
772       }
773       if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
774         previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
775         xbt_free(heapinfo1[previous_area->block].busy_block.equal_to);
776         heapinfo1[previous_area->block].busy_block.equal_to = NULL;
777         xbt_free(previous_area);
778       }
779
780       heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
781       heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
782
783     }
784   }
785
786
787 }
788
789 #ifndef max
790 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
791 #endif
792
793 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
794
795   if(heap1 == NULL && heap1 == NULL){
796     XBT_DEBUG("Malloc descriptors null");
797     return 0;
798   }
799
800   if(heap1->heaplimit != heap2->heaplimit){
801     XBT_DEBUG("Different limit of valid info table indices");
802     return 1;
803   }
804
805   /* Heap information */
806   heaplimit = ((struct mdesc *)heap1)->heaplimit;
807
808   s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
809
810   heapbase1 = (char *)heap1 + BLOCKSIZE;
811   heapbase2 = (char *)heap2 + BLOCKSIZE;
812
813   heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
814   heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
815
816   heapsize1 = heap1->heapsize;
817   heapsize2 = heap2->heapsize;
818
819   /* Start comparison */
820   size_t i, j, k;
821   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
822
823   int distance = 0;
824
825   /* Check busy blocks*/
826
827   i = 1;
828
829   while(i <= heaplimit){
830
831     addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
832     addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
833
834     if(heapinfo1[i].type != heapinfo2[i].type){
835   
836       distance += BLOCKSIZE;
837       XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
838       i++;
839     
840     }else{
841
842       if(heapinfo1[i].type == -1){ /* Free block */
843         i++;
844         continue;
845       }
846
847       if(heapinfo1[i].type == 0){ /* Large block */
848        
849         if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
850           distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
851           i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
852           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);
853           continue;
854         }
855
856         /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
857           distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
858           i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
859           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);
860           continue;
861           }*/
862
863         k = 0;
864
865         //while(k < (heapinfo1[i].busy_block.busy_size)){
866         while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
867           if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
868             distance ++;
869           }
870           k++;
871         } 
872
873         i++;
874
875       }else { /* Fragmented block */
876
877         for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
878
879           addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
880           addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
881
882           if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
883             continue;
884           }
885           
886           
887           /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
888             distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
889             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); 
890             continue;
891             }*/
892    
893           k=0;
894
895           //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
896           while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
897             if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
898               distance ++;
899             }
900             k++;
901           }
902
903         }
904
905         i++;
906
907       }
908       
909     }
910
911   }
912
913   return distance;
914   
915 }
916
917 static int is_stack(void *address){
918   unsigned int cursor = 0;
919   stack_region_t stack;
920
921   xbt_dynar_foreach(stacks_areas, cursor, stack){
922     if(address == stack->address)
923       return 1;
924   }
925
926   return 0;
927 }
928
929 static int is_block_stack(int block){
930   unsigned int cursor = 0;
931   stack_region_t stack;
932
933   xbt_dynar_foreach(stacks_areas, cursor, stack){
934     if(block == stack->block)
935       return 1;
936   }
937
938   return 0;
939 }
940
941 static void add_heap_equality(xbt_dynar_t equals, void *a1, void *a2){
942   
943   if(xbt_dynar_is_empty(equals)){
944
945     heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
946     he->address1 = a1;
947     he->address2 = a2;
948
949     xbt_dynar_insert_at(equals, 0, &he);
950   
951   }else{
952
953     unsigned int cursor = 0;
954     int start = 0;
955     int end = xbt_dynar_length(equals) - 1;
956     heap_equality_t current_equality = NULL;
957
958     while(start <= end){
959       cursor = (start + end) / 2;
960       current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
961       if(current_equality->address1 == a1){
962         if(current_equality->address2 == a2)
963           return;
964         if(current_equality->address2 < a2)
965           start = cursor + 1;
966         if(current_equality->address2 > a2)
967           end = cursor - 1;
968       }
969       if(current_equality->address1 < a1)
970         start = cursor + 1;
971       if(current_equality->address1 > a1)
972         end = cursor - 1; 
973     }
974
975     heap_equality_t he = xbt_new0(s_heap_equality_t, 1);
976     he->address1 = a1;
977     he->address2 = a2;
978   
979     if(current_equality->address1 < a1)
980       xbt_dynar_insert_at(equals, cursor + 1 , &he);
981     else
982        xbt_dynar_insert_at(equals, cursor, &he); 
983
984   }
985
986 }
987
988 static void remove_heap_equality(xbt_dynar_t equals, int address, void *a){
989   
990   unsigned int cursor = 0;
991   heap_equality_t current_equality;
992   int found = 0;
993
994   if(address == 1){
995
996     int start = 0;
997     int end = xbt_dynar_length(equals) - 1;
998
999
1000     while(start <= end && found == 0){
1001       cursor = (start + end) / 2;
1002       current_equality = (heap_equality_t)xbt_dynar_get_as(equals, cursor, heap_equality_t);
1003       if(current_equality->address1 == a)
1004         found = 1;
1005       if(current_equality->address1 < a)
1006         start = cursor + 1;
1007       if(current_equality->address1 > a)
1008         end = cursor - 1; 
1009     }
1010
1011     if(found == 1)
1012       xbt_dynar_remove_at(equals, cursor, NULL);
1013   
1014   }else{
1015
1016     xbt_dynar_foreach(equals, cursor, current_equality){
1017       if(current_equality->address2 == a){
1018         found = 1;
1019         break;
1020       }
1021     }
1022
1023     if(found == 1)
1024       xbt_dynar_remove_at(equals, cursor, NULL);
1025
1026   }
1027   
1028 }
1029
1030 int is_free_area(void *area, xbt_mheap_t heap){
1031
1032   void *sheap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1033   malloc_info *heapinfo = (malloc_info *)((char *)heap + ((uintptr_t)((char *)heap->heapinfo - (char *)sheap)));
1034   size_t heapsize = heap->heapsize;
1035
1036   /* Get block number */ 
1037   size_t block = ((char*)area - (char*)((xbt_mheap_t)sheap)->heapbase) / BLOCKSIZE + 1;
1038   size_t fragment;
1039
1040   /* Check if valid block number */
1041   if((char *)area < (char*)((xbt_mheap_t)sheap)->heapbase || block > heapsize || block < 1)
1042     return 0;
1043
1044   if(heapinfo[block].type < 0)
1045     return 1;
1046
1047   if(heapinfo[block].type == 0)
1048     return 0;
1049
1050   if(heapinfo[block].type > 0){
1051     fragment = ((uintptr_t) (ADDR2UINT(area) % (BLOCKSIZE))) >> heapinfo[block].type;
1052     if(heapinfo[block].busy_frag.frag_size[fragment] == 0)
1053       return 1;  
1054   }
1055
1056   return 0;
1057  
1058 }
1059
1060 static int equal_blocks(int b1, int b2){
1061   if(heapinfo1[b1].busy_block.equal_to != NULL){
1062     if(heapinfo2[b2].busy_block.equal_to != NULL){
1063       if(((heap_area_t)(heapinfo1[b1].busy_block.equal_to))->block == b2 && ((heap_area_t)(heapinfo2[b2].busy_block.equal_to))->block == b1)
1064         return 1;
1065     }
1066   }
1067   return 0;
1068 }
1069
1070 static int equal_fragments(int b1, int f1, int b2, int f2){
1071   if(heapinfo1[b1].busy_frag.equal_to[f1] != NULL){
1072     if(heapinfo2[b2].busy_frag.equal_to[f2] != NULL){
1073       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)
1074         return 1;
1075     }
1076   }
1077   return 0;
1078 }