Logo AND Algorithmique Numérique Distribuée

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