-
Notifications
You must be signed in to change notification settings - Fork 713
/
mempool.h
285 lines (206 loc) · 8.55 KB
/
mempool.h
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include "seal/util/common.h"
#include "seal/util/defines.h"
#include "seal/util/globals.h"
#include "seal/util/locks.h"
#include <algorithm>
#include <atomic>
#include <cstdint>
#include <cstring>
#include <limits>
#include <memory>
#include <new>
#include <stdexcept>
#include <type_traits>
#include <vector>
namespace seal
{
namespace util
{
template <typename T = void, typename = std::enable_if_t<std::is_standard_layout<T>::value>>
class ConstPointer;
template <>
class ConstPointer<seal_byte>;
template <typename T = void, typename = std::enable_if_t<std::is_standard_layout<T>::value>>
class Pointer;
class MemoryPoolItem
{
public:
MemoryPoolItem(seal_byte *data) noexcept : data_(data)
{}
SEAL_NODISCARD inline seal_byte *data() noexcept
{
return data_;
}
SEAL_NODISCARD inline const seal_byte *data() const noexcept
{
return data_;
}
SEAL_NODISCARD inline MemoryPoolItem *&next() noexcept
{
return next_;
}
SEAL_NODISCARD inline const MemoryPoolItem *next() const noexcept
{
return next_;
}
private:
MemoryPoolItem(const MemoryPoolItem ©) = delete;
MemoryPoolItem &operator=(const MemoryPoolItem &assign) = delete;
seal_byte *data_ = nullptr;
MemoryPoolItem *next_ = nullptr;
};
class MemoryPoolHead
{
public:
struct allocation
{
allocation() : size(0), data_ptr(nullptr), free(0), head_ptr(nullptr)
{}
// Size of the allocation (number of items it can hold)
std::size_t size;
// Pointer to start of the allocation
seal_byte *data_ptr;
// How much free space is left (number of items that still fit)
std::size_t free;
// Pointer to current head of allocation
seal_byte *head_ptr;
};
// The overriding functions are noexcept(false)
virtual ~MemoryPoolHead() = default;
// Byte size of the allocations (items) owned by this pool
virtual std::size_t item_byte_count() const noexcept = 0;
// Total number of items allocated
virtual std::size_t item_count() const noexcept = 0;
virtual MemoryPoolItem *get() = 0;
// Return item back to this pool
virtual void add(MemoryPoolItem *new_first) noexcept = 0;
};
class MemoryPoolHeadMT : public MemoryPoolHead
{
public:
// Creates a new MemoryPoolHeadMT with allocation for one single item.
MemoryPoolHeadMT(std::size_t item_byte_count, bool clear_on_destruction = false);
~MemoryPoolHeadMT() noexcept override;
// Byte size of the allocations (items) owned by this pool
SEAL_NODISCARD inline std::size_t item_byte_count() const noexcept override
{
return item_byte_count_;
}
// Returns the total number of items allocated
SEAL_NODISCARD inline std::size_t item_count() const noexcept override
{
return item_count_;
}
MemoryPoolItem *get() override;
inline void add(MemoryPoolItem *new_first) noexcept override
{
bool expected = false;
while (!locked_.compare_exchange_strong(expected, true, std::memory_order_acquire))
{
expected = false;
}
MemoryPoolItem *old_first = first_item_;
new_first->next() = old_first;
first_item_ = new_first;
locked_.store(false, std::memory_order_release);
}
private:
MemoryPoolHeadMT(const MemoryPoolHeadMT ©) = delete;
MemoryPoolHeadMT &operator=(const MemoryPoolHeadMT &assign) = delete;
const bool clear_on_destruction_;
mutable std::atomic<bool> locked_;
const std::size_t item_byte_count_;
volatile std::size_t item_count_;
std::vector<allocation> allocs_;
MemoryPoolItem *volatile first_item_;
};
class MemoryPoolHeadST : public MemoryPoolHead
{
public:
// Creates a new MemoryPoolHeadST with allocation for one single item.
MemoryPoolHeadST(std::size_t item_byte_count, bool clear_on_destruction = false);
~MemoryPoolHeadST() noexcept override;
// Byte size of the allocations (items) owned by this pool
SEAL_NODISCARD inline std::size_t item_byte_count() const noexcept override
{
return item_byte_count_;
}
// Returns the total number of items allocated
SEAL_NODISCARD inline std::size_t item_count() const noexcept override
{
return item_count_;
}
SEAL_NODISCARD MemoryPoolItem *get() override;
inline void add(MemoryPoolItem *new_first) noexcept override
{
new_first->next() = first_item_;
first_item_ = new_first;
}
private:
MemoryPoolHeadST(const MemoryPoolHeadST ©) = delete;
MemoryPoolHeadST &operator=(const MemoryPoolHeadST &assign) = delete;
const bool clear_on_destruction_;
std::size_t item_byte_count_;
std::size_t item_count_;
std::vector<allocation> allocs_;
MemoryPoolItem *first_item_;
};
class MemoryPool
{
public:
static constexpr double alloc_size_multiplier = 1.05;
// Largest size of single allocation that can be requested from memory pool
static const std::size_t max_single_alloc_byte_count;
// Number of different size allocations allowed by a single memory pool
static constexpr std::size_t max_pool_head_count = (std::numeric_limits<std::size_t>::max)();
// Largest allowed size of batch allocation
static const std::size_t max_batch_alloc_byte_count;
static constexpr std::size_t first_alloc_count = 1;
virtual ~MemoryPool() = default;
virtual Pointer<seal_byte> get_for_byte_count(std::size_t byte_count) = 0;
virtual std::size_t pool_count() const = 0;
virtual std::size_t alloc_byte_count() const = 0;
};
class MemoryPoolMT : public MemoryPool
{
public:
MemoryPoolMT(bool clear_on_destruction = false) : clear_on_destruction_(clear_on_destruction)
{}
~MemoryPoolMT() noexcept override;
SEAL_NODISCARD Pointer<seal_byte> get_for_byte_count(std::size_t byte_count) override;
SEAL_NODISCARD inline std::size_t pool_count() const override
{
ReaderLock lock(pools_locker_.acquire_read());
return pools_.size();
}
SEAL_NODISCARD std::size_t alloc_byte_count() const override;
protected:
MemoryPoolMT(const MemoryPoolMT ©) = delete;
MemoryPoolMT &operator=(const MemoryPoolMT &assign) = delete;
const bool clear_on_destruction_;
mutable ReaderWriterLocker pools_locker_;
std::vector<MemoryPoolHead *> pools_;
};
class MemoryPoolST : public MemoryPool
{
public:
MemoryPoolST(bool clear_on_destruction = false) : clear_on_destruction_(clear_on_destruction)
{}
~MemoryPoolST() noexcept override;
SEAL_NODISCARD Pointer<seal_byte> get_for_byte_count(std::size_t byte_count) override;
SEAL_NODISCARD inline std::size_t pool_count() const override
{
return pools_.size();
}
std::size_t alloc_byte_count() const override;
protected:
MemoryPoolST(const MemoryPoolST ©) = delete;
MemoryPoolST &operator=(const MemoryPoolST &assign) = delete;
const bool clear_on_destruction_;
std::vector<MemoryPoolHead *> pools_;
};
} // namespace util
} // namespace seal