Logo AND Algorithmique Numérique Distribuée

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