pbr-cpp-memory-pool 1.2.0
Fixed-block-size O(1) memory pool — C++17 with an ANSI C public surface
Loading...
Searching...
No Matches
pool_memory_resource.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 Daniel Polo
3
4#ifndef IT_D4NP_MEMORYPOOL_POOL_MEMORY_RESOURCE_HPP_
5#define IT_D4NP_MEMORYPOOL_POOL_MEMORY_RESOURCE_HPP_
6
40
41#if defined(__has_include)
42#if __has_include(<memory_resource>)
43#include <memory_resource>
44#endif
45#endif
46
47// Availability gate (see the @file note). This drives `#if` here, in the
48// dedicated test, and in consumer code, so it must be a preprocessor macro
49// rather than a constexpr constant — hence the macro-usage suppression.
50/* NOLINTBEGIN(cppcoreguidelines-macro-usage) */
51#if defined(__cpp_lib_memory_resource) && __cpp_lib_memory_resource >= 201603L
52#define PBR_MEMORY_POOL_HAS_PMR 1
53#else
54#define PBR_MEMORY_POOL_HAS_PMR 0
55#endif
56/* NOLINTEND(cppcoreguidelines-macro-usage) */
57
58#if PBR_MEMORY_POOL_HAS_PMR
59
60#include <cstddef>
61#include <new>
62
63namespace it::d4np::memorypool {
64
95class PoolMemoryResource : public std::pmr::memory_resource {
96public:
106 explicit PoolMemoryResource(Pool& pool,
107 std::pmr::memory_resource* upstream = std::pmr::get_default_resource()) noexcept
108 : pool_(&pool), upstream_(upstream) {}
109
111 [[nodiscard]] Pool& pool() const noexcept {
112 return *pool_;
113 }
114
116 [[nodiscard]] std::pmr::memory_resource* upstream_resource() const noexcept {
117 return upstream_;
118 }
119
120private:
128 // The `(bytes, alignment)` pair is the std::pmr::memory_resource override
129 // signature and cannot be restructured into a parameter struct.
130 // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
131 void* do_allocate(std::size_t bytes, std::size_t alignment) override {
132 if (routes_to_pool(bytes, alignment)) {
133 void* const block = pool_->try_allocate();
134 if (block == nullptr) {
135 throw std::bad_alloc{};
136 }
137 return block;
138 }
139 return upstream_->allocate(bytes, alignment);
140 }
141
147 // Fixed override signature — see ::do_allocate.
148 // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
149 void do_deallocate(void* ptr, std::size_t bytes, std::size_t alignment) override {
150 if (routes_to_pool(bytes, alignment)) {
151 pool_->deallocate(ptr);
152 return;
153 }
154 upstream_->deallocate(ptr, bytes, alignment);
155 }
156
158 [[nodiscard]] bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
159 const auto* const rhs = dynamic_cast<const PoolMemoryResource*>(&other);
160 return rhs != nullptr && rhs->pool_ == pool_ && rhs->upstream_ == upstream_;
161 }
162
164 // Mirrors the std::pmr `(bytes, alignment)` shape — see ::do_allocate.
165 // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
166 [[nodiscard]] bool routes_to_pool(std::size_t bytes, std::size_t alignment) const noexcept {
167 return bytes <= pool_->block_size() && alignment <= alignof(std::max_align_t);
168 }
169
170 Pool* pool_;
171 std::pmr::memory_resource* upstream_;
172};
173
174} // namespace it::d4np::memorypool
175
176#endif // PBR_MEMORY_POOL_HAS_PMR
177
178#endif // IT_D4NP_MEMORYPOOL_POOL_MEMORY_RESOURCE_HPP_
C++17 RAII wrapper around the C memory pool.