gwnavruntime/containers/poolchunk.h Source File
Go to the documentation of this file.
8 #ifndef Navigation_PoolChunk_H
9 #define Navigation_PoolChunk_H
30 KY_INLINE
void Delete(T* ptr) { Delete(GetIdxInChunk(ptr)); }
33 const T* Get(
KyUInt32 index)
const {
return ((T*)m_buffer) + index; }
34 T* Get(
KyUInt32 index) {
return ((T*)m_buffer) + index; }
36 KY_INLINE
bool IsFull()
const {
return (m_nbFreeSlots == 0); }
37 KY_INLINE
bool IsEmpty()
const {
return (m_nbFreeSlots == GetNbSlots()); }
39 KY_INLINE
bool IsPtrInChunk(
const T* ptr)
const {
return ((T*)m_buffer) <= ptr && ptr < (((T*)m_buffer) + m_nbTotalSlots); }
41 KY_INLINE
KyUInt32 GetNbSlots()
const {
return m_nbTotalSlots; }
42 KY_INLINE
KyUInt32 GetNbUsedSlots()
const {
return m_nbTotalSlots - m_nbFreeSlots; }
44 KyUInt32 GetIdxInChunk(
const T* ptr)
const {
return KyUInt32(ptr - (T*)m_buffer); }
46 bool SuperSlow_IsSlotFree(
KyUInt32 index)
const;
51 void SetNextFreeSlot(
KyUInt32 index,
KyUInt32 freeSlotIndex) { *((
KyUInt32*) (m_buffer + (
sizeof(T) * index))) = freeSlotIndex; }
66 PoolChunk<T>::PoolChunk(MemoryHeap* heap,
KyUInt32 slotCount,
KyUInt32 memStat) :
67 m_buffer((char*)KY_HEAP_ALLOC(heap, (sizeof(T) * slotCount), memStat)),
68 m_nbFreeSlots(slotCount),
69 m_nbTotalSlots(slotCount),
72 KY_DEBUG_ASSERTN(
sizeof (T) >= 4, (
"Type should be bigger to enable some functionalities"));
73 KY_DEBUG_ASSERTN(slotCount != 0, (
"Bad parameter"));
77 for (
KyUInt32 i = 0; i < slotCount; ++i)
78 SetNextFreeSlot(i, i + 1);
83 PoolChunk<T>::~PoolChunk()
85 if (IsEmpty() ==
false)
87 KY_LOG_WARNING((
"Bad usage of PoolChunk : you should call 'Delete' for each 'New'."));
96 KY_ASSERT(IsFull() ==
false);
98 const KyUInt32 index = GetFreeSlotAndMakeUsed();
106 KyUInt32 PoolChunk<T>::New(
const T& data)
108 KY_ASSERT(IsFull() ==
false);
110 const KyUInt32 index = GetFreeSlotAndMakeUsed();
118 void PoolChunk<T>::Delete(
KyUInt32 index)
120 KY_ASSERT(index < m_nbTotalSlots);
128 bool PoolChunk<T>::SuperSlow_IsSlotFree(
KyUInt32 index)
const
130 KyUInt32 freeSlotIdx = m_firstFreeSlot;
132 for (
KyUInt32 i = 0; i != m_nbFreeSlots; ++i)
134 if (freeSlotIdx == index)
136 freeSlotIdx = GetNextFreeSlot(freeSlotIdx);
143 KyUInt32 PoolChunk<T>::GetFreeSlotAndMakeUsed()
145 const KyUInt32 res = m_firstFreeSlot;
146 m_firstFreeSlot = GetNextFreeSlot(m_firstFreeSlot);
153 void PoolChunk<T>::MakeSlotFree(
KyUInt32 index)
155 SetNextFreeSlot(index, m_firstFreeSlot);
156 m_firstFreeSlot = index;
163 #endif // Navigation_PoolChunk_H
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