Logo AND Algorithmique Numérique Distribuée

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