-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_pow2_pool.h
254 lines (202 loc) · 6.41 KB
/
simple_pow2_pool.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
/*
* Copyright (C) 2018 XMM SWAP LTD
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
*/
#ifndef FILE_simple_pow2_pool_h_INCLUDED
#define FILE_simple_pow2_pool_h_INCLUDED
#include <assert.h>
#include <stddef.h>
#ifdef USE_STDCXX_LIBRARY
#include <new>
#include <utility>
#endif
namespace impl {
class simple_pow2_pool_base
{
protected:
struct FreeNode
{
struct FreeNode *m_next;
};
static size_t constexpr constexpr_pow2(int n)
{
return size_t(1) << n;
}
static bool constexpr constexpr_is_pow2(size_t n)
{
return !(n & (n - 1));
}
simple_pow2_pool_base(void *pool, size_t size) noexcept
: m_cur(pool)
, m_left(size)
{}
static void add_to_list(FreeNode *&head, void *ptr)
{
auto node = static_cast<FreeNode *>(ptr);
node->m_next = head;
head = node;
}
static void *remove_from_list(FreeNode *&head)
{
void *ret = head;
head = head->m_next;
return ret;
}
// Allocate a new object of a given size
// when there are no free slots available.
void *extend(size_t alloc_size, size_t align_size, FreeNode **);
// Special case when POW_MIN == POW_ALIGN.
void *extend(size_t size)
{
void *ret = m_cur;
auto cur = static_cast<char *>(m_cur);
if (m_left < size)
out_of_memory();
m_cur = cur + size;
m_left -= size;
return ret;
}
[[noreturn]] static void out_of_memory();
void *m_cur;
size_t m_left;
};
} // namespace impl
// Simple pool for small power 2 sized objects. Max object size is 2^9.
//
// For instance, assuming that cacheline is 2^6 bytes and we want to
// allocate objects of size 2^5 (half-cacheline), 2^6 (one cacheline)
// and 2^7 (two cachelines) and we want a cacheline alignment for
// objects that are multiple of a cacheline, the following object can
// be instantiated: simple_pow2_pool<5,7,6>.
//
template<int POW_MIN, int POW_MAX = POW_MIN, int POW_ALIGN = POW_MIN>
class simple_pow2_pool : impl::simple_pow2_pool_base
{
static_assert(POW_MIN > 0 && POW_MAX <= 9 &&
POW_MIN <= POW_MAX && POW_ALIGN >= POW_MIN && POW_ALIGN <= POW_MAX,
"template argument is out of range");
static_assert(constexpr_pow2(POW_MIN) >= sizeof(FreeNode),
"not enough space to hide a pointer inside a free slot");
static bool constexpr constexpr_good_size(size_t n)
{
return constexpr_is_pow2(n) &&
n >= constexpr_pow2(POW_MIN) &&
n <= constexpr_pow2(POW_MAX);
}
static size_t min_size(size_t a, size_t b)
{
return a < b ? a : b;
}
FreeNode *m_freelists[POW_MAX - POW_MIN + 1] {};
public:
simple_pow2_pool(simple_pow2_pool const &) = delete;
void operator=(simple_pow2_pool const &) = delete;
simple_pow2_pool(simple_pow2_pool &&) = default;
simple_pow2_pool &operator=(simple_pow2_pool &&) = default;
simple_pow2_pool(void *pool, size_t size) noexcept
: impl::simple_pow2_pool_base(pool, size)
{}
template<class T>
void *alloc()
{
static_assert(constexpr_good_size(sizeof(T)), "bad type");
for (auto i = POW_MIN; i <= POW_MAX; i++) {
size_t const alloc_size = constexpr_pow2(i);
size_t constexpr align_size = constexpr_pow2(POW_ALIGN);
if (alloc_size != sizeof(T))
continue;
auto *&freelist = m_freelists[i - POW_MIN];
if (freelist) {
return this->remove_from_list(freelist);
} else if (POW_MIN == POW_ALIGN) {
return this->extend(alloc_size);
} else {
return this->extend(alloc_size,
align_size, &freelist);
}
}
assert(false); // [[unreachable]]
return nullptr;
}
#ifdef USE_STDCXX_LIBRARY
template<class T, class ... Args>
T *emplace_alloc(Args && ... args)
{
return new(alloc<T>()) T(std::forward<Args>(args) ...);
}
#endif
template<class T>
void dealloc(void *ptr) noexcept
{
static_assert(constexpr_good_size(sizeof(T)), "bad type");
for (auto i = POW_MIN; i <= POW_MAX; i++) {
size_t const size = constexpr_pow2(i);
if (size != sizeof(T))
continue;
return this->add_to_list(m_freelists[i - POW_MIN], ptr);
}
assert(false); // [[unreachable]]
}
// XXX Pass rw and locality to prefetch functions.
void prefetch_next_alloc()
{
for (auto i = POW_MIN; i <= POW_MAX; i++)
__builtin_prefetch(m_freelists[i - POW_MIN]);
}
template<class T>
void prefetch_next_alloc()
{
static_assert(constexpr_good_size(sizeof(T)), "bad type");
for (auto i = POW_MIN; i <= POW_MAX; i++) {
size_t const alloc_size = constexpr_pow2(i);
if (alloc_size != sizeof(T))
continue;
__builtin_prefetch(m_freelists[i - POW_MIN]);
}
}
// Prefetch up to n cachelines.
void prefetch_next_alloc_deep(size_t n, size_t cacheline = 64)
{
for (auto i = POW_MIN; i <= POW_MAX; i++) {
size_t const alloc_size = constexpr_pow2(i);
size_t const stop = min_size(n * cacheline, alloc_size);
auto p = static_cast<void *>(m_freelists[i - POW_MIN]);
for (size_t i = 0; i < stop; i += cacheline)
__builtin_prefetch(static_cast<char *>(p) + i);
}
}
template<class T>
void prefetch_next_alloc_deep(size_t n, size_t cacheline = 64)
{
static_assert(constexpr_good_size(sizeof(T)), "bad type");
for (auto i = POW_MIN; i <= POW_MAX; i++) {
size_t const alloc_size = constexpr_pow2(i);
size_t const stop = min_size(n * cacheline, alloc_size);
auto p = static_cast<void *>(m_freelists[i - POW_MIN]);
if (alloc_size != sizeof(T))
continue;
for (size_t i = 0; i < stop; i += cacheline)
__builtin_prefetch(static_cast<char *>(p) + i);
}
}
};
#endif // FILE_simple_pow2_pool_h_INCLUDED