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:
28 explicit PoolResource(size_t blocks_per_chunk)
29 : blocks_per_chunk{blocks_per_chunk} {}
30
34 PoolResource(const PoolResource&) = delete;
35
42
47
54
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
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
93 bool is_equal(const PoolResource& other) const noexcept {
94 return this == &other;
95 }
96
102 size_t blocks_in_use() const noexcept {
103 return m_buffer.size() * blocks_per_chunk - m_free_list.size();
104 }
105
106 private:
107 gch::small_vector<std::unique_ptr<std::byte[]>> m_buffer;
108 gch::small_vector<void*> m_free_list;
109 size_t blocks_per_chunk;
110
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
130template <typename T>
132 public:
136 using value_type = T;
137
143 explicit constexpr PoolAllocator(PoolResource* r) : m_memory_resource{r} {}
144
148 constexpr PoolAllocator(const PoolAllocator<T>&) = default;
149
155 constexpr PoolAllocator<T>& operator=(const PoolAllocator<T>&) = default;
156
163 [[nodiscard]]
164 constexpr T* allocate(size_t n) {
165 return static_cast<T*>(m_memory_resource->allocate(n));
166 }
167
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
185SLEIPNIR_DLLEXPORT PoolResource& global_pool_resource();
186
192template <typename T>
193PoolAllocator<T> global_pool_allocator() {
194 return PoolAllocator<T>{&global_pool_resource()};
195}
196
197} // namespace slp
Definition pool.hpp:131
constexpr PoolAllocator(const PoolAllocator< T > &)=default
constexpr PoolAllocator(PoolResource *r)
Definition pool.hpp:143
constexpr void deallocate(T *p, size_t n)
Definition pool.hpp:174
constexpr PoolAllocator< T > & operator=(const PoolAllocator< T > &)=default
T value_type
Definition pool.hpp:136
constexpr T * allocate(size_t n)
Definition pool.hpp:164
Definition pool.hpp:21
PoolResource(size_t blocks_per_chunk)
Definition pool.hpp:28
PoolResource(PoolResource &&)=default
void * allocate(size_t bytes, size_t alignment=alignof(std::max_align_t))
Definition pool.hpp:63
bool is_equal(const PoolResource &other) const noexcept
Definition pool.hpp:93
PoolResource(const PoolResource &)=delete
size_t blocks_in_use() const noexcept
Definition pool.hpp:102
PoolResource & operator=(PoolResource &&)=default
PoolResource & operator=(const PoolResource &)=delete
void deallocate(void *p, size_t bytes, size_t alignment=alignof(std::max_align_t))
Definition pool.hpp:81