Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
object_pool.h
1/*
2 * Copyright © 2025 Katherine Whitlock
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18#include <array>
19#include <cstddef>
20#include <cstdint>
21#include <gsl/gsl>
22#include <memory>
23
24namespace deluge::memory {
36template <typename T, template <typename> typename Alloc = std::allocator>
37class ObjectPool {
38 static constexpr size_t kDefaultSize = 48;
41 static constexpr size_t kMaxSlabs = 4;
42
43 // Free objects have been destroyed, so their storage holds the free-list link.
44 struct FreeNode {
45 FreeNode* next;
46 };
47
48 struct Slab {
49 T* begin = nullptr;
50 size_t count = 0;
51 [[nodiscard]] bool contains(const T* obj) const {
52 auto addr = reinterpret_cast<uintptr_t>(obj);
53 auto start = reinterpret_cast<uintptr_t>(begin);
54 return addr >= start && addr < start + count * sizeof(T);
55 }
56 };
57
58 ObjectPool() { repopulate(); };
59
60public:
62 using value_type = T;
63
64 ~ObjectPool() { clear(); };
65
68 static constexpr ObjectPool& get() {
69 static thread_local ObjectPool instance;
70 return instance;
71 }
72
77 void resize(size_t n) {
78 capacity_ = n;
79 while (size() > capacity_ && overflow_free_ != nullptr) {
80 alloc_.deallocate(pop(overflow_free_, num_overflow_free_), 1);
81 }
82 if (size() > capacity_ && num_slab_free_ == slabObjectCount()) {
83 releaseSlabs();
84 repopulate();
85 }
86 }
87
90 [[nodiscard]] size_t capacity() const { return capacity_; }
91
94 [[nodiscard]] size_t size() const { return num_slab_free_ + num_overflow_free_; }
95
98 static void recycle(gsl::owner<T*> obj) {
99 obj->~T();
100 get().put(obj);
101 }
102
105 void repopulate() noexcept(false) {
106 if (size() >= capacity_) {
107 return;
108 }
109 size_t needed = capacity_ - size();
110 if (needed > 1 && num_slabs_ < kMaxSlabs) {
111 T* block = alloc_.allocate(needed);
112 slabs_[num_slabs_++] = {block, needed};
113 for (size_t i = needed; i-- > 0;) {
114 push(slab_free_, num_slab_free_, &block[i]);
115 }
116 return;
117 }
118 for (; needed > 0; --needed) {
119 push(overflow_free_, num_overflow_free_, alloc_.allocate(1));
120 }
121 }
122
126 using pointer_type = std::unique_ptr<T, decltype(&recycle)>;
127
133 template <typename... Args>
134 [[nodiscard]] pointer_type acquire(Args&&... args) noexcept(false) {
135 T* obj;
136 // Prefer slab objects, so idle overflow objects can be freed as the pool refills
137 if (slab_free_ != nullptr) {
138 obj = pop(slab_free_, num_slab_free_);
139 }
140 else if (overflow_free_ != nullptr) {
141 obj = pop(overflow_free_, num_overflow_free_);
142 }
143 else {
144 obj = alloc_.allocate(1);
145 }
146 return {new (obj) T(std::forward<Args>(args)...), &recycle};
147 }
148
151 void clear() {
152 while (overflow_free_ != nullptr) {
153 alloc_.deallocate(pop(overflow_free_, num_overflow_free_), 1);
154 }
155 if (num_slab_free_ == slabObjectCount()) {
156 releaseSlabs();
157 }
158 }
159
162 [[nodiscard]] constexpr bool empty() const { return size() == 0; }
163
164private:
165 static void push(FreeNode*& list, size_t& count, T* obj) {
166 // Checked here rather than at class scope, so the pool can be named while T is incomplete
167 static_assert(sizeof(T) >= sizeof(FreeNode) && alignof(T) >= alignof(FreeNode),
168 "pooled objects must be able to hold a free-list link");
169 auto* node = reinterpret_cast<FreeNode*>(obj);
170 node->next = list;
171 list = node;
172 ++count;
173 }
174
175 static T* pop(FreeNode*& list, size_t& count) {
176 FreeNode* node = list;
177 list = node->next;
178 --count;
179 return reinterpret_cast<T*>(node);
180 }
181
182 [[nodiscard]] bool isSlabObject(const T* obj) const {
183 for (size_t i = 0; i < num_slabs_; ++i) {
184 if (slabs_[i].contains(obj)) {
185 return true;
186 }
187 }
188 return false;
189 }
190
191 [[nodiscard]] size_t slabObjectCount() const {
192 size_t total = 0;
193 for (size_t i = 0; i < num_slabs_; ++i) {
194 total += slabs_[i].count;
195 }
196 return total;
197 }
198
199 void put(T* obj) {
200 if (isSlabObject(obj)) {
201 push(slab_free_, num_slab_free_, obj);
202 // Slab objects can't be freed, so make room by freeing an idle overflow object instead
203 if (size() > capacity_ && overflow_free_ != nullptr) {
204 alloc_.deallocate(pop(overflow_free_, num_overflow_free_), 1);
205 }
206 }
207 else if (size() < capacity_) {
208 push(overflow_free_, num_overflow_free_, obj);
209 }
210 else {
211 alloc_.deallocate(obj, 1);
212 }
213 }
214
215 // Only call when every slab object is free
216 void releaseSlabs() {
217 slab_free_ = nullptr;
218 num_slab_free_ = 0;
219 for (size_t i = 0; i < num_slabs_; ++i) {
220 alloc_.deallocate(slabs_[i].begin, slabs_[i].count);
221 slabs_[i] = {};
222 }
223 num_slabs_ = 0;
224 }
225
226 size_t capacity_ = kDefaultSize;
227 FreeNode* slab_free_ = nullptr;
228 FreeNode* overflow_free_ = nullptr;
229 size_t num_slab_free_ = 0;
230 size_t num_overflow_free_ = 0;
231 std::array<Slab, kMaxSlabs> slabs_{};
232 size_t num_slabs_ = 0;
233 Alloc<T> alloc_;
234};
235} // namespace deluge::memory
static constexpr size_t kMaxSlabs
Definition object_pool.h:41
void resize(size_t n)
Sets the size of the pool.
Definition object_pool.h:77
pointer_type acquire(Args &&... args) noexcept(false)
Acquires an object from the pool.
Definition object_pool.h:134
size_t capacity() const
Gets the capacity of the pool.
Definition object_pool.h:90
void repopulate() noexcept(false)
Repopulates the pool to its original size.
Definition object_pool.h:105
static constexpr ObjectPool & get()
Gets the global pool for a given object.
Definition object_pool.h:68
std::unique_ptr< DxVoice, decltype(&recycle)> pointer_type
Definition object_pool.h:126
static void recycle(gsl::owner< T * > obj)
Definition object_pool.h:98
constexpr bool empty() const
Checks if the pool is empty.
Definition object_pool.h:162
Definition object_pool.h:44
Definition object_pool.h:48