Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
merge branches
[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
12 static void *__mmalloc_current_heap = NULL;     /* The heap we are currently using. */
13
14 #include "xbt_modinter.h"
15
16 void *mmalloc_get_current_heap(void)
17 {
18   return __mmalloc_current_heap;
19 }
20
21 void mmalloc_set_current_heap(void *new_heap)
22 {
23   __mmalloc_current_heap = new_heap;
24 }
25
26 #ifdef MMALLOC_WANT_OVERIDE_LEGACY
27 void *malloc(size_t n)
28 {
29   void *mdp = __mmalloc_current_heap;
30 #ifdef HAVE_MMAP
31   if (!mdp)
32     mmalloc_preinit();
33 #endif
34   LOCK(mdp);
35   void *ret = mmalloc(mdp, n);
36   UNLOCK(mdp);
37
38   return ret;
39 }
40
41 void *calloc(size_t nmemb, size_t size)
42 {
43   size_t total_size = nmemb * size;
44   void *mdp = __mmalloc_current_heap;
45 #ifdef HAVE_MMAP
46   if (!mdp)
47     mmalloc_preinit();
48 #endif
49   LOCK(mdp);
50   void *ret = mmalloc(mdp, total_size);
51   UNLOCK(mdp);
52
53   /* Fill the allocated memory with zeroes to mimic calloc behaviour */
54   memset(ret, '\0', total_size);
55
56   return ret;
57 }
58
59 void *realloc(void *p, size_t s)
60 {
61   void *ret = NULL;
62   void *mdp = __mmalloc_current_heap;
63 #ifdef HAVE_MMAP
64   if (!mdp)
65     mmalloc_preinit();
66 #endif
67   LOCK(mdp);
68   if (s) {
69     if (p)
70       ret = mrealloc(mdp, p, s);
71     else
72       ret = mmalloc(mdp, s);
73   } else {
74     if (p)
75       mfree(mdp, p);
76   }
77   UNLOCK(mdp);
78
79   return ret;
80 }
81
82 void free(void *p)
83 {
84   void *mdp = __mmalloc_current_heap;
85 #ifdef HAVE_GTNETS
86   if(!mdp) return;
87 #endif
88   LOCK(mdp);
89   mfree(mdp, p);
90   UNLOCK(mdp);
91 }
92 #endif
93
94 /* Make sure it works with md==NULL */
95
96 /* Safety gap from the heap's break address.
97  * Try to increase this first if you experience strange errors under
98  * valgrind. */
99 #define HEAP_OFFSET   (128UL<<20)
100
101 void *mmalloc_get_default_md(void)
102 {
103   xbt_assert(__mmalloc_default_mdp);
104   return __mmalloc_default_mdp;
105 }
106
107 static void mmalloc_fork_prepare(void)
108 {
109   struct mdesc* mdp = NULL;
110   if ((mdp =__mmalloc_default_mdp)){
111     while(mdp){
112       LOCK(mdp);
113       if(mdp->fd >= 0){
114         mdp->refcount++;
115       }
116       mdp = mdp->next_mdesc;
117     }
118   }
119 }
120
121 static void mmalloc_fork_parent(void)
122 {
123   struct mdesc* mdp = NULL;
124   if ((mdp =__mmalloc_default_mdp)){
125     while(mdp){
126       if(mdp->fd < 0)
127         UNLOCK(mdp);
128       mdp = mdp->next_mdesc;
129     }
130   }
131 }
132
133 static void mmalloc_fork_child(void)
134 {
135   struct mdesc* mdp = NULL;
136   if ((mdp =__mmalloc_default_mdp)){
137     while(mdp){
138       UNLOCK(mdp);
139       mdp = mdp->next_mdesc;
140     }
141   }
142 }
143
144 /* Initialize the default malloc descriptor. */
145 void mmalloc_preinit(void)
146 {
147   int res;
148   if (!__mmalloc_default_mdp) {
149     unsigned long mask = ~((unsigned long)getpagesize() - 1);
150     void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask);
151     __mmalloc_default_mdp = mmalloc_attach(-1, addr);
152     /* Fixme? only the default mdp in protected against forks */
153     res = xbt_os_thread_atfork(mmalloc_fork_prepare,
154                                mmalloc_fork_parent, mmalloc_fork_child);
155     if (res != 0)
156       THROWF(system_error,0,"xbt_os_thread_atfork() failed: return value %d",res);
157   }
158   xbt_assert(__mmalloc_default_mdp != NULL);
159 }
160
161 void mmalloc_postexit(void)
162 {
163   /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
164   //  mmalloc_detach(__mmalloc_default_mdp);
165   mmalloc_pre_detach(__mmalloc_default_mdp);
166 }