1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/*************************************************************************/
/* memory.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef MEMORY_H
#define MEMORY_H
#include <stddef.h>
#include "safe_refcount.h"
#include "os/memory_pool_dynamic.h"
#include "os/memory_pool_static.h"
/**
@author Juan Linietsky <reduzio@gmail.com>
*/
class MID {
struct Data {
SafeRefCount refcount;
MemoryPoolDynamic::ID id;
};
mutable Data *data;
void unref() {
if (!data)
return;
if (data->refcount.unref()) {
if (data->id!=MemoryPoolDynamic::INVALID_ID)
MemoryPoolDynamic::get_singleton()->free(data->id);
MemoryPoolStatic::get_singleton()->free(data);
}
data=NULL;
}
void ref(Data *p_data) {
if (data==p_data)
return;
unref();
if (p_data && p_data->refcount.ref())
data=p_data;
}
friend class MID_Lock;
inline void lock() {
if (data && data->id!=MemoryPoolDynamic::INVALID_ID)
MemoryPoolDynamic::get_singleton()->lock(data->id);
}
inline void unlock() {
if (data && data->id!=MemoryPoolDynamic::INVALID_ID)
MemoryPoolDynamic::get_singleton()->unlock(data->id);
}
inline void * get() {
if (data && data->id!=MemoryPoolDynamic::INVALID_ID)
return MemoryPoolDynamic::get_singleton()->get(data->id);
return NULL;
}
Error _resize(size_t p_size) {
if (p_size==0 && (!data || data->id==MemoryPoolDynamic::INVALID_ID))
return OK;
if (p_size && !data) {
// create data because we'll need it
data = (Data*)MemoryPoolStatic::get_singleton()->alloc(sizeof(Data),"MID::Data");
ERR_FAIL_COND_V( !data,ERR_OUT_OF_MEMORY );
data->refcount.init();
data->id=MemoryPoolDynamic::INVALID_ID;
}
if (p_size==0 && data && data->id==MemoryPoolDynamic::INVALID_ID) {
MemoryPoolDynamic::get_singleton()->free(data->id);
data->id=MemoryPoolDynamic::INVALID_ID;
}
if (p_size>0) {
if (data->id==MemoryPoolDynamic::INVALID_ID) {
data->id=MemoryPoolDynamic::get_singleton()->alloc(p_size,"Unnamed MID");
ERR_FAIL_COND_V( data->id==MemoryPoolDynamic::INVALID_ID, ERR_OUT_OF_MEMORY );
} else {
MemoryPoolDynamic::get_singleton()->realloc(data->id,p_size);
ERR_FAIL_COND_V( data->id==MemoryPoolDynamic::INVALID_ID, ERR_OUT_OF_MEMORY );
}
}
return OK;
}
friend class Memory;
MID(MemoryPoolDynamic::ID p_id) {
data = (Data*)MemoryPoolStatic::get_singleton()->alloc(sizeof(Data),"MID::Data");
data->refcount.init();
data->id=p_id;
}
public:
bool is_valid() const { return data; }
operator bool() const { return data; }
size_t get_size() const { return (data && data->id!=MemoryPoolDynamic::INVALID_ID) ? MemoryPoolDynamic::get_singleton()->get_size(data->id) : 0; }
Error resize(size_t p_size) { return _resize(p_size); }
inline void operator=(const MID& p_mid) { ref( p_mid.data ); }
inline bool is_locked() const { return (data && data->id!=MemoryPoolDynamic::INVALID_ID) ? MemoryPoolDynamic::get_singleton()->is_locked(data->id) : false; }
inline MID(const MID& p_mid) { data=NULL; ref( p_mid.data ); }
inline MID() { data = NULL; }
~MID() { unref(); }
};
class MID_Lock {
MID mid;
public:
void *data() { return mid.get(); }
void operator=(const MID_Lock& p_lock ) { mid.unlock(); mid = p_lock.mid; mid.lock(); }
inline MID_Lock(const MID& p_mid) { mid=p_mid; mid.lock(); }
inline MID_Lock(const MID_Lock& p_lock) { mid=p_lock.mid; mid.lock(); }
MID_Lock() {}
~MID_Lock() { mid.unlock(); }
};
class Memory{
Memory();
public:
static void * alloc_static(size_t p_bytes,const char *p_descr="");
static void * realloc_static(void *p_memory,size_t p_bytes);
static void free_static(void *p_ptr);
static size_t get_static_mem_available();
static size_t get_static_mem_usage();
static size_t get_static_mem_max_usage();
static void dump_static_mem_to_file(const char* p_file);
static MID alloc_dynamic(size_t p_bytes, const char *p_descr="");
static Error realloc_dynamic(MID p_mid,size_t p_bytes);
static size_t get_dynamic_mem_available();
static size_t get_dynamic_mem_usage();
};
template<class T>
struct MemAalign {
static _FORCE_INLINE_ int get_align() { return DEFAULT_ALIGNMENT; }
};
class DefaultAllocator {
public:
_FORCE_INLINE_ static void *alloc(size_t p_memory) { return Memory::alloc_static(p_memory, ""); }
_FORCE_INLINE_ static void free(void *p_ptr) { return Memory::free_static(p_ptr); }
};
void * operator new(size_t p_size,const char *p_description); ///< operator new that takes a description and uses MemoryStaticPool
void * operator new(size_t p_size,void* (*p_allocfunc)(size_t p_size)); ///< operator new that takes a description and uses MemoryStaticPool
void * operator new(size_t p_size,void *p_pointer,size_t check, const char *p_description); ///< operator new that takes a description and uses a pointer to the preallocated memory
#ifdef DEBUG_MEMORY_ENABLED
#define memalloc(m_size) Memory::alloc_static(m_size, __FILE__":"__STR(__LINE__)", memalloc.")
#define memrealloc(m_mem,m_size) Memory::realloc_static(m_mem,m_size)
#define memfree(m_size) Memory::free_static(m_size)
#else
#define memalloc(m_size) Memory::alloc_static(m_size)
#define memrealloc(m_mem,m_size) Memory::realloc_static(m_mem,m_size)
#define memfree(m_size) Memory::free_static(m_size)
#endif
#ifdef DEBUG_MEMORY_ENABLED
#define dynalloc(m_size) Memory::alloc_dynamic(m_size, __FILE__":"__STR(__LINE__)", type: DYNAMIC")
#define dynrealloc(m_mem,m_size) m_mem.resize(m_size)
#else
#define dynalloc(m_size) Memory::alloc_dynamic(m_size)
#define dynrealloc(m_mem,m_size) m_mem.resize(m_size)
#endif
_FORCE_INLINE_ void postinitialize_handler(void *) {}
template<class T>
_FORCE_INLINE_ T *_post_initialize(T *p_obj) {
postinitialize_handler(p_obj);
return p_obj;
}
#ifdef DEBUG_MEMORY_ENABLED
#define memnew(m_class) _post_initialize(new(__FILE__":"__STR(__LINE__)", memnew type: "__STR(m_class)) m_class)
#define memnew_placement(m_placement,m_class) _post_initialize(new(m_placement,sizeof(m_class),__FILE__":"__STR(__LINE__)", type: "__STR(m_class)) m_class)
#else
#define memnew(m_class) _post_initialize(new("") m_class)
#define memnew_placement(m_placement,m_class) _post_initialize(new(m_placement,sizeof(m_class),"") m_class)
#endif
#define memnew_allocator(m_class,m_allocator) _post_initialize(new(m_allocator::alloc) m_class)
_FORCE_INLINE_ bool predelete_handler(void *) { return true; }
template<class T>
void memdelete(T *p_class) {
if (!predelete_handler(p_class))
return; // doesn't want to be deleted
p_class->~T();
Memory::free_static(p_class);
}
template<class T,class A>
void memdelete_allocator(T *p_class) {
if (!predelete_handler(p_class))
return; // doesn't want to be deleted
p_class->~T();
A::free(p_class);
}
#define memdelete_notnull(m_v) { if (m_v) memdelete(m_v); }
#ifdef DEBUG_MEMORY_ENABLED
#define memnew_arr( m_class, m_count ) memnew_arr_template<m_class>(m_count,__FILE__":"__STR(__LINE__)", memnew_arr type: "_STR(m_class))
#else
#define memnew_arr( m_class, m_count ) memnew_arr_template<m_class>(m_count)
#endif
template<typename T>
T* memnew_arr_template(size_t p_elements,const char *p_descr="") {
if (p_elements==0)
return 0;
/** overloading operator new[] cannot be done , because it may not return the real allocated address (it may pad the 'element count' before the actual array). Because of that, it must be done by hand. This is the
same strategy used by std::vector, and the DVector class, so it should be safe.*/
size_t len = sizeof(T) * p_elements;
unsigned int *mem = (unsigned int*)Memory::alloc_static( len + DEFAULT_ALIGNMENT, p_descr );
T *failptr=0; //get rid of a warning
ERR_FAIL_COND_V( !mem, failptr );
*mem=p_elements;
mem = (unsigned int *)( ((uint8_t*)mem) + DEFAULT_ALIGNMENT);
T* elems = (T*)mem;
/* call operator new */
for (size_t i=0;i<p_elements;i++) {
new(&elems[i],sizeof(T),p_descr) T;
}
return (T*)mem;
}
/**
* Wonders of having own array functions, you can actually check the length of
* an allocated-with memnew_arr() array
*/
template<typename T>
size_t memarr_len(const T *p_class) {
uint8_t* ptr = ((uint8_t*)p_class) - DEFAULT_ALIGNMENT;
return *(size_t*)ptr;
}
template<typename T>
void memdelete_arr(T *p_class) {
unsigned int * elems = (unsigned int*)(((uint8_t*)p_class) - DEFAULT_ALIGNMENT);
for (unsigned int i=0;i<*elems;i++) {
p_class[i].~T();
};
Memory::free_static(elems);
}
struct _GlobalNil {
int color;
_GlobalNil *right;
_GlobalNil *left;
_GlobalNil *parent;
_GlobalNil();
};
struct _GlobalNilClass {
static _GlobalNil _nil;
};
#endif
|