gwnavruntime/querysystem/workingmemory.h Source File

workingmemory.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 typedef KyUInt32 BufferIndex;
17 static const BufferIndex BufferIndex_Invalid = KyUInt32MAXVAL;
18 
19 // 13 is enough given our current usage of queries but we should create buffers on demand instead
20 // of having hardcoded limites like that
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(nullptr), m_memorySize(0), m_inUse(false) {}
48 
49  void* m_memory;
50  KyUInt32 m_memorySize;
51  bool m_inUse;
52  };
53 
54  // \param memoryLimitSize specifies the local heap memory allocation limit
55  // \param granularityOfBufferResize specifies how the buffer size increase if more memory is needed
56  WorkingMemory(KyUInt32 memoryLimitSize = 4 * 1024 * 1024 /* 4 Mb */, KyUInt32 granularityOfBufferResize = 5 * 1024/* 5 Kb */);
57  ~WorkingMemory();
58 
59  void Init(KyUInt32 memoryLimitSize, KyUInt32 granularityOfBufferResize);
60  void InitMemoryHeap(KyUInt32 memoryLimitSize);
61 
62  void SetNewLimit(KyUInt32 memoryLimitSize);
63 
64  void* AllocBiggerBuffer(BufferIndex bufferIdx, KyUInt32 newMinByteSize = 0);
65 
66  void ReleaseAllMemoryBuffer();
67  void ReleaseUnusedMemoryBuffer();
68  void ReleaseMemory(void* memory) { KY_HEAP_FREE(m_memoryHeap, memory); }
69 
70  void InitBuffersToNull();
71 
72  BufferIndex TakeUsageOfFirstUnusedBufferIdx();
73  void ReleaseBuffer(BufferIndex bufferIdx);
74 
75  void* GetBuffer(BufferIndex bufferIdx) const;
76  KyUInt32 GetBufferSize(BufferIndex bufferIdx) const;
77 
78  KyUInt32 GetTotalAllocatedSize() const;
79 
80  // Used by astarQuery
81  AStarTraversalContext* GetOrCreateAStarTraversalContext();
82  PathRefinerContext* GetOrCreatePathRefinerContext();
83  PathClamperContext* GetOrCreatePathClamperContext();
84  NavDataChangeIndexInGrid* GetOrCreateNavDataChangeIndexInGrid();
85  ChannelArrayComputer* GetOrCreateChannelArrayComputer();
86  DynamicNavMeshContext* GetOrCreateDynamicNavMeshContext();
87  SpatializedPointCollectorContext* GetOrCreateSpatializedPointCollectorContext();
88 
89 private:
90  void CreateAllPathfinderContexts();
91  void CreateChannelArrayComputer();
92  void CreateDynamicNavMeshContext();
93  void CreateSpatializedPointCollectorContext();
94 
95 public:
96  MemoryHeap* m_memoryHeap;
97  MyLimitHandler m_myLimitHandler;
98  KyUInt32 m_granularityOfBufferResize;
99  WorkingBuffer m_workingBuffer[BufferIndex_Count];
100 
101 public:
102  AStarTraversalContext* m_astarContext;
103  PathRefinerContext* m_refinerContext;
104  PathClamperContext* m_clamperContext;
105  NavDataChangeIndexInGrid* m_navDataChangeIndexInGrid;
106  ChannelArrayComputer* m_channelArrayComputer;
107 
108  SpatializedPointCollectorContext* m_collectorContext;
109 
110  DynamicNavMeshContext* m_dynamicNavMeshContext;
111 
112  Ptr<QueryDynamicOutput> m_queryDynamicOutput; // Do not use the workingMemory but can be used by all queries
113 };
114 
115 KY_INLINE void WorkingMemory::ReleaseBuffer(BufferIndex bufferIdx)
116 {
117  KY_ASSERT(bufferIdx < BufferIndex_Count);
118  m_workingBuffer[bufferIdx].m_inUse = false;
119 }
120 KY_INLINE void* WorkingMemory::GetBuffer(BufferIndex bufferIdx) const
121 {
122  KY_ASSERT(bufferIdx < BufferIndex_Count);
123  return m_workingBuffer[bufferIdx].m_memory;
124 }
125 
126 KY_INLINE KyUInt32 WorkingMemory::GetBufferSize(BufferIndex bufferIdx) const
127 {
128  KY_ASSERT(bufferIdx < BufferIndex_Count);
129  return m_workingBuffer[bufferIdx].m_memorySize;
130 }
131 
132 
133 KY_INLINE AStarTraversalContext* WorkingMemory::GetOrCreateAStarTraversalContext()
134 {
135  if (m_astarContext == nullptr)
136  CreateAllPathfinderContexts();
137  return m_astarContext;
138 }
139 KY_INLINE PathRefinerContext* WorkingMemory::GetOrCreatePathRefinerContext()
140 {
141  if (m_refinerContext == nullptr)
142  CreateAllPathfinderContexts();
143  return m_refinerContext;
144 }
145 KY_INLINE PathClamperContext* WorkingMemory::GetOrCreatePathClamperContext()
146 {
147  if (m_clamperContext == nullptr)
148  CreateAllPathfinderContexts();
149  return m_clamperContext;
150 }
151 KY_INLINE NavDataChangeIndexInGrid* WorkingMemory::GetOrCreateNavDataChangeIndexInGrid()
152 {
153  if (m_navDataChangeIndexInGrid == nullptr)
154  CreateAllPathfinderContexts();
155  return m_navDataChangeIndexInGrid;
156 }
157 KY_INLINE ChannelArrayComputer* WorkingMemory::GetOrCreateChannelArrayComputer()
158 {
159  if (m_channelArrayComputer == nullptr)
160  CreateChannelArrayComputer();
161  return m_channelArrayComputer;
162 }
163 KY_INLINE DynamicNavMeshContext* WorkingMemory::GetOrCreateDynamicNavMeshContext()
164 {
165  if (m_dynamicNavMeshContext == nullptr)
166  CreateDynamicNavMeshContext();
167  return m_dynamicNavMeshContext;
168 }
169 KY_INLINE SpatializedPointCollectorContext* WorkingMemory::GetOrCreateSpatializedPointCollectorContext()
170 {
171  if (m_collectorContext == nullptr)
172  CreateSpatializedPointCollectorContext();
173  return m_collectorContext;
174 }
175 
176 }
177 
178 
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
#define KY_CLASS_WITHOUT_COPY(ClassName)
Define to forbid copy constructor and copy assignment.
Definition: types.h:196
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
#define KyUInt32MAXVAL
KyUInt32 max value
Definition: types.h:68