Logo AND Algorithmique Numérique Distribuée

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