Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
819b161cb460715ebbd803d8301f24e8cabf8dd7
[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 #include "mc/datatypes.h"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mm_diff, xbt,
15                                 "Logging specific to mm_diff in mmalloc");
16
17 xbt_dynar_t mc_heap_comparison_ignore;
18 xbt_dynar_t stacks_areas;
19 void *maestro_stack_start, *maestro_stack_end;
20
21
22 /********************************* Backtrace ***********************************/
23 /******************************************************************************/
24
25 static void mmalloc_backtrace_block_display(void* heapinfo, int block){
26
27   /* xbt_ex_t e; */
28
29   /* if (((malloc_info *)heapinfo)[block].busy_block.bt_size == 0) { */
30   /*   fprintf(stderr, "No backtrace available for that block, sorry.\n"); */
31   /*   return; */
32   /* } */
33
34   /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_block.bt),sizeof(void*)*XBT_BACKTRACE_SIZE); */
35   /* e.used = ((malloc_info *)heapinfo)[block].busy_block.bt_size; */
36
37   /* xbt_ex_setup_backtrace(&e); */
38   /* if (e.used == 0) { */
39   /*   fprintf(stderr, "(backtrace not set)\n"); */
40   /* } else if (e.bt_strings == NULL) { */
41   /*   fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
42   /* } else { */
43   /*   int i; */
44
45   /*   fprintf(stderr, "Backtrace of where the block %d was malloced (%d frames):\n", block ,e.used); */
46   /*   for (i = 0; i < e.used; i++)       /\* no need to display "xbt_backtrace_display" *\/{ */
47   /*     fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
48   /*   } */
49   /* } */
50 }
51
52 static void mmalloc_backtrace_fragment_display(void* heapinfo, int block, int frag){
53
54   /* xbt_ex_t e; */
55
56   /* memcpy(&e.bt,&(((malloc_info *)heapinfo)[block].busy_frag.bt[frag]),sizeof(void*)*XBT_BACKTRACE_SIZE); */
57   /* e.used = XBT_BACKTRACE_SIZE; */
58
59   /* xbt_ex_setup_backtrace(&e); */
60   /* if (e.used == 0) { */
61   /*   fprintf(stderr, "(backtrace not set)\n"); */
62   /* } else if (e.bt_strings == NULL) { */
63   /*   fprintf(stderr, "(backtrace not ready to be computed. %s)\n",xbt_binary_name?"Dunno why":"xbt_binary_name not setup yet"); */
64   /* } else { */
65   /*   int i; */
66
67   /*   fprintf(stderr, "Backtrace of where the fragment %d in block %d was malloced (%d frames):\n", frag, block ,e.used); */
68   /*   for (i = 0; i < e.used; i++)       /\* no need to display "xbt_backtrace_display" *\/{ */
69   /*     fprintf(stderr, "%d ---> %s\n",i, e.bt_strings[i] + 4); */
70   /*   } */
71   /* } */
72
73 }
74
75 static void mmalloc_backtrace_display(void *addr){
76
77   /* size_t block, frag_nb; */
78   /* int type; */
79   
80   /* xbt_mheap_t heap = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit(); */
81
82   /* block = (((char*) (addr) - (char*) heap -> heapbase) / BLOCKSIZE + 1); */
83
84   /* type = heap->heapinfo[block].type; */
85
86   /* switch(type){ */
87   /* case -1 : /\* Free block *\/ */
88   /*   fprintf(stderr, "Asked to display the backtrace of a block that is free. I'm puzzled\n"); */
89   /*   xbt_abort(); */
90   /*   break;  */
91   /* case 0: /\* Large block *\/ */
92   /*   mmalloc_backtrace_block_display(heap->heapinfo, block); */
93   /*   break; */
94   /* default: /\* Fragmented block *\/ */
95   /*   frag_nb = RESIDUAL(addr, BLOCKSIZE) >> type; */
96   /*   if(heap->heapinfo[block].busy_frag.frag_size[frag_nb] == -1){ */
97   /*     fprintf(stderr , "Asked to display the backtrace of a fragment that is free. I'm puzzled\n"); */
98   /*     xbt_abort(); */
99   /*   } */
100   /*   mmalloc_backtrace_fragment_display(heap->heapinfo, block, frag_nb); */
101   /*   break; */
102   /* } */
103 }
104
105
106 static int compare_backtrace(int b1, int f1, int b2, int f2){
107   /*int i = 0;
108   if(f1 != -1){
109     for(i=0; i< XBT_BACKTRACE_SIZE; i++){
110       if(heapinfo1[b1].busy_frag.bt[f1][i] != heapinfo2[b2].busy_frag.bt[f2][i]){
111         //mmalloc_backtrace_fragment_display((void*)heapinfo1, b1, f1);
112         //mmalloc_backtrace_fragment_display((void*)heapinfo2, b2, f2);
113         return 1;
114       }
115     }
116   }else{
117     for(i=0; i< heapinfo1[b1].busy_block.bt_size; i++){
118       if(heapinfo1[b1].busy_block.bt[i] != heapinfo2[b2].busy_block.bt[i]){
119         //mmalloc_backtrace_block_display((void*)heapinfo1, b1);
120         //mmalloc_backtrace_block_display((void*)heapinfo2, b2);
121         return 1;
122       }
123     }
124     }*/
125   return 0;
126 }
127
128
129 /*********************************** Heap comparison ***********************************/
130 /***************************************************************************************/
131
132 void *s_heap = NULL, *heapbase1 = NULL, *heapbase2 = NULL;
133 malloc_info *heapinfo1 = NULL, *heapinfo2 = NULL;
134 size_t heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
135 xbt_dynar_t to_ignore1 = NULL, to_ignore2 = NULL;
136
137 /*********************************** Free functions ************************************/
138
139 static void heap_area_pair_free(heap_area_pair_t pair){
140   xbt_free(pair);
141   pair = NULL;
142 }
143
144 static void heap_area_pair_free_voidp(void *d){
145   heap_area_pair_free((heap_area_pair_t) * (void **) d);
146 }
147
148 /************************************************************************************/
149
150 static heap_area_t new_heap_area(int block, int fragment){
151   heap_area_t area = NULL;
152   area = xbt_new0(s_heap_area_t, 1);
153   area->block = block;
154   area->fragment = fragment;
155   return area;
156 }
157
158  
159 static int is_new_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
160   
161   unsigned int cursor = 0;
162   heap_area_pair_t current_pair;
163
164   xbt_dynar_foreach(list, cursor, current_pair){
165     if(current_pair->block1 == block1 && current_pair->block2 == block2 && current_pair->fragment1 == fragment1 && current_pair->fragment2 == fragment2)
166       return 0; 
167   }
168   
169   return 1;
170 }
171
172 static int add_heap_area_pair(xbt_dynar_t list, int block1, int fragment1, int block2, int fragment2){
173
174   if(is_new_heap_area_pair(list, block1, fragment1, block2, fragment2)){
175     heap_area_pair_t pair = NULL;
176     pair = xbt_new0(s_heap_area_pair_t, 1);
177     pair->block1 = block1;
178     pair->fragment1 = fragment1;
179     pair->block2 = block2;
180     pair->fragment2 = fragment2;
181     
182     xbt_dynar_push(list, &pair); 
183
184     return 1;
185   }
186
187   return 0;
188 }
189
190 static size_t heap_comparison_ignore_size(xbt_dynar_t ignore_list, void *address){
191
192   unsigned int cursor = 0;
193   int start = 0;
194   int end = xbt_dynar_length(ignore_list) - 1;
195   mc_heap_ignore_region_t region;
196
197   while(start <= end){
198     cursor = (start + end) / 2;
199     region = (mc_heap_ignore_region_t)xbt_dynar_get_as(ignore_list, cursor, mc_heap_ignore_region_t);
200     if(region->address == address)
201       return region->size;
202     if(region->address < address)
203       start = cursor + 1;
204     if(region->address > address)
205       end = cursor - 1;   
206   }
207
208   return 0;
209 }
210
211 static int is_stack(void *address){
212   unsigned int cursor = 0;
213   stack_region_t stack;
214
215   xbt_dynar_foreach(stacks_areas, cursor, stack){
216     if(address == stack->address)
217       return 1;
218   }
219
220   return 0;
221 }
222
223 static int is_block_stack(int block){
224   unsigned int cursor = 0;
225   stack_region_t stack;
226
227   xbt_dynar_foreach(stacks_areas, cursor, stack){
228     if(block == stack->block)
229       return 1;
230   }
231
232   return 0;
233 }
234
235 static void match_equals(xbt_dynar_t list){
236
237   unsigned int cursor = 0;
238   heap_area_pair_t current_pair;
239   heap_area_t previous_area;
240
241   xbt_dynar_foreach(list, cursor, current_pair){
242
243     if(current_pair->fragment1 != -1){
244       
245       if(heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] != NULL){    
246         previous_area = heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1];
247         xbt_free(heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
248         heapinfo2[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
249         xbt_free(previous_area); 
250       }
251       if(heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] != NULL){        
252         previous_area = heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2];
253         xbt_free(heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment]);
254         heapinfo1[previous_area->block].busy_frag.equal_to[previous_area->fragment] = NULL;
255         xbt_free(previous_area);
256       }
257
258       heapinfo1[current_pair->block1].busy_frag.equal_to[current_pair->fragment1] = new_heap_area(current_pair->block2, current_pair->fragment2);
259       heapinfo2[current_pair->block2].busy_frag.equal_to[current_pair->fragment2] = new_heap_area(current_pair->block1, current_pair->fragment1);
260       
261     }else{
262
263       if(heapinfo1[current_pair->block1].busy_block.equal_to != NULL){
264         previous_area = heapinfo1[current_pair->block1].busy_block.equal_to;
265         xbt_free(heapinfo2[previous_area->block].busy_block.equal_to);
266         heapinfo2[previous_area->block].busy_block.equal_to = NULL; 
267         xbt_free(previous_area);
268       }
269       if(heapinfo2[current_pair->block2].busy_block.equal_to != NULL){
270         previous_area = heapinfo2[current_pair->block2].busy_block.equal_to;
271         xbt_free(heapinfo1[previous_area->block].busy_block.equal_to);
272         heapinfo1[previous_area->block].busy_block.equal_to = NULL;
273         xbt_free(previous_area);
274       }
275
276       heapinfo1[current_pair->block1].busy_block.equal_to = new_heap_area(current_pair->block2, current_pair->fragment2);
277       heapinfo2[current_pair->block2].busy_block.equal_to = new_heap_area(current_pair->block1, current_pair->fragment1);
278
279     }
280
281   }
282 }
283
284 static int equal_blocks(int b1, int b2){
285   if(heapinfo1[b1].busy_block.equal_to != NULL){
286     if(heapinfo2[b2].busy_block.equal_to != NULL){
287       if(((heap_area_t)(heapinfo1[b1].busy_block.equal_to))->block == b2 && ((heap_area_t)(heapinfo2[b2].busy_block.equal_to))->block == b1)
288         return 1;
289     }
290   }
291   return 0;
292 }
293
294 static int equal_fragments(int b1, int f1, int b2, int f2){
295   if(heapinfo1[b1].busy_frag.equal_to[f1] != NULL){
296     if(heapinfo2[b2].busy_frag.equal_to[f2] != NULL){
297       if(((heap_area_t)(heapinfo1[b1].busy_frag.equal_to[f1]))->block == b2 && ((heap_area_t)(heapinfo2[b2].busy_frag.equal_to[f2]))->block == b1 && ((heap_area_t)(heapinfo1[b1].busy_frag.equal_to[f1]))->fragment == f2 && ((heap_area_t)(heapinfo2[b2].busy_frag.equal_to[f2]))->fragment == f1)
298         return 1;
299     }
300   }
301   return 0;
302 }
303
304 void init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t i1, xbt_dynar_t i2){
305
306   heaplimit = ((struct mdesc *)heap1)->heaplimit;
307
308   s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
309
310   heapbase1 = (char *)heap1 + BLOCKSIZE;
311   heapbase2 = (char *)heap2 + BLOCKSIZE;
312
313   heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)((struct mdesc *)heap1)->heapinfo - (char *)s_heap)));
314   heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)((struct mdesc *)heap2)->heapinfo - (char *)s_heap)));
315
316   heapsize1 = heap1->heapsize;
317   heapsize2 = heap2->heapsize;
318
319   to_ignore1 = i1;
320   to_ignore2 = i2;
321
322   if(MC_is_active()){
323     MC_ignore_global_variable("heaplimit", 1);
324     MC_ignore_global_variable("s_heap", 1);
325     MC_ignore_global_variable("heapbase1", 1);
326     MC_ignore_global_variable("heapbase2", 1);
327     MC_ignore_global_variable("heapinfo1", 1);
328     MC_ignore_global_variable("heapinfo2", 1);
329     MC_ignore_global_variable("heapsize1", 1);
330     MC_ignore_global_variable("heapsize2", 1);
331     MC_ignore_global_variable("to_ignore1", 1);
332     MC_ignore_global_variable("to_ignore2", 1);
333   }
334
335 }
336
337 void reset_heap_information(){
338
339   size_t i = 0, j;
340
341   while(i<=heaplimit){
342     if(heapinfo1[i].type == 0){
343       xbt_free(heapinfo1[i].busy_block.equal_to);
344       heapinfo1[i].busy_block.equal_to = NULL;
345     }
346     if(heapinfo1[i].type > 0){
347       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
348         xbt_free(heapinfo1[i].busy_frag.equal_to[j]);
349         heapinfo1[i].busy_frag.equal_to[j] = NULL;
350       }
351     }
352     i++; 
353   }
354
355   i = 0;
356
357   while(i<=heaplimit){
358     if(heapinfo2[i].type == 0){
359       xbt_free(heapinfo2[i].busy_block.equal_to);
360       heapinfo2[i].busy_block.equal_to = NULL;
361     }
362     if(heapinfo2[i].type > 0){
363       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
364         xbt_free(heapinfo2[i].busy_frag.equal_to[j]);
365         heapinfo2[i].busy_frag.equal_to[j] = NULL;
366       }
367     }
368     i++; 
369   }
370
371   s_heap = NULL, heapbase1 = NULL, heapbase2 = NULL;
372   heapinfo1 = NULL, heapinfo2 = NULL;
373   heaplimit = 0, heapsize1 = 0, heapsize2 = 0;
374   to_ignore1 = NULL, to_ignore2 = NULL;
375
376 }
377
378 int mmalloc_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
379
380   if(heap1 == NULL && heap1 == NULL){
381     XBT_DEBUG("Malloc descriptors null");
382     return 0;
383   }
384
385   if(heap1->heaplimit != heap2->heaplimit){
386     XBT_DEBUG("Different limit of valid info table indices");
387     return 1;
388   }
389
390   /* Start comparison */
391   size_t i1, i2, j1, j2, k;
392   size_t current_block = -1;    /* avoid "maybe uninitialized" warning */
393   size_t current_fragment;
394   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
395   int nb_diff1 = 0, nb_diff2 = 0;
396
397   xbt_dynar_t previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
398
399   int equal, res_compare = 0;
400
401   /* Check busy blocks*/
402
403   i1 = 1;
404
405   while(i1 <= heaplimit){
406
407     current_block = i1;
408
409     if(heapinfo1[i1].type == -1){ /* Free block */
410       i1++;
411       continue;
412     }
413
414     addr_block1 = ((void*) (((ADDR2UINT(i1)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
415
416     if(heapinfo1[i1].type == 0){  /* Large block */
417       
418       if(is_stack(addr_block1)){
419         for(k=0; k < heapinfo1[i1].busy_block.size; k++)
420           heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i1, -1);
421         for(k=0; k < heapinfo2[i1].busy_block.size; k++)
422           heapinfo2[i1+k].busy_block.equal_to = new_heap_area(i1, -1);
423         i1 = i1 + heapinfo1[current_block].busy_block.size;
424         continue;
425       }
426
427       if(heapinfo1[i1].busy_block.equal_to != NULL){
428         i1++;
429         continue;
430       }
431     
432       i2 = 1;
433       equal = 0;
434       res_compare = 0;
435   
436       /* Try first to associate to same block in the other heap */
437       if(heapinfo2[current_block].type == heapinfo1[current_block].type){
438
439         if(heapinfo2[current_block].busy_block.equal_to == NULL){  
440         
441           addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
442         
443           res_compare = compare_heap_area(addr_block1, addr_block2, NULL, NULL, NULL, NULL, 0); /*FIXME*/
444         
445           if(res_compare == 0){
446             for(k=1; k < heapinfo2[current_block].busy_block.size; k++)
447               heapinfo2[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
448             for(k=1; k < heapinfo1[current_block].busy_block.size; k++)
449               heapinfo1[current_block+k].busy_block.equal_to = new_heap_area(i1, -1);
450             equal = 1;
451             i1 = i1 + heapinfo1[current_block].busy_block.size;
452           }
453         
454           xbt_dynar_reset(previous);
455         
456         }
457         
458       }
459
460       while(i2 <= heaplimit && !equal){
461
462         addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));        
463            
464         if(i2 == current_block){
465           i2++;
466           continue;
467         }
468
469         if(heapinfo2[i2].type != 0){
470           i2++;
471           continue;
472         }
473
474         if(heapinfo2[i2].busy_block.equal_to != NULL){         
475           i2++;
476           continue;
477         }
478           
479         res_compare = compare_heap_area(addr_block1, addr_block2, NULL, NULL, NULL, NULL, 0); /*FIXME */
480         
481         if(res_compare == 0){
482           for(k=1; k < heapinfo2[i2].busy_block.size; k++)
483             heapinfo2[i2+k].busy_block.equal_to = new_heap_area(i1, -1);
484           for(k=1; k < heapinfo1[i1].busy_block.size; k++)
485             heapinfo1[i1+k].busy_block.equal_to = new_heap_area(i2, -1);
486           equal = 1;
487           i1 = i1 + heapinfo1[i1].busy_block.size;
488         }
489
490         xbt_dynar_reset(previous);
491
492         i2++;
493
494       }
495
496       if(!equal){
497         XBT_DEBUG("Block %zu not found (size_used = %zu, addr = %p)", i1, heapinfo1[i1].busy_block.busy_size, addr_block1);
498         i1 = heaplimit + 1;
499         nb_diff1++;
500           //i1++;
501       }
502       
503     }else{ /* Fragmented block */
504
505       for(j1=0; j1 < (size_t) (BLOCKSIZE >> heapinfo1[i1].type); j1++){
506
507         current_fragment = j1;
508
509         if(heapinfo1[i1].busy_frag.frag_size[j1] == -1) /* Free fragment */
510           continue;
511
512         if(heapinfo1[i1].busy_frag.equal_to[j1] != NULL)
513           continue;
514
515         addr_frag1 = (void*) ((char *)addr_block1 + (j1 << heapinfo1[i1].type));
516
517         i2 = 1;
518         equal = 0;
519         
520         /* Try first to associate to same fragment in the other heap */
521         if(heapinfo2[current_block].type == heapinfo1[current_block].type){
522
523           if(heapinfo2[current_block].busy_frag.equal_to[current_fragment] == NULL){  
524           
525             addr_block2 = ((void*) (((ADDR2UINT(current_block)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
526             addr_frag2 = (void*) ((char *)addr_block2 + (current_fragment << ((xbt_mheap_t)s_heap)->heapinfo[current_block].type));
527
528             res_compare = compare_heap_area(addr_frag1, addr_frag2, NULL, NULL, NULL, NULL, 0); /*FIXME*/
529
530             if(res_compare == 0)
531               equal = 1;
532         
533             xbt_dynar_reset(previous);
534
535           }
536
537         }
538
539         while(i2 <= heaplimit && !equal){
540
541           
542           if(heapinfo2[i2].type <= 0){
543             i2++;
544             continue;
545           }
546
547           for(j2=0; j2 < (size_t) (BLOCKSIZE >> heapinfo2[i2].type); j2++){
548
549             if(i2 == current_block && j2 == current_fragment)
550               continue;
551
552             if(heapinfo2[i2].busy_frag.equal_to[j2] != NULL)                
553               continue;            
554                           
555             addr_block2 = ((void*) (((ADDR2UINT(i2)) - 1) * BLOCKSIZE + (char*)((xbt_mheap_t)s_heap)->heapbase));
556             addr_frag2 = (void*) ((char *)addr_block2 + (j2 << ((xbt_mheap_t)s_heap)->heapinfo[i2].type));
557
558             res_compare = compare_heap_area(addr_frag1, addr_frag2, NULL, NULL, NULL, NULL, 0); /*FIXME*/
559             
560             if(res_compare == 0){
561               equal = 1;
562               xbt_dynar_reset(previous);
563               break;
564             }
565
566             xbt_dynar_reset(previous);
567
568           }
569
570           i2++;
571
572         }
573
574         if(heapinfo1[i1].busy_frag.equal_to[j1] == NULL){
575           fprintf(stderr,"Block %zu, fragment %zu not found (size_used = %zd, address = %p, ignore %d)\n", i1, j1, heapinfo1[i1].busy_frag.frag_size[j1], addr_frag1, heapinfo1[i1].busy_frag.ignore[j1]);
576           i2 = heaplimit + 1;
577           i1 = heaplimit + 1;
578           nb_diff1++;
579           break;
580         }
581
582       }
583
584       i1++;
585       
586     }
587
588   }
589
590   /* All blocks/fragments are equal to another block/fragment ? */
591   size_t i = 1, j = 0;
592   void *real_addr_frag1 = NULL, *real_addr_block1 = NULL, *real_addr_block2 = NULL, *real_addr_frag2 = NULL;
593  
594   while(i<=heaplimit){
595     if(heapinfo1[i].type == 0){
596       if(current_block == heaplimit){
597         if(heapinfo1[i].busy_block.busy_size > 0){
598           if(heapinfo1[i].busy_block.equal_to == NULL){
599             if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
600               addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
601               XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block1, heapinfo1[i].busy_block.busy_size);
602               //mmalloc_backtrace_block_display((void*)heapinfo1, i);
603             }
604             nb_diff1++;
605           }
606         }
607       }
608     }
609     if(heapinfo1[i].type > 0){
610       addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
611       real_addr_block1 =  ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)s_heap)->heapbase));
612       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
613         if(current_block == heaplimit){
614           if(heapinfo1[i].busy_frag.frag_size[j] > 0){
615             if(heapinfo1[i].busy_frag.equal_to[j] == NULL){
616               if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
617                 addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
618                 real_addr_frag1 = (void*) ((char *)real_addr_block1 + (j << ((struct mdesc *)s_heap)->heapinfo[i].type));
619                 XBT_DEBUG("Block %zu, Fragment %zu (%p - %p) not found (size used = %zd)", i, j, addr_frag1, real_addr_frag1, heapinfo1[i].busy_frag.frag_size[j]);
620                 //mmalloc_backtrace_fragment_display((void*)heapinfo1, i, j);
621               }
622               nb_diff1++;
623             }
624           }
625         }
626       }
627     }
628     i++; 
629   }
630
631   if(current_block == heaplimit)
632     XBT_DEBUG("Number of blocks/fragments not found in heap1 : %d", nb_diff1);
633
634   i = 1;
635
636   while(i<=heaplimit){
637     if(heapinfo2[i].type == 0){
638       if(current_block == heaplimit){
639         if(heapinfo2[i].busy_block.busy_size > 0){
640           if(heapinfo2[i].busy_block.equal_to == NULL){
641             if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
642               addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
643               XBT_DEBUG("Block %zu (%p) not found (size used = %zu)", i, addr_block2, heapinfo2[i].busy_block.busy_size);
644               //mmalloc_backtrace_block_display((void*)heapinfo2, i);
645             }
646             nb_diff2++;
647           }
648         }
649       }
650     }
651     if(heapinfo2[i].type > 0){
652       addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
653       real_addr_block2 =  ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)((struct mdesc *)s_heap)->heapbase));
654       for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo2[i].type); j++){
655         if(current_block == heaplimit){
656           if(heapinfo2[i].busy_frag.frag_size[j] > 0){
657             if(heapinfo2[i].busy_frag.equal_to[j] == NULL){
658               if(XBT_LOG_ISENABLED(mm_diff, xbt_log_priority_debug)){
659                 addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
660                 real_addr_frag2 = (void*) ((char *)real_addr_block2 + (j << ((struct mdesc *)s_heap)->heapinfo[i].type));
661                 XBT_DEBUG( "Block %zu, Fragment %zu (%p - %p) not found (size used = %zd)", i, j, addr_frag2, real_addr_frag2, heapinfo2[i].busy_frag.frag_size[j]);
662                 //mmalloc_backtrace_fragment_display((void*)heapinfo2, i, j);
663               }
664               nb_diff2++;
665             }
666           }
667         }
668       }
669     }
670     i++; 
671   }
672
673   if(current_block == heaplimit)
674     XBT_DEBUG("Number of blocks/fragments not found in heap2 : %d", nb_diff2);
675
676   xbt_dynar_free(&previous);
677   real_addr_frag1 = NULL, real_addr_block1 = NULL, real_addr_block2 = NULL, real_addr_frag2 = NULL;
678
679   return ((nb_diff1 > 0) || (nb_diff2 > 0));
680 }
681
682 static int compare_heap_area_without_type(void *real_area1, void *real_area2, void *area1, void *area2, xbt_dynar_t previous, xbt_dict_t all_types, xbt_dict_t other_types, int size, int check_ignore){
683
684   int i = 0;
685   void *addr_pointed1, *addr_pointed2;
686   int pointer_align, ignore1, ignore2, res_compare;
687
688   while(i<size){
689
690     if(check_ignore > 0){
691       if((ignore1 = heap_comparison_ignore_size(to_ignore1, (char *)real_area1 + i)) > 0){
692         if((ignore2 = heap_comparison_ignore_size(to_ignore2, (char *)real_area2 + i))  == ignore1){
693           i = i + ignore2;
694           check_ignore--;
695           continue;
696         }
697       }
698     }
699
700     if(memcmp(((char *)area1) + i, ((char *)area2) + i, 1) != 0){
701
702       pointer_align = (i / sizeof(void*)) * sizeof(void*);
703       addr_pointed1 = *((void **)((char *)area1 + pointer_align));
704       addr_pointed2 = *((void **)((char *)area2 + pointer_align));
705       
706       if(addr_pointed1 > maestro_stack_start && addr_pointed1 < maestro_stack_end && addr_pointed2 > maestro_stack_start && addr_pointed2 < maestro_stack_end){
707         i = pointer_align + sizeof(void *);
708         continue;
709       }else if((addr_pointed1 > s_heap) && ((char *)addr_pointed1 < (char *)s_heap + STD_HEAP_SIZE) 
710                && (addr_pointed2 > s_heap) && ((char *)addr_pointed2 < (char *)s_heap + STD_HEAP_SIZE)){
711         res_compare = compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, NULL, 0); 
712         if(res_compare != 0){
713           return res_compare;
714         }
715         i = pointer_align + sizeof(void *);
716         continue;
717       }else{
718         return 1;
719       }
720       
721     }
722     
723     i++;
724
725   }
726
727   return 0;
728  
729 }
730
731
732 static int compare_heap_area_with_type(void *real_area1, void *real_area2, void *area1, void *area2, 
733                                        xbt_dynar_t previous, xbt_dict_t all_types, xbt_dict_t other_types, char *type_id, 
734                                        int area_size, int check_ignore, int pointer_level){
735
736   if(is_stack(real_area1) && is_stack(real_area2))
737     return 0;
738
739   size_t ignore1, ignore2;
740
741   if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2))  == ignore1))
742     return 0;
743   
744   dw_type_t type = xbt_dict_get_or_null(all_types, type_id);
745   dw_type_t subtype, subsubtype;
746   int res, elm_size, i, switch_types = 0;
747   unsigned int cursor = 0;
748   dw_type_t member;
749   void *addr_pointed1, *addr_pointed2;;
750   char *type_desc;
751
752   switch(type->type){
753   case e_dw_base_type:
754     if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2))  == ignore1))
755       return 0;
756     if(strcmp(type->name, "char") == 0){ /* Chaine de caractères, donc taille aléatoire */
757       return (memcmp(area1, area2, area_size) != 0);
758     }else{
759       if(area_size != -1 && type->size != area_size)
760         return -1;
761       else
762         return (memcmp(area1, area2, type->size) != 0);
763     }
764     break;
765   case e_dw_enumeration_type:
766     if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2))  == ignore1))
767       return 0;
768     if(area_size != -1 && type->size != area_size)
769       return -1;
770     else
771       return (memcmp(area1, area2, type->size) != 0);
772     break;
773   case e_dw_typedef:
774     return compare_heap_area_with_type(real_area1, real_area2, area1, area2, previous, all_types, other_types, type->dw_type_id, area_size, check_ignore, pointer_level);
775     break;
776   case e_dw_const_type:
777     return 0;
778     break;
779   case e_dw_array_type:
780     subtype = xbt_dict_get_or_null(all_types, type->dw_type_id);
781     switch(subtype->type){
782     case e_dw_base_type:
783     case e_dw_enumeration_type:
784     case e_dw_pointer_type:
785     case e_dw_structure_type:
786     case e_dw_union_type:
787       if(subtype->size == 0){ /*declaration of the type, need the complete description */
788         type_desc = get_type_description(all_types, subtype->name);
789         if(type_desc){
790           subtype = xbt_dict_get_or_null(all_types, type_desc);
791         }else{
792           subtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subtype->name));
793           switch_types = 1;
794         }
795       }
796       elm_size = subtype->size;
797       break;
798     case e_dw_typedef:
799     case e_dw_volatile_type:
800       subsubtype = xbt_dict_get_or_null(all_types, subtype->dw_type_id);
801       if(subsubtype->size == 0){ /*declaration of the type, need the complete description */
802         type_desc = get_type_description(all_types, subsubtype->name);
803         if(type_desc){
804           subsubtype = xbt_dict_get_or_null(all_types, type_desc);
805         }else{
806           subsubtype = xbt_dict_get_or_null(other_types, get_type_description(other_types, subtype->name));
807           switch_types = 1;
808         }
809       }
810       elm_size = subsubtype->size;
811       break;
812     default : 
813       return 0;
814       break;
815     }
816     for(i=0; i<type->size; i++){
817       if(switch_types)
818         res = compare_heap_area_with_type((char *)real_area1 + (i*elm_size), (char *)real_area2 + (i*elm_size), (char *)area1 + (i*elm_size), (char *)area2 + (i*elm_size), previous, other_types, all_types, type->dw_type_id, type->size, check_ignore, pointer_level);
819       else
820         res = compare_heap_area_with_type((char *)real_area1 + (i*elm_size), (char *)real_area2 + (i*elm_size), (char *)area1 + (i*elm_size), (char *)area2 + (i*elm_size), previous, all_types, other_types, type->dw_type_id, type->size, check_ignore, pointer_level);
821       if(res != 0)
822         return res;
823     }
824     break;
825   case e_dw_pointer_type:
826     if(type->dw_type_id && ((dw_type_t)xbt_dict_get_or_null(all_types, type->dw_type_id))->type == e_dw_subroutine_type){
827       addr_pointed1 = *((void **)(area1)); 
828       addr_pointed2 = *((void **)(area2));
829       return (addr_pointed1 != addr_pointed2);;
830     }else{
831       pointer_level++;
832       if(pointer_level > 1){ /* Array of pointers */
833         for(i=0; i<(area_size/sizeof(void *)); i++){
834           addr_pointed1 = *((void **)((char *)area1 + (i*sizeof(void *)))); 
835           addr_pointed2 = *((void **)((char *)area2 + (i*sizeof(void *)))); 
836           if(addr_pointed1 > s_heap && (char *)addr_pointed1 < (char*) s_heap + STD_HEAP_SIZE && addr_pointed2 > s_heap && (char *)addr_pointed2 < (char*) s_heap + STD_HEAP_SIZE)
837             res =  compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, type->dw_type_id, pointer_level); 
838           else
839             res =  (addr_pointed1 != addr_pointed2);
840           if(res != 0)
841             return res;
842         }
843       }else{
844         addr_pointed1 = *((void **)(area1)); 
845         addr_pointed2 = *((void **)(area2));
846         if(addr_pointed1 > s_heap && (char *)addr_pointed1 < (char*) s_heap + STD_HEAP_SIZE && addr_pointed2 > s_heap && (char *)addr_pointed2 < (char*) s_heap + STD_HEAP_SIZE)
847           return compare_heap_area(addr_pointed1, addr_pointed2, previous, all_types, other_types, type->dw_type_id, pointer_level); 
848         else
849           return  (addr_pointed1 != addr_pointed2);
850       }
851     }
852     break;
853   case e_dw_structure_type:
854     if(type->size == 0){ /*declaration of the structure, need the complete description */
855       type_desc = get_type_description(all_types, type->name);
856       if(type_desc){
857         type = xbt_dict_get_or_null(all_types, type_desc);
858       }else{
859         type = xbt_dict_get_or_null(other_types, get_type_description(other_types, type->name));
860         switch_types = 1;
861       }
862     }
863     if(area_size != -1 && type->size != area_size){
864       if(area_size>type->size && area_size%type->size == 0){
865         for(i=0; i<(area_size/type->size); i++){
866           if(switch_types)
867             res = compare_heap_area_with_type((char *)real_area1 + (i*type->size), (char *)real_area2 + (i*type->size), (char *)area1 + (i*type->size), (char *)area2 + (i*type->size), previous, other_types, all_types, type_id, -1, check_ignore, 0); 
868           else
869             res = compare_heap_area_with_type((char *)real_area1 + (i*type->size), (char *)real_area2 + (i*type->size), (char *)area1 + (i*type->size), (char *)area2 + (i*type->size), previous, all_types, other_types, type_id, -1, check_ignore, 0); 
870           if(res != 0)
871             return res;
872         }
873       }else{
874         return -1;
875       }
876     }else{
877       cursor = 0;
878       xbt_dynar_foreach(type->members, cursor, member){
879         if(switch_types)
880           res = compare_heap_area_with_type((char *)real_area1 + member->offset, (char *)real_area2 + member->offset, (char *)area1 + member->offset, (char *)area2 + member->offset, previous, other_types, all_types, member->dw_type_id, -1, check_ignore, 0);
881         else
882           res = compare_heap_area_with_type((char *)real_area1 + member->offset, (char *)real_area2 + member->offset, (char *)area1 + member->offset, (char *)area2 + member->offset, previous, all_types, other_types, member->dw_type_id, -1, check_ignore, 0);  
883         if(res != 0)
884           return res;        
885       }
886     }
887     break;
888   case e_dw_union_type:
889     if((check_ignore > 0) && ((ignore1 = heap_comparison_ignore_size(to_ignore1, real_area1)) > 0) && ((ignore2 = heap_comparison_ignore_size(to_ignore2, real_area2))  == ignore1))
890       return 0;
891     return compare_heap_area_without_type(real_area1, real_area2, area1, area2, previous, all_types, other_types, type->size, check_ignore);
892     break;
893   case e_dw_volatile_type:
894     return compare_heap_area_with_type(real_area1, real_area2, area1, area2, previous, all_types, other_types, type->dw_type_id, area_size, check_ignore, pointer_level);
895     break;
896   default:
897     break;
898   }
899
900   return 0;
901
902 }
903
904 int compare_heap_area(void *area1, void* area2, xbt_dynar_t previous, xbt_dict_t all_types, xbt_dict_t other_types, char *type_id, int pointer_level){
905
906   int res_compare;
907   ssize_t block1, frag1, block2, frag2;
908   ssize_t size;
909   int check_ignore = 0;
910
911   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
912   void *area1_to_compare, *area2_to_compare;
913
914   int match_pairs = 0;
915
916   if(previous == NULL){
917     previous = xbt_dynar_new(sizeof(heap_area_pair_t), heap_area_pair_free_voidp);
918     match_pairs = 1;
919   }
920
921   block1 = ((char*)area1 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
922   block2 = ((char*)area2 - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
923
924   if(is_block_stack((int)block1) && is_block_stack((int)block2)){
925     add_heap_area_pair(previous, block1, -1, block2, -1);
926     if(match_pairs){
927       match_equals(previous);
928       xbt_dynar_free(&previous);
929     }
930     return 0;
931   }
932
933   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)){
934     if(match_pairs){
935       xbt_dynar_free(&previous);
936     }
937     return 1;
938   }
939
940   addr_block1 = ((void*) (((ADDR2UINT(block1)) - 1) * BLOCKSIZE + (char*)heapbase1));
941   addr_block2 = ((void*) (((ADDR2UINT(block2)) - 1) * BLOCKSIZE + (char*)heapbase2));
942   
943   if(heapinfo1[block1].type == heapinfo2[block2].type){
944     
945     if(heapinfo1[block1].type == -1){ /* Free block */
946       if(match_pairs){
947         match_equals(previous);
948         xbt_dynar_free(&previous);
949       }
950       return 0;
951
952     }else if(heapinfo1[block1].type == 0){ /* Complete block */
953
954       if(heapinfo1[block1].busy_block.equal_to != NULL && heapinfo2[block2].busy_block.equal_to != NULL){
955         if(equal_blocks(block1, block2)){
956           if(match_pairs){
957             match_equals(previous);
958             xbt_dynar_free(&previous);
959           }
960           return 0;
961         }
962       }
963
964       if(heapinfo1[block1].busy_block.size != heapinfo2[block2].busy_block.size){
965         if(match_pairs){
966           xbt_dynar_free(&previous);
967         }
968         return 1;
969       }
970
971       if(heapinfo1[block1].busy_block.busy_size != heapinfo2[block2].busy_block.busy_size){
972         if(match_pairs){
973           xbt_dynar_free(&previous);
974         }
975         return 1;
976       }
977
978       if(!add_heap_area_pair(previous, block1, -1, block2, -1)){
979         if(match_pairs){
980           match_equals(previous);
981           xbt_dynar_free(&previous);
982         }
983         return 0;
984       }
985  
986       size = heapinfo1[block1].busy_block.busy_size;
987
988       if(size <= 0){
989         if(match_pairs){
990           match_equals(previous);
991           xbt_dynar_free(&previous);
992         }
993         return 0;
994       }
995
996       frag1 = -1;
997       frag2 = -1;
998
999       area1_to_compare = addr_block1;
1000       area2_to_compare = addr_block2;
1001
1002       if((heapinfo1[block1].busy_block.ignore > 0) && (heapinfo2[block2].busy_block.ignore == heapinfo1[block1].busy_block.ignore))
1003         check_ignore = heapinfo1[block1].busy_block.ignore;
1004       
1005     }else{ /* Frgamented block */
1006
1007       frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
1008       frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
1009       
1010       addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
1011       addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
1012       
1013       area1_to_compare = addr_frag1;
1014       area2_to_compare = addr_frag2;
1015
1016       if(heapinfo1[block1].busy_frag.equal_to[frag1] != NULL && heapinfo2[block2].busy_frag.equal_to[frag2] != NULL){
1017         if(equal_fragments(block1, frag1, block2, frag2)){
1018           if(match_pairs){
1019             match_equals(previous);
1020             xbt_dynar_free(&previous);
1021           }
1022           return 0;
1023         }
1024       }
1025
1026       if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2]){
1027         if(match_pairs){
1028           xbt_dynar_free(&previous);
1029         }
1030         return 1;  
1031       }
1032       
1033       if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
1034         if(match_pairs){
1035           match_equals(previous);
1036           xbt_dynar_free(&previous);
1037         }
1038         return 0;
1039       }
1040
1041       size = heapinfo1[block1].busy_frag.frag_size[frag1];
1042
1043       if(size <= 0){
1044         if(match_pairs){
1045           match_equals(previous);
1046           xbt_dynar_free(&previous);
1047         }
1048         return 0;
1049       }
1050       
1051       if((heapinfo1[block1].busy_frag.ignore[frag1] > 0) && ( heapinfo2[block2].busy_frag.ignore[frag2] == heapinfo1[block1].busy_frag.ignore[frag1]))
1052         check_ignore = heapinfo1[block1].busy_frag.ignore[frag1];
1053       
1054     }
1055
1056   }else if((heapinfo1[block1].type > 0) && (heapinfo2[block2].type > 0)){
1057
1058     frag1 = ((uintptr_t) (ADDR2UINT (area1) % (BLOCKSIZE))) >> heapinfo1[block1].type;
1059     frag2 = ((uintptr_t) (ADDR2UINT (area2) % (BLOCKSIZE))) >> heapinfo2[block2].type;
1060
1061     if(heapinfo1[block1].busy_frag.equal_to[frag1] != NULL || heapinfo2[block2].busy_frag.equal_to[frag2] != NULL){
1062       if(equal_fragments(block1, frag1, block2, frag2)){
1063         if(match_pairs){
1064           match_equals(previous);
1065           xbt_dynar_free(&previous);
1066         }
1067         return 0;
1068       }
1069     }
1070
1071     if(heapinfo1[block1].busy_frag.frag_size[frag1] != heapinfo2[block2].busy_frag.frag_size[frag2]){
1072       if(match_pairs){
1073         xbt_dynar_free(&previous);
1074       }
1075       return 1;
1076     }
1077     
1078     if(!add_heap_area_pair(previous, block1, frag1, block2, frag2)){
1079       if(match_pairs){
1080         match_equals(previous);
1081         xbt_dynar_free(&previous);
1082       }
1083       return 0;
1084     }
1085
1086     addr_frag1 = (void*) ((char *)addr_block1 + (frag1 << heapinfo1[block1].type));
1087     addr_frag2 = (void*) ((char *)addr_block2 + (frag2 << heapinfo2[block2].type));
1088
1089     area1_to_compare = addr_frag1;
1090     area2_to_compare = addr_frag2;
1091       
1092     size = heapinfo1[block1].busy_frag.frag_size[frag1];
1093
1094     if(size <= 0){
1095       if(match_pairs){
1096         match_equals(previous);
1097         xbt_dynar_free(&previous);
1098       }
1099       return 0;
1100     }
1101
1102     if((heapinfo1[block1].busy_frag.ignore[frag1] > 0) && (heapinfo2[block2].busy_frag.ignore[frag2] == heapinfo1[block1].busy_frag.ignore[frag1]))
1103       check_ignore = heapinfo1[block1].busy_frag.ignore[frag1];   
1104     
1105   }else{
1106     if(match_pairs){
1107       xbt_dynar_free(&previous);
1108     }
1109     return 1;
1110   }
1111
1112
1113   /* Start comparison*/
1114   if(type_id != NULL){
1115     res_compare = compare_heap_area_with_type(area1, area2, area1_to_compare, area2_to_compare, previous, all_types, other_types, type_id, size, check_ignore, pointer_level);
1116     if(res_compare != 0){
1117       if(match_pairs)
1118         xbt_dynar_free(&previous);
1119       return res_compare;
1120     }
1121   }else{
1122     res_compare = compare_heap_area_without_type(area1, area2, area1_to_compare, area2_to_compare, previous, all_types, other_types, size, check_ignore);
1123       if(res_compare != 0){
1124         if(match_pairs)
1125           xbt_dynar_free(&previous);
1126         return res_compare;
1127       }
1128   }
1129
1130   if(match_pairs){
1131     match_equals(previous);
1132     xbt_dynar_free(&previous);
1133   }
1134
1135   return 0;
1136 }
1137
1138 /*********************************************** Miscellaneous ***************************************************/
1139 /****************************************************************************************************************/
1140
1141
1142 int get_pointed_area_size(void *area, int heap){
1143
1144   int block, frag;
1145   malloc_info *heapinfo;
1146
1147   if(heap == 1)
1148     heapinfo = heapinfo1;
1149   else
1150     heapinfo = heapinfo2;
1151
1152   block = ((char*)area - (char*)((xbt_mheap_t)s_heap)->heapbase) / BLOCKSIZE + 1;
1153
1154   if(((char *)area < (char*)((xbt_mheap_t)s_heap)->heapbase)  || (block > heapsize1) || (block < 1))
1155     return -1;
1156
1157   if(heapinfo[block].type == -1){ /* Free block */
1158     return -1;  
1159   }else if(heapinfo[block].type == 0){ /* Complete block */
1160     return (int)heapinfo[block].busy_block.busy_size;
1161   }else{
1162     frag = ((uintptr_t) (ADDR2UINT (area) % (BLOCKSIZE))) >> heapinfo[block].type;
1163     return (int)heapinfo[block].busy_frag.frag_size[frag];
1164   }
1165
1166 }
1167
1168 char *get_type_description(xbt_dict_t types, char *type_name){
1169
1170   xbt_dict_cursor_t dict_cursor;
1171   char *type_origin;
1172   dw_type_t type;
1173
1174   xbt_dict_foreach(types, dict_cursor, type_origin, type){
1175     if(type->name && (strcmp(type->name, type_name) == 0) && type->size > 0){
1176       xbt_dict_cursor_free(&dict_cursor);
1177       return type_origin;
1178     }
1179   }
1180
1181   xbt_dict_cursor_free(&dict_cursor);
1182   return NULL;
1183 }
1184
1185
1186 #ifndef max
1187 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
1188 #endif
1189
1190 int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){
1191
1192   if(heap1 == NULL && heap1 == NULL){
1193     XBT_DEBUG("Malloc descriptors null");
1194     return 0;
1195   }
1196
1197   if(heap1->heaplimit != heap2->heaplimit){
1198     XBT_DEBUG("Different limit of valid info table indices");
1199     return 1;
1200   }
1201
1202   /* Heap information */
1203   heaplimit = ((struct mdesc *)heap1)->heaplimit;
1204
1205   s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize();
1206
1207   heapbase1 = (char *)heap1 + BLOCKSIZE;
1208   heapbase2 = (char *)heap2 + BLOCKSIZE;
1209
1210   heapinfo1 = (malloc_info *)((char *)heap1 + ((uintptr_t)((char *)heap1->heapinfo - (char *)s_heap)));
1211   heapinfo2 = (malloc_info *)((char *)heap2 + ((uintptr_t)((char *)heap2->heapinfo - (char *)s_heap)));
1212
1213   heapsize1 = heap1->heapsize;
1214   heapsize2 = heap2->heapsize;
1215
1216   /* Start comparison */
1217   size_t i, j, k;
1218   void *addr_block1, *addr_block2, *addr_frag1, *addr_frag2;
1219
1220   int distance = 0;
1221
1222   /* Check busy blocks*/
1223
1224   i = 1;
1225
1226   while(i <= heaplimit){
1227
1228     addr_block1 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase1));
1229     addr_block2 = ((void*) (((ADDR2UINT(i)) - 1) * BLOCKSIZE + (char*)heapbase2));
1230
1231     if(heapinfo1[i].type != heapinfo2[i].type){
1232   
1233       distance += BLOCKSIZE;
1234       XBT_DEBUG("Different type of blocks (%zu) : %d - %d -> distance = %d", i, heapinfo1[i].type, heapinfo2[i].type, distance);
1235       i++;
1236     
1237     }else{
1238
1239       if(heapinfo1[i].type == -1){ /* Free block */
1240         i++;
1241         continue;
1242       }
1243
1244       if(heapinfo1[i].type == 0){ /* Large block */
1245        
1246         if(heapinfo1[i].busy_block.size != heapinfo2[i].busy_block.size){
1247           distance += BLOCKSIZE * max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1248           i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1249           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);
1250           continue;
1251         }
1252
1253         /*if(heapinfo1[i].busy_block.busy_size != heapinfo2[i].busy_block.busy_size){
1254           distance += max(heapinfo1[i].busy_block.busy_size, heapinfo2[i].busy_block.busy_size);
1255           i += max(heapinfo1[i].busy_block.size, heapinfo2[i].busy_block.size);
1256           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);
1257           continue;
1258           }*/
1259
1260         k = 0;
1261
1262         //while(k < (heapinfo1[i].busy_block.busy_size)){
1263         while(k < heapinfo1[i].busy_block.size * BLOCKSIZE){
1264           if(memcmp((char *)addr_block1 + k, (char *)addr_block2 + k, 1) != 0){
1265             distance ++;
1266           }
1267           k++;
1268         } 
1269
1270         i++;
1271
1272       }else { /* Fragmented block */
1273
1274         for(j=0; j < (size_t) (BLOCKSIZE >> heapinfo1[i].type); j++){
1275
1276           addr_frag1 = (void*) ((char *)addr_block1 + (j << heapinfo1[i].type));
1277           addr_frag2 = (void*) ((char *)addr_block2 + (j << heapinfo2[i].type));
1278
1279           if(heapinfo1[i].busy_frag.frag_size[j] == 0 && heapinfo2[i].busy_frag.frag_size[j] == 0){
1280             continue;
1281           }
1282           
1283           
1284           /*if(heapinfo1[i].busy_frag.frag_size[j] != heapinfo2[i].busy_frag.frag_size[j]){
1285             distance += max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j]);
1286             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); 
1287             continue;
1288             }*/
1289    
1290           k=0;
1291
1292           //while(k < max(heapinfo1[i].busy_frag.frag_size[j], heapinfo2[i].busy_frag.frag_size[j])){
1293           while(k < (BLOCKSIZE / (BLOCKSIZE >> heapinfo1[i].type))){
1294             if(memcmp((char *)addr_frag1 + k, (char *)addr_frag2 + k, 1) != 0){
1295               distance ++;
1296             }
1297             k++;
1298           }
1299
1300         }
1301
1302         i++;
1303
1304       }
1305       
1306     }
1307
1308   }
1309
1310   return distance;
1311   
1312 }
1313