Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reset t_simdata->comm after comm is over.
[simgrid.git] / src / xbt / mmalloc / mm_legacy.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /* Redefine the classical malloc/free/realloc functions so that they fit well in the mmalloc framework */
8
9 #include "mmprivate.h"
10 #include "gras_config.h"
11 #include <math.h>
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_mm_legacy, xbt,
14                                 "Logging specific to mm_legacy in mmalloc");
15
16 static void *__mmalloc_current_heap = NULL;     /* The heap we are currently using. */
17
18 #include "xbt_modinter.h"
19
20 void *mmalloc_get_current_heap(void)
21 {
22   return __mmalloc_current_heap;
23 }
24
25 void mmalloc_set_current_heap(void *new_heap)
26 {
27   __mmalloc_current_heap = new_heap;
28 }
29
30 #ifdef MMALLOC_WANT_OVERIDE_LEGACY
31 void *malloc(size_t n)
32 {
33   void *mdp = __mmalloc_current_heap;
34 #ifdef HAVE_MMAP
35   if (!mdp)
36     mmalloc_preinit();
37 #endif
38   LOCK(mdp);
39   void *ret = mmalloc(mdp, n);
40   UNLOCK(mdp);
41
42   return ret;
43 }
44
45 void *calloc(size_t nmemb, size_t size)
46 {
47   size_t total_size = nmemb * size;
48   void *mdp = __mmalloc_current_heap;
49 #ifdef HAVE_MMAP
50   if (!mdp)
51     mmalloc_preinit();
52 #endif
53   LOCK(mdp);
54   void *ret = mmalloc(mdp, total_size);
55   UNLOCK(mdp);
56
57   /* Fill the allocated memory with zeroes to mimic calloc behaviour */
58   memset(ret, '\0', total_size);
59
60   return ret;
61 }
62
63 void *realloc(void *p, size_t s)
64 {
65   void *ret = NULL;
66   void *mdp = __mmalloc_current_heap;
67 #ifdef HAVE_MMAP
68   if (!mdp)
69     mmalloc_preinit();
70 #endif
71   LOCK(mdp);
72   if (s) {
73     if (p)
74       ret = mrealloc(mdp, p, s);
75     else
76       ret = mmalloc(mdp, s);
77   } else {
78     mfree(mdp, p);
79   }
80   UNLOCK(mdp);
81
82   return ret;
83 }
84
85 void free(void *p)
86 {
87   void *mdp = __mmalloc_current_heap;
88 #ifdef HAVE_GTNETS
89   if(!mdp) return;
90 #endif
91   LOCK(mdp);
92   mfree(mdp, p);
93   UNLOCK(mdp);
94 }
95 #endif
96
97 /* Make sure it works with md==NULL */
98
99 /* Safety gap from the heap's break address.
100  * Try to increase this first if you experience strange errors under
101  * valgrind. */
102 #define HEAP_OFFSET   (128UL<<20)
103
104 void *mmalloc_get_default_md(void)
105 {
106   xbt_assert(__mmalloc_default_mdp);
107   return __mmalloc_default_mdp;
108 }
109
110 static void mmalloc_fork_prepare(void)
111 {
112   struct mdesc* mdp = NULL;
113   if ((mdp =__mmalloc_default_mdp)){
114     while(mdp){
115       LOCK(mdp);
116       if(mdp->fd >= 0){
117         mdp->refcount++;
118       }
119       mdp = mdp->next_mdesc;
120     }
121   }
122 }
123
124 static void mmalloc_fork_parent(void)
125 {
126   struct mdesc* mdp = NULL;
127   if ((mdp =__mmalloc_default_mdp)){
128     while(mdp){
129       if(mdp->fd < 0)
130         UNLOCK(mdp);
131       mdp = mdp->next_mdesc;
132     }
133   }
134 }
135
136 static void mmalloc_fork_child(void)
137 {
138   struct mdesc* mdp = NULL;
139   if ((mdp =__mmalloc_default_mdp)){
140     while(mdp){
141       UNLOCK(mdp);
142       mdp = mdp->next_mdesc;
143     }
144   }
145 }
146
147 /* Initialize the default malloc descriptor. */
148 void mmalloc_preinit(void)
149 {
150   int res;
151   if (!__mmalloc_default_mdp) {
152     unsigned long mask = ~((unsigned long)getpagesize() - 1);
153     void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
154     __mmalloc_default_mdp = mmalloc_attach(-1, addr);
155     /* Fixme? only the default mdp in protected against forks */
156     res = xbt_os_thread_atfork(mmalloc_fork_prepare,
157                                mmalloc_fork_parent, mmalloc_fork_child);
158     if (res != 0)
159       THROWF(system_error,0,"xbt_os_thread_atfork() failed: return value %d",res);
160   }
161   xbt_assert(__mmalloc_default_mdp != NULL);
162 }
163
164 void mmalloc_postexit(void)
165 {
166   /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
167   //  mmalloc_detach(__mmalloc_default_mdp);
168   mmalloc_pre_detach(__mmalloc_default_mdp);
169 }
170
171 int mmalloc_compare_heap(void *h1, void *h2, void *std_heap_addr){
172
173   if(h1 == NULL && h2 == NULL){
174     XBT_DEBUG("Malloc descriptors null");
175     return 0;
176   }
177
178   /* Heapstats */
179
180   int errors = 0;
181
182   struct mstats ms1 = mmstats(h1);
183   struct mstats ms2 = mmstats(h2);
184
185   if(ms1.chunks_used !=  ms2.chunks_used){
186     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
187       XBT_DEBUG("Different chunks allocated by the user : %zu - %zu", ms1.chunks_used, ms2.chunks_used);
188       errors++;
189     }else{
190       return 1;
191     }
192   }
193
194   if(ms1.bytes_used !=  ms2.bytes_used){
195     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
196       XBT_DEBUG("Different byte total of user-allocated chunks : %zu - %zu", ms1.bytes_used, ms2.bytes_used);
197       errors++;
198     }else{
199       return 1;
200     }
201   }
202
203   if(ms1.bytes_free !=  ms2.bytes_free){
204     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
205       XBT_DEBUG("Different byte total of chunks in the free list : %zu - %zu", ms1.bytes_free, ms2.bytes_free);
206       errors++;
207     }else{
208       return 1;
209     }
210   }
211
212   if(ms1.chunks_free !=  ms2.chunks_free){
213     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
214       XBT_DEBUG("Different chunks in the free list : %zu - %zu", ms1.chunks_free, ms2.chunks_free);
215       errors++;
216     }else{
217       return 1;
218     }
219   }
220
221   struct mdesc *mdp1, *mdp2;
222   mdp1 = MD_TO_MDP(h1);
223   mdp2 = MD_TO_MDP(h2);
224
225   int res = mmalloc_compare_mdesc(mdp1, mdp2, std_heap_addr);
226
227   return ((errors + res ) > 0);
228
229 }
230
231 int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2, void *std_heap_addr){
232
233   int errors = 0;
234
235   if(mdp1->headersize != mdp2->headersize){
236     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
237       XBT_DEBUG("Different size of the file header for the mapped files");
238       errors++;
239     }else{
240       return 1;
241     }
242   }
243
244   if(mdp1->refcount != mdp2->refcount){
245     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
246       XBT_DEBUG("Different number of processes that attached the heap");
247       errors++;
248     }else{
249       return 1;
250     }
251   }
252  
253   if(strcmp(mdp1->magic, mdp2->magic) != 0){
254     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
255       XBT_DEBUG("Different magic number");
256       errors++;
257     }else{
258       return 1;
259     }
260   }
261
262   if(mdp1->flags != mdp2->flags){
263     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
264       XBT_DEBUG("Different flags");
265       errors++;
266     }else{
267       return 1;
268     }
269   }
270
271   if(mdp1->heapsize != mdp2->heapsize){
272     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
273       XBT_DEBUG("Different number of info entries");
274       errors++;
275     }else{
276       return 1;
277     }
278   }
279
280   //XBT_DEBUG("Heap size : %zu", mdp1->heapsize);
281
282   if(mdp1->heapbase != mdp2->heapbase){
283     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
284       XBT_DEBUG("Different first block of the heap");
285       errors++;
286     }else{
287       return 1;
288     }
289   }
290
291
292   if(mdp1->heapindex != mdp2->heapindex){
293     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
294       XBT_DEBUG("Different index for the heap table : %zu - %zu", mdp1->heapindex, mdp2->heapindex);
295       errors++;
296     }else{
297       return 1;
298     }
299   }
300
301   //XBT_DEBUG("Heap index : %zu", mdp1->heapindex);
302
303   if(mdp1->base != mdp2->base){
304     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
305       XBT_DEBUG("Different base address of the memory region");
306       errors++;
307     }else{
308       return 1;
309     }
310   }
311
312   if(mdp1->breakval != mdp2->breakval){
313     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
314       XBT_DEBUG("Different current location in the memory region");
315       errors++;
316     }else{
317       return 1;
318     }
319   }
320
321   if(mdp1->top != mdp2->top){
322     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
323       XBT_DEBUG("Different end of the current location in the memory region");
324       errors++;
325     }else{
326       return 1;
327     }
328   }
329   
330   if(mdp1->heaplimit != mdp2->heaplimit){
331     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
332       XBT_DEBUG("Different limit of valid info table indices");
333       errors++;
334     }else{
335       return 1;
336     }
337   }
338
339   if(mdp1->fd != mdp2->fd){
340     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
341       XBT_DEBUG("Different file descriptor for the file to which this malloc heap is mapped");
342       errors++;
343     }else{
344       return 1;
345     }
346   }
347
348   if(mdp1->saved_errno != mdp2->saved_errno){
349     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
350       XBT_DEBUG("Different errno");
351       errors++;
352     }else{
353       return 1;
354     }
355   }
356
357   if(mdp1->version != mdp2->version){
358     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
359       XBT_DEBUG("Different version of the mmalloc package");
360       errors++;
361     }else{
362       return 1;
363     }
364   }
365
366  
367   size_t block_free1, block_free2 , next_block_free, first_block_free, block_free ;
368   size_t i, j;
369   void *addr_block1, *addr_block2;
370   size_t frag_size;
371  
372
373   /* Search index of the first free block */
374
375   block_free1 = mdp1->heapindex; 
376   block_free2 = mdp2->heapindex;
377
378   while(mdp1->heapinfo[block_free1].free.prev != 0){
379     block_free1 = mdp1->heapinfo[block_free1].free.prev;
380   }
381
382   while(mdp2->heapinfo[block_free2].free.prev != 0){
383     block_free2 = mdp1->heapinfo[block_free2].free.prev;
384   }
385
386   if(block_free1 !=  block_free2){
387     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
388       XBT_DEBUG("Different first free block");
389       errors++;
390     }else{
391       return 1;
392     }
393   }
394
395   first_block_free = block_free1;
396
397   if(mdp1->heapinfo[first_block_free].free.size != mdp2->heapinfo[first_block_free].free.size){ 
398     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
399       XBT_DEBUG("Different size (in blocks) of the first free cluster");
400       errors++;
401     }else{
402       return 1;
403     }
404   }
405
406   /* Check busy blocks (circular checking)*/
407
408   i = first_block_free + mdp1->heapinfo[first_block_free].free.size;
409
410   if(mdp1->heapinfo[first_block_free].free.next != mdp2->heapinfo[first_block_free].free.next){
411     if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
412       XBT_DEBUG("Different next block free");
413       errors++;
414     }else{
415       return 1;
416     }
417   }
418   
419   block_free = first_block_free;
420   next_block_free = mdp1->heapinfo[first_block_free].free.next;
421
422   if(next_block_free == 0)
423     next_block_free = mdp1->heaplimit;
424
425   while(i != first_block_free){
426
427     while(i<next_block_free){
428
429       if(mdp1->heapinfo[i].busy.type != mdp2->heapinfo[i].busy.type){
430         if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
431           XBT_DEBUG("Different type of busy block");
432           errors++;
433         }else{
434           return 1;
435         }
436       }else{
437
438         addr_block1 = (char *)mdp1 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE); 
439         addr_block2 = (char *)mdp2 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE); 
440         
441         switch(mdp1->heapinfo[i].busy.type){
442         case 0 :
443           if(mdp1->heapinfo[i].busy.info.block.size != mdp2->heapinfo[i].busy.info.block.size){
444             if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
445               XBT_DEBUG("Different size of a large cluster");
446               errors++;
447             }else{
448               return 1;
449             }
450           }else{
451             if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy.info.block.size * BLOCKSIZE)) != 0){
452               if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){           
453                 XBT_DEBUG("Different data in block %zu (size = %zu) (addr_block1 = %p (current = %p) - addr_block2 = %p)", i, mdp1->heapinfo[i].busy.info.block.size, addr_block1, (char *)std_heap_addr + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE), addr_block2);
454                 errors++;
455               }else{
456                 return 1;
457               }
458             } 
459           }
460           i = i+mdp1->heapinfo[i].busy.info.block.size;
461
462           break;
463         default :         
464           if(mdp1->heapinfo[i].busy.info.frag.nfree != mdp2->heapinfo[i].busy.info.frag.nfree){
465             if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
466               XBT_DEBUG("Different free fragments in the fragmented block %zu", i);
467               errors++;
468             }else{
469               return 1;
470             }
471           }else{
472             if(mdp1->heapinfo[i].busy.info.frag.first != mdp2->heapinfo[i].busy.info.frag.first){
473               if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
474                 XBT_DEBUG("Different first free fragments in the block %zu", i);
475                 errors++;
476               }else{
477                 return 1;
478               } 
479             }else{
480               frag_size = pow(2,mdp1->heapinfo[i].busy.type);
481               for(j=0 ; j< (BLOCKSIZE/frag_size); j++){
482                 if(memcmp((char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size), frag_size) != 0){
483                   if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
484                     XBT_DEBUG("Different data in fragment %zu (addr_frag1 = %p - addr_frag2 = %p) of block %zu", j + 1, (char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size),  i);
485                     errors++;
486                   }else{
487                     return 1;
488                   }
489                 } 
490               }
491             }
492           }
493
494           i++;
495
496           break;
497         }
498
499       }
500     }
501
502     if( i != first_block_free){
503
504       if(mdp1->heapinfo[block_free].free.next != mdp2->heapinfo[block_free].free.next){
505         if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
506           XBT_DEBUG("Different next block free");
507           errors++;
508         }else{
509           return 1;
510         }
511       }
512      
513       block_free = mdp1->heapinfo[block_free].free.next;
514       next_block_free = mdp1->heapinfo[block_free].free.next;
515
516       i = block_free + mdp1->heapinfo[block_free].free.size;
517
518       if((next_block_free == 0) && (i != mdp1->heaplimit)){
519
520         while(i < mdp1->heaplimit){
521
522           if(mdp1->heapinfo[i].busy.type != mdp2->heapinfo[i].busy.type){
523             if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
524               XBT_DEBUG("Different type of busy block");
525               errors++;
526             }else{
527               return 1;
528             }
529           }else{
530
531             addr_block1 = (char *)mdp1 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE); 
532             addr_block2 = (char *)mdp2 + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE); 
533         
534             switch(mdp1->heapinfo[i].busy.type){
535             case 0 :
536               if(mdp1->heapinfo[i].busy.info.block.size != mdp2->heapinfo[i].busy.info.block.size){
537                 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
538                   XBT_DEBUG("Different size of a large cluster");
539                   errors++;
540                 }else{
541                   return 1;
542                 }
543               }else{
544                 if(memcmp(addr_block1, addr_block2, (mdp1->heapinfo[i].busy.info.block.size * BLOCKSIZE)) != 0){
545                   if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){       
546                     XBT_DEBUG("Different data in block %zu (addr_block1 = %p (current = %p) - addr_block2 = %p)", i, addr_block1, (char *)std_heap_addr + sizeof(struct mdesc) + ((i-1) * BLOCKSIZE), addr_block2);
547                     errors++;
548                   }else{
549                     return 1;
550                   }
551                 } 
552               }
553             
554               i = i+mdp1->heapinfo[i].busy.info.block.size;
555
556               break;
557             default :     
558               if(mdp1->heapinfo[i].busy.info.frag.nfree != mdp2->heapinfo[i].busy.info.frag.nfree){
559                 if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
560                   XBT_DEBUG("Different free fragments in the fragmented block %zu", i);
561                   errors++;
562                 }else{
563                   return 1;
564                 }
565               }else{
566                 if(mdp1->heapinfo[i].busy.info.frag.first != mdp2->heapinfo[i].busy.info.frag.first){
567                   if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
568                     XBT_DEBUG("Different first free fragments in the block %zu", i);
569                     errors++;
570                   }else{
571                     return 1;
572                   } 
573                 }else{
574                   frag_size = pow(2,mdp1->heapinfo[i].busy.type);
575                   for(j=0 ; j< (BLOCKSIZE/frag_size); j++){
576                     if(memcmp((char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size), frag_size) != 0){
577                       if(XBT_LOG_ISENABLED(xbt_mm_legacy, xbt_log_priority_debug)){
578                         XBT_DEBUG("Different data in fragment %zu (addr_frag1 = %p - addr_frag2 = %p) of block %zu", j + 1, (char *)addr_block1 + (j * frag_size), (char *)addr_block2 + (j * frag_size),  i);
579                         errors++;
580                       }else{
581                         return 1;
582                       }
583                     } 
584                   }
585                 }
586               }
587
588               i++;
589
590               break;
591             }
592           }
593         }
594
595       }
596
597     }
598   }
599   
600   return (errors>0);    
601 }
602
603  
604 void mmalloc_display_info_heap(void *h){
605
606 }  
607   
608