-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFreeListAllocator.cppm
More file actions
125 lines (101 loc) · 4.1 KB
/
FreeListAllocator.cppm
File metadata and controls
125 lines (101 loc) · 4.1 KB
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
// Copyright (C) 2026 mxreal64
//
// This program 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.
//
// This program 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 <https://gnu.org>.
export module FreeListAllocator;
import <cstdint>;
import <cstddef>;
import <atomic>;
import <new>;
import <utility>;
import <cstring>;
export template <std::size_t BlockCount>
class FixedSizeFreeList {
private:
static_assert(BlockCount > 0, "Block count must be greater than zero.");
static_assert(BlockCount <= 0xFFFFFFFF, "Block count exceeds 32-bit index space.");
struct Node {
uint32_t next_index;
uint32_t padding;
};
struct TaggedIndex {
uint32_t index;
uint32_t tag;
};
alignas(64) std::byte storage_[BlockCount * 64];
alignas(64) std::atomic<uint64_t> head_{0};
static constexpr uint64_t pack(TaggedIndex ti) noexcept {
return (static_cast<uint64_t>(ti.tag) << 32) | ti.index;
}
static constexpr TaggedIndex unpack(uint64_t val) noexcept {
return TaggedIndex{
.index = static_cast<uint32_t>(val & 0xFFFFFFFFULL),
.tag = static_cast<uint32_t>(val >> 32)
};
}
public:
FixedSizeFreeList() noexcept {
for (std::size_t i = 0; i < BlockCount - 1; ++i) {
Node n{.next_index = static_cast<uint32_t>(i + 1), .padding = 0};
std::memcpy(&storage_[i * 64], &n, sizeof(Node));
}
Node last{.next_index = 0xFFFFFFFF, .padding = 0};
std::memcpy(&storage_[(BlockCount - 1) * 64], &last, sizeof(Node));
head_.store(pack({.index = 0, .tag = 0}), std::memory_order_relaxed);
}
~FixedSizeFreeList() = default;
FixedSizeFreeList(const FixedSizeFreeList&) = delete;
FixedSizeFreeList& operator=(const FixedSizeFreeList&) = delete;
FixedSizeFreeList(FixedSizeFreeList&&) = delete;
FixedSizeFreeList& operator=(FixedSizeFreeList&&) = delete;
void* allocate() noexcept {
uint64_t current_head = head_.load(std::memory_order_acquire);
while (true) {
TaggedIndex unpacked = unpack(current_head);
if (unpacked.index == 0xFFFFFFFF) {
return nullptr;
}
Node current_node;
std::memcpy(¤t_node, &storage_[unpacked.index * 64], sizeof(Node));
uint64_t next_head = pack({
.index = current_node.next_index,
.tag = unpacked.tag + 1
});
if (head_.compare_exchange_weak(current_head, next_head, std::memory_order_acquire, std::memory_order_relaxed)) {
return static_cast<void*>(&storage_[unpacked.index * 64]);
}
}
}
void deallocate(void* ptr) noexcept {
if (!ptr) return;
std::size_t offset = static_cast<std::byte*>(ptr) - storage_;
uint32_t block_index = static_cast<uint32_t>(offset / 64);
uint64_t current_head = head_.load(std::memory_order_acquire);
uint32_t last_known_next = 0xFFFFFFFF;
while (true) {
TaggedIndex unpacked = unpack(current_head);
if (unpacked.index != last_known_next) {
Node new_node{.next_index = unpacked.index, .padding = 0};
std::memcpy(&storage_[block_index * 64], &new_node, sizeof(Node));
last_known_next = unpacked.index;
}
uint64_t next_head = pack({
.index = block_index,
.tag = unpacked.tag + 1
});
if (head_.compare_exchange_weak(current_head, next_head, std::memory_order_release, std::memory_order_acquire)) {
break;
}
}
}
};