Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mm] Allow to disable the mm based `malloc` at runtime
[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 #include <dlfcn.h>
10
11 #include "mmprivate.h"
12 #include "xbt_modinter.h"
13 #include "internal_config.h"
14 #include <math.h>
15
16 //#define MM_LEGACY_VERBOSE 1 /* define this to see which version of malloc gets used */
17
18 /* The mmalloc() package can use a single implicit malloc descriptor
19    for mmalloc/mrealloc/mfree operations which do not supply an explicit
20    descriptor.  This allows mmalloc() to provide
21    backwards compatibility with the non-mmap'd version. */
22 xbt_mheap_t __mmalloc_default_mdp = NULL;
23
24
25 static xbt_mheap_t __mmalloc_current_heap = NULL;     /* The heap we are currently using. */
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_OVERRIDE_LEGACY
38
39 /* Fake implementations, they are used to fool dlsym:
40  * dlsym used calloc and falls back to some other mechanism
41  * if this fails.
42  */
43 static void* mm_fake_calloc(size_t nmemb, size_t size) { return NULL; }
44 static void* mm_fake_malloc(size_t n)                  { return NULL; }
45 static void* mm_fake_realloc(void *p, size_t s)        { return NULL; }
46
47 /* Function signatures for the main malloc functions: */
48 typedef void* (*mm_malloc_t)(size_t size);
49 typedef void  (*mm_free_t)(void*);
50 typedef void* (*mm_calloc_t)(size_t nmemb, size_t size);
51 typedef void* (*mm_realloc_t)(void *ptr, size_t size);
52
53 /* Function pointers to the real/next implementations: */
54 static mm_malloc_t mm_real_malloc   = mm_fake_malloc;
55 static mm_free_t mm_real_free;
56 static mm_calloc_t mm_real_calloc   = mm_fake_calloc;
57 static mm_realloc_t mm_real_realloc = mm_fake_realloc;
58
59 #define GET_HEAP() __mmalloc_current_heap
60
61 /** Constructor functions used to initialize the malloc implementation
62  */
63 static void __attribute__((constructor(101))) mm_legacy_constructor()
64 {
65   bool use_mm = !getenv("SIMGRID_MALLOC_NO_USE_MM");
66   if (use_mm) {
67     __mmalloc_current_heap = mmalloc_preinit();
68   } else {
69     mm_real_realloc  = (mm_realloc_t) dlsym(RTLD_NEXT, "realloc");
70     mm_real_malloc   = (mm_malloc_t)  dlsym(RTLD_NEXT, "malloc");
71     mm_real_free     = (mm_free_t)    dlsym(RTLD_NEXT, "free");
72     mm_real_calloc   = (mm_calloc_t)  dlsym(RTLD_NEXT, "calloc");
73   }
74 }
75
76 void *malloc(size_t n)
77 {
78   if (!__mmalloc_current_heap) {
79     return mm_real_malloc(n);
80   }
81
82   xbt_mheap_t mdp = GET_HEAP();
83   if (!mdp)
84     return NULL;
85
86   LOCK(mdp);
87   void *ret = mmalloc(mdp, n);
88   UNLOCK(mdp);
89   return ret;
90 }
91
92 void *calloc(size_t nmemb, size_t size)
93 {
94   if (!__mmalloc_current_heap) {
95     return mm_real_calloc(nmemb, size);
96   }
97
98   xbt_mheap_t mdp = GET_HEAP();
99   if (!mdp)
100     return NULL;
101
102   LOCK(mdp);
103   void *ret = mmalloc(mdp, nmemb*size);
104   UNLOCK(mdp);
105   // This was already done in the callee:
106   if(!(mdp->options & XBT_MHEAP_OPTION_MEMSET)) {
107     memset(ret, 0, nmemb * size);
108   }
109   return ret;
110 }
111
112 void *realloc(void *p, size_t s)
113 {
114   if (!__mmalloc_current_heap) {
115     return mm_real_realloc(p, s);
116   }
117
118   xbt_mheap_t mdp = GET_HEAP();
119   if (!mdp)
120     return NULL;
121
122   LOCK(mdp);
123   void* ret = mrealloc(mdp, p, s);
124   UNLOCK(mdp);
125   return ret;
126 }
127
128 void free(void *p)
129 {
130   if (!p)
131     return;
132
133   if (!__mmalloc_current_heap) {
134     mm_real_free(p);
135     return;
136   }
137
138   xbt_mheap_t mdp = GET_HEAP();
139   LOCK(mdp);
140   mfree(mdp, p);
141   UNLOCK(mdp);
142 }
143 #endif /* WANT_MALLOC_OVERRIDE */