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
memoryoverride.h
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
19
20
21
22 #ifndef __RAK_MEMORY_H
23 #define __RAK_MEMORY_H
24
25 #include "Export.h"
26 #include "defines.h"
27 #include <new>
28
29
30
31
32
33
34
35 #include "alloca.h"
36
37 // #if _USE_RAK_MEMORY_OVERRIDE==1
38 // #if defined(new)
39 // #pragma push_macro("new")
40 // #undef new
41 // #define RMO_NEW_UNDEF
42 // #endif
43 // #endif
44
45
46 // These pointers are statically and globally defined in RakMemoryOverride.cpp
47 // Change them to point to your own allocators if you want.
48 // Use the functions for a DLL, or just reassign the variable if using source
49 extern RAK_DLL_EXPORT void * (*rakMalloc) (size_t size);
50 extern RAK_DLL_EXPORT void * (*rakRealloc) (void *p, size_t size);
51 extern RAK_DLL_EXPORT void (*rakFree) (void *p);
52 extern RAK_DLL_EXPORT void * (*rakMalloc_Ex) (size_t size, const char *file, unsigned int line);
53 extern RAK_DLL_EXPORT void * (*rakRealloc_Ex) (void *p, size_t size, const char *file, unsigned int line);
54 extern RAK_DLL_EXPORT void (*rakFree_Ex) (void *p, const char *file, unsigned int line);
55 extern RAK_DLL_EXPORT void (*notifyOutOfMemory) (const char *file, const long line);
56 extern RAK_DLL_EXPORT void * (*dlMallocMMap) (size_t size);
57 extern RAK_DLL_EXPORT void * (*dlMallocDirectMMap) (size_t size);
58 extern RAK_DLL_EXPORT int (*dlMallocMUnmap) (void* ptr, size_t size);
59
60 // Change to a user defined allocation function
61 void RAK_DLL_EXPORT SetMalloc( void* (*userFunction)(size_t size) );
62 void RAK_DLL_EXPORT SetRealloc( void* (*userFunction)(void *p, size_t size) );
63 void RAK_DLL_EXPORT SetFree( void (*userFunction)(void *p) );
64 void RAK_DLL_EXPORT SetMalloc_Ex( void* (*userFunction)(size_t size, const char *file, unsigned int line) );
65 void RAK_DLL_EXPORT SetRealloc_Ex( void* (*userFunction)(void *p, size_t size, const char *file, unsigned int line) );
66 void RAK_DLL_EXPORT SetFree_Ex( void (*userFunction)(void *p, const char *file, unsigned int line) );
67 // Change to a user defined out of memory function
68 void RAK_DLL_EXPORT SetNotifyOutOfMemory( void (*userFunction)(const char *file, const long line) );
69 void RAK_DLL_EXPORT SetDLMallocMMap( void* (*userFunction)(size_t size) );
70 void RAK_DLL_EXPORT SetDLMallocDirectMMap( void* (*userFunction)(size_t size) );
71 void RAK_DLL_EXPORT SetDLMallocMUnmap( int (*userFunction)(void* ptr, size_t size) );
72
73 extern RAK_DLL_EXPORT void * (*GetMalloc()) (size_t size);
74 extern RAK_DLL_EXPORT void * (*GetRealloc()) (void *p, size_t size);
75 extern RAK_DLL_EXPORT void (*GetFree()) (void *p);
76 extern RAK_DLL_EXPORT void * (*GetMalloc_Ex()) (size_t size, const char *file, unsigned int line);
77 extern RAK_DLL_EXPORT void * (*GetRealloc_Ex()) (void *p, size_t size, const char *file, unsigned int line);
78 extern RAK_DLL_EXPORT void (*GetFree_Ex()) (void *p, const char *file, unsigned int line);
79 extern RAK_DLL_EXPORT void *(*GetDLMallocMMap())(size_t size);
80 extern RAK_DLL_EXPORT void *(*GetDLMallocDirectMMap())(size_t size);
81 extern RAK_DLL_EXPORT int (*GetDLMallocMUnmap())(void* ptr, size_t size);
82
83 namespace SLNet
84 {
85
86 template <class Type>
87 RAK_DLL_EXPORT Type* OP_NEW(const char *file, unsigned int line)
88 {
89 #if _USE_RAK_MEMORY_OVERRIDE==1
90 char *buffer = (char *) (GetMalloc_Ex())(sizeof(Type), file, line);
91 Type *t = new (buffer) Type;
92 return t;
93 #else
94 (void) file;
95 (void) line;
96 return new Type;
97 #endif
98 }
99
100 template <class Type, class P1>
101 RAK_DLL_EXPORT Type* OP_NEW_1(const char *file, unsigned int line, const P1 &p1)
102 {
103 #if _USE_RAK_MEMORY_OVERRIDE==1
104 char *buffer = (char *) (GetMalloc_Ex())(sizeof(Type), file, line);
105 Type *t = new (buffer) Type(p1);
106 return t;
107 #else
108 (void) file;
109 (void) line;
110 return new Type(p1);
111 #endif
112 }
113
114 template <class Type, class P1, class P2>
115 RAK_DLL_EXPORT Type* OP_NEW_2(const char *file, unsigned int line, const P1 &p1, const P2 &p2)
116 {
117 #if _USE_RAK_MEMORY_OVERRIDE==1
118 char *buffer = (char *) (GetMalloc_Ex())(sizeof(Type), file, line);
119 Type *t = new (buffer) Type(p1, p2);
120 return t;
121 #else
122 (void) file;
123 (void) line;
124 return new Type(p1, p2);
125 #endif
126 }
127
128 template <class Type, class P1, class P2, class P3>
129 RAK_DLL_EXPORT Type* OP_NEW_3(const char *file, unsigned int line, const P1 &p1, const P2 &p2, const P3 &p3)
130 {
131 #if _USE_RAK_MEMORY_OVERRIDE==1
132 char *buffer = (char *) (GetMalloc_Ex())(sizeof(Type), file, line);
133 Type *t = new (buffer) Type(p1, p2, p3);
134 return t;
135 #else
136 (void) file;
137 (void) line;
138 return new Type(p1, p2, p3);
139 #endif
140 }
141
142 template <class Type, class P1, class P2, class P3, class P4>
143 RAK_DLL_EXPORT Type* OP_NEW_4(const char *file, unsigned int line, const P1 &p1, const P2 &p2, const P3 &p3, const P4 &p4)
144 {
145 #if _USE_RAK_MEMORY_OVERRIDE==1
146 char *buffer = (char *) (GetMalloc_Ex())(sizeof(Type), file, line);
147 Type *t = new (buffer) Type(p1, p2, p3, p4);
148 return t;
149 #else
150 (void) file;
151 (void) line;
152 return new Type(p1, p2, p3, p4);
153 #endif
154 }
155
156
157 template <class Type>
158 RAK_DLL_EXPORT Type* OP_NEW_ARRAY(const int count, const char *file, unsigned int line)
159 {
160 if (count==0)
161 return 0;
162
163 #if _USE_RAK_MEMORY_OVERRIDE==1
164 // Type *t;
165 char *buffer = (char *) (GetMalloc_Ex())(sizeof(int)+sizeof(Type)*count, file, line);
166 ((int*)buffer)[0]=count;
167 for (int i=0; i<count; i++)
168 {
169 //t =
170 new(buffer+sizeof(int)+i*sizeof(Type)) Type;
171 }
172 return (Type *) (buffer+sizeof(int));
173 #else
174 (void) file;
175 (void) line;
176 return new Type[count];
177 #endif
178
179 }
180
181 template <class Type>
182 RAK_DLL_EXPORT void OP_DELETE(Type *buff, const char *file, unsigned int line)
183 {
184 #if _USE_RAK_MEMORY_OVERRIDE==1
185 if (buff==0) return;
186 buff->~Type();
187 (GetFree_Ex())((char*)buff, file, line );
188 #else
189 (void) file;
190 (void) line;
191 delete buff;
192 #endif
193
194 }
195
196 template <class Type>
197 RAK_DLL_EXPORT void OP_DELETE_ARRAY(Type *buff, const char *file, unsigned int line)
198 {
199 #if _USE_RAK_MEMORY_OVERRIDE==1
200 if (buff==0)
201 return;
202
203 int count = ((int*)((char*)buff-sizeof(int)))[0];
204 Type *t;
205 for (int i=0; i<count; i++)
206 {
207 t = buff+i;
208 t->~Type();
209 }
210 (GetFree_Ex())((char*)buff-sizeof(int), file, line );
211 #else
212 (void) file;
213 (void) line;
214 delete [] buff;
215 #endif
216
217 }
218
219 void RAK_DLL_EXPORT * _RakMalloc (size_t size);
220 void RAK_DLL_EXPORT * _RakRealloc (void *p, size_t size);
221 void RAK_DLL_EXPORT _RakFree (void *p);
222 void RAK_DLL_EXPORT * _RakMalloc_Ex (size_t size, const char *file, unsigned int line);
223 void RAK_DLL_EXPORT * _RakRealloc_Ex (void *p, size_t size, const char *file, unsigned int line);
224 void RAK_DLL_EXPORT _RakFree_Ex (void *p, const char *file, unsigned int line);
225 void RAK_DLL_EXPORT * _DLMallocMMap (size_t size);
226 void RAK_DLL_EXPORT * _DLMallocDirectMMap (size_t size);
227 int RAK_DLL_EXPORT _DLMallocMUnmap (void *p, size_t size);
228
229 }
230
231 // Call to make RakNet allocate a large block of memory, and do all subsequent allocations in that memory block
232 // Initial and reallocations will be done through whatever function is pointed to by yourMMapFunction, and yourDirectMMapFunction (default is malloc)
233 // Allocations will be freed through whatever function is pointed to by yourMUnmapFunction (default free)
234 void UseRaknetFixedHeap(size_t initialCapacity,
235 void * (*yourMMapFunction) (size_t size) = SLNet::_DLMallocMMap,
236 void * (*yourDirectMMapFunction) (size_t size) = SLNet::_DLMallocDirectMMap,
237 int (*yourMUnmapFunction) (void *p, size_t size) = SLNet::_DLMallocMUnmap);
238
239 // Free memory allocated from UseRaknetFixedHeap
240 void FreeRakNetFixedHeap(void);
241
242 // #if _USE_RAK_MEMORY_OVERRIDE==1
243 // #if defined(RMO_NEW_UNDEF)
244 // #pragma pop_macro("new")
245 // #undef RMO_NEW_UNDEF
246 // #endif
247 // #endif
248
249 #endif