Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7b1d98315f4276e096fe661006b775755804a973
[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
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
13                                 "Logging specific to mm_diff in mmalloc");
14
15 extern char *xbt_binary_name;
16
17 xbt_dynar_t mmalloc_ignore;
18
19 typedef struct s_heap_area_pair{
20   int block1;
21   int fragment1;
22   int block2;
23   int fragment2;
24 }s_heap_area_pair_t, *heap_area_pair_t;
25
26 static void heap_area_pair_free(heap_area_pair_t pair);
27 static void heap_area_pair_free_voidp(void *d);
28 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
29 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2);
30
31 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore);
32 static void match_equals(xbt_dynar_t list);
33
34 static int in_mmalloc_ignore(int block, int fragment);
35 static size_t heap_comparison_ignore(void *address);
36
37 void mmalloc_backtrace_block_display(void* heapinfo, int block){
38
39   xbt_ex_t e;
40
41   if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) {
42     XBT_DEBUG("No backtrace available for that block, sorry.");
43     return;
44   }
45
46   memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE);
47   e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size;
48
49   xbt_ex_setup_backtrace(&e);
50   if (e.used == 0) {
51     XBT_DEBUG("(backtrace not set)");
52   } else if (e.bt_strings == NULL) {
53     XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
54   } else {
55     int i;
56
57     XBT_DEBUG("Backtrace of where the block %d was malloced (%d frames):", block ,e.used);
58     for (i = 0; i < e.used; i++)       /* no need to display "xbt_backtrace_display" */{
59       XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
60     }
61   }
62
63 }
64
65 void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
66
67   xbt_ex_t e;
68
69   memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE);
70   e.used = XBT_BACKTRACE_SIZE;
71
72   xbt_ex_setup_backtrace(&e);
73   if (e.used == 0) {
74     XBT_DEBUG("(backtrace not set)");
75   } else if (e.bt_strings == NULL) {
76     XBT_DEBUG("(backtrace not ready to be computed. %s)",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet");
77   } else {
78     int i;
79
80     XBT_DEBUG("Backtrace of where the fragment %d in block %d was malloced (%d frames):", frag, block ,e.used);
81     for (i = 0; i < e.used; i++)       /* no need to display "xbt_backtrace_display" */{
82       XBT_DEBUG("%d ---> %s",i, e.bt_strings[i] + 4);
83     }
84   }
85
86 }
87
88 void *s_heap, *heapbase1, *heapbase2;
89 malloc_info *heapinfo1, *heapinfo2;
90 size_t heaplimit, heapsize1, heapsize2;
91
92 int ignore_done;
93
94 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
95
96   if(heap1 == NULL && heap1 == NULL){
97     XBT_DEBUG("Malloc descriptors null");
98     return 0;
99   }
100
101   if(heap1->heaplimit != heap2->heaplimit){
102     XBT_DEBUG("Different limit of valid info table indices");
103     return 1;
104   }
105
106   /* Heap information */
107   heaplimit = ((struct mdesc *)heap1)->heaplimit;
108
109   s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
110
111   heapbase1 = (char *)heap1 + BLOCKSIZE;
112   heapbase2 = (char *)heap2 + BLOCKSIZE;
113
114   heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
115   heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
116
117   heapsize1 = heap1->heapsize;
118   heapsize2 = heap2->heapsize;
119
120   /* Start comparison */
121   size_t i1, i2, j1, j2, k, current_block, current_fragment;
122   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
123
124   xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
125
126   int equal, res_compare;
127
128   ignore_done = 0;
129
130   /* Init equal information */
131   i1 = 1;
132
133   while(i1<=heaplimit){
134     if(heapinfo1[i1].type == 0){
135       heapinfo1[i1].busy_block.equal_to = -1;
136     }
137     if(heapinfo1[i1].type > 0){
138       for(j1=0; j1 < MAX_FRAGMENT_PER_BLOCK; j1++){
139         heapinfo1[i1].busy_frag.equal_to[j1] = -1;
140       }
141     }
142     i1++; 
143   }
144
145   i2 = 1;
146
147   while(i2<=heaplimit){
148     if(heapinfo2[i2].type == 0){
149       heapinfo2[i2].busy_block.equal_to = -1;
150     }
151     if(heapinfo2[i2].type > 0){
152       for(j2=0; j2 < MAX_FRAGMENT_PER_BLOCK; j2++){
153         heapinfo2[i2].busy_frag.equal_to[j2] = -1;
154       }
155     }
156     i2++; 
157   }
158
159   /* Check busy blocks*/
160
161   i1 = 1;
162
163   while(i1 <= heaplimit){
164
165     current_block = i1;
166
167     if(heapinfo1[i1].type == -1){ /* Free block */
168       i1++;
169       continue;
170     }
171
172     addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)heapbase1));
173
174     if(heapinfo1[i1].type == 0){  /* Large block */
175
176       if(heapinfo1[i1].busy_block.busy_size == 0){
177         i1++;
178         continue;
179       }
180       
181       i2 = 1;
182       equal = 0;
183       
184       /* Try first to associate to same block in the other heap */
185       if(heapinfo2[current_block].type == heapinfo1[current_block].type){
186         
187         if(heapinfo1[current_block].busy_block.busy_size == heapinfo2[current_block].busy_block.busy_size){
188
189           addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
190           
191           add_heap_area_pair(previous, current_block, -1, current_block, -1);
192           
193           if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
194             if(in_mmalloc_ignore((int)current_block, -1))
195               res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 1);
196             else
197               res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
198           }else{
199             res_compare = compare_area(addr_block1, addr_block2, heapinfo1[current_block].busy_block.busy_size, previous, 0);
200           }
201           
202           if(res_compare == 0){
203             for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
204               heapinfo2[current_block+k].busy_block.equal_to = 1 ;
205             for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
206               heapinfo1[current_block+k].busy_block.equal_to = 1 ;
207             equal = 1;
208             match_equals(previous);
209             i1 = i1 + heapinfo1[i1].busy_block.size;
210           }
211
212           xbt_dynar_reset(previous);
213           
214         }
215
216       }
217
218       while(i2 <= heaplimit && !equal){
219
220         if(i2 == current_block){
221           i2++;
222           continue;
223         }
224
225         if(heapinfo2[i2].type != 0){
226           i2++;
227           continue;
228         }
229
230         if(heapinfo2[i2].busy_block.equal_to == 1){         
231           i2++;
232           continue;
233         }
234         
235         if(heapinfo1[i1].busy_block.size != heapinfo2[i2].busy_block.size){
236           i2++;
237           continue;
238         }
239         
240         if(heapinfo1[i1].busy_block.busy_size != heapinfo2[i2].busy_block.busy_size){
241           i2++;
242           continue;
243         }
244
245         addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
246
247         /* Comparison */
248         add_heap_area_pair(previous, i1, -1, i2, -1);
249         
250         if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
251           if(in_mmalloc_ignore((int)i1, -1))
252             res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 1);
253           else
254             res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
255         }else{
256           res_compare = compare_area(addr_block1, addr_block2, heapinfo1[i1].busy_block.busy_size, previous, 0);
257         }
258         
259         if(!res_compare){
260           for(k=1; k < heapinfo2[i2].busy_block.size; k++)
261             heapinfo2[i2+k].busy_block.equal_to = 1;
262           for(k=1; k < heapinfo1[i1].busy_block.size; k++)
263             heapinfo1[i1+k].busy_block.equal_to = 1;
264           equal = 1;
265           match_equals(previous);
266         }
267
268         xbt_dynar_reset(previous);
269
270         i2++;
271
272       }
273
274       if(!equal)
275         i1++;
276       
277     }else{ /* Fragmented block */
278
279       for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
280
281         current_fragment = j1;
282
283         if(heapinfo1[i1].busy_frag.frag_size[j1] == 0) /* Free fragment */
284           continue;
285         
286         addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
287
288         i2 = 1;
289         equal = 0;
290         
291         /* Try first to associate to same fragment in the other heap */
292         if(heapinfo2[current_block].type == heapinfo1[current_block].type){
293           
294           if(heapinfo1[current_block].busy_frag.frag_size[current_fragment] == heapinfo2[current_block].busy_frag.frag_size[current_fragment]){
295
296             addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)heapbase2));
297             addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << heapinfo2[current_block].type));
298
299             add_heap_area_pair(previous, current_block, current_fragment, current_block, current_fragment);
300             
301             if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
302               if(in_mmalloc_ignore((int)current_block, (int)current_fragment))
303                 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 1);
304               else
305                 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
306             }else{
307               res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[current_block].busy_frag.frag_size[current_fragment], previous, 0);
308             }
309
310             if(res_compare == 0){
311               equal = 1;
312               match_equals(previous);
313             }
314             
315             xbt_dynar_reset(previous);
316             
317           }
318
319         }
320
321
322         while(i2 <= heaplimit && !equal){
323           
324           if(heapinfo2[i2].type <= 0){
325             i2++;
326             continue;
327           }
328
329           for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
330
331             if(heapinfo2[i2].type == heapinfo1[i1].type && i2 == current_block && j2 == current_fragment)
332               continue;
333
334             if(heapinfo2[i2].busy_frag.equal_to[j2] == 1)                
335               continue;              
336              
337             if(heapinfo1[i1].busy_frag.frag_size[j1] != heapinfo2[i2].busy_frag.frag_size[j2]) /* Different size_used */    
338               continue;
339              
340             addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)heapbase2));
341             addr_frag2 = (void*) ((char *)addr_block2 + (j2 << heapinfo2[i2].type));
342              
343             /* Comparison */
344             add_heap_area_pair(previous, i1, j1, i2, j2);
345             
346             if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
347               if(in_mmalloc_ignore((int)i1, (int)j1))
348                 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 1);
349               else
350                 res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
351             }else{
352               res_compare = compare_area(addr_frag1, addr_frag2, heapinfo1[i1].busy_frag.frag_size[j1], previous, 0);
353             }
354
355             if(!res_compare){
356               equal = 1;
357               match_equals(previous);
358               xbt_dynar_reset(previous);
359               break;
360             }
361
362             xbt_dynar_reset(previous);
363
364           }
365
366           i2++;
367
368         }
369
370       }
371
372       i1++;
373       
374     }
375
376   }
377
378   /* All blocks/fragments are equal to another block/fragment ? */
379   size_t i = 1, j = 0;
380   int nb_diff1 = 0, nb_diff2 = 0;
381  
382   while(i<heaplimit){
383     if(heapinfo1[i].type == 0){
384       if(heapinfo1[i].busy_block.busy_size > 0){
385         if(heapinfo1[i].busy_block.equal_to == -1){
386           if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
387             addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
388             XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
389             mmalloc_backtrace_block_display((void*)heapinfo1, i);
390           }
391           nb_diff1++;
392         }
393       }
394     }
395     if(heapinfo1[i].type > 0){
396       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
397         if(heapinfo1[i].busy_frag.frag_size[j] > 0){
398           if(heapinfo1[i].busy_frag.equal_to[j] == -1){
399             if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
400               addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
401               addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
402               XBT_DEBUG("Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
403               mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
404             }
405             nb_diff1++;
406           }
407         }
408       }
409     }
410     
411     i++; 
412   }
413
414   XBT_DEBUG("Different blocks or fragments in heap1 : %d\n", nb_diff1);
415
416   i = 1;
417
418   while(i<heaplimit){
419     if(heapinfo2[i].type == 0){
420       if(heapinfo2[i].busy_block.busy_size > 0){
421         if(heapinfo2[i].busy_block.equal_to == -1){
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     if(heapinfo2[i].type > 0){
432       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
433         if(heapinfo2[i].busy_frag.frag_size[j] > 0){
434           if(heapinfo2[i].busy_frag.equal_to[j] == -1){
435             if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
436               addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
437               addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
438               XBT_DEBUG( "Block %zu, Fragment %zu (%p) not found (size used = %d)", i, j, addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
439               mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
440             }
441             nb_diff2++;
442           }
443         }
444       }
445     }
446     i++; 
447   }
448
449   XBT_DEBUG("Different blocks or fragments in heap2 : %d\n", nb_diff2);
450
451   xbt_dynar_free(&previous);
452  
453   return ((nb_diff1 > 0) || (nb_diff2 > 0));
454
455 }
456
457 static int in_mmalloc_ignore(int block, int fragment){
458
459   unsigned int cursor = 0;
460   int start = 0;
461   int end = xbt_dynar_length(mmalloc_ignore) - 1;
462   mc_ignore_region_t region;
463
464   while(start <= end){
465     cursor = (start + end) / 2;
466     region = (mc_ignore_region_t)xbt_dynar_get_as(mmalloc_ignore, cursor, mc_ignore_region_t);
467     if(region->block == block){
468       if(region->fragment == fragment)
469         return 1;
470       if(region->fragment < fragment)
471         start = cursor + 1;
472       if(region->fragment > fragment)
473         end = cursor - 1;
474     }
475     if(region->block < block)
476       start = cursor + 1;
477     if(region->block > block)
478       end = cursor - 1; 
479   }
480
481   return 0;
482 }
483
484 static size_t heap_comparison_ignore(void *address){
485   unsigned int cursor = 0;
486   int start = 0;
487   int end = xbt_dynar_length(mmalloc_ignore) - 1;
488   mc_ignore_region_t region;
489
490   while(start <= end){
491     cursor = (start + end) / 2;
492     region = (mc_ignore_region_t)xbt_dynar_get_as(mmalloc_ignore, cursor, mc_ignore_region_t);
493     if(region->address == address)
494       return region->size;
495     if(region->address < address)
496       start = cursor + 1;
497     if(region->address > address)
498       end = cursor - 1;   
499   }
500
501   return 0;
502 }
503
504
505 static int compare_area(void *area1, void* area2, size_t size, xbt_dynar_t previous, int check_ignore){
506
507   size_t i = 0, pointer_align = 0, ignore1 = 0, ignore2 = 0;
508   void *address_pointed1, *address_pointed2, *addr_block_pointed1, *addr_block_pointed2, *addr_frag_pointed1, *addr_frag_pointed2;
509   size_t block_pointed1, block_pointed2, frag_pointed1, frag_pointed2;
510   int res_compare;
511   void *current_area1, *current_area2;
512  
513   while(i<size){
514
515     if(check_ignore){
516
517       current_area1 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area1) + i) - (char *)heapbase1);
518       if((ignore1 = heap_comparison_ignore(current_area1)) > 0){
519         current_area2 = (char*)((xbt_mheap_t)s_heap)->heapbase + ((((char *)area2) + i) - (char *)heapbase2);
520         if((ignore2 = heap_comparison_ignore(current_area2))  == ignore1){
521           i = i + ignore2;
522           ignore_done++;
523           continue;
524         }
525       }
526
527     }
528    
529     if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
530
531       /* Check pointer difference */
532       pointer_align = (i / sizeof(void*)) * sizeof(void*);
533       address_pointed1 = *((void **)((char *)area1 + pointer_align));
534       address_pointed2 = *((void **)((char *)area2 + pointer_align));
535
536       /* Get pointed blocks number */ 
537       block_pointed1 = ((char*)address_pointed1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
538       block_pointed2 = ((char*)address_pointed2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
539
540       /* Check if valid blocks number */
541       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)
542         return 1;
543
544       if(heapinfo1[block_pointed1].type == heapinfo2[block_pointed2].type){ /* Same type of block (large or fragmented) */
545
546         addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
547         addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
548         
549         if(heapinfo1[block_pointed1].type == 0){ /* Large block */
550
551           if(heapinfo1[block_pointed1].busy_block.size != heapinfo2[block_pointed2].busy_block.size){
552             return 1;
553           }
554
555           if(heapinfo1[block_pointed1].busy_block.busy_size != heapinfo2[block_pointed2].busy_block.busy_size){
556             return 1;
557           }
558
559           if(add_heap_area_pair(previous, block_pointed1, -1, block_pointed2, -1)){
560
561             if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
562               if(in_mmalloc_ignore(block_pointed1, -1))
563                 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 1);
564               else
565                 res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
566             }else{
567               res_compare = compare_area(addr_block_pointed1, addr_block_pointed2, heapinfo1[block_pointed1].busy_block.busy_size, previous, 0);
568             }
569             
570             if(res_compare)    
571               return 1;
572            
573           }
574           
575         }else{ /* Fragmented block */
576
577           /* Get pointed fragments number */ 
578           frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
579           frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
580          
581           if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */    
582             return 1;
583             
584           addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
585           addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
586
587           if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
588
589             if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
590               if(in_mmalloc_ignore(block_pointed1, frag_pointed1))
591                 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
592               else
593                 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
594             }else{
595               res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
596             }
597
598             if(res_compare)
599               return 1;
600            
601           }
602           
603         }
604           
605       }else{
606
607         if((heapinfo1[block_pointed1].type > 0) && (heapinfo2[block_pointed2].type > 0)){
608           
609           addr_block_pointed1 = ((void*) (((ADDR2UINT(block_pointed1)) - 1) * BLOCKSIZE + (char*)heapbase1));
610           addr_block_pointed2 = ((void*) (((ADDR2UINT(block_pointed2)) - 1) * BLOCKSIZE + (char*)heapbase2));
611        
612           frag_pointed1 = ((uintptr_t) (ADDR2UINT (address_pointed1) % (BLOCKSIZE))) >> heapinfo1[block_pointed1].type;
613           frag_pointed2 = ((uintptr_t) (ADDR2UINT (address_pointed2) % (BLOCKSIZE))) >> heapinfo2[block_pointed2].type;
614
615           if(heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1] != heapinfo2[block_pointed2].busy_frag.frag_size[frag_pointed2]) /* Different size_used */    
616             return 1;
617
618           addr_frag_pointed1 = (void*) ((char *)addr_block_pointed1 + (frag_pointed1 << heapinfo1[block_pointed1].type));
619           addr_frag_pointed2 = (void*) ((char *)addr_block_pointed2 + (frag_pointed2 << heapinfo2[block_pointed2].type));
620
621           if(add_heap_area_pair(previous, block_pointed1, frag_pointed1, block_pointed2, frag_pointed2)){
622
623             if(ignore_done < xbt_dynar_length(mmalloc_ignore)){
624               if(in_mmalloc_ignore(block_pointed1, frag_pointed1))
625                 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 1);
626               else
627                 res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
628             }else{
629               res_compare = compare_area(addr_frag_pointed1, addr_frag_pointed2, heapinfo1[block_pointed1].busy_frag.frag_size[frag_pointed1], previous, 0);
630             }
631             
632             if(res_compare)
633               return 1;
634            
635           }
636
637         }else{
638           return 1;
639         }
640
641       }
642
643       i = pointer_align + sizeof(void *);
644       
645     }else{
646
647       i++;
648
649     }
650   }
651
652   return 0;
653   
654
655 }
656
657 static void heap_area_pair_free(heap_area_pair_t pair){
658   if (pair){
659     free(pair);
660     pair = NULL;
661   }
662 }
663
664 static void heap_area_pair_free_voidp(void *d)
665 {
666   heap_area_pair_free((heap_area_pair_t) * (void **) d);
667 }
668
669 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
670
671   if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
672     heap_area_pair_t pair = NULL;
673     pair = xbt_new0(s_heap_area_pair_t, 1);
674     pair->block1 = block1;
675     pair->fragment1 = fragment1;
676     pair->block2 = block2;
677     pair->fragment2 = fragment2;
678     
679     xbt_dynar_push(list, &pair); 
680
681     return 1;
682   }
683
684   return 0;
685 }
686  
687 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
688   
689   unsigned int cursor = 0;
690   heap_area_pair_t current_pair;
691
692   xbt_dynar_foreach(list, cursor, current_pair){
693     if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
694       return 0; 
695   }
696   
697   return 1;
698 }
699
700 static void match_equals(xbt_dynar_t list){
701
702   unsigned int cursor = 0;
703   heap_area_pair_t current_pair;
704
705   xbt_dynar_foreach(list, cursor, current_pair){
706     if(current_pair->fragment1 != -1){
707       heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = 1;
708       heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = 1;
709     }else{
710       heapinfo1[current_pair->block1].busy_block.equal_to = 1;
711       heapinfo2[current_pair->block2].busy_block.equal_to = 1;
712     }
713   }
714
715 }
716
717 #ifndef max
718         #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
719 #endif
720
721 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
722
723   if(heap1 == NULL && heap1 == NULL){
724     XBT_DEBUG("Malloc descriptors null");
725     return 0;
726   }
727
728   if(heap1->heaplimit != heap2->heaplimit){
729     XBT_DEBUG("Different limit of valid info table indices");
730     return 1;
731   }
732
733   /* Heap information */
734   heaplimit = ((struct mdesc *)heap1)->heaplimit;
735
736   s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
737
738   heapbase1 = (char *)heap1 + BLOCKSIZE;
739   heapbase2 = (char *)heap2 + BLOCKSIZE;
740
741   heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
742   heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
743
744   heapsize1 = heap1->heapsize;
745   heapsize2 = heap2->heapsize;
746
747   /* Start comparison */
748   size_t i, j, k;
749   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
750
751   int distance = 0;
752
753   /* Check busy blocks*/
754
755   i = 1;
756
757   while(i <= heaplimit){
758
759     addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
760     addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
761
762     if(heapinfo1[i].type != heapinfo2[i].type){
763   
764       distance += BLOCKSIZE;
765       XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
766       i++;
767     
768     }else{
769
770       if(heapinfo1[i].type == -1){ /* Free block */
771         i++;
772         continue;
773       }
774
775       if(heapinfo1[i].type == 0){ /* Large block */
776        
777         if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
778           distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
779           i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
780           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);
781           continue;
782         }
783
784         /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
785           distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
786           i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
787           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);
788           continue;
789           }*/
790
791         k = 0;
792
793         //while(k < (heapinfo1[i].busy_block.busy_size)){
794         while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
795           if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
796             distance ++;
797           }
798           k++;
799         } 
800
801         i++;
802
803       }else { /* Fragmented block */
804
805         for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
806
807           addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
808           addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
809
810           if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
811             continue;
812           }
813           
814           
815           /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
816             distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
817             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); 
818             continue;
819             }*/
820    
821           k=0;
822
823           //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
824           while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
825             if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
826               distance ++;
827             }
828             k++;
829           }
830
831         }
832
833         i++;
834
835       }
836       
837     }
838
839   }
840
841   return distance;
842   
843 }
844