gwnavruntime/base/memory.h Source File

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