2014-06-16 10:12:50 -04:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-28 10:01:59 -04:00
|
|
|
template <class T>
|
2014-06-16 10:12:50 -04:00
|
|
|
class cAllocationPool
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
class cStarvationCallbacks
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~cStarvationCallbacks() {}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Is called when the reserve buffer starts to be used */
|
2014-06-16 10:12:50 -04:00
|
|
|
virtual void OnStartUsingReserve() = 0;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Is called once the reserve buffer has returned to normal size */
|
2014-06-16 10:12:50 -04:00
|
|
|
virtual void OnEndUsingReserve() = 0;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-06-16 10:12:50 -04:00
|
|
|
/** Is called when the allocation pool is unable to allocate memory. Will be repeatedly
|
2015-07-31 10:49:10 -04:00
|
|
|
called if it does not free sufficient memory */
|
2014-06-16 10:12:50 -04:00
|
|
|
virtual void OnOutOfReserve() = 0;
|
|
|
|
};
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-06-16 10:12:50 -04:00
|
|
|
virtual ~cAllocationPool() {}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Allocates a pointer to T */
|
2014-06-16 10:12:50 -04:00
|
|
|
virtual T * Allocate() = 0;
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Frees the pointer passed in a_ptr, invalidating it */
|
2014-06-16 10:12:50 -04:00
|
|
|
virtual void Free(T * a_ptr) = 0;
|
|
|
|
};
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-16 10:12:50 -04:00
|
|
|
/** Allocates memory storing unused elements in a linked list. Keeps at least NumElementsInReserve
|
2015-07-31 10:49:10 -04:00
|
|
|
elements in the list unless malloc fails so that the program has a reserve to handle OOM. */
|
2014-08-28 10:01:59 -04:00
|
|
|
template <class T, size_t NumElementsInReserve>
|
2015-07-31 10:49:10 -04:00
|
|
|
class cListAllocationPool:
|
|
|
|
public cAllocationPool<T>
|
2014-06-16 10:12:50 -04:00
|
|
|
{
|
2015-07-31 10:49:10 -04:00
|
|
|
public:
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
cListAllocationPool(std::unique_ptr<typename cAllocationPool<T>::cStarvationCallbacks> a_Callbacks):
|
|
|
|
m_Callbacks(std::move(a_Callbacks))
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < NumElementsInReserve; i++)
|
2014-06-16 10:12:50 -04:00
|
|
|
{
|
2015-07-31 10:49:10 -04:00
|
|
|
void * space = malloc(sizeof(T));
|
|
|
|
if (space == nullptr)
|
2014-06-16 10:12:50 -04:00
|
|
|
{
|
2015-07-31 10:49:10 -04:00
|
|
|
m_Callbacks->OnStartUsingReserve();
|
|
|
|
break;
|
2014-06-16 10:12:50 -04:00
|
|
|
}
|
2015-07-31 10:49:10 -04:00
|
|
|
m_FreeList.push_front(space);
|
2014-06-16 10:12:50 -04:00
|
|
|
}
|
2015-07-31 10:49:10 -04:00
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
|
2017-05-20 02:16:28 -04:00
|
|
|
virtual ~cListAllocationPool() override
|
2015-07-31 10:49:10 -04:00
|
|
|
{
|
|
|
|
while (!m_FreeList.empty())
|
2014-06-16 10:12:50 -04:00
|
|
|
{
|
2015-07-31 10:49:10 -04:00
|
|
|
free (m_FreeList.front());
|
|
|
|
m_FreeList.pop_front();
|
2014-07-17 10:33:09 -04:00
|
|
|
}
|
2015-07-31 10:49:10 -04:00
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
|
|
|
|
virtual T * Allocate() override
|
|
|
|
{
|
|
|
|
if (m_FreeList.size() <= NumElementsInReserve)
|
2014-06-16 10:12:50 -04:00
|
|
|
{
|
2015-07-31 10:49:10 -04:00
|
|
|
void * space = malloc(sizeof(T));
|
|
|
|
if (space != nullptr)
|
2014-06-16 10:12:50 -04:00
|
|
|
{
|
2017-06-19 05:09:16 -04:00
|
|
|
#if defined(_MSC_VER) && defined(_DEBUG)
|
|
|
|
// The debugging-new that is set up using macros in Globals.h doesn't support the placement-new syntax used here.
|
|
|
|
// Temporarily disable the macro
|
|
|
|
#pragma push_macro("new")
|
|
|
|
#undef new
|
|
|
|
#endif
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
return new(space) T;
|
2017-06-19 05:09:16 -04:00
|
|
|
|
|
|
|
#if defined(_MSC_VER) && defined(_DEBUG)
|
|
|
|
// Re-enable the debugging-new macro
|
|
|
|
#pragma pop_macro("new")
|
|
|
|
#endif
|
2014-06-16 10:12:50 -04:00
|
|
|
}
|
2015-07-31 10:49:10 -04:00
|
|
|
else if (m_FreeList.size() == NumElementsInReserve)
|
2014-06-16 10:12:50 -04:00
|
|
|
{
|
2015-07-31 10:49:10 -04:00
|
|
|
m_Callbacks->OnStartUsingReserve();
|
2014-06-16 10:12:50 -04:00
|
|
|
}
|
2015-07-31 10:49:10 -04:00
|
|
|
else if (m_FreeList.empty())
|
2014-06-16 10:12:50 -04:00
|
|
|
{
|
2015-07-31 10:49:10 -04:00
|
|
|
m_Callbacks->OnOutOfReserve();
|
|
|
|
// Try again until the memory is avalable
|
|
|
|
return Allocate();
|
2014-06-16 10:12:50 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-31 10:49:10 -04:00
|
|
|
// placement new, used to initalize the object
|
2017-06-19 05:09:16 -04:00
|
|
|
|
|
|
|
#if defined(_MSC_VER) && defined(_DEBUG)
|
|
|
|
// The debugging-new that is set up using macros in Globals.h doesn't support the placement-new syntax used here.
|
|
|
|
// Temporarily disable the macro
|
|
|
|
#pragma push_macro("new")
|
|
|
|
#undef new
|
|
|
|
#endif
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
T * ret = new (m_FreeList.front()) T;
|
2017-06-19 05:09:16 -04:00
|
|
|
|
|
|
|
#if defined(_MSC_VER) && defined(_DEBUG)
|
|
|
|
// Re-enable the debugging-new macro
|
|
|
|
#pragma pop_macro("new")
|
|
|
|
#endif
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
m_FreeList.pop_front();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void Free(T * a_ptr) override
|
|
|
|
{
|
|
|
|
if (a_ptr == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// placement destruct.
|
|
|
|
a_ptr->~T();
|
|
|
|
m_FreeList.push_front(a_ptr);
|
|
|
|
if (m_FreeList.size() == NumElementsInReserve)
|
|
|
|
{
|
|
|
|
m_Callbacks->OnEndUsingReserve();
|
|
|
|
}
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
private:
|
|
|
|
std::list<void *> m_FreeList;
|
|
|
|
std::unique_ptr<typename cAllocationPool<T>::cStarvationCallbacks> m_Callbacks;
|
2014-06-16 10:12:50 -04:00
|
|
|
};
|
2014-07-17 10:33:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|