1
0
cuberite-2a/src/AllocationPool.h

153 lines
3.2 KiB
C
Raw Normal View History

#pragma once
#include <memory>
2015-07-31 10:49:10 -04:00
2014-08-28 10:01:59 -04:00
template <class T>
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 */
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 */
virtual void OnEndUsingReserve() = 0;
2016-02-05 16:45:45 -05: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 */
virtual void OnOutOfReserve() = 0;
};
2016-02-05 16:45:45 -05:00
virtual ~cAllocationPool() {}
2016-02-05 16:45:45 -05:00
2015-07-31 10:49:10 -04:00
/** Allocates a pointer to T */
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 */
virtual void Free(T * a_ptr) = 0;
};
2015-07-31 10:49:10 -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>
{
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++)
{
2015-07-31 10:49:10 -04:00
void * space = malloc(sizeof(T));
if (space == nullptr)
{
2015-07-31 10:49:10 -04:00
m_Callbacks->OnStartUsingReserve();
break;
}
2015-07-31 10:49:10 -04:00
m_FreeList.push_front(space);
}
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 ~cListAllocationPool() override
2015-07-31 10:49:10 -04:00
{
while (!m_FreeList.empty())
{
2015-07-31 10:49:10 -04:00
free (m_FreeList.front());
m_FreeList.pop_front();
}
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)
{
2015-07-31 10:49:10 -04:00
void * space = malloc(sizeof(T));
if (space != nullptr)
{
#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;
#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
else if (m_FreeList.size() == NumElementsInReserve)
{
2015-07-31 10:49:10 -04:00
m_Callbacks->OnStartUsingReserve();
}
2015-07-31 10:49:10 -04:00
else if (m_FreeList.empty())
{
2015-07-31 10:49:10 -04:00
m_Callbacks->OnOutOfReserve();
// Try again until the memory is avalable
return Allocate();
}
}
2015-07-31 10:49:10 -04:00
// placement new, used to initalize the object
#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;
#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;
};