summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/pmempool.h
blob: 60ea24c664184f7ebfc734527d161620c22d0f47 (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
// license:GPL-2.0+
// copyright-holders:Couriersud

#ifndef PMEMPOOL_H_
#define PMEMPOOL_H_

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

#include "palloc.h"
#include "pconfig.h"
#include "pstream.h"  // perrlogger
//#include "pstring.h"
#include "ptypes.h"
//#include "putil.h"

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

namespace plib {

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

	template <typename BASEARENA, std::size_t MINALIGN = PALIGN_MIN_SIZE>
	class mempool_arena : public arena_base<mempool_arena<BASEARENA, MINALIGN>, false, false>
	{
	public:

		using size_type = typename BASEARENA::size_type;
		using base_type = arena_base<mempool_arena<BASEARENA, MINALIGN>, false, false>;
		template <class T>
		using base_allocator_type = typename BASEARENA::template allocator_type<T>;

		mempool_arena(size_t min_align = MINALIGN, size_t min_alloc = (1<<21))
		: m_min_alloc(min_alloc)
		, m_min_align(min_align)
		, m_block_align(1024)
		, m_blocks(base_allocator_type<block *>(m_arena))
		{
		}

		PCOPYASSIGNMOVE(mempool_arena, delete)

		~mempool_arena()
		{
			for (auto & b : m_blocks)
			{
				if (b->m_num_alloc != 0)
				{
					plib::perrlogger("Found {} info blocks\n", m_info.size());
					plib::perrlogger("Found block with {} dangling allocations\n", b->m_num_alloc);
				}
				detail::free(m_arena, b);
			}
			if (!m_info.empty())
				plib::perrlogger("Still found {} info blocks after mempool deleted\n", m_info.size());
		}

		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);
			m_info.insert({ ret, info(b, b->m_cur)});
			rs -= (capacity - size);
			b->m_cur += rs;
			this->inc_alloc_stat(size);

			return ret;
		}

		void deallocate(void *ptr, size_t size) noexcept
		{
			auto it = m_info.find(ptr);
			if (it == m_info.end())
				plib::terminate("mempool::free - pointer not found");
			block *b = it->second.m_block;
			if (b->m_num_alloc == 0)
				plib::terminate("mempool::free - double free was called");
			else
			{
				mempool_arena &mp = b->m_mempool;
				b->m_num_alloc--;
				mp.dec_alloc_stat(size);
				if (b->m_num_alloc == 0)
				{
					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");

					mp.m_blocks.erase(itb);
					detail::free(mp.base_arena(), b);
				}
				m_info.erase(it);
			}
		}

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

		BASEARENA &base_arena() noexcept { return m_arena; }
	private:
		struct block
		{
			block(mempool_arena &mp, size_type 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;
				m_bytes_allocated = (min_bytes + mp.m_block_align); // - 1); // & ~(mp.m_min_align - 1);
				// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
				//m_data_allocated = new std::uint8_t[alloc_bytes];
				m_data_allocated = static_cast<std::uint8_t *>(mp.base_arena().allocate(mp.m_block_align, m_bytes_allocated));
				void *r = m_data_allocated;
				std::align(mp.m_block_align, min_bytes, r, m_bytes_allocated);
				m_data  = reinterpret_cast<std::uint8_t *>(r);
			}
			~block()
			{
				//::operator delete(m_data_allocated);
				//delete [] m_data_allocated;
				m_mempool.base_arena().deallocate(m_data_allocated, m_bytes_allocated);
			}

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

			size_type m_num_alloc;
			size_type m_free;
			size_type m_cur;
			size_type m_bytes_allocated;
			std::uint8_t *m_data;
			std::uint8_t *m_data_allocated;
			mempool_arena &m_mempool;
		};

		struct info
		{
			info(block *b, size_type p) : m_block(b), m_pos(p) { }
			~info() = default;
			PCOPYASSIGNMOVE(info, default)

			block * m_block;
			size_type m_pos;
		};

		block * new_block(size_type min_bytes)
		{
			auto *b = detail::alloc<block>(m_arena, *this, min_bytes);
			m_blocks.push_back(b);
			return b;
		}

		size_t m_min_alloc;
		size_t m_min_align;
		size_t m_block_align;
		BASEARENA m_arena;

		using base_allocator_typex = typename BASEARENA::template allocator_type<std::pair<void * const, info>>;
		std::unordered_map<void *, info, std::hash<void *>, std::equal_to<void *>,
			base_allocator_typex> m_info;
//      std::unordered_map<void *, info> m_info;
		std::vector<block *, typename BASEARENA::template allocator_type<block *>> m_blocks;

	};

} // namespace plib

#endif // PMEMPOOL_H_