summaryrefslogtreecommitdiffstats
path: root/src/AllocationPool.h
blob: 1b27462abca931f027b332915fbdd8ffeb839c5c (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

#pragma once





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;

	/** Two pools compare equal if memory allocated by one can be freed by the other */
	bool IsEqual(const cAllocationPool & a_Other) const NOEXCEPT
	{
		return ((this == &a_Other) || DoIsEqual(a_Other) || a_Other.DoIsEqual(*this));
	}

	friend bool operator == (const cAllocationPool & a_Lhs, const cAllocationPool & a_Rhs)
	{
		return a_Lhs.IsEqual(a_Rhs);
	}

	friend bool operator != (const cAllocationPool & a_Lhs, const cAllocationPool & a_Rhs)
	{
		return !a_Lhs.IsEqual(a_Rhs);
	}

private:
	virtual bool DoIsEqual(const cAllocationPool & a_Other) const NOEXCEPT = 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>
class cListAllocationPool:
	public cAllocationPool<T>
{
public:

	cListAllocationPool(std::unique_ptr<typename cAllocationPool<T>::cStarvationCallbacks> a_Callbacks, size_t a_NumElementsInReserve):
		m_NumElementsInReserve(a_NumElementsInReserve),
		m_Callbacks(std::move(a_Callbacks))
	{
		for (size_t i = 0; i < m_NumElementsInReserve; i++)
		{
			void * space = malloc(sizeof(T));
			if (space == nullptr)
			{
				m_Callbacks->OnStartUsingReserve();
				break;
			}
			m_FreeList.push_front(space);
		}
	}


	virtual ~cListAllocationPool() override
	{
		while (!m_FreeList.empty())
		{
			free (m_FreeList.front());
			m_FreeList.pop_front();
		}
	}


	virtual T * Allocate() override
	{
		if (m_FreeList.size() <= m_NumElementsInReserve)
		{
			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

				return new(space) T;

				#if defined(_MSC_VER) && defined(_DEBUG)
					// Re-enable the debugging-new macro
					#pragma pop_macro("new")
				#endif
			}
			else if (m_FreeList.size() == m_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

		#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

		T * ret = new (m_FreeList.front()) T;

		#if defined(_MSC_VER) && defined(_DEBUG)
			// Re-enable the debugging-new macro
			#pragma pop_macro("new")
		#endif

		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() == m_NumElementsInReserve)
		{
			m_Callbacks->OnEndUsingReserve();
		}
	}

private:
	/** The minimum number of elements to keep in the free list before malloc fails */
	size_t m_NumElementsInReserve;
	std::list<void *> m_FreeList;
	std::unique_ptr<typename cAllocationPool<T>::cStarvationCallbacks> m_Callbacks;

	virtual bool DoIsEqual(const cAllocationPool<T> & a_Other) const NOEXCEPT override
	{
		return (dynamic_cast<const cListAllocationPool<T>*>(&a_Other) != nullptr);
	}
};