Sleipnir C++ API
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
10
11namespace sleipnir {
12
21 public:
27 explicit PoolResource(size_t blocksPerChunk)
28 : blocksPerChunk{blocksPerChunk} {}
29
30 PoolResource(const PoolResource&) = delete;
34
41 [[nodiscard]]
42 void* allocate(size_t bytes, [[maybe_unused]] size_t alignment =
43 alignof(std::max_align_t)) {
44 if (m_freeList.empty()) {
45 AddChunk(bytes);
46 }
47
48 auto ptr = m_freeList.back();
49 m_freeList.pop_back();
50 return ptr;
51 }
52
61 void* p, [[maybe_unused]] size_t bytes,
62 [[maybe_unused]] size_t alignment = alignof(std::max_align_t)) {
63 m_freeList.emplace_back(p);
64 }
65
69 bool is_equal(const PoolResource& other) const noexcept {
70 return this == &other;
71 }
72
76 size_t blocks_in_use() const noexcept {
77 return m_buffer.size() * blocksPerChunk - m_freeList.size();
78 }
79
80 private:
82 small_vector<void*> m_freeList;
83 size_t blocksPerChunk;
84
91 void AddChunk(size_t bytesPerBlock) {
92 m_buffer.emplace_back(new std::byte[bytesPerBlock * blocksPerChunk]);
93 for (int i = blocksPerChunk - 1; i >= 0; --i) {
94 m_freeList.emplace_back(m_buffer.back().get() + bytesPerBlock * i);
95 }
96 }
97};
98
104template <typename T>
106 public:
110 using value_type = T;
111
117 explicit constexpr PoolAllocator(PoolResource* r) : m_memoryResource{r} {}
118
119 constexpr PoolAllocator(const PoolAllocator<T>& other) = default;
120 constexpr PoolAllocator<T>& operator=(const PoolAllocator<T>&) = default;
121
127 [[nodiscard]]
128 constexpr T* allocate(size_t n) {
129 return static_cast<T*>(m_memoryResource->allocate(n));
130 }
131
138 constexpr void deallocate(T* p, size_t n) {
139 m_memoryResource->deallocate(p, n);
140 }
141
142 private:
143 PoolResource* m_memoryResource;
144};
145
150
156template <typename T>
160
161} // namespace sleipnir
#define SLEIPNIR_DLLEXPORT
Definition SymbolExports.hpp:34
Definition Pool.hpp:105
constexpr PoolAllocator< T > & operator=(const PoolAllocator< T > &)=default
constexpr T * allocate(size_t n)
Definition Pool.hpp:128
constexpr PoolAllocator(const PoolAllocator< T > &other)=default
constexpr PoolAllocator(PoolResource *r)
Definition Pool.hpp:117
constexpr void deallocate(T *p, size_t n)
Definition Pool.hpp:138
T value_type
Definition Pool.hpp:110
Definition Pool.hpp:20
void * allocate(size_t bytes, size_t alignment=alignof(std::max_align_t))
Definition Pool.hpp:42
PoolResource & operator=(const PoolResource &)=delete
PoolResource(PoolResource &&)=default
PoolResource & operator=(PoolResource &&)=default
PoolResource(size_t blocksPerChunk)
Definition Pool.hpp:27
size_t blocks_in_use() const noexcept
Definition Pool.hpp:76
void deallocate(void *p, size_t bytes, size_t alignment=alignof(std::max_align_t))
Definition Pool.hpp:60
bool is_equal(const PoolResource &other) const noexcept
Definition Pool.hpp:69
PoolResource(const PoolResource &)=delete
Definition small_vector.hpp:3616
::value &&MoveInsertable constexpr reference emplace_back(Args &&... args)
Definition small_vector.hpp:4071
constexpr reference back()
Definition small_vector.hpp:3952
Definition Expression.hpp:18
IntrusiveSharedPtr< T > AllocateIntrusiveShared(Alloc alloc, Args &&... args)
Definition IntrusiveSharedPtr.hpp:275
PoolAllocator< T > GlobalPoolAllocator()
Definition Pool.hpp:157
SLEIPNIR_DLLEXPORT PoolResource & GlobalPoolResource()