-
-
Notifications
You must be signed in to change notification settings - Fork 772
/
memoryBase.h
70 lines (64 loc) · 3.01 KB
/
memoryBase.h
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
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file memoryBase.h
* @author drose
* @date 2006-11-16
*/
#ifndef MEMORYBASE_H
#define MEMORYBASE_H
#include "dtoolbase.h"
#include "memoryHook.h"
// Place this macro within a class definition to define appropriate operator
// new and delete methods that hook into the MemoryInfo class to provide
// memory tracking. Of course, it is better simply to inherit from
// MemoryBase; this macro is provided to resolve problems with multiple
// inheritance or some such.
#define ALLOC_MEMORY_BASE \
inline void *operator new(size_t size) RETURNS_ALIGNED(MEMORY_HOOK_ALIGNMENT) { \
return PANDA_MALLOC_SINGLE(size); \
} \
inline void *operator new(size_t size, void *ptr) { \
(void) size; \
return ptr; \
} \
inline void operator delete(void *ptr) { \
if (ptr != nullptr) { \
PANDA_FREE_SINGLE(ptr); \
} \
} \
inline void operator delete(void *, void *) { \
} \
inline void *operator new[](size_t size) RETURNS_ALIGNED(MEMORY_HOOK_ALIGNMENT) { \
return PANDA_MALLOC_ARRAY(size); \
} \
inline void *operator new[](size_t size, void *ptr) { \
(void) size; \
return ptr; \
} \
inline void operator delete[](void *ptr) { \
if (ptr != nullptr) { \
PANDA_FREE_ARRAY(ptr); \
} \
} \
inline void operator delete[](void *, void *) { \
}
/**
* This class is intended to be the base class of all objects in Panda that
* might be allocated and deleted via the new and delete operators. It
* redefines these operators to provide some memory tracking support.
*
* We used to try to override the global operator new and delete methods, but
* that seems to cause problems when including header files for C++-based
* system libraries (such as are found on OSX).
*/
class EXPCL_DTOOL_DTOOLBASE MemoryBase {
public:
ALLOC_MEMORY_BASE;
};
#endif