summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/pmempool.h
blob: 59ca2d6f56c739812e830fff58375d0f0e36b3e6 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// license:GPL-2.0+
// copyright-holders:Couriersud

#ifndef PMEMPOOL_H_
#define PMEMPOOL_H_

///
/// \file pmempool.h
///

#include "palloc.h"
#include "pstream.h"
#include "pstring.h"
#include "ptypes.h"
#include "putil.h"

#include <algorithm>
#include <memory>
#include <unordered_map>
#include <utility>
#include <vector>

namespace plib {

	//============================================================
	//  Memory pool
	//============================================================

	class mempool
	{
	private:
		struct block
		{
			block(mempool &mp, std::size_t min_bytes)
			: m_num_alloc(0)
			, m_cur(0)
			, m_data(nullptr)
			, m_mempool(mp)
			{
				min_bytes = std::max(mp.m_min_alloc, min_bytes);
				m_free = min_bytes;
				std::size_t alloc_bytes = (min_bytes + mp.m_min_align); // - 1); // & ~(mp.m_min_align - 1);
				//m_data_allocated = ::operator new(alloc_bytes);
				m_data_allocated = new char[alloc_bytes];
				void *r = m_data_allocated;
				std::align(mp.m_min_align, min_bytes, r, alloc_bytes);
				m_data  = reinterpret_cast<char *>(r);
			}
			~block()
			{
				//::operator delete(m_data_allocated);
				delete [] m_data_allocated;
			}

			block(const block &) = delete;
			block(block &&) = delete;
			block &operator =(const block &) = delete;
			block &operator =(block &&) = delete;

			std::size_t m_num_alloc;
			std::size_t m_free;
			std::size_t m_cur;
			char *m_data;
			char *m_data_allocated;
			mempool &m_mempool;
		};

		struct info
		{
			info(block *b, std::size_t p) : m_block(b), m_pos(p) { }
			~info() = default;
			COPYASSIGNMOVE(info, default)

			block * m_block;
			std::size_t m_pos;
		};


		block * new_block(std::size_t min_bytes)
		{
			auto *b = plib::pnew<block>(*this, min_bytes);
			m_blocks.push_back(b);
			return b;
		}


		static std::unordered_map<void *, info> &sinfo()
		{
			static std::unordered_map<void *, info> spinfo;
			return spinfo;
		}

		size_t m_min_alloc;
		size_t m_min_align;

		std::vector<block *> m_blocks;

	public:
		static constexpr const bool is_stateless = false;
		template <class T, std::size_t ALIGN = alignof(T)>
		using allocator_type = arena_allocator<mempool, T, ALIGN>;

		mempool(size_t min_alloc = (1<<21), size_t min_align = 16)
		: m_min_alloc(min_alloc), m_min_align(min_align)
		{
		}

		COPYASSIGNMOVE(mempool, delete)

		~mempool()
		{

			for (auto & b : m_blocks)
			{
				if (b->m_num_alloc != 0)
				{
					plib::perrlogger("Found {} info blocks\n", sinfo().size());
					plib::perrlogger("Found block with {} dangling allocations\n", b->m_num_alloc);
				}
				plib::pdelete(b);
				//::operator delete(b->m_data);
			}
		}

		static inline mempool &instance()
		{
			static mempool s_mempool;
			return s_mempool;
		}

		void *allocate(size_t align, size_t size)
		{
			block *b = nullptr;

			if (align < m_min_align)
				align = m_min_align;

			size_t rs = size + align;
			for (auto &bs : m_blocks)
			{
				if (bs->m_free > rs)
				{
					b = bs;
					break;
				}
			}
			if (b == nullptr)
			{
				b = new_block(rs);
			}
			b->m_free -= rs;
			b->m_num_alloc++;
			void *ret = reinterpret_cast<void *>(b->m_data + b->m_cur);
			auto capacity(rs);
			ret = std::align(align, size, ret, capacity);
			sinfo().insert({ ret, info(b, b->m_cur)});
			rs -= (capacity - size);
			b->m_cur += rs;

			return ret;
		}

		static void deallocate(void *ptr)
		{

			auto it = sinfo().find(ptr);
			if (it == sinfo().end())
				plib::terminate("mempool::free - pointer not found\n");
			block *b = it->second.m_block;
			if (b->m_num_alloc == 0)
				plib::terminate("mempool::free - double free was called\n");
			else
			{
				b->m_num_alloc--;
				//printf("Freeing in block %p %lu\n", b, b->m_num_alloc);
				if (b->m_num_alloc == 0)
				{
					mempool &mp = b->m_mempool;
					auto itb = std::find(mp.m_blocks.begin(), mp.m_blocks.end(), b);
					if (itb == mp.m_blocks.end())
						plib::terminate("mempool::free - block not found\n");

					mp.m_blocks.erase(itb);
					plib::pdelete(b);
				}
				sinfo().erase(it);
			}
		}

		template <typename T>
		using owned_pool_ptr = plib::owned_ptr<T, arena_deleter<mempool, T>>;

		template <typename T>
		using unique_pool_ptr = std::unique_ptr<T, arena_deleter<mempool, T>>;

		template<typename T, typename... Args>
		owned_pool_ptr<T> make_owned(Args&&... args)
		{
			auto *mem = this->allocate(alignof(T), sizeof(T));
			try
			{
				auto *mema = new (mem) T(std::forward<Args>(args)...);
				return owned_pool_ptr<T>(mema, true, arena_deleter<mempool, T>(this));
			}
			catch (...)
			{
				this->deallocate(mem);
				throw;
			}
		}

		template<typename T, typename... Args>
		unique_pool_ptr<T> make_unique(Args&&... args)
		{
			auto *mem = this->allocate(alignof(T), sizeof(T));
			try
			{
				auto *mema = new (mem) T(std::forward<Args>(args)...);
				return unique_pool_ptr<T>(mema, arena_deleter<mempool, T>(this));
			}
			catch (...)
			{
				this->deallocate(mem);
				throw;
			}
		}

		bool operator ==(const mempool &rhs) const noexcept { return this == &rhs; }

	};

} // namespace plib

#endif // PMEMPOOL_H_