Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Do not call malloc_no_memset in mc_snapshot
[simgrid.git] / src / xbt / mmalloc / mm_legacy.c
1 /* Copyright (c) 2010-2014. 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 #define _GNU_SOURCE
9
10 #include <stdlib.h>
11
12 #include <dlfcn.h>
13
14 #include "../../mc/mc_base.h"
15 #include "mmprivate.h"
16 #include "xbt_modinter.h"
17 #include "internal_config.h"
18 #include <math.h>
19 #include "../mc/mc_protocol.h"
20
21 //#define MM_LEGACY_VERBOSE 1 /* define this to see which version of malloc gets used */
22
23 /* The mmalloc() package can use a single implicit malloc descriptor
24    for mmalloc/mrealloc/mfree operations which do not supply an explicit
25    descriptor.  This allows mmalloc() to provide
26    backwards compatibility with the non-mmap'd version. */
27 xbt_mheap_t __mmalloc_default_mdp = NULL;
28
29 static int __malloc_use_mmalloc;
30
31 int malloc_use_mmalloc(void)
32 {
33   return __malloc_use_mmalloc;
34 }
35
36 static xbt_mheap_t __mmalloc_current_heap = NULL;     /* The heap we are currently using. */
37
38 xbt_mheap_t mmalloc_get_current_heap(void)
39 {
40   return __mmalloc_current_heap;
41 }
42
43 void mmalloc_set_current_heap(xbt_mheap_t new_heap)
44 {
45   __mmalloc_current_heap = new_heap;
46 }
47
48 #ifdef MMALLOC_WANT_OVERRIDE_LEGACY
49
50 /* Fake implementations, they are used to fool dlsym:
51  * dlsym used calloc and falls back to some other mechanism
52  * if this fails.
53  */
54 static void* mm_fake_calloc(size_t nmemb, size_t size) { return NULL; }
55 static void* mm_fake_malloc(size_t n)                  { return NULL; }
56 static void* mm_fake_realloc(void *p, size_t s)        { return NULL; }
57
58 /* Function signatures for the main malloc functions: */
59 typedef void* (*mm_malloc_t)(size_t size);
60 typedef void  (*mm_free_t)(void*);
61 typedef void* (*mm_calloc_t)(size_t nmemb, size_t size);
62 typedef void* (*mm_realloc_t)(void *ptr, size_t size);
63
64 /* Function pointers to the real/next implementations: */
65 static mm_malloc_t mm_real_malloc   = mm_fake_malloc;
66 static mm_free_t mm_real_free;
67 static mm_calloc_t mm_real_calloc   = mm_fake_calloc;
68 static mm_realloc_t mm_real_realloc = mm_fake_realloc;
69
70 #define GET_HEAP() __mmalloc_current_heap
71
72 /** Constructor functions used to initialize the malloc implementation
73  */
74 static void __attribute__((constructor(101))) mm_legacy_constructor()
75 {
76   __malloc_use_mmalloc = getenv(MC_ENV_VARIABLE) ? 1 : 0;
77   if (__malloc_use_mmalloc) {
78     __mmalloc_current_heap = mmalloc_preinit();
79   } else {
80     mm_real_realloc  = (mm_realloc_t) dlsym(RTLD_NEXT, "realloc");
81     mm_real_malloc   = (mm_malloc_t)  dlsym(RTLD_NEXT, "malloc");
82     mm_real_free     = (mm_free_t)    dlsym(RTLD_NEXT, "free");
83     mm_real_calloc   = (mm_calloc_t)  dlsym(RTLD_NEXT, "calloc");
84   }
85 }
86
87 void* malloc_no_memset(size_t n)
88 {
89   if (!__malloc_use_mmalloc) {
90     return mm_real_malloc(n);
91   }
92
93   xbt_mheap_t mdp = GET_HEAP();
94   if (!mdp)
95     return NULL;
96
97   LOCK(mdp);
98   void *ret = mmalloc_no_memset(mdp, n);
99   UNLOCK(mdp);
100   return ret;
101 }
102
103 void *malloc(size_t n)
104 {
105   if (!__malloc_use_mmalloc) {
106     return mm_real_malloc(n);
107   }
108
109   xbt_mheap_t mdp = GET_HEAP();
110   if (!mdp)
111     return NULL;
112
113   LOCK(mdp);
114   void *ret = mmalloc(mdp, n);
115   UNLOCK(mdp);
116   return ret;
117 }
118
119 void *calloc(size_t nmemb, size_t size)
120 {
121   if (!__malloc_use_mmalloc) {
122     return mm_real_calloc(nmemb, size);
123   }
124
125   xbt_mheap_t mdp = GET_HEAP();
126   if (!mdp)
127     return NULL;
128
129   LOCK(mdp);
130   void *ret = mmalloc(mdp, nmemb*size);
131   UNLOCK(mdp);
132   // This was already done in the callee:
133   if(!(mdp->options & XBT_MHEAP_OPTION_MEMSET)) {
134     memset(ret, 0, nmemb * size);
135   }
136   return ret;
137 }
138
139 void *realloc(void *p, size_t s)
140 {
141   if (!__malloc_use_mmalloc) {
142     return mm_real_realloc(p, s);
143   }
144
145   xbt_mheap_t mdp = GET_HEAP();
146   if (!mdp)
147     return NULL;
148
149   LOCK(mdp);
150   void* ret = mrealloc(mdp, p, s);
151   UNLOCK(mdp);
152   return ret;
153 }
154
155 void free(void *p)
156 {
157   if (!__malloc_use_mmalloc) {
158     mm_real_free(p);
159     return;
160   }
161
162   if (!p)
163     return;
164
165   xbt_mheap_t mdp = GET_HEAP();
166   LOCK(mdp);
167   mfree(mdp, p);
168   UNLOCK(mdp);
169 }
170 #endif /* WANT_MALLOC_OVERRIDE */