gwnavruntime/base/memory.h Source File

memory.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 
8 // primary contact: JAPA - secondary contact: GUAL
9 
10 #ifndef Navigation_Memory_H
11 #define Navigation_Memory_H
12 
15 
16 namespace Kaim
17 {
18 
19 // Stat identifiers are organized into groups corresponding to different sub-systems such as World, etc.
20 // Every group receives its own Id range, allowing identifiers within that system to be added without collision
21 enum MemStatGroupStartEnum
22 {
23  MemStatGroupStart_NonGroup = 1 << 6, // [64 ; 128[
24  MemStatGroupStart_World = 2 << 6, // [128; 192[
25  MemStatGroupStart_Data = 3 << 6, // [192; 256[
26  MemStatGroupStart_WorkingMem = 4 << 6, // [256; 320[
27  MemStatGroupStart_VisualDebug = 5 << 6, // [320; 384[
28  MemStatGroupStart_NavDataGen = 6 << 6, // [384; 448[
29  MemStatGroupStart_GameKit = 7 << 6, //
30  MemStatGroupStart_External = 8 << 6, //
31 };
32 
33 enum MemStatEnum_NonGroup
34 {
35  MemStat_BaseSystem = MemStatGroupStart_NonGroup
36 };
37 
38 enum MemStatEnum_World
39 {
40  MemStatGroup_World = MemStatGroupStart_World,
41  MemStat_WorldFwk,
42  MemStat_BoxObstacle,
43  MemStat_TagVolume,
44  MemStat_CylinderObstacle,
45  MemStat_PointOfInterest,
46  MemStat_Bot,
47  MemStat_PathFollowing,
48  MemStat_LivePath,
49  MemStat_Path,
50  MemStat_Channel,
51  MemStat_Query,
52  MemStat_QueryOutput,
53  MemStat_QuerySystem,
54  MemStat_DynamicNavMesh,
55  MemStat_VisualDebugRegistry,
56  MemStat_World_Other,
57 };
58 
59 enum MemStatEnum_Data
60 {
61  MemStatGroup_Data = MemStatGroupStart_Data,
62  MemStat_Blob,
63  MemStat_NavDataBlob,
64  MemStat_NavData,
65  MemStat_NavMesh,
66  MemStat_NavGraph,
67  MemStat_Spatialization,
68  MemStat_BlobBuilder,
69  MemStat_CollisionDataBlob,
70  MemStat_CollisionData,
71  MemStat_CostMap,
72  MemStat_Data_Other,
73 };
74 
75 enum MemStatEnum_WorkingMem
76 {
77  MemStatGroup_WorkingMem = MemStatGroupStart_WorkingMem,
78  MemStat_QueryWorkingMem,
79  MemStat_DynNavMesh,
80 };
81 
82 enum MemStatEnum_VisualDebug
83 {
84  MemStatGroup_VisualDebug = MemStatGroupStart_VisualDebug,
85  MemStat_VisualDebug,
86  MemStat_VisualSystem,
87  MemStat_VisualDebugMessage,
88  MemStat_NavLabFrame,
89  MemStat_Statistics,
90  MemStat_VisualDebug_Other,
91 };
92 
93 enum MemStatEnum_NavDataGen
94 {
95  MemStatGroup_NavDataGen = MemStatGroupStart_NavDataGen,
96  MemStat_NavDataGen,
97 };
98 
99 enum MemStatEnum_GameKit
100 {
101  MemStatGroup_GameKit = MemStatGroupStart_GameKit,
102  MemStat_GameKit,
103 };
104 
105 enum MemStatEnum_External
106 {
107  MemStatGroup_External = MemStatGroupStart_External,
108  MemStat_LabEngine,
109  MemStat_LabGame,
110  MemStat_NavigationLab,
111  MemStat_UnitTest,
112  MemStat_Client,
113 };
114 
115 
116 void LinkMemoryStats();
117 
118 }
119 
120 //--------------------------------------------------------------------------------------------
121 // macros for built-in types - no constructor or destructor called
122 // same as KY_ALLOC but take a T parameter and automatically multiply by sizeof(T)
123 #define KY_MALLOC(T, count, memStat) ( (T*)KY_ALLOC((sizeof(T) * (count)), memStat) )
124 #define KY_MALLOC_ALIGNED(T, count, alignment, memStat) ( (T*)KY_MEMALIGN((sizeof(T) * (count)), alignment, memStat) )
125 #define KY_HEAP_MALLOC(heap, T, n, memStat) ( (T*)KY_HEAP_ALLOC(heap, (sizeof(T) * (n)), memStat) )
126 
127 
128 //--------------------------------------------------------------------------------------------
129 // Use Gameware Navigation allocation for classes that do not have KY_DEFINE_NEW_DELETE_OPERATORS
130 // Requires exact class for destruction
131 #define KY_NEW_EXTERN(T, memStat) new(KY_ALLOC(sizeof(T), memStat)) T
132 #define KY_DELETE_EXTERN(T, p) if (p) { p->~T(); KY_FREE(p); }
133 
134 
137 #define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat) \
138  public: \
139  KY_MEMORY_REDEFINE_NEW(NavigationClass, MemStat) \
140  KY_MEMORY_DEFINE_PLACEMENT_NEW \
141  private:
142 
145 #define KY_REFCOUNT_MALLOC_FREE(ClassName) \
146  public: \
147  KY_INLINE void AddRef() { RefCount ++; } \
148  KY_INLINE void Release() \
149  { \
150  if ((RefCount.ExchangeAdd_NoSync(-1) - 1) == 0) \
151  KY_FREE(this); \
152  } \
153  int GetRefCount() const { return RefCount; } \
154  private: \
155  ClassName(); /* Default constructor implementation mandatory */ \
156  static KY_INLINE ClassName* Create(char* memoryStart) \
157  { \
158  ClassName* instance = (::new((ClassName*)memoryStart) ClassName()); \
159  if (instance) instance->RefCount = 1; \
160  return instance; \
161  } \
162  private: \
163  mutable AtomicInt<int> RefCount;
164 
165 namespace Kaim
166 {
167 
168 // Add KY_NEW_ID to alreadt existing KY_HEAP_NEW, KY_HEAP_NEW_ID, KY_HEAP_AUTO_NEW, KY_NEW
169 #ifdef KY_MEMORY_ENABLE_DEBUG_INFO
170 # define KY_NEW_ID(id) new(Kaim::Memory::GetGlobalHeap(), id, __FILE__,__LINE__)
171 #else
172 # define KY_NEW_ID(id) new(Kaim::Memory::GetGlobalHeap(), id)
173 #endif
174 
175 // Simple macro for repetitive code
176 #define KY_DELETE_AND_SET_NULL(x) \
177 { \
178  if (x != KY_NULL) \
179  { \
180  delete x; \
181  x = KY_NULL; \
182  } \
183 } \
184 
185 
186 }
187 
188 #endif
Definition: gamekitcrowddispersion.h:20