SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RakMemoryOverride.cpp
Go to the documentation of this file.
1 /*
2 * Original work: Copyright (c) 2014, Oculus VR, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
16 #include "slikenet/memoryoverride.h"
17 #include "slikenet/assert.h"
18 #include <stdlib.h>
19
20 #ifdef _RAKNET_SUPPORT_DL_MALLOC
21 #include "rdlmalloc.h"
22 #endif
23
24
25
26
27
28
29
30 using namespace SLNet;
31
32 #if _USE_RAK_MEMORY_OVERRIDE==1
33 #if defined(malloc)
34 #pragma push_macro("malloc")
35 #undef malloc
36 #define RMO_MALLOC_UNDEF
37 #endif
38
39 #if defined(realloc)
40 #pragma push_macro("realloc")
41 #undef realloc
42 #define RMO_REALLOC_UNDEF
43 #endif
44
45 #if defined(free)
46 #pragma push_macro("free")
47 #undef free
48 #define RMO_FREE_UNDEF
49 #endif
50 #endif
51
52 void DefaultOutOfMemoryHandler(const char *file, const long line)
53 {
54 (void) file;
55 (void) line;
56 RakAssert(0);
57 }
58
59 void * (*rakMalloc) (size_t size) = SLNet::_RakMalloc;
60 void* (*rakRealloc) (void *p, size_t size) = SLNet::_RakRealloc;
61 void (*rakFree) (void *p) = SLNet::_RakFree;
62 void* (*rakMalloc_Ex) (size_t size, const char *file, unsigned int line) = SLNet::_RakMalloc_Ex;
63 void* (*rakRealloc_Ex) (void *p, size_t size, const char *file, unsigned int line) = SLNet::_RakRealloc_Ex;
64 void (*rakFree_Ex) (void *p, const char *file, unsigned int line) = SLNet::_RakFree_Ex;
65 void (*notifyOutOfMemory) (const char *file, const long line)=DefaultOutOfMemoryHandler;
66 void * (*dlMallocMMap) (size_t size) = SLNet::_DLMallocMMap;
67 void * (*dlMallocDirectMMap) (size_t size) = SLNet::_DLMallocDirectMMap;
68 int (*dlMallocMUnmap) (void* ptr, size_t size) = SLNet::_DLMallocMUnmap;
69
70 void SetMalloc( void* (*userFunction)(size_t size) )
71 {
72 rakMalloc=userFunction;
73 }
74 void SetRealloc( void* (*userFunction)(void *p, size_t size) )
75 {
76 rakRealloc=userFunction;
77 }
78 void SetFree( void (*userFunction)(void *p) )
79 {
80 rakFree=userFunction;
81 }
82 void SetMalloc_Ex( void* (*userFunction)(size_t size, const char *file, unsigned int line) )
83 {
84 rakMalloc_Ex=userFunction;
85 }
86 void SetRealloc_Ex( void* (*userFunction)(void *p, size_t size, const char *file, unsigned int line) )
87 {
88 rakRealloc_Ex=userFunction;
89 }
90 void SetFree_Ex( void (*userFunction)(void *p, const char *file, unsigned int line) )
91 {
92 rakFree_Ex=userFunction;
93 }
94 void SetNotifyOutOfMemory( void (*userFunction)(const char *file, const long line) )
95 {
96 notifyOutOfMemory=userFunction;
97 }
98 void SetDLMallocMMap( void* (*userFunction)(size_t size) )
99 {
100 dlMallocMMap=userFunction;
101 }
102 void SetDLMallocDirectMMap( void* (*userFunction)(size_t size) )
103 {
104 dlMallocDirectMMap=userFunction;
105 }
106 void SetDLMallocMUnmap( int (*userFunction)(void* ptr, size_t size) )
107 {
108 dlMallocMUnmap=userFunction;
109 }
110 void * (*GetMalloc()) (size_t size)
111 {
112 return rakMalloc;
113 }
114 void * (*GetRealloc()) (void *p, size_t size)
115 {
116 return rakRealloc;
117 }
118 void (*GetFree()) (void *p)
119 {
120 return rakFree;
121 }
122 void * (*GetMalloc_Ex()) (size_t size, const char *file, unsigned int line)
123 {
124 return rakMalloc_Ex;
125 }
126 void * (*GetRealloc_Ex()) (void *p, size_t size, const char *file, unsigned int line)
127 {
128 return rakRealloc_Ex;
129 }
130 void (*GetFree_Ex()) (void *p, const char *file, unsigned int line)
131 {
132 return rakFree_Ex;
133 }
134 void *(*GetDLMallocMMap())(size_t size)
135 {
136 return dlMallocMMap;
137 }
138 void *(*GetDLMallocDirectMMap())(size_t size)
139 {
140 return dlMallocDirectMMap;
141 }
142 int (*GetDLMallocMUnmap())(void* ptr, size_t size)
143 {
144 return dlMallocMUnmap;
145 }
146 void* SLNet::_RakMalloc (size_t size)
147 {
148 return malloc(size);
149 }
150
151 void* SLNet::_RakRealloc (void *p, size_t size)
152 {
153 return realloc(p,size);
154 }
155
156 void SLNet::_RakFree (void *p)
157 {
158 free(p);
159 }
160
161 void* SLNet::_RakMalloc_Ex (size_t size, const char *file, unsigned int line)
162 {
163 (void) file;
164 (void) line;
165
166 return malloc(size);
167 }
168
169 void* SLNet::_RakRealloc_Ex (void *p, size_t size, const char *file, unsigned int line)
170 {
171 (void) file;
172 (void) line;
173
174 return realloc(p,size);
175 }
176
177 void SLNet::_RakFree_Ex (void *p, const char *file, unsigned int line)
178 {
179 (void) file;
180 (void) line;
181
182 free(p);
183 }
184 #ifdef _RAKNET_SUPPORT_DL_MALLOC
185 void * SLNet::_DLMallocMMap (size_t size)
186 {
187 return RAK_MMAP_DEFAULT(size);
188 }
189 void * SLNet::_DLMallocDirectMMap (size_t size)
190 {
191 return RAK_DIRECT_MMAP_DEFAULT(size);
192 }
193 int SLNet::_DLMallocMUnmap (void *p, size_t size)
194 {
195 return RAK_MUNMAP_DEFAULT(p,size);
196 }
197
198 static mspace rakNetFixedHeapMSpace=0;
199
200 void* _DLMalloc(size_t size)
201 {
202 return rak_mspace_malloc(rakNetFixedHeapMSpace,size);
203 }
204
205 void* _DLRealloc(void *p, size_t size)
206 {
207 return rak_mspace_realloc(rakNetFixedHeapMSpace,p,size);
208 }
209
210 void _DLFree(void *p)
211 {
212 if (p)
213 rak_mspace_free(rakNetFixedHeapMSpace,p);
214 }
215 void* _DLMalloc_Ex (size_t size, const char *file, unsigned int line)
216 {
217 (void) file;
218 (void) line;
219
220 return rak_mspace_malloc(rakNetFixedHeapMSpace,size);
221 }
222
223 void* _DLRealloc_Ex (void *p, size_t size, const char *file, unsigned int line)
224 {
225 (void) file;
226 (void) line;
227
228 return rak_mspace_realloc(rakNetFixedHeapMSpace,p,size);
229 }
230
231 void _DLFree_Ex (void *p, const char *file, unsigned int line)
232 {
233 (void) file;
234 (void) line;
235
236 if (p)
237 rak_mspace_free(rakNetFixedHeapMSpace,p);
238 }
239
240 void UseRaknetFixedHeap(size_t initialCapacity,
241 void * (*yourMMapFunction) (size_t size),
242 void * (*yourDirectMMapFunction) (size_t size),
243 int (*yourMUnmapFunction) (void *p, size_t size))
244 {
245 SetDLMallocMMap(yourMMapFunction);
246 SetDLMallocDirectMMap(yourDirectMMapFunction);
247 SetDLMallocMUnmap(yourMUnmapFunction);
248 SetMalloc(_DLMalloc);
249 SetRealloc(_DLRealloc);
250 SetFree(_DLFree);
251 SetMalloc_Ex(_DLMalloc_Ex);
252 SetRealloc_Ex(_DLRealloc_Ex);
253 SetFree_Ex(_DLFree_Ex);
254
255 rakNetFixedHeapMSpace=rak_create_mspace(initialCapacity, 0);
256 }
257 void FreeRakNetFixedHeap(void)
258 {
259 if (rakNetFixedHeapMSpace)
260 {
261 rak_destroy_mspace(rakNetFixedHeapMSpace);
262 rakNetFixedHeapMSpace=0;
263 }
264
265 SetMalloc(_RakMalloc);
266 SetRealloc(_RakRealloc);
267 SetFree(_RakFree);
268 SetMalloc_Ex(_RakMalloc_Ex);
269 SetRealloc_Ex(_RakRealloc_Ex);
270 SetFree_Ex(_RakFree_Ex);
271 }
272 #else
273 void * SLNet::_DLMallocMMap (size_t size) {(void) size; return 0;}
274 void * SLNet::_DLMallocDirectMMap (size_t size) {(void) size; return 0;}
275 int SLNet::_DLMallocMUnmap (void *p, size_t size) {(void) size; (void) p; return 0;}
276 void* _DLMalloc(size_t size) {(void) size; return 0;}
277 void* _DLRealloc(void *p, size_t size) {(void) p; (void) size; return 0;}
278 void _DLFree(void *p) {(void) p;}
279 void* _DLMalloc_Ex (size_t size, const char *file, unsigned int line) {(void) size; (void) file; (void) line; return 0;}
280 void* _DLRealloc_Ex (void *p, size_t size, const char *file, unsigned int line) {(void) p; (void) size; (void) file; (void) line; return 0;}
281 void _DLFree_Ex (void *p, const char *file, unsigned int line) {(void) p; (void) file; (void) line;}
282
283 void UseRaknetFixedHeap(size_t initialCapacity,
284 void * (*yourMMapFunction) (size_t size),
285 void * (*yourDirectMMapFunction) (size_t size),
286 int (*yourMUnmapFunction) (void *p, size_t size))
287 {
288 (void) initialCapacity;
289 (void) yourMMapFunction;
290 (void) yourDirectMMapFunction;
291 (void) yourMUnmapFunction;
292 }
293 void FreeRakNetFixedHeap(void) {}
294 #endif
295
296 #if _USE_RAK_MEMORY_OVERRIDE==1
297 #if defined(RMO_MALLOC_UNDEF)
298 #pragma pop_macro("malloc")
299 #undef RMO_MALLOC_UNDEF
300 #endif
301
302 #if defined(RMO_REALLOC_UNDEF)
303 #pragma pop_macro("realloc")
304 #undef RMO_REALLOC_UNDEF
305 #endif
306
307 #if defined(RMO_FREE_UNDEF)
308 #pragma pop_macro("free")
309 #undef RMO_FREE_UNDEF
310 #endif
311 #endif