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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#pragma once
#include "../ds/ds.h"
#include "../mem/mem.h"
#include "buddy.h"
#include "empty_range.h"
#include "range_helpers.h"
namespace snmalloc
{
/**
* Class for using the pagemap entries for the buddy allocator.
*/
template<SNMALLOC_CONCEPT(IsWritablePagemap) Pagemap>
class BuddyChunkRep
{
public:
/*
* The values we store in our rbtree are the addresses of (combined spans
* of) chunks of the address space; as such, bits in (MIN_CHUNK_SIZE - 1)
* are unused and so the RED_BIT is packed therein. However, in practice,
* these are not "just any" uintptr_t-s, but specifically the uintptr_t-s
* inside the Pagemap's BackendAllocator::Entry structures.
*
* The BackendAllocator::Entry provides us with helpers that guarantee that
* we use only the bits that we are allowed to.
* @{
*/
using Handle = MetaEntryBase::BackendStateWordRef;
using Contents = uintptr_t;
///@}
/**
* The bit that we will use to mark an entry as red.
* This has constraints in two directions, it must not be one of the
* reserved bits from the perspective of the meta entry and it must not be
* a bit that is a valid part of the address of a chunk.
* @{
*/
static constexpr address_t RED_BIT = 1 << 8;
static_assert(RED_BIT < MIN_CHUNK_SIZE);
static_assert(MetaEntryBase::is_backend_allowed_value(
MetaEntryBase::Word::One, RED_BIT));
static_assert(MetaEntryBase::is_backend_allowed_value(
MetaEntryBase::Word::Two, RED_BIT));
///@}
/// The value of a null node, as returned by `get`
static constexpr Contents null = 0;
/// The value of a null node, as stored in a `uintptr_t`.
static constexpr Contents root = 0;
/**
* Set the value. Preserve the red/black colour.
*/
static void set(Handle ptr, Contents r)
{
ptr = r | (static_cast<address_t>(ptr.get()) & RED_BIT);
}
/**
* Returns the value, stripping out the red/black colour.
*/
static Contents get(const Handle ptr)
{
return ptr.get() & ~RED_BIT;
}
/**
* Returns a pointer to the tree node for the specified address.
*/
static Handle ref(bool direction, Contents k)
{
// Special case for accessing the null entry. We want to make sure
// that this is never modified by the back end, so we make it point to
// a constant entry and use the MMU to trap even in release modes.
static const Contents null_entry = 0;
if (SNMALLOC_UNLIKELY(address_cast(k) == 0))
{
return {const_cast<Contents*>(&null_entry)};
}
auto& entry = Pagemap::template get_metaentry_mut<false>(address_cast(k));
if (direction)
return entry.get_backend_word(Pagemap::Entry::Word::One);
return entry.get_backend_word(Pagemap::Entry::Word::Two);
}
static bool is_red(Contents k)
{
return (ref(true, k).get() & RED_BIT) == RED_BIT;
}
static void set_red(Contents k, bool new_is_red)
{
if (new_is_red != is_red(k))
{
auto v = ref(true, k);
v = v.get() ^ RED_BIT;
}
SNMALLOC_ASSERT(is_red(k) == new_is_red);
}
static Contents offset(Contents k, size_t size)
{
return k + size;
}
static Contents buddy(Contents k, size_t size)
{
return k ^ size;
}
static Contents align_down(Contents k, size_t size)
{
return k & ~(size - 1);
}
static bool compare(Contents k1, Contents k2)
{
return k1 > k2;
}
static bool equal(Contents k1, Contents k2)
{
return k1 == k2;
}
static uintptr_t printable(Contents k)
{
return k;
}
/**
* Convert the pointer wrapper into something that the snmalloc debug
* printing code can print.
*/
static address_t printable(Handle k)
{
return k.printable_address();
}
/**
* Returns the name for use in debugging traces. Not used in normal builds
* (release or debug), only when tracing is enabled.
*/
static const char* name()
{
return "BuddyChunkRep";
}
static bool can_consolidate(Contents k, size_t size)
{
// Need to know both entries exist in the pagemap.
// This must only be called if that has already been
// ascertained.
// The buddy could be in a part of the pagemap that has
// not been registered and thus could segfault on access.
auto larger = bits::max(k, buddy(k, size));
auto& entry =
Pagemap::template get_metaentry_mut<false>(address_cast(larger));
return !entry.is_boundary();
}
};
/**
* Used to represent a consolidating range of memory. Uses a buddy allocator
* to consolidate adjacent blocks.
*
* ParentRange - Represents the range to get memory from to fill this range.
*
* REFILL_SIZE_BITS - Maximum size of a refill, may ask for less during warm
* up phase.
*
* MAX_SIZE_BITS - Maximum size that this range will store.
*
* Pagemap - How to access the pagemap, which is used to store the red black
* tree nodes for the buddy allocators.
*
* MIN_REFILL_SIZE_BITS - The minimum size that the ParentRange can be asked
* for
*/
template<
size_t REFILL_SIZE_BITS,
size_t MAX_SIZE_BITS,
SNMALLOC_CONCEPT(IsWritablePagemap) Pagemap,
size_t MIN_REFILL_SIZE_BITS = 0>
class LargeBuddyRange
{
static_assert(
REFILL_SIZE_BITS <= MAX_SIZE_BITS, "REFILL_SIZE_BITS > MAX_SIZE_BITS");
static_assert(
MIN_REFILL_SIZE_BITS <= REFILL_SIZE_BITS,
"MIN_REFILL_SIZE_BITS > REFILL_SIZE_BITS");
/**
* Maximum size of a refill
*/
static constexpr size_t REFILL_SIZE = bits::one_at_bit(REFILL_SIZE_BITS);
/**
* Minimum size of a refill
*/
static constexpr size_t MIN_REFILL_SIZE =
bits::one_at_bit(MIN_REFILL_SIZE_BITS);
public:
template<typename ParentRange = EmptyRange<>>
class Type : public ContainsParent<ParentRange>
{
using ContainsParent<ParentRange>::parent;
/**
* The size of memory requested so far.
*
* This is used to determine the refill size.
*/
size_t requested_total = 0;
/**
* Buddy allocator used to represent this range of memory.
*/
Buddy<BuddyChunkRep<Pagemap>, MIN_CHUNK_BITS, MAX_SIZE_BITS> buddy_large;
/**
* The parent might not support deallocation if this buddy allocator
* covers the whole range. Uses template insanity to make this work.
*/
template<bool exists = MAX_SIZE_BITS != (bits::BITS - 1)>
stl::enable_if_t<exists>
parent_dealloc_range(capptr::Arena<void> base, size_t size)
{
static_assert(
MAX_SIZE_BITS != (bits::BITS - 1), "Don't set SFINAE parameter");
parent.dealloc_range(base, size);
}
void dealloc_overflow(capptr::Arena<void> overflow)
{
if constexpr (MAX_SIZE_BITS != (bits::BITS - 1))
{
if (overflow != nullptr)
{
parent.dealloc_range(overflow, bits::one_at_bit(MAX_SIZE_BITS));
}
}
else
{
if (overflow != nullptr)
abort();
}
}
/**
* Add a range of memory to the address space.
* Divides blocks into power of two sizes with natural alignment
*/
void add_range(capptr::Arena<void> base, size_t length)
{
range_to_pow_2_blocks<MIN_CHUNK_BITS>(
base, length, [this](capptr::Arena<void> base, size_t align, bool) {
auto overflow =
capptr::Arena<void>::unsafe_from(reinterpret_cast<void*>(
buddy_large.add_block(base.unsafe_uintptr(), align)));
dealloc_overflow(overflow);
});
}
capptr::Arena<void> refill(size_t size)
{
if (ParentRange::Aligned)
{
// Use amount currently requested to determine refill size.
// This will gradually increase the usage of the parent range.
// So small examples can grow local caches slowly, and larger
// examples will grow them by the refill size.
//
// The heuristic is designed to allocate the following sequence for
// 16KiB requests 16KiB, 16KiB, 32Kib, 64KiB, ..., REFILL_SIZE/2,
// REFILL_SIZE, REFILL_SIZE, ... Hence if this if they are coming from
// a contiguous aligned range, then they could be consolidated. This
// depends on the ParentRange behaviour.
size_t refill_size = bits::min(REFILL_SIZE, requested_total);
refill_size = bits::max(refill_size, MIN_REFILL_SIZE);
refill_size = bits::max(refill_size, size);
refill_size = bits::next_pow2(refill_size);
auto refill_range = parent.alloc_range(refill_size);
if (refill_range != nullptr)
{
requested_total += refill_size;
add_range(pointer_offset(refill_range, size), refill_size - size);
}
return refill_range;
}
// Note the unaligned parent path does not use
// requested_total in the heuristic for the initial size
// this is because the request needs to introduce alignment.
// Currently the unaligned variant is not used as a local cache.
// So the gradual growing of refill_size is not needed.
// Need to overallocate to get the alignment right.
bool overflow = false;
size_t needed_size = bits::umul(size, 2, overflow);
if (overflow)
{
return nullptr;
}
auto refill_size = bits::max(needed_size, REFILL_SIZE);
while (needed_size <= refill_size)
{
auto refill = parent.alloc_range(refill_size);
if (refill != nullptr)
{
requested_total += refill_size;
add_range(refill, refill_size);
SNMALLOC_ASSERT(refill_size < bits::one_at_bit(MAX_SIZE_BITS));
static_assert(
(REFILL_SIZE < bits::one_at_bit(MAX_SIZE_BITS)) ||
ParentRange::Aligned,
"Required to prevent overflow.");
return alloc_range(size);
}
refill_size >>= 1;
}
return nullptr;
}
public:
static constexpr bool Aligned = true;
static constexpr bool ConcurrencySafe = false;
/* The large buddy allocator always deals in Arena-bounded pointers. */
using ChunkBounds = capptr::bounds::Arena;
static_assert(
stl::is_same_v<typename ParentRange::ChunkBounds, ChunkBounds>);
constexpr Type() = default;
capptr::Arena<void> alloc_range(size_t size)
{
SNMALLOC_ASSERT(size >= MIN_CHUNK_SIZE);
SNMALLOC_ASSERT(bits::is_pow2(size));
if (size >= bits::mask_bits(MAX_SIZE_BITS))
{
if (ParentRange::Aligned)
return parent.alloc_range(size);
return nullptr;
}
auto result = capptr::Arena<void>::unsafe_from(
reinterpret_cast<void*>(buddy_large.remove_block(size)));
if (result != nullptr)
return result;
return refill(size);
}
void dealloc_range(capptr::Arena<void> base, size_t size)
{
SNMALLOC_ASSERT(size >= MIN_CHUNK_SIZE);
SNMALLOC_ASSERT(bits::is_pow2(size));
if constexpr (MAX_SIZE_BITS != (bits::BITS - 1))
{
if (size >= bits::mask_bits(MAX_SIZE_BITS))
{
parent_dealloc_range(base, size);
return;
}
}
auto overflow =
capptr::Arena<void>::unsafe_from(reinterpret_cast<void*>(
buddy_large.add_block(base.unsafe_uintptr(), size)));
dealloc_overflow(overflow);
}
};
};
} // namespace snmalloc