gwnavruntime/querysystem/workingmemory.h Source File

workingmemory.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: JUBA - secondary contact: NOBODY
9 #ifndef Navigation_WorkingMemory_H
10 #define Navigation_WorkingMemory_H
11 
15 
16 namespace Kaim
17 {
18 
19 typedef KyUInt32 BufferIndex;
20 static const BufferIndex BufferIndex_Invalid = KyUInt32MAXVAL;
21 static const BufferIndex BufferIndex_Count = 13;
22 
23 class AStarTraversalContext;
24 class PathRefinerContext;
25 class PathClamperContext;
26 class NavDataChangeIndexInGrid;
27 class SpatializedPointCollectorContext;
28 class ChannelArrayComputer;
29 class DynamicNavMeshContext;
30 class QueryDynamicOutput;
31 
32 class WorkingMemory
33 {
34  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_WorldFwk)
35  KY_CLASS_WITHOUT_COPY(WorkingMemory)
36 
37 public:
38  struct MyLimitHandler : public MemoryHeap::LimitHandler
39  {
40  virtual ~MyLimitHandler() {}
41  virtual bool OnExceedLimit(MemoryHeap* /*heap*/, UPInt /*overLimit*/) { return false; };
42  virtual void OnFreeSegment(MemoryHeap* /*heap*/, UPInt /*freeingSize*/) { };
43  };
44 
45  struct WorkingBuffer
46  {
47  WorkingBuffer() : m_memory(KY_NULL), m_memorySize(0), m_inUse(false) {}
48 
49  void* m_memory;
50  KyUInt32 m_memorySize;
51  bool m_inUse;
52  };
53 
54  /*
55  \param memoryLimitSize specifies the local heap memory allocation limit
56  \param granularityOfBufferResize specifies how the buffer size increase if more memory is needed
57  */
58  WorkingMemory(KyUInt32 memoryLimitSize = 4 * 1024 * 1024 /* 4 Mb */, KyUInt32 granularityOfBufferResize = 5 * 1024/* 5 Kb */);
59  ~WorkingMemory();
60 
61  void Init(KyUInt32 memoryLimitSize, KyUInt32 granularityOfBufferResize);
62  void InitMemoryHeap(KyUInt32 memoryLimitSize);
63 
64  void SetNewLimit(KyUInt32 memoryLimitSize);
65 
66  void* ResizeBuffer(BufferIndex bufferIdx, KyUInt32 newMinByteSize = 0);
67 
68  void ReleaseAllMemoryBuffer();
69  void ReleaseUnusedMemoryBuffer();
70  void ReleaseMemory(void* memory)
71  {
72  /*KY_DEBUG_MESSAGEN(("release %x\n", (int)memory));*/
73  KY_HEAP_FREE(m_memoryHeap, memory);
74  }
75 
76  void InitBuffersToNull();
77 
78  BufferIndex TakeUsageOfFirstUnusedBufferIdx();
79  void ReleaseBuffer(BufferIndex bufferIdx);
80 
81  void* GetBuffer(BufferIndex bufferIdx) const;
82  KyUInt32 GetBufferSize(BufferIndex bufferIdx) const;
83 
84  KyUInt32 GetTotalAllocatedSize() const;
85 
86  // Used by astarQuery
87  AStarTraversalContext* GetOrCreateAStarTraversalContext();
88  PathRefinerContext* GetOrCreatePathRefinerContext();
89  PathClamperContext* GetOrCreatePathClamperContext();
90  NavDataChangeIndexInGrid* GetOrCreateNavDataChangeIndexInGrid();
91  ChannelArrayComputer* GetOrCreateChannelArrayComputer();
92  DynamicNavMeshContext* GetOrCreateDynamicNavMeshContext();
93  SpatializedPointCollectorContext* GetOrCreateSpatializedPointCollectorContext();
94 private:
95  void CreateAllPathfinderContexts();
96  void CreateChannelArrayComputer();
97  void CreateDynamicNavMeshContext();
98  void CreateSpatializedPointCollectorContext();
99 public:
100  MemoryHeap* m_memoryHeap;
101  MyLimitHandler m_myLimitHandler;
102  KyUInt32 m_granularityOfBufferResize;
103  WorkingBuffer m_workingBuffer[BufferIndex_Count];
104 
105 public:
106  AStarTraversalContext* m_astarContext;
107  PathRefinerContext* m_refinerContext;
108  PathClamperContext* m_clamperContext;
109  NavDataChangeIndexInGrid* m_navDataChangeIndexInGrid;
110  ChannelArrayComputer* m_channelArrayComputer;
111 
112  SpatializedPointCollectorContext* m_collectorContext;
113 
114  DynamicNavMeshContext* m_dynamicNavMeshContext;
115 
116  Ptr<QueryDynamicOutput> m_queryDynamicOutput; // Do not use the workingMemory but can be used by all queries
117 };
118 
119 KY_INLINE void WorkingMemory::ReleaseBuffer(BufferIndex bufferIdx)
120 {
121  KY_ASSERT(bufferIdx < BufferIndex_Count);
122  m_workingBuffer[bufferIdx].m_inUse = false;
123 }
124 KY_INLINE void* WorkingMemory::GetBuffer(BufferIndex bufferIdx) const
125 {
126  KY_ASSERT(bufferIdx < BufferIndex_Count);
127  return m_workingBuffer[bufferIdx].m_memory;
128 }
129 
130 KY_INLINE KyUInt32 WorkingMemory::GetBufferSize(BufferIndex bufferIdx) const
131 {
132  KY_ASSERT(bufferIdx < BufferIndex_Count);
133  return m_workingBuffer[bufferIdx].m_memorySize;
134 }
135 
136 
137 
138 KY_INLINE AStarTraversalContext* WorkingMemory::GetOrCreateAStarTraversalContext()
139 {
140  if (m_astarContext == KY_NULL)
141  CreateAllPathfinderContexts();
142  return m_astarContext;
143 }
144 KY_INLINE PathRefinerContext* WorkingMemory::GetOrCreatePathRefinerContext()
145 {
146  if (m_refinerContext == KY_NULL)
147  CreateAllPathfinderContexts();
148  return m_refinerContext;
149 }
150 KY_INLINE PathClamperContext* WorkingMemory::GetOrCreatePathClamperContext()
151 {
152  if (m_clamperContext == KY_NULL)
153  CreateAllPathfinderContexts();
154  return m_clamperContext;
155 }
156 KY_INLINE NavDataChangeIndexInGrid* WorkingMemory::GetOrCreateNavDataChangeIndexInGrid()
157 {
158  if (m_navDataChangeIndexInGrid == KY_NULL)
159  CreateAllPathfinderContexts();
160  return m_navDataChangeIndexInGrid;
161 }
162 KY_INLINE ChannelArrayComputer* WorkingMemory::GetOrCreateChannelArrayComputer()
163 {
164  if (m_channelArrayComputer == KY_NULL)
165  CreateChannelArrayComputer();
166  return m_channelArrayComputer;
167 }
168 KY_INLINE DynamicNavMeshContext* WorkingMemory::GetOrCreateDynamicNavMeshContext()
169 {
170  if (m_dynamicNavMeshContext == KY_NULL)
171  CreateDynamicNavMeshContext();
172  return m_dynamicNavMeshContext;
173 }
174 KY_INLINE SpatializedPointCollectorContext* WorkingMemory::GetOrCreateSpatializedPointCollectorContext()
175 {
176  if (m_collectorContext == KY_NULL)
177  CreateSpatializedPointCollectorContext();
178  return m_collectorContext;
179 }
180 
181 }
182 
183 #endif //Navigation_NavMeshQueryBuffer_H
184 
#define KY_NULL
Null value.
Definition: types.h:247
#define KY_CLASS_WITHOUT_COPY(ClassName)
Define to forbid copy constructor and copy assignment.
Definition: types.h:387
Definition: gamekitcrowddispersion.h:20
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36
#define KyUInt32MAXVAL
The maximum value that can be stored in the KyUInt32 variable type.
Definition: types.h:226