Logo AND Algorithmique Numérique Distribuée

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