34 virtual void Free(T * a_ptr) = 0;
39 return ((
this == &a_Other) ||
DoIsEqual(a_Other) || a_Other.DoIsEqual(*
this));
69 m_MinElementsInReserve(a_MinElementsInReserve),
70 m_MaxElementsInReserve(a_MaxElementsInReserve),
71 m_Callbacks(
std::move(a_Callbacks))
73 for (
size_t i = 0; i < m_MinElementsInReserve; i++)
75 void * space = malloc(
sizeof(T));
78 m_Callbacks->OnStartUsingReserve();
81 m_FreeList.push_front(space);
88 while (!m_FreeList.empty())
90 free(m_FreeList.front());
91 m_FreeList.pop_front();
98 if (m_FreeList.size() <= m_MinElementsInReserve)
100 void * space = malloc(
sizeof(T));
101 if (space !=
nullptr)
103 #if defined(_MSC_VER) && defined(_DEBUG) 106 #pragma push_macro("new") 112 #if defined(_MSC_VER) && defined(_DEBUG) 114 #pragma pop_macro("new") 117 else if (m_FreeList.size() == m_MinElementsInReserve)
119 m_Callbacks->OnStartUsingReserve();
121 else if (m_FreeList.empty())
123 m_Callbacks->OnOutOfReserve();
130 #if defined(_MSC_VER) && defined(_DEBUG) 133 #pragma push_macro("new") 137 T * ret =
new (m_FreeList.front()) T;
139 #if defined(_MSC_VER) && defined(_DEBUG) 141 #pragma pop_macro("new") 144 m_FreeList.pop_front();
149 virtual void Free(T * a_ptr)
override 151 if (a_ptr ==
nullptr)
158 if (m_FreeList.size() >= m_MaxElementsInReserve)
164 m_FreeList.push_front(a_ptr);
165 if (m_FreeList.size() == m_MinElementsInReserve)
167 m_Callbacks->OnEndUsingReserve();
177 std::unique_ptr<typename cAllocationPool<T>::cStarvationCallbacks>
m_Callbacks;
Allocates memory storing unused elements in a linked list.
virtual void OnStartUsingReserve()=0
Is called when the reserve buffer starts to be used.
size_t m_MaxElementsInReserve
Maximum free list size before returning memory to the OS.
friend bool operator==(const cAllocationPool &a_Lhs, const cAllocationPool &a_Rhs)
virtual T * Allocate() override
Allocates a pointer to T.
size_t m_MinElementsInReserve
The minimum number of elements to keep in the free list before malloc fails.
virtual void Free(T *a_ptr)=0
Frees the pointer passed in a_ptr, invalidating it.
virtual ~cStarvationCallbacks()
cListAllocationPool(std::unique_ptr< typename cAllocationPool< T >::cStarvationCallbacks > a_Callbacks, size_t a_MinElementsInReserve, size_t a_MaxElementsInReserve)
virtual T * Allocate()=0
Allocates a pointer to T.
virtual void Free(T *a_ptr) override
Frees the pointer passed in a_ptr, invalidating it.
virtual bool DoIsEqual(const cAllocationPool &a_Other) const NOEXCEPT=0
std::unique_ptr< typename cAllocationPool< T >::cStarvationCallbacks > m_Callbacks
std::list< void * > m_FreeList
virtual void OnOutOfReserve()=0
Is called when the allocation pool is unable to allocate memory.
virtual ~cAllocationPool()
virtual bool DoIsEqual(const cAllocationPool< T > &a_Other) const NOEXCEPT override
bool IsEqual(const cAllocationPool &a_Other) const NOEXCEPT
Two pools compare equal if memory allocated by one can be freed by the other.
friend bool operator!=(const cAllocationPool &a_Lhs, const cAllocationPool &a_Rhs)
virtual ~cListAllocationPool() override
virtual void OnEndUsingReserve()=0
Is called once the reserve buffer has returned to normal size.