Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'v3_11_x'
[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
9 #include "mmprivate.h"
10 #include "xbt_modinter.h"
11 #include "internal_config.h"
12 #include <math.h>
13
14 //#define MM_LEGACY_VERBOSE 1 /* define this to see which version of malloc gets used */
15
16 /* The mmalloc() package can use a single implicit malloc descriptor
17    for mmalloc/mrealloc/mfree operations which do not supply an explicit
18    descriptor.  This allows mmalloc() to provide
19    backwards compatibility with the non-mmap'd version. */
20 xbt_mheap_t __mmalloc_default_mdp = NULL;
21
22
23 static xbt_mheap_t __mmalloc_current_heap = NULL;     /* The heap we are currently using. */
24
25 xbt_mheap_t mmalloc_get_current_heap(void)
26 {
27   return __mmalloc_current_heap;
28 }
29
30 void mmalloc_set_current_heap(xbt_mheap_t new_heap)
31 {
32   __mmalloc_current_heap = new_heap;
33 }
34
35
36 #ifdef MMALLOC_WANT_OVERRIDE_LEGACY
37 #ifdef HAVE_GNU_LD
38
39 #undef _GNU_SOURCE
40 #define _GNU_SOURCE 1
41 #include <dlfcn.h>
42
43 static void * (*real_malloc) (size_t) = NULL;
44 static void * (*real_realloc) (void*,size_t) = NULL;
45 static void * (*real_free) (void*) = NULL;
46
47 static void mm_gnuld_legacy_init(void) { /* This function is called from mmalloc_preinit(); it works even if it's static because all mm is in mm.c */
48   real_malloc = (void * (*) (size_t)) dlsym(RTLD_NEXT, "malloc");
49   real_realloc = (void * (*) (void*,size_t)) dlsym(RTLD_NEXT, "realloc");
50   real_free = (void * (*) (void*)) dlsym(RTLD_NEXT, "free");
51   __mmalloc_current_heap = __mmalloc_default_mdp;
52 }
53
54 /* Hello pimple!
55  * DL needs some memory while resolving the malloc symbol, that is somehow problematic
56  * To that extend, we have a little area here living in .BSS that we return if asked for memory before the malloc is resolved.
57  */
58 static int allocated_junk = 0; /* keep track of many blocks of our little area was already given to someone */
59 #define JUNK_SIZE 8
60 #define MAX_JUNK_AREAS (32 * 1024 / JUNK_SIZE)
61 static char junkareas[MAX_JUNK_AREAS][JUNK_SIZE];
62
63 /* This version use mmalloc if there is a current heap, or the legacy implem if not */
64 void *malloc(size_t n) {
65   xbt_mheap_t mdp = __mmalloc_current_heap;
66   void *ret;
67 #ifdef MM_LEGACY_VERBOSE
68   static int warned_raw = 0;
69   static int warned_mmalloc = 0;
70 #endif
71
72   if (mdp) {
73     LOCK(mdp);
74     ret = mmalloc(mdp, n);
75     UNLOCK(mdp);
76 #ifdef MM_LEGACY_VERBOSE
77     if (!warned_mmalloc) {
78       fprintf(stderr,"Using mmalloc; enabling the model-checker in cmake may have a bad impact on your simulation performance\n");
79       warned_mmalloc = 1;
80     }
81 #endif
82   } else {
83     if (!real_malloc) {
84       size_t needed_areas = n / JUNK_SIZE;
85       if(needed_areas * JUNK_SIZE != n) needed_areas++;
86       if (allocated_junk+needed_areas>=MAX_JUNK_AREAS) {
87         fprintf(stderr,
88           "Panic: real malloc symbol not resolved yet, and I already gave my little private memory chunk away.\n");
89         exit(1);
90       } else {
91         size_t i = allocated_junk;
92         allocated_junk += needed_areas;
93         return junkareas[i];
94       }
95     }
96 #ifdef MM_LEGACY_VERBOSE
97     if (!warned_raw) {
98       fprintf(stderr,"Using system malloc after interception; you seem to be currently model-checking\n");
99       warned_raw = 1;
100     }
101 #endif
102     ret = real_malloc(n);
103   }
104   return ret;
105 }
106
107
108 void *calloc(size_t nmemb, size_t size)
109 {
110   void *ret = malloc(nmemb*size);
111   memset(ret, 0, nmemb * size);
112   return ret;
113 }
114
115 void *realloc(void *p, size_t s)
116 {
117   xbt_mheap_t mdp = __mmalloc_current_heap;
118   void *ret;
119
120   if (mdp) {
121     LOCK(mdp);
122     ret = mrealloc(mdp, p, s);
123     UNLOCK(mdp);
124   } else {
125     ret = real_realloc(p,s);
126   }
127
128   return ret;
129 }
130
131 void free(void *p)
132 {
133   if (p==NULL)
134     return;
135   if (p<=(void*)junkareas || p>(void*)(junkareas[MAX_JUNK_AREAS]) ) {
136     xbt_mheap_t mdp = __mmalloc_current_heap;
137
138     if (mdp) {
139       LOCK(mdp);
140       mfree(mdp, p);
141       UNLOCK(mdp);
142     } else {
143       real_free(p);
144     }
145   } else if(allocated_junk && p==junkareas[allocated_junk-1]) {
146     allocated_junk--;
147   } else {
148     // Leaked memory.
149   }
150 }
151
152
153 #else /* NO GNU_LD */
154 void *malloc(size_t n)
155 {
156   xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
157
158   LOCK(mdp);
159   void *ret = mmalloc(mdp, n);
160   UNLOCK(mdp);
161
162   return ret;
163 }
164
165 void *calloc(size_t nmemb, size_t size)
166 {
167   xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
168
169   LOCK(mdp);
170   void *ret = mmalloc(mdp, nmemb*size);
171   UNLOCK(mdp);
172   memset(ret, 0, nmemb * size);
173
174
175   return ret;
176 }
177
178 void *realloc(void *p, size_t s)
179 {
180   void *ret = NULL;
181   xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
182
183   LOCK(mdp);
184   ret = mrealloc(mdp, p, s);
185   UNLOCK(mdp);
186
187   return ret;
188 }
189
190 void free(void *p)
191 {
192   if (p != NULL) {
193     xbt_mheap_t mdp = __mmalloc_current_heap ?: (xbt_mheap_t) mmalloc_preinit();
194
195     LOCK(mdp);
196     mfree(mdp, p);
197     UNLOCK(mdp);
198   }
199 }
200 #endif /* NO GNU_LD */
201 #endif /* WANT_MALLOC_OVERRIDE */
202
203