Logo AND Algorithmique Numérique Distribuée

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