Logo AND Algorithmique Numérique Distribuée

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