WPILibC++ 2027.0.0-alpha-4
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/// This class implements a pool memory resource.
15///
16/// The pool allocates chunks of memory and splits them into blocks managed by a
17/// free list. Allocations return pointers from the free list, and deallocations
18/// return pointers to the free list.
20 public:
21 /// Constructs a default PoolResource.
22 ///
23 /// @param blocks_per_chunk Number of blocks per chunk of memory.
24 explicit PoolResource(size_t blocks_per_chunk)
25 : blocks_per_chunk{blocks_per_chunk} {}
26
27 /// Copy constructor.
28 PoolResource(const PoolResource&) = delete;
29
30 /// Copy assignment operator.
31 ///
32 /// @return This pool resource.
34
35 /// Move constructor.
37
38 /// Move assignment operator.
39 ///
40 /// @return This pool resource.
42
43 /// Returns a block of memory from the pool.
44 ///
45 /// @param bytes Number of bytes in the block.
46 /// @param alignment Alignment of the block (unused).
47 /// @return A block of memory from the pool.
48 [[nodiscard]]
49 void* allocate(size_t bytes, [[maybe_unused]] size_t alignment =
50 alignof(std::max_align_t)) {
51 if (m_free_list.empty()) {
52 add_chunk(bytes);
53 }
54
55 auto ptr = m_free_list.back();
56 m_free_list.pop_back();
57 return ptr;
58 }
59
60 /// Gives a block of memory back to the pool.
61 ///
62 /// @param p A pointer to the block of memory.
63 /// @param bytes Number of bytes in the block (unused).
64 /// @param alignment Alignment of the block (unused).
66 void* p, [[maybe_unused]] size_t bytes,
67 [[maybe_unused]] size_t alignment = alignof(std::max_align_t)) {
68 m_free_list.emplace_back(p);
69 }
70
71 /// Returns true if this pool resource has the same backing storage as
72 /// another.
73 ///
74 /// @param other The other pool resource.
75 /// @return True if this pool resource has the same backing storage as
76 /// another.
77 bool is_equal(const PoolResource& other) const noexcept {
78 return this == &other;
79 }
80
81 /// Returns the number of blocks from this pool resource that are in use.
82 ///
83 /// @return The number of blocks from this pool resource that are in use.
84 size_t blocks_in_use() const noexcept {
85 return m_buffer.size() * blocks_per_chunk - m_free_list.size();
86 }
87
88 private:
90 gch::small_vector<void*> m_free_list;
91 size_t blocks_per_chunk;
92
93 /// Adds a memory chunk to the pool, partitions it into blocks with the given
94 /// number of bytes, and appends pointers to them to the free list.
95 ///
96 /// @param bytes_per_block Number of bytes in the block.
97 void add_chunk(size_t bytes_per_block) {
98 m_buffer.emplace_back(new std::byte[bytes_per_block * blocks_per_chunk]);
99 for (int i = blocks_per_chunk - 1; i >= 0; --i) {
100 m_free_list.emplace_back(m_buffer.back().get() + bytes_per_block * i);
101 }
102 }
103};
104
105/// This class is an allocator for the pool resource.
106///
107/// @tparam T The type of object in the pool.
108template <typename T>
110 public:
111 /// The type of object in the pool.
112 using value_type = T;
113
114 /// Constructs a pool allocator with the given pool memory resource.
115 ///
116 /// @param r The pool resource.
117 explicit constexpr PoolAllocator(PoolResource* r) : m_memory_resource{r} {}
118
119 /// Copy constructor.
120 constexpr PoolAllocator(const PoolAllocator<T>&) = default;
121
122 /// Copy assignment operator.
123 ///
124 /// @return This pool allocator.
125 constexpr PoolAllocator<T>& operator=(const PoolAllocator<T>&) = default;
126
127 /// Returns a block of memory from the pool.
128 ///
129 /// @param n Number of bytes in the block.
130 /// @return A block of memory from the pool.
131 [[nodiscard]]
132 constexpr T* allocate(size_t n) {
133 return static_cast<T*>(m_memory_resource->allocate(n));
134 }
135
136 /// Gives a block of memory back to the pool.
137 ///
138 /// @param p A pointer to the block of memory.
139 /// @param n Number of bytes in the block.
140 constexpr void deallocate(T* p, size_t n) {
141 m_memory_resource->deallocate(p, n);
142 }
143
144 private:
145 PoolResource* m_memory_resource;
146};
147
148/// Returns a global pool memory resource.
150
151/// Returns an allocator for a global pool memory resource.
152///
153/// @tparam T The type of object in the pool.
154template <typename T>
158
159} // namespace slp
This class is an allocator for the pool resource.
Definition pool.hpp:109
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:117
constexpr void deallocate(T *p, size_t n)
Gives a block of memory back to the pool.
Definition pool.hpp:140
constexpr PoolAllocator< T > & operator=(const PoolAllocator< T > &)=default
Copy assignment operator.
T value_type
The type of object in the pool.
Definition pool.hpp:112
constexpr T * allocate(size_t n)
Returns a block of memory from the pool.
Definition pool.hpp:132
This class implements a pool memory resource.
Definition pool.hpp:19
PoolResource(size_t blocks_per_chunk)
Constructs a default PoolResource.
Definition pool.hpp:24
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:49
bool is_equal(const PoolResource &other) const noexcept
Returns true if this pool resource has the same backing storage as another.
Definition pool.hpp:77
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:84
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:65
T * p
Definition format.h:758
auto ptr(T p) -> const void *
Converts p to const void* for pointer formatting.
Definition format.h:3975
wpi::util::SmallVector< T > small_vector
Definition small_vector.hpp:10
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:155
Definition format.h:4013
#define SLEIPNIR_DLLEXPORT
Definition symbol_exports.hpp:34