Panda3D
memoryBase.h
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file memoryBase.h
10  * @author drose
11  * @date 2006-11-16
12  */
13 
14 #ifndef MEMORYBASE_H
15 #define MEMORYBASE_H
16 
17 #include "dtoolbase.h"
18 #include "memoryHook.h"
19 
20 // Place this macro within a class definition to define appropriate operator
21 // new and delete methods that hook into the MemoryInfo class to provide
22 // memory tracking. Of course, it is better simply to inherit from
23 // MemoryBase; this macro is provided to resolve problems with multiple
24 // inheritance or some such.
25 
26 #define ALLOC_MEMORY_BASE \
27  inline void *operator new(size_t size) RETURNS_ALIGNED(MEMORY_HOOK_ALIGNMENT) { \
28  return PANDA_MALLOC_SINGLE(size); \
29  } \
30  inline void *operator new(size_t size, void *ptr) { \
31  (void) size; \
32  return ptr; \
33  } \
34  inline void operator delete(void *ptr) { \
35  if (ptr != nullptr) { \
36  PANDA_FREE_SINGLE(ptr); \
37  } \
38  } \
39  inline void operator delete(void *, void *) { \
40  } \
41  inline void *operator new[](size_t size) RETURNS_ALIGNED(MEMORY_HOOK_ALIGNMENT) { \
42  return PANDA_MALLOC_ARRAY(size); \
43  } \
44  inline void *operator new[](size_t size, void *ptr) { \
45  (void) size; \
46  return ptr; \
47  } \
48  inline void operator delete[](void *ptr) { \
49  if (ptr != nullptr) { \
50  PANDA_FREE_ARRAY(ptr); \
51  } \
52  } \
53  inline void operator delete[](void *, void *) { \
54  }
55 
56 /**
57  * This class is intended to be the base class of all objects in Panda that
58  * might be allocated and deleted via the new and delete operators. It
59  * redefines these operators to provide some memory tracking support.
60  *
61  * We used to try to override the global operator new and delete methods, but
62  * that seems to cause problems when including header files for C++-based
63  * system libraries (such as are found on OSX).
64  */
65 class EXPCL_DTOOL_DTOOLBASE MemoryBase {
66 public:
67  ALLOC_MEMORY_BASE;
68 };
69 
70 #endif
This class is intended to be the base class of all objects in Panda that might be allocated and delet...
Definition: memoryBase.h:65
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.