Logo AND Algorithmique Numérique Distribuée

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