Sleipnir C++ API
Loading...
Searching...
No Matches
pool.hpp
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <cstddef>
6#include <memory>
7
8#include <gch/small_vector.hpp>
9
10#include "sleipnir/util/symbol_exports.hpp"
11
12namespace slp {
13
21class SLEIPNIR_DLLEXPORT PoolResource {
22 public:
26 explicit PoolResource(size_t blocks_per_chunk);
27
30
32 PoolResource(const PoolResource&) = delete;
33
38
41
46
53 [[nodiscard]]
54 void* allocate(size_t bytes, size_t alignment = alignof(std::max_align_t));
55
64 void deallocate(void* p, size_t bytes,
65 size_t alignment = alignof(std::max_align_t));
66
73 bool is_equal(const PoolResource& other) const noexcept {
74 return this == &other;
75 }
76
80 size_t blocks_in_use() const noexcept {
81 size_t free_blocks = 0;
82 for (const auto& pool : m_pools) {
83 free_blocks += pool.free_list.size();
84 }
85 return m_chunks.size() * blocks_per_chunk - free_blocks;
86 }
87
88 private:
90 struct Pool {
92 size_t block_size;
93
95 gch::small_vector<void*> free_list;
96 };
97
99 struct Chunk {
101 std::byte* begin;
102
104 size_t pool_index;
105 };
106
107 gch::small_vector<std::unique_ptr<std::byte[]>> m_buffer;
108
110 gch::small_vector<Chunk> m_chunks;
111
112 gch::small_vector<Pool> m_pools;
113 size_t blocks_per_chunk;
114
119 Pool& get_pool(size_t block_size);
120
125 void add_chunk(Pool& pool);
126};
127
131template <typename T>
133 public:
135 using value_type = T;
136
140 explicit constexpr PoolAllocator(PoolResource* r) : m_memory_resource{r} {}
141
143 constexpr PoolAllocator(const PoolAllocator<T>&) = default;
144
148 constexpr PoolAllocator<T>& operator=(const PoolAllocator<T>&) = default;
149
154 [[nodiscard]]
155 constexpr T* allocate(size_t n) {
156 return static_cast<T*>(
157 m_memory_resource->allocate(n * sizeof(T), alignof(T)));
158 }
159
164 constexpr void deallocate(T* p, size_t n) {
165 m_memory_resource->deallocate(p, n * sizeof(T), alignof(T));
166 }
167
168 private:
169 PoolResource* m_memory_resource;
170};
171
173SLEIPNIR_DLLEXPORT PoolResource& global_pool_resource();
174
178template <typename T>
179PoolAllocator<T> global_pool_allocator() {
180 return PoolAllocator<T>{&global_pool_resource()};
181}
182
183} // namespace slp
Definition pool.hpp:132
constexpr PoolAllocator(const PoolAllocator< T > &)=default
Copy constructor.
constexpr PoolAllocator(PoolResource *r)
Definition pool.hpp:140
constexpr void deallocate(T *p, size_t n)
Definition pool.hpp:164
constexpr PoolAllocator< T > & operator=(const PoolAllocator< T > &)=default
T value_type
The type of object in the pool.
Definition pool.hpp:135
constexpr T * allocate(size_t n)
Definition pool.hpp:155
Definition pool.hpp:21
PoolResource(size_t blocks_per_chunk)
void * allocate(size_t bytes, size_t alignment=alignof(std::max_align_t))
bool is_equal(const PoolResource &other) const noexcept
Definition pool.hpp:73
PoolResource & operator=(PoolResource &&)
PoolResource(const PoolResource &)=delete
Copy constructor.
size_t blocks_in_use() const noexcept
Definition pool.hpp:80
PoolResource(PoolResource &&)
Move constructor.
PoolResource & operator=(const PoolResource &)=delete
void deallocate(void *p, size_t bytes, size_t alignment=alignof(std::max_align_t))
~PoolResource()
Destructor.