summaryrefslogtreecommitdiffstats
path: root/src/AllocationPool.h
blob: 98ab9b3890f6782a20acc80d7246362d017f04a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

#pragma once

#include <memory>

template<class T>
class cAllocationPool
{
public:
	class cStarvationCallbacks
	{
		public:
			virtual ~cStarvationCallbacks() {}
			
			/** Is called when the reserve buffer starts to be used **/
			virtual void OnStartUsingReserve() = 0;
			
			/** Is called once the reserve buffer has returned to normal size **/
			virtual void OnEndUsingReserve() = 0;
			
			/** Is called when the allocation pool is unable to allocate memory. Will be repeatedly
			called if it does not free sufficient memory **/
			virtual void OnOutOfReserve() = 0;
	};
	
	virtual ~cAllocationPool() {}
	
	/** Allocates a pointer to T **/
	virtual T * Allocate() = 0;
	
	/** Frees the pointer passed in a_ptr, invalidating it **/
	virtual void Free(T * a_ptr) = 0;
};

/** Allocates memory storing unused elements in a linked list. Keeps at least NumElementsInReserve
elements in the list unless malloc fails so that the program has a reserve to handle OOM.**/
template<class T, size_t NumElementsInReserve>
class cListAllocationPool : public cAllocationPool<T>
{
	public:
		
		cListAllocationPool(std::auto_ptr<typename cAllocationPool<T>::cStarvationCallbacks> a_Callbacks) :
			m_Callbacks(a_Callbacks)
		{
			for (size_t i = 0; i < NumElementsInReserve; i++)
			{
				void * space = malloc(sizeof(T));
				if (space == NULL)
				{
					m_Callbacks->OnStartUsingReserve();
					break;
				}
				m_FreeList.push_front(space);
			}
		}
		
		virtual ~cListAllocationPool()
		{
			while (!m_FreeList.empty())
			{
				free (m_FreeList.front());
				m_FreeList.pop_front();
			}
		}
		
		virtual T * Allocate() /*override*/
		{
			if (m_FreeList.size() <= NumElementsInReserve)
			{
				void * space = malloc(sizeof(T));
				if (space != NULL)
				{
					return new(space) T;
				}
				else if (m_FreeList.size() == NumElementsInReserve)
				{
					m_Callbacks->OnStartUsingReserve();
				}
				else if (m_FreeList.empty())
				{
					m_Callbacks->OnOutOfReserve();
					// Try again until the memory is avalable
					return Allocate();
				}
			}
			// placement new, used to initalize the object
			T * ret = new (m_FreeList.front()) T;
			m_FreeList.pop_front();
			return ret;
		}
		virtual void Free(T * a_ptr) /*override*/
		{
			if (a_ptr == NULL)
			{
				return;
			}
			// placement destruct.
			a_ptr->~T();
			m_FreeList.push_front(a_ptr);
			if (m_FreeList.size() == NumElementsInReserve)
			{
				m_Callbacks->OnEndUsingReserve();
			}
		}
		
	private:
		std::list<void *> m_FreeList;
		std::auto_ptr<typename cAllocationPool<T>::cStarvationCallbacks> m_Callbacks;
};