-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathalloc.cpp
1042 lines (859 loc) · 32.2 KB
/
alloc.cpp
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of Peredvizhnikov Engine
* Copyright (C) 2023 Eduard Permyakov
*
* Peredvizhnikov Engine is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Peredvizhnikov Engine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
module;
#include <stddef.h>
#ifdef __linux__
#include <sys/mman.h>
#endif
export module alloc;
import assert;
import platform;
import concurrency;
import logger;
import mman;
import unistd;
import pthread;
import resource;
import meta;
import <cmath>;
import <array>;
import <concepts>;
import <optional>;
namespace pe{
export{
inline constexpr unsigned kMinBlockSize = 8;
inline constexpr unsigned kMaxBlockSize = 16 * 1024;
inline constexpr unsigned kSuperblockSize = 4 * kMaxBlockSize;
inline constexpr unsigned kAddressUsedBits = 48;
}
template <unsigned x>
inline consteval unsigned log2()
{
if constexpr(x == 1)
return 1;
return 1 + log2<x/2>();
}
template <unsigned x>
inline consteval unsigned pow2()
{
if constexpr(x == 0)
return 1;
if constexpr(x == 1)
return 2;
else
return 2 * pow2<x-1>();
}
template <unsigned x>
inline consteval bool valid_block_size()
{
if constexpr(x < kMinBlockSize || x > kMaxBlockSize)
return false;
if constexpr(x % sizeof(uintptr_t))
return false;
return true;
}
template <unsigned x>
inline consteval unsigned num_size_classes()
{
if constexpr(x < log2<kMinBlockSize>())
return 1;
else{
return (valid_block_size<pow2<x>()>() ? 1 : 0)
+ (valid_block_size<pow2<x>() + pow2<x-2>()>() ? 1 : 0)
+ (valid_block_size<pow2<x>() + pow2<x-1>()>() ? 1 : 0)
+ (valid_block_size<pow2<x>() + pow2<x-1>() + pow2<x-2>()>() ? 1 : 0)
+ num_size_classes<x - 1>();
}
}
export
inline constinit const unsigned kNumSizeClasses = num_size_classes<log2<kMaxBlockSize>()>();
inline constinit auto s_size_classes{[]() constexpr{
int j = 1;
std::array<unsigned, kNumSizeClasses + 1> sc{0};
constexpr_for<3, log2<kMaxBlockSize>()+1, 1>([&]<std::size_t X>{
if(valid_block_size<pow2<X>()>())
sc[j++] = pow2<X>();
if(valid_block_size<pow2<X>() + pow2<X-2>()>())
sc[j++] = pow2<X>() + pow2<X-2>();
if(valid_block_size<pow2<X>() + pow2<X-1>()>())
sc[j++] = pow2<X>() + pow2<X-1>();
if(valid_block_size<pow2<X>() + pow2<X-1>() + pow2<X-2>()>())
sc[j++] = pow2<X>() + pow2<X-1>() + pow2<X-2>();
});
return sc;
}()};
inline std::size_t s_page_size = 4096;
/*****************************************************************************/
/* SUPERBLOCK STATE */
/*****************************************************************************/
enum class SuperblockState
{
/* If every block in the superblock is being used by a thread cache or by the application */
eFull = 0,
/* If the superblock has some used blocks and some free blocks remaining */
ePartial = 1,
/* If the superblock has only free blocks */
eEmpty = 2
};
/*****************************************************************************/
/* SUPERBLOCK ANCHOR */
/*****************************************************************************/
struct alignas(8) SuperblockAnchor
{
uint64_t m_state : 2;
uint64_t m_avail : 31; /* index of the first free block */
uint64_t m_count : 31; /* number of free blocks */
};
using AtomicSuperblockAnchor = std::atomic<SuperblockAnchor>;
static_assert(sizeof(SuperblockAnchor) == sizeof(uint64_t));
static_assert(AtomicSuperblockAnchor::is_always_lock_free);
/*****************************************************************************/
/* SUPERBLOCK DESCRIPTOR */
/*****************************************************************************/
export
struct alignas(16) SuperblockDescriptor
{
AtomicSuperblockAnchor m_anchor;
std::uintptr_t m_superblock; /* pointer to superblock */
std::size_t m_blocksize; /* size of each block in superblock */
std::size_t m_maxcount; /* number of blocks */
std::size_t m_sizeclass; /* size class of blocks in superblock */
};
static_assert(std::is_standard_layout_v<SuperblockDescriptor>);
struct DescriptorNode
{
SuperblockDescriptor m_desc;
std::atomic<DescriptorNode*> m_next_free;
std::atomic<DescriptorNode*> m_next_partial;
struct alignas(16) Pointer
{
DescriptorNode *m_ptr{nullptr};
uintptr_t m_count{0};
bool operator==(const Pointer& rhs) const
{
return (m_ptr == rhs.m_ptr) && (m_count == rhs.m_count);
}
bool operator!=(const Pointer& rhs) const
{
return !(*this == rhs);
}
};
using AtomicPointer = DoubleQuadWordAtomic<Pointer>;
};
/*****************************************************************************/
/* DESCRIPTOR FREELIST */
/*****************************************************************************/
/*
* The superblock descriptors can be recycled, but the memory allocated for
* them is not returned to the OS until the application terminates.
*/
export
class DescriptorFreelist
{
private:
static constexpr unsigned kDescriptorBlockSize = 16384;
DescriptorNode::AtomicPointer m_freehead;
constexpr std::size_t descs_per_block();
DescriptorNode *initialize_block(void *ptr);
public:
DescriptorFreelist();
SuperblockDescriptor &Allocate();
void Retire(SuperblockDescriptor& desc);
};
/*****************************************************************************/
/* HEAP */
/*****************************************************************************/
class Heap
{
private:
DescriptorFreelist m_desclist;
std::array<DescriptorNode::AtomicPointer, kNumSizeClasses + 1> m_partial_superblocks;
public:
Heap();
SuperblockDescriptor *GetPartialSB(std::size_t size_class);
void PutPartialSB(SuperblockDescriptor *desc);
SuperblockDescriptor* AllocateSuperblock(std::size_t size_class);
void RetireSuperblock(SuperblockDescriptor *desc);
SuperblockDescriptor *AllocateDescriptor();
void RetireDescriptor(SuperblockDescriptor *desc);
};
/*****************************************************************************/
/* PAGEMAP */
/*****************************************************************************/
/*
* The pagemap contains metadata for each OS page in use by the allocator.
*/
export
class Pagemap
{
private:
uint64_t *m_metadata;
std::size_t desc_array_size() const;
std::size_t addr_to_key(std::byte *block) const;
public:
Pagemap();
SuperblockDescriptor *GetDescriptor(std::byte *block);
void RegisterDescriptor(SuperblockDescriptor *desc);
void UnregisterDescriptor(SuperblockDescriptor *desc);
std::size_t GetSizeClass(std::byte *block);
};
/*****************************************************************************/
/* STATIC STACK */
/*****************************************************************************/
template <typename T, std::size_t Capacity>
requires (std::is_default_constructible_v<T>)
class StaticStack
{
private:
std::array<T, Capacity> m_array{};
std::size_t m_size{};
std::size_t m_capacity;
public:
StaticStack()
: m_capacity{Capacity}
{}
StaticStack(std::size_t reduced_capacity)
: m_capacity{reduced_capacity}
{
assert(reduced_capacity <= Capacity);
}
template <typename U = T>
bool Push(U&& value)
{
if(m_size == m_capacity)
return false;
m_array[m_size++] = std::forward<U>(value);
return true;
}
std::optional<T> Pop()
{
if(m_size == 0)
return std::nullopt;
return m_array[--m_size];
}
std::optional<T> Peek()
{
if(m_size == 0)
return std::nullopt;
return m_array[m_size - 1];
}
std::size_t GetSize() { return m_size; }
std::size_t GetCapacity() { return m_capacity; }
bool Empty() { return (m_size == 0); }
bool Full() { return (m_size == m_capacity); }
};
/*****************************************************************************/
/* BLOCK FREELIST */
/*****************************************************************************/
/*
* A freelist of blocks for a specific size class.
*/
class BlockFreelist
{
private:
static constexpr std::size_t kMaxStackSize = kSuperblockSize / kMinBlockSize;
Heap& m_heap;
Pagemap& m_pagemap;
const std::size_t m_sizeclass;
StaticStack<std::byte*, kMaxStackSize> m_blocks;
std::size_t compute_idx(std::byte *superblock,
std::byte *ptr, std::size_t size_class) const;
bool FillFromPartialSB();
void FillFromNewSB();
public:
BlockFreelist(Heap&, Pagemap&, std::size_t);
void Fill();
void Flush();
bool IsEmpty();
bool IsFull();
std::byte *PopBlock();
void PushBlock(std::byte *block);
};
/*****************************************************************************/
/* SIMPLE SEGREGATED STORAGE */
/*****************************************************************************/
/*
* Holds a sepagage block freelist for each size class.
*/
class SimpleSegregatedStorage
{
private:
std::array<BlockFreelist, kNumSizeClasses + 1> m_lists;
template<typename T, std::size_t N, std::size_t... I>
constexpr auto create_array_impl(Heap& heap, Pagemap& pagemap,
decltype(s_size_classes) scs, std::index_sequence<I...>)
{
return std::array<T, N>{ BlockFreelist{heap, pagemap, I}... };
}
template<typename T, std::size_t N>
constexpr auto create_array(Heap& heap, Pagemap& pagemap, decltype(s_size_classes) scs)
{
return create_array_impl<T, N>(heap, pagemap, scs, std::make_index_sequence<N>{});
}
public:
SimpleSegregatedStorage(Heap& heap, Pagemap& pagemap)
: m_lists{ create_array<BlockFreelist, kNumSizeClasses + 1>(heap, pagemap, s_size_classes) }
{}
BlockFreelist& GetForSizeClass(std::size_t sc)
{
return m_lists[sc];
}
};
/*****************************************************************************/
/* THREAD CACHE */
/*****************************************************************************/
/*
* Thread-local cache of blocks for different size classes.
* Ultimately, the thread cahes' goal is to provide extremely
* fast, synchronization-free memory allocations and deallocations.
* It does so by ensuring that most malloc() calls are just simple
* stack pops and most free() calls are just simple stack pushes.
* This simple and speed-efficient average case is needed to
* amortize the costs of synchronization that is necessary to
* transfer blocks to and from the thread caches.
*/
class ThreadCache
{
private:
SimpleSegregatedStorage m_blocklists;
public:
ThreadCache(Heap& heap, Pagemap& pagemap)
: m_blocklists{heap, pagemap}
{}
BlockFreelist& GetBlocksForSizeClass(std::size_t sc)
{
if(sc > kNumSizeClasses)
throw std::out_of_range{"Invalid size class."};
return m_blocklists.GetForSizeClass(sc);
}
};
/*****************************************************************************/
/* ALLOCATOR */
/*****************************************************************************/
/*
* A lock-free general-purpose memory allocator, based on the
* paper of LRMalloc.
*/
export
class Allocator
{
private:
Heap& m_heap;
Pagemap& m_pagemap;
pthread_key_t m_thread_cache_key;
void *allocate_large_block(std::size_t size);
void deallocate_large_block(void *ptr);
std::size_t compute_size_class(std::size_t size);
ThreadCache& get_thread_cache() const;
Allocator(Heap& heap, Pagemap& pagemap);
~Allocator();
public:
static inline Allocator& Instance();
void *Allocate(std::size_t size);
void Free(void *ptr);
void *AllocateAligned(std::size_t size, std::align_val_t align);
void FreeAligned(void *ptr, std::align_val_t align);
std::size_t NextAlignedBlockSize(std::size_t size, std::size_t align);
std::size_t AllocationSize(void *ptr);
};
/*****************************************************************************/
/* IMPLEMENTATION */
/*****************************************************************************/
DescriptorFreelist::DescriptorFreelist()
: m_freehead{{nullptr, 0}}
{}
constexpr std::size_t DescriptorFreelist::descs_per_block()
{
return kDescriptorBlockSize / sizeof(DescriptorNode);
}
DescriptorNode *DescriptorFreelist::initialize_block(void *block)
{
DescriptorNode *nodes = std::launder(reinterpret_cast<DescriptorNode*>(block));
const std::size_t nnodes = descs_per_block();
for(int i = 0; i < nnodes - 1; i++) {
new (&nodes[i]) DescriptorNode{};
nodes[i].m_next_free.store(&nodes[i + 1], std::memory_order_relaxed);
}
new (&nodes[nnodes - 1]) DescriptorNode{};
nodes[nnodes - 1].m_next_free.store(nullptr, std::memory_order_relaxed);
return nodes;
}
SuperblockDescriptor &DescriptorFreelist::Allocate()
{
restart:
DescriptorNode::Pointer freehead = m_freehead.Load(std::memory_order_acquire);
while(!freehead.m_ptr) {
void *newblock = mmap(0, kDescriptorBlockSize, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if(newblock == reinterpret_cast<void*>(-1ull))
throw std::bad_alloc{};
DescriptorNode *next = initialize_block(newblock);
AnnotateHappensBefore(__FILE__, __LINE__, &m_freehead);
while(!m_freehead.CompareExchange(freehead, {next, freehead.m_count + 1},
std::memory_order_release, std::memory_order_acquire)) {
/* If we found that we have freehead already,
* then chain the existing block after the
* newly allocated one.
*/
const std::size_t nnodes = descs_per_block();
next[nnodes - 1].m_next_free.store(freehead.m_ptr, std::memory_order_release);
}
freehead = m_freehead.Load(std::memory_order_acquire);
}
AnnotateHappensAfter(__FILE__, __LINE__, &m_freehead);
DescriptorNode *nextfree = freehead.m_ptr->m_next_free.load(std::memory_order_acquire);
while(!m_freehead.CompareExchange(freehead, {nextfree, freehead.m_count + 1},
std::memory_order_release, std::memory_order_acquire)) {
if(!freehead.m_ptr)
goto restart;
nextfree = freehead.m_ptr->m_next_free.load(std::memory_order_acquire);
}
return freehead.m_ptr->m_desc;
}
void DescriptorFreelist::Retire(SuperblockDescriptor& desc)
{
/* For standard-layout classes, the address of the struct/class
* is the same as the address of its first non-static member.
*/
static_assert(offsetof(DescriptorNode, m_desc) == 0);
DescriptorNode *curr = reinterpret_cast<DescriptorNode*>(&desc);
DescriptorNode::Pointer freehead = m_freehead.Load(std::memory_order_acquire);
do{
curr->m_next_free.store(freehead.m_ptr, std::memory_order_release);
AnnotateHappensBefore(__FILE__, __LINE__, &m_freehead);
if(m_freehead.CompareExchange(freehead, {curr, freehead.m_count + 1},
std::memory_order_release, std::memory_order_acquire)) {
AnnotateHappensAfter(__FILE__, __LINE__, &m_freehead);
break;
}
}while(true);
}
std::size_t Pagemap::desc_array_size() const
{
return (std::exp2(kAddressUsedBits) / s_page_size);
}
std::size_t Pagemap::addr_to_key(std::byte *block) const
{
uint64_t pageshift = std::log2(s_page_size);
uintptr_t page = reinterpret_cast<uintptr_t>(block);
uintptr_t idx = page >> pageshift;
pe::assert(idx < desc_array_size());
return idx;
}
Pagemap::Pagemap()
: m_metadata{}
{
const std::size_t size = sizeof(SuperblockDescriptor*) * desc_array_size();
m_metadata = static_cast<uint64_t*>(
mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0));
if(m_metadata == reinterpret_cast<decltype(m_metadata)>(-1))
throw std::bad_alloc{};
madvise(m_metadata, size, MADV_DONTDUMP);
}
SuperblockDescriptor *Pagemap::GetDescriptor(std::byte *block)
{
uint64_t mask = std::exp2(64 - kAddressUsedBits) - 1;
mask <<= kAddressUsedBits;
auto ret = reinterpret_cast<SuperblockDescriptor*>(m_metadata[addr_to_key(block)] & ~mask);
pe::assert(ret != 0);
return ret;
}
void Pagemap::RegisterDescriptor(SuperblockDescriptor *desc)
{
std::size_t npages = kSuperblockSize / s_page_size;
npages += !!(kSuperblockSize % s_page_size);
std::byte *base = reinterpret_cast<std::byte*>(desc->m_superblock);
for(int i = 0; i < npages; i++) {
/* Although we can fetch the sizeclass by dereferencing
* the descriptor pointer, we can pack it into the unused
* higher bits in order to save on one pointer jump in the
* hot case.
*/
uint64_t value = desc->m_sizeclass << kAddressUsedBits | reinterpret_cast<uintptr_t>(desc);
m_metadata[addr_to_key(base + (i * s_page_size))] = value;
/* No need to register every page for large allocations.
*/
if(desc->m_sizeclass == 0)
break;
}
}
void Pagemap::UnregisterDescriptor(SuperblockDescriptor *desc)
{
std::size_t npages = kSuperblockSize / s_page_size;
npages += !!(kSuperblockSize % s_page_size);
std::byte *base = reinterpret_cast<std::byte*>(desc->m_superblock);
for(int i = 0; i < npages; i++) {
m_metadata[addr_to_key(base + (i * s_page_size))] = 0;
}
}
std::size_t Pagemap::GetSizeClass(std::byte *block)
{
uint64_t meta = m_metadata[addr_to_key(block)];
return (meta >> kAddressUsedBits);
}
Heap::Heap()
: m_desclist{}
, m_partial_superblocks{DescriptorNode::Pointer{nullptr, 0}}
{}
SuperblockDescriptor* Heap::GetPartialSB(std::size_t size_class)
{
DescriptorNode::Pointer head = m_partial_superblocks[size_class].Load(std::memory_order_acquire);
DescriptorNode *next;
do{
if(!head.m_ptr)
return nullptr;
next = head.m_ptr->m_next_partial.load(std::memory_order_relaxed);
}while(!m_partial_superblocks[size_class].CompareExchange(head, {next, head.m_count + 1},
std::memory_order_release, std::memory_order_acquire));
return &head.m_ptr->m_desc;
}
void Heap::PutPartialSB(SuperblockDescriptor *desc)
{
std::size_t size_class = desc->m_sizeclass;
static_assert(offsetof(DescriptorNode, m_desc) == 0);
DescriptorNode *curr = reinterpret_cast<DescriptorNode*>(desc);
[[maybe_unused]] SuperblockAnchor anchor = desc->m_anchor.load(std::memory_order_relaxed);
pe::assert(anchor.m_state == static_cast<uint64_t>(SuperblockState::ePartial));
DescriptorNode::Pointer head = m_partial_superblocks[size_class].Load(std::memory_order_acquire);
do{
curr->m_next_partial.store(head.m_ptr, std::memory_order_relaxed);
}while(!m_partial_superblocks[size_class].CompareExchange(head, {curr, head.m_count + 1},
std::memory_order_release, std::memory_order_acquire));
}
SuperblockDescriptor* Heap::AllocateSuperblock(std::size_t size_class)
{
SuperblockDescriptor *ret = &m_desclist.Allocate();
void *superblock = mmap(0, kSuperblockSize, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if(superblock == reinterpret_cast<void*>(-1ull))
throw std::bad_alloc{};
ret->m_superblock = reinterpret_cast<uintptr_t>(superblock);
ret->m_blocksize = s_size_classes[size_class];
ret->m_maxcount = kSuperblockSize / ret->m_blocksize;
ret->m_sizeclass = size_class;
/* Form an ad-hoc freelist of blocks inside the superblock.
*/
size_t nblocks = ret->m_maxcount;
for(int i = 0; i < nblocks; i++) {
std::byte *block = reinterpret_cast<std::byte*>(superblock) + (i * ret->m_blocksize);
std::byte *next = reinterpret_cast<std::byte*>(superblock) + ((i + 1) * ret->m_blocksize);
if(i == nblocks - 1)
next = nullptr;
*reinterpret_cast<std::byte**>(block) = next;
}
/* Because all the blocks in a newly allocated superblock
* are immediately given to a thread cache, a superblock
* that was just allocated from the OS is full and will only
* become partial/empty as blocks are flushed from thread
* caches.
*/
ret->m_anchor.store({static_cast<uint64_t>(SuperblockState::eFull), ret->m_maxcount, 0},
std::memory_order_release);
return ret;
}
void Heap::RetireSuperblock(SuperblockDescriptor *desc)
{
munmap(reinterpret_cast<void*>(desc->m_superblock), kSuperblockSize);
}
SuperblockDescriptor* Heap::AllocateDescriptor()
{
return &m_desclist.Allocate();
}
void Heap::RetireDescriptor(SuperblockDescriptor *desc)
{
m_desclist.Retire(*desc);
}
BlockFreelist::BlockFreelist(Heap& heap, Pagemap& pagemap, std::size_t sc)
: m_heap{heap}
, m_pagemap{pagemap}
, m_sizeclass{sc}
, m_blocks{s_size_classes[sc] > 0 ? (kSuperblockSize / s_size_classes[sc]) : 0}
{
pe::assert(sc <= kNumSizeClasses);
}
std::size_t BlockFreelist::compute_idx(std::byte *superblock,
std::byte *ptr, std::size_t size_class) const
{
pe::assert(ptr >= superblock);
uintptr_t diff = ptr - superblock;
pe::assert(diff < kSuperblockSize);
pe::assert(diff % s_size_classes[size_class] == 0);
return diff / s_size_classes[size_class];
}
bool BlockFreelist::FillFromPartialSB()
{
SuperblockDescriptor *desc = m_heap.GetPartialSB(m_sizeclass);
if(!desc)
return false;
SuperblockAnchor new_anchor, old_anchor;
do{
old_anchor = desc->m_anchor.load(std::memory_order_acquire);
if(old_anchor.m_state == static_cast<uint64_t>(SuperblockState::eEmpty)) {
m_heap.RetireDescriptor(desc);
return FillFromPartialSB();
}
pe::assert(desc->m_sizeclass != 0);
pe::assert(old_anchor.m_state == static_cast<uint64_t>(SuperblockState::ePartial));
new_anchor.m_state = static_cast<uint64_t>(SuperblockState::eFull);
new_anchor.m_avail = desc->m_maxcount;
new_anchor.m_count = 0;
}while(!desc->m_anchor.compare_exchange_weak(old_anchor, new_anchor,
std::memory_order_release, std::memory_order_relaxed));
std::byte *block = reinterpret_cast<std::byte*>(desc->m_superblock)
+ (old_anchor.m_avail * desc->m_blocksize);
std::size_t block_count = old_anchor.m_count;
pe::assert(block_count > 0);
while(block_count--) {
PushBlock(block);
block = *reinterpret_cast<std::byte**>(block);
}
return true;
}
void BlockFreelist::FillFromNewSB()
{
SuperblockDescriptor *desc = m_heap.AllocateSuperblock(m_sizeclass);
pe::assert(desc->m_maxcount > 0);
pe::assert(desc->m_superblock);
pe::assert(desc->m_anchor.load(std::memory_order_relaxed).m_state
== static_cast<uint64_t>(SuperblockState::eFull));
for(int i = 0; i < desc->m_maxcount; i++) {
std::byte *block = reinterpret_cast<std::byte*>(desc->m_superblock)
+ (i * desc->m_blocksize);
PushBlock(block);
}
m_pagemap.RegisterDescriptor(desc);
}
void BlockFreelist::Fill()
{
/* Try to fill the cache from a single partial superblock
*/
bool result = FillFromPartialSB();
/* If that fails, create a new superblock
*/
if(!result)
FillFromNewSB();
}
void BlockFreelist::Flush()
{
while(!m_blocks.Empty()) {
/* Form a list of blocks to return to a common superblock.
*/
std::byte *head, *tail;
head = tail = m_blocks.Pop().value();
SuperblockDescriptor *desc = m_pagemap.GetDescriptor(head);
[[maybe_unused]] SuperblockAnchor anchor = desc->m_anchor.load(std::memory_order_relaxed);
pe::assert(anchor.m_state != static_cast<uint64_t>(SuperblockState::eEmpty));
pe::assert(desc->m_sizeclass != 0);
std::size_t block_count = 1;
while(!m_blocks.Empty()) {
std::byte *block = m_blocks.Peek().value();
if(m_pagemap.GetDescriptor(block) != desc)
break;
m_blocks.Pop();
++block_count;
*reinterpret_cast<std::byte**>(tail) = block;
tail = block;
}
/* Add list to descriptor and update anchor.
*/
std::byte *superblock = reinterpret_cast<std::byte*>(desc->m_superblock);
size_t idx = compute_idx(superblock, head, desc->m_sizeclass);
SuperblockAnchor old_anchor, new_anchor;
do{
old_anchor = new_anchor = desc->m_anchor.load(std::memory_order_relaxed);
*reinterpret_cast<std::byte**>(tail) = superblock
+ (old_anchor.m_avail * desc->m_blocksize);
new_anchor.m_state = static_cast<uint64_t>(SuperblockState::ePartial);
new_anchor.m_avail = idx;
new_anchor.m_count += block_count;
pe::assert(new_anchor.m_count <= desc->m_maxcount);
if(new_anchor.m_count == desc->m_maxcount) {
/* Can free superblock */
new_anchor.m_state = static_cast<uint64_t>(SuperblockState::eEmpty);
}else if(old_anchor.m_state == static_cast<uint64_t>(SuperblockState::eFull)) {
new_anchor.m_state = static_cast<uint64_t>(SuperblockState::ePartial);
}
pe::assert(new_anchor.m_state == static_cast<uint64_t>(SuperblockState::eFull)
? (new_anchor.m_count == 0) : true);
pe::assert(new_anchor.m_state == static_cast<uint64_t>(SuperblockState::eEmpty)
? (new_anchor.m_count == desc->m_maxcount) : true);
}while(!desc->m_anchor.compare_exchange_weak(old_anchor, new_anchor,
std::memory_order_release, std::memory_order_relaxed));
if(new_anchor.m_state == static_cast<uint64_t>(SuperblockState::eEmpty)) {
m_pagemap.UnregisterDescriptor(desc);
m_heap.RetireSuperblock(desc);
}else if(old_anchor.m_state == static_cast<uint64_t>(SuperblockState::eFull)) {
pe::assert(new_anchor.m_state == static_cast<uint64_t>(SuperblockState::ePartial));
m_heap.PutPartialSB(desc);
}
}
}
bool BlockFreelist::IsEmpty()
{
return m_blocks.Empty();
}
bool BlockFreelist::IsFull()
{
return m_blocks.Full();
}
void BlockFreelist::PushBlock(std::byte *block)
{
m_blocks.Push(block);
}
std::byte *BlockFreelist::PopBlock()
{
auto ret = m_blocks.Pop();
return ret.value_or(nullptr);
}
std::size_t Allocator::compute_size_class(std::size_t size)
{
if(size > kMaxBlockSize)
return 0;
return std::distance(std::begin(s_size_classes),
std::lower_bound(std::begin(s_size_classes), std::end(s_size_classes), size));
}
ThreadCache& Allocator::get_thread_cache() const
{
static thread_local ThreadCache t_thread_cache{m_heap, m_pagemap};
void *raw = pthread_getspecific(m_thread_cache_key);
if(!raw) {
raw = reinterpret_cast<void*>(&t_thread_cache);
if(pthread_setspecific(m_thread_cache_key, raw)) [[unlikely]]
throw std::runtime_error{"Failed to set Thread-Local Storage."};
}
return *reinterpret_cast<ThreadCache*>(raw);
}
void *Allocator::allocate_large_block(std::size_t size)
{
void *ret = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if(ret == reinterpret_cast<void*>(-1ull))
throw std::bad_alloc{};
SuperblockDescriptor *desc = m_heap.AllocateDescriptor();
desc->m_superblock = reinterpret_cast<uintptr_t>(ret);
desc->m_blocksize = size;
desc->m_maxcount = 1;
desc->m_sizeclass = 0;
desc->m_anchor.store({static_cast<uint64_t>(SuperblockState::eFull), desc->m_maxcount, 0},
std::memory_order_release);
m_pagemap.RegisterDescriptor(desc);
return ret;
}
void Allocator::deallocate_large_block(void *ptr)
{
auto desc = m_pagemap.GetDescriptor(reinterpret_cast<std::byte*>(ptr));
munmap(ptr, desc->m_blocksize);
m_heap.RetireDescriptor(desc);
}
Allocator::Allocator(Heap& heap, Pagemap& pagemap)
: m_heap{heap}
, m_pagemap{pagemap}
, m_thread_cache_key{}
{
int result = pthread_key_create(&m_thread_cache_key, nullptr);
if(result) [[unlikely]]
throw std::runtime_error{"Failed to allocate Thread-Local Storage."};
}
Allocator::~Allocator()
{
pthread_key_delete(m_thread_cache_key);
}
Allocator& Allocator::Instance()
{
static bool initialized = false;
if(!initialized) {
s_page_size = getpagesize();
initialized = true;
}
static Heap s_heap{};
static Pagemap s_pagemap{};
static Allocator s_instance{s_heap, s_pagemap};
return s_instance;
}
void *Allocator::Allocate(std::size_t size)
{
std::size_t sc = compute_size_class(size);
if(sc == 0)
return allocate_large_block(size);
auto& cache = get_thread_cache().GetBlocksForSizeClass(sc);
if(cache.IsEmpty())
cache.Fill();
return cache.PopBlock();
}
void Allocator::Free(void *ptr)
{
std::byte *block = reinterpret_cast<std::byte*>(ptr);
if(!block) [[unlikely]]
return;
size_t sc = m_pagemap.GetSizeClass(block);
if(sc == 0)
return deallocate_large_block(block);
auto& cache = get_thread_cache().GetBlocksForSizeClass(sc);
if(cache.IsFull())
cache.Flush();
cache.PushBlock(block);
}
void *Allocator::AllocateAligned(std::size_t size, std::align_val_t align)
{
std::size_t alignment = static_cast<std::size_t>(align);
if(alignment <= kMaxBlockSize) {
size = NextAlignedBlockSize(size, alignment);
return Allocate(size);
}
std::size_t extra_bytes = size + alignment - 1 + sizeof(void*);
void *allocation = Allocate(size + extra_bytes);
if(!allocation)
return allocation;
/* round up to a multiple of alignment
*/
uintptr_t uptr = reinterpret_cast<uintptr_t>(allocation);
uptr = (uptr + extra_bytes) & ~(alignment - 1);