Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
merge back the master trunk into the smpi branch
[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     mfree(mdp, p);
75   }
76   UNLOCK(mdp);
77
78   return ret;
79 }
80
81 void free(void *p)
82 {
83   void *mdp = __mmalloc_current_heap;
84 #ifdef HAVE_GTNETS
85   if(!mdp) return;
86 #endif
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 void *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   struct mdesc* 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   struct mdesc* 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) {
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
160 void mmalloc_postexit(void)
161 {
162   /* Do not detach the default mdp or ldl won't be able to free the memory it allocated since we're in memory */
163   //  mmalloc_detach(__mmalloc_default_mdp);
164   mmalloc_pre_detach(__mmalloc_default_mdp);
165 }