WPILibC++ 2025.1.1
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
8#include <wpi/SmallVector.h>
9
11
12namespace sleipnir {
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 blocksPerChunk Number of blocks per chunk of memory.
27 */
28 explicit PoolResource(size_t blocksPerChunk)
29 : blocksPerChunk{blocksPerChunk} {}
30
31 PoolResource(const PoolResource&) = delete;
35
36 /**
37 * Returns a block of memory from the pool.
38 *
39 * @param bytes Number of bytes in the block.
40 * @param alignment Alignment of the block (unused).
41 */
42 [[nodiscard]]
43 void* allocate(size_t bytes, [[maybe_unused]] size_t alignment =
44 alignof(std::max_align_t)) {
45 if (m_freeList.empty()) {
46 AddChunk(bytes);
47 }
48
49 auto ptr = m_freeList.back();
50 m_freeList.pop_back();
51 return ptr;
52 }
53
54 /**
55 * Gives a block of memory back to the pool.
56 *
57 * @param p A pointer to the block of memory.
58 * @param bytes Number of bytes in the block (unused).
59 * @param alignment Alignment of the block (unused).
60 */
62 void* p, [[maybe_unused]] size_t bytes,
63 [[maybe_unused]] size_t alignment = alignof(std::max_align_t)) {
64 m_freeList.emplace_back(p);
65 }
66
67 /**
68 * Returns true if this pool resource has the same backing storage as another.
69 */
70 bool is_equal(const PoolResource& other) const noexcept {
71 return this == &other;
72 }
73
74 /**
75 * Returns the number of blocks from this pool resource that are in use.
76 */
77 size_t blocks_in_use() const noexcept {
78 return m_buffer.size() * blocksPerChunk - m_freeList.size();
79 }
80
81 private:
83 wpi::SmallVector<void*> m_freeList;
84 size_t blocksPerChunk;
85
86 /**
87 * Adds a memory chunk to the pool, partitions it into blocks with the given
88 * number of bytes, and appends pointers to them to the free list.
89 *
90 * @param bytesPerBlock Number of bytes in the block.
91 */
92 void AddChunk(size_t bytesPerBlock) {
93 m_buffer.emplace_back(new std::byte[bytesPerBlock * blocksPerChunk]);
94 for (int i = blocksPerChunk - 1; i >= 0; --i) {
95 m_freeList.emplace_back(m_buffer.back().get() + bytesPerBlock * i);
96 }
97 }
98};
99
100/**
101 * This class is an allocator for the pool resource.
102 *
103 * @tparam T The type of object in the pool.
104 */
105template <typename T>
107 public:
108 /**
109 * The type of object in the pool.
110 */
111 using value_type = T;
112
113 /**
114 * Constructs a pool allocator with the given pool memory resource.
115 *
116 * @param r The pool resource.
117 */
118 explicit constexpr PoolAllocator(PoolResource* r) : m_memoryResource{r} {}
119
120 constexpr PoolAllocator(const PoolAllocator<T>& other) = default;
121 constexpr PoolAllocator<T>& operator=(const PoolAllocator<T>&) = default;
122
123 /**
124 * Returns a block of memory from the pool.
125 *
126 * @param n Number of bytes in the block.
127 */
128 [[nodiscard]]
129 constexpr T* allocate(size_t n) {
130 return static_cast<T*>(m_memoryResource->allocate(n));
131 }
132
133 /**
134 * Gives a block of memory back to the pool.
135 *
136 * @param p A pointer to the block of memory.
137 * @param n Number of bytes in the block.
138 */
139 constexpr void deallocate(T* p, size_t n) {
140 m_memoryResource->deallocate(p, n);
141 }
142
143 private:
144 PoolResource* m_memoryResource;
145};
146
147/**
148 * Returns a global pool memory resource.
149 */
151
152/**
153 * Returns an allocator for a global pool memory resource.
154 *
155 * @tparam T The type of object in the pool.
156 */
157template <typename T>
161
162} // namespace sleipnir
This file defines the SmallVector class.
#define SLEIPNIR_DLLEXPORT
Definition SymbolExports.hpp:34
This class is an allocator for the pool resource.
Definition Pool.hpp:106
constexpr PoolAllocator< T > & operator=(const PoolAllocator< T > &)=default
constexpr T * allocate(size_t n)
Returns a block of memory from the pool.
Definition Pool.hpp:129
constexpr PoolAllocator(const PoolAllocator< T > &other)=default
constexpr PoolAllocator(PoolResource *r)
Constructs a pool allocator with the given pool memory resource.
Definition Pool.hpp:118
constexpr void deallocate(T *p, size_t n)
Gives a block of memory back to the pool.
Definition Pool.hpp:139
T value_type
The type of object in the pool.
Definition Pool.hpp:111
This class implements a pool memory resource.
Definition Pool.hpp:21
void * allocate(size_t bytes, size_t alignment=alignof(std::max_align_t))
Returns a block of memory from the pool.
Definition Pool.hpp:43
PoolResource & operator=(const PoolResource &)=delete
PoolResource(PoolResource &&)=default
PoolResource & operator=(PoolResource &&)=default
PoolResource(size_t blocksPerChunk)
Constructs a default PoolResource.
Definition Pool.hpp:28
size_t blocks_in_use() const noexcept
Returns the number of blocks from this pool resource that are in use.
Definition Pool.hpp:77
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:61
bool is_equal(const PoolResource &other) const noexcept
Returns true if this pool resource has the same backing storage as another.
Definition Pool.hpp:70
PoolResource(const PoolResource &)=delete
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1212
reference emplace_back(ArgTypes &&... Args)
Definition SmallVector.h:953
reference back()
Definition SmallVector.h:324
auto ptr(T p) -> const void *
Converts p to const void* for pointer formatting.
Definition format.h:3821
Definition Hessian.hpp:18
PoolAllocator< T > GlobalPoolAllocator()
Returns an allocator for a global pool memory resource.
Definition Pool.hpp:158
SLEIPNIR_DLLEXPORT PoolResource & GlobalPoolResource()
Returns a global pool memory resource.
Definition format.h:3858