gwnavruntime/querysystem/workingmemcontainers/workingmemcontainerbase.h Source File

workingmemcontainerbase.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 
10 #include <string.h>
11 
12 namespace Kaim
13 {
14 
15 // TODO
16 // Rename to WorkingMemBuffer
17 // Put code relative to the buffer in this call and NOT in WorkingMemory
18 // put code in a .cpp, especially what uses workingMemory
19 class WorkingMemContainerBase
20 {
21 public:
22  WorkingMemContainerBase() : m_workingMemory(nullptr), m_bufferIdx(BufferIndex_Invalid) {}
23  WorkingMemContainerBase(WorkingMemory* workingMemory) : m_workingMemory(nullptr), m_bufferIdx(BufferIndex_Invalid) { Init(workingMemory); }
24  ~WorkingMemContainerBase() { ReleaseBuffer(); }
25 
26  KyResult Init(WorkingMemory* workingMemory)
27  {
28  ReleaseBuffer();
29 
30  BufferIndex bufferIdx = workingMemory->TakeUsageOfFirstUnusedBufferIdx();
31 
32  if (bufferIdx == BufferIndex_Invalid)
33  return KY_ERROR;
34 
35  m_workingMemory = workingMemory;
36  m_bufferIdx = bufferIdx;
37 
38  if (workingMemory->GetBuffer(bufferIdx) == nullptr)
39  {
40  if (workingMemory->AllocBiggerBuffer(bufferIdx) == nullptr)
41  {
42  return KY_ERROR;
43  }
44  }
45 
46  return KY_SUCCESS;
47  }
48 
49  bool IsInitialized() const { return m_workingMemory != nullptr; }
50 
51  void* GetBuffer() const { return m_workingMemory->GetBuffer(m_bufferIdx); }
52  KyUInt32 GetBufferSize() const { return m_workingMemory->GetBufferSize(m_bufferIdx); }
53 
54  char* GetDataPtr() const { return (char*)m_workingMemory->GetBuffer(m_bufferIdx); } // same as GetBuffer() but with a much better name
55  KyUInt32 GetCapacity() const { return m_workingMemory->GetBufferSize(m_bufferIdx); } // same as GetBufferSize() but with a much better name
56 
57  // AllocBiggerBuffer DOES NOT memcpy the previous buffer content...
58  // newMinByteSize == 0 means no specific size is wanted, this will use granularity to compute a new size
59  void* AllocBiggerBuffer(KyUInt32 newMinByteSize = 0) { return m_workingMemory->AllocBiggerBuffer(m_bufferIdx, newMinByteSize); }
60 
61  // memcpy the previous buffer content
62  void* Realloc(KyUInt32 oldSize, KyUInt32 newSize)
63  {
64  void* oldBuffer = GetBuffer();
65 
66  if (newSize <= GetBufferSize())
67  return oldBuffer;
68 
69  void* newBuffer = AllocBiggerBuffer(newSize);
70  if (newBuffer == nullptr)
71  return nullptr;
72 
73  memcpy(newBuffer, oldBuffer, oldSize);
74 
75  #if defined(KY_BUILD_DEBUG)
76  memset(oldBuffer, 0xAB, oldSize);
77  #endif
78 
79  ReleaseMemory(oldBuffer);
80  return newBuffer;
81  }
82 
83  void ReleaseMemory(void* memory) { m_workingMemory->ReleaseMemory(memory); }
84 
85  // free the idx in the Working Memory
86  void ReleaseBuffer()
87  {
88  if (m_workingMemory != nullptr)
89  {
90  m_workingMemory->ReleaseBuffer(m_bufferIdx);
91 
92  m_workingMemory = nullptr;
93  m_bufferIdx = BufferIndex_Invalid;
94  }
95  }
96 
97 public:
98  WorkingMemory* m_workingMemory;
99  BufferIndex m_bufferIdx;
100 };
101 
102 
103 }
104 
105 
106 
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
Navigation return code class.
Definition: types.h:108
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
#define KY_ERROR
use result == KY_ERROR to test for error
Definition: types.h:132