WPILibC++ 2027.0.0-alpha-2
Loading...
Searching...
No Matches
pool.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <cstddef>
6#include <memory>
7
9
11
12namespace slp {
13
14/**
15 * This class implements a pool memory resource.
16 *
17 * The pool allocates chunks of memory and splits them into blocks managed by a
18 * free list. Allocations return pointers from the free list, and deallocations
19 * return pointers to the free list.
20 */
22 public:
23 /**
24 * Constructs a default PoolResource.
25 *
26 * @param blocks_per_chunk Number of blocks per chunk of memory.
27 */
28 explicit PoolResource(size_t blocks_per_chunk)
29 : blocks_per_chunk{blocks_per_chunk} {}
30
31 /**
32 * Copy constructor.
33 */
34 PoolResource(const PoolResource&) = delete;
35
36 /**
37 * Copy assignment operator.
38 *
39 * @return This pool resource.
40 */
42
43 /**
44 * Move constructor.
45 */
47
48 /**
49 * Move assignment operator.
50 *
51 * @return This pool resource.
52 */
54
55 /**
56 * Returns a block of memory from the pool.
57 *
58 * @param bytes Number of bytes in the block.
59 * @param alignment Alignment of the block (unused).
60 * @return A block of memory from the pool.
61 */
62 [[nodiscard]]
63 void* allocate(size_t bytes, [[maybe_unused]] size_t alignment =
64 alignof(std::max_align_t)) {
65 if (m_free_list.empty()) {
66 add_chunk(bytes);
67 }
68
69 auto ptr = m_free_list.back();
70 m_free_list.pop_back();
71 return ptr;
72 }
73
74 /**
75 * Gives a block of memory back to the pool.
76 *
77 * @param p A pointer to the block of memory.
78 * @param bytes Number of bytes in the block (unused).
79 * @param alignment Alignment of the block (unused).
80 */
82 void* p, [[maybe_unused]] size_t bytes,
83 [[maybe_unused]] size_t alignment = alignof(std::max_align_t)) {
84 m_free_list.emplace_back(p);
85 }
86
87 /**
88 * Returns true if this pool resource has the same backing storage as another.
89 *
90 * @param other The other pool resource.
91 * @return True if this pool resource has the same backing storage as another.
92 */
93 bool is_equal(const PoolResource& other) const noexcept {
94 return this == &other;
95 }
96
97 /**
98 * Returns the number of blocks from this pool resource that are in use.
99 *
100 * @return The number of blocks from this pool resource that are in use.
101 */
102 size_t blocks_in_use() const noexcept {
103 return m_buffer.size() * blocks_per_chunk - m_free_list.size();
104 }
105
106 private:
108 gch::small_vector<void*> m_free_list;
109 size_t blocks_per_chunk;
110
111 /**
112 * Adds a memory chunk to the pool, partitions it into blocks with the given
113 * number of bytes, and appends pointers to them to the free list.
114 *
115 * @param bytes_per_block Number of bytes in the block.
116 */
117 void add_chunk(size_t bytes_per_block) {
118 m_buffer.emplace_back(new std::byte[bytes_per_block * blocks_per_chunk]);
119 for (int i = blocks_per_chunk - 1; i >= 0; --i) {
120 m_free_list.emplace_back(m_buffer.back().get() + bytes_per_block * i);
121 }
122 }
123};
124
125/**
126 * This class is an allocator for the pool resource.
127 *
128 * @tparam T The type of object in the pool.
129 */
130template <typename T>
132 public:
133 /**
134 * The type of object in the pool.
135 */
136 using value_type = T;
137
138 /**
139 * Constructs a pool allocator with the given pool memory resource.
140 *
141 * @param r The pool resource.
142 */
143 explicit constexpr PoolAllocator(PoolResource* r) : m_memory_resource{r} {}
144
145 /**
146 * Copy constructor.
147 */
148 constexpr PoolAllocator(const PoolAllocator<T>&) = default;
149
150 /**
151 * Copy assignment operator.
152 *
153 * @return This pool allocator.
154 */
155 constexpr PoolAllocator<T>& operator=(const PoolAllocator<T>&) = default;
156
157 /**
158 * Returns a block of memory from the pool.
159 *
160 * @param n Number of bytes in the block.
161 * @return A block of memory from the pool.
162 */
163 [[nodiscard]]
164 constexpr T* allocate(size_t n) {
165 return static_cast<T*>(m_memory_resource->allocate(n));
166 }
167
168 /**
169 * Gives a block of memory back to the pool.
170 *
171 * @param p A pointer to the block of memory.
172 * @param n Number of bytes in the block.
173 */
174 constexpr void deallocate(T* p, size_t n) {
175 m_memory_resource->deallocate(p, n);
176 }
177
178 private:
179 PoolResource* m_memory_resource;
180};
181
182/**
183 * Returns a global pool memory resource.
184 */
186
187/**
188 * Returns an allocator for a global pool memory resource.
189 *
190 * @tparam T The type of object in the pool.
191 */
192template <typename T>
196
197} // namespace slp
This class is an allocator for the pool resource.
Definition pool.hpp:131
constexpr PoolAllocator(const PoolAllocator< T > &)=default
Copy constructor.
constexpr PoolAllocator(PoolResource *r)
Constructs a pool allocator with the given pool memory resource.
Definition pool.hpp:143
constexpr void deallocate(T *p, size_t n)
Gives a block of memory back to the pool.
Definition pool.hpp:174
constexpr PoolAllocator< T > & operator=(const PoolAllocator< T > &)=default
Copy assignment operator.
T value_type
The type of object in the pool.
Definition pool.hpp:136
constexpr T * allocate(size_t n)
Returns a block of memory from the pool.
Definition pool.hpp:164
This class implements a pool memory resource.
Definition pool.hpp:21
PoolResource(size_t blocks_per_chunk)
Constructs a default PoolResource.
Definition pool.hpp:28
PoolResource(PoolResource &&)=default
Move constructor.
void * allocate(size_t bytes, size_t alignment=alignof(std::max_align_t))
Returns a block of memory from the pool.
Definition pool.hpp:63
bool is_equal(const PoolResource &other) const noexcept
Returns true if this pool resource has the same backing storage as another.
Definition pool.hpp:93
PoolResource(const PoolResource &)=delete
Copy constructor.
size_t blocks_in_use() const noexcept
Returns the number of blocks from this pool resource that are in use.
Definition pool.hpp:102
PoolResource & operator=(PoolResource &&)=default
Move assignment operator.
PoolResource & operator=(const PoolResource &)=delete
Copy assignment operator.
void deallocate(void *p, size_t bytes, size_t alignment=alignof(std::max_align_t))
Gives a block of memory back to the pool.
Definition pool.hpp:81
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1198
reference emplace_back(ArgTypes &&... Args)
Definition SmallVector.h:939
reference back()
Definition SmallVector.h:310
auto ptr(T p) -> const void *
Converts p to const void* for pointer formatting.
Definition format.h:3925
Definition expression_graph.hpp:11
SLEIPNIR_DLLEXPORT PoolResource & global_pool_resource()
Returns a global pool memory resource.
PoolAllocator< T > global_pool_allocator()
Returns an allocator for a global pool memory resource.
Definition pool.hpp:193
Definition format.h:3963
#define SLEIPNIR_DLLEXPORT
Definition symbol_exports.hpp:34