Logo AND Algorithmique Numérique Distribuée

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