9 #ifndef GwNavGen_GrowingPool_H
10 #define GwNavGen_GrowingPool_H
38 explicit GrowingPool(MemoryHeap* heap,
KyUInt32 byteCountInChunk = 0);
40 ~GrowingPool() { Release(); }
49 KyUInt32 GetElementCount()
const {
return m_elementCount; }
50 KyUInt32 GetChunkCount()
const {
return m_chunkCount; }
51 KyUInt32 ByteCountAllocated()
const {
return m_byteCountAllocated; }
61 KyArrayDH_POD<T*> m_chunks;
73 inline GrowingPool<T>::GrowingPool(MemoryHeap* heap,
KyUInt32 byteCountInChunk) : m_chunks(heap)
75 if (byteCountInChunk <= 0)
76 byteCountInChunk = 512 * 1024;
79 for (
KyUInt32 i = 0 ; i < m_chunks.GetCount(); ++i)
84 m_maxElementCountInChunk = CalculateElementCountFromByteCount(byteCountInChunk);
85 m_maxWordCountInChunk = CalculateWordCount(m_maxElementCountInChunk);
86 m_elementCountInLastChunk = m_maxElementCountInChunk;
87 m_byteCountAllocated = 0;
93 inline T* GrowingPool<T>::GetNew()
95 if (m_elementCountInLastChunk == m_maxElementCountInChunk)
97 KyUInt32 oldSize = m_chunks.GetCount();
98 if (m_chunkCount >= oldSize)
100 m_chunks.Resize(oldSize * 2);
101 for (
KyUInt32 i = oldSize; i < m_chunks.GetCount(); ++i)
105 if (m_chunks[m_chunkCount] ==
KY_NULL)
107 m_chunks[m_chunkCount] = (T*)KY_HEAP_MALLOC(m_heap,
KyUInt32, m_maxWordCountInChunk, MemStat_NavDataGen);
108 m_byteCountAllocated += m_maxWordCountInChunk *
sizeof(
KyUInt32);
112 m_elementCountInLastChunk = 0;
117 T* ptr = &m_chunks[m_chunkCount - 1][m_elementCountInLastChunk];
120 ++m_elementCountInLastChunk;
126 inline
void GrowingPool<T>::Clear()
132 for (
KyUInt32 chunkIdx = 0; chunkIdx < GetChunkCount(); ++chunkIdx)
134 GetChunk(chunkIdx, chunk, countInChunk);
135 for (
KyUInt32 idx = 0; idx < countInChunk; ++idx)
141 m_elementCountInLastChunk = m_maxElementCountInChunk;
146 inline void GrowingPool<T>::Release()
150 for (
KyUInt32 i = 0; i < m_chunks.GetCount(); ++i)
151 KY_FREE(m_chunks[i]);
153 m_chunks.ClearAndRelease();
154 m_byteCountAllocated = 0;
159 KY_INLINE
void GrowingPool<T>::GetChunk(
KyUInt32 idx, T*& chunk,
KyUInt32& countInChunk)
161 chunk = m_chunks[idx];
162 countInChunk = (idx != m_chunkCount - 1) ? m_maxElementCountInChunk : m_elementCountInLastChunk;
167 KY_INLINE
KyUInt32 GrowingPool<T>::CalculateElementCountFromByteCount(
KyInt32 byteCount)
169 return (byteCount - 1) / (
KyInt32)
sizeof (T) + 1;
173 KY_INLINE
KyUInt32 GrowingPool<T>::CalculateWordCount(
KyInt32 elementCount)
175 return (elementCount * (
KyInt32)
sizeof (T) - 1) /
sizeof(
KyUInt32) + 1;
int KyInt32
Type used internally to represent a 32-bit integer.
Definition: types.h:35
#define KY_NULL
Null value.
Definition: types.h:247
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