gwnavruntime/kernel/SF_StackMemPool.h Source File
Go to the documentation of this file.
18 #ifndef INC_KY_Kernel_StackMemPool_H
19 #define INC_KY_Kernel_StackMemPool_H
30 struct MemPoolImmediateFree
32 MemPoolImmediateFree(MemoryHeap* pheap) : pHeap(pheap) {}
34 void* Alloc(UPInt nbytes, UPInt align)
36 return pHeap ? Memory::AllocInHeap(pHeap, nbytes, align) : Memory::Alloc(nbytes, align);
48 struct MemPoolPostponeFree
50 MemPoolPostponeFree(MemoryHeap* pheap) : pHeap(pheap) {}
51 ~MemPoolPostponeFree()
53 for (UPInt i = 0; i < AllocatedMem.GetSize(); ++i)
55 Memory::Free(AllocatedMem[i]);
59 void* Alloc(UPInt nbytes, UPInt align)
61 void* mem = pHeap ? Memory::AllocInHeap(pHeap, nbytes, align) : Memory::Alloc(nbytes, align);
65 AllocatedMem.PushBack(mem);
70 static void Free(
void* )
77 Array<void*> AllocatedMem;
84 template <UPInt N = 512, UPInt A = sizeof(
void*),
class AFP = MemPoolImmediateFree>
85 class StackMemPool : AFP
88 StackMemPool(MemoryHeap* pheap = NULL)
89 : AFP(pheap), BuffPtr(AlignMem(Buff)), BuffSize(buff_max_size - (BuffPtr - Buff))
94 void* Alloc(UPInt nbytes)
96 void* tmpBuffPtr = NULL;
98 if (nbytes <= BuffSize)
101 tmpBuffPtr = BuffPtr;
103 BuffPtr = AlignMem(BuffPtr + nbytes);
104 const UPInt curSize = BuffPtr - Buff;
105 BuffSize = buff_max_size > curSize ? buff_max_size - curSize : 0;
109 tmpBuffPtr = AFP::Alloc(nbytes, align);
116 if (!(p >= Buff && p < Buff + buff_max_size))
126 char* AlignMem(
char* offset)
128 return reinterpret_cast<char*
>(A + ((
reinterpret_cast<UPInt
>(offset) - 1) & ~(A - 1)));
133 enum { buff_max_size = N, align = A };
135 char Buff[buff_max_size];
144 template <Kaim::UPInt N, Kaim::UPInt A>
146 void*
operator new (Kaim::UPInt nbytes, Kaim::StackMemPool<N, A>& pool)
148 return pool.Alloc(nbytes);
151 template <Kaim::UPInt N, Kaim::UPInt A>
153 void operator delete (
void* p, Kaim::StackMemPool<N, A>& pool)
159 #if defined(KY_DEFINE_NEW)
160 #define new KY_DEFINE_NEW
166 #endif // INC_GMEMSTACKPOOL_H
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17