pbr-cpp-memory-pool 1.1.2
Fixed-block-size O(1) memory pool — C++17 with an ANSI C public surface
Loading...
Searching...
No Matches
free_list_iterator.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_FREE_LIST_ITERATOR_HPP_
5#define IT_D4NP_MEMORYPOOL_FREE_LIST_ITERATOR_HPP_
6
27
28#if PBR_MEMORY_POOL_DIAGNOSTICS
29
31
32#include <cstddef>
33#include <iterator>
34
35namespace it::d4np::memorypool {
36
50public:
51 using iterator_category = std::forward_iterator_tag;
52 using value_type = const void*;
53 using difference_type = std::ptrdiff_t;
54 using pointer = const value_type*;
55 using reference = const value_type&;
56
58 FreeListIterator() noexcept = default;
59
61 FreeListIterator(const memory_pool_t* pool, const void* current) noexcept : pool_(pool), current_(current) {}
62
64 [[nodiscard]] reference operator*() const noexcept {
65 return current_;
66 }
67
69 [[nodiscard]] pointer operator->() const noexcept {
70 return &current_;
71 }
72
75 current_ = ::memory_pool_debug_free_list_next(pool_, current_);
76 return *this;
77 }
78
84 // NOLINTNEXTLINE(readability-const-return-type)
85 const FreeListIterator operator++(int) noexcept {
86 const FreeListIterator previous = *this;
87 ++(*this);
88 return previous;
89 }
90
92 [[nodiscard]] bool operator==(const FreeListIterator& rhs) const noexcept {
93 return current_ == rhs.current_;
94 }
95
97 [[nodiscard]] bool operator!=(const FreeListIterator& rhs) const noexcept {
98 return !(*this == rhs);
99 }
100
101private:
102 const memory_pool_t* pool_ = nullptr;
103 const void* current_ = nullptr;
104};
105
122public:
124 explicit FreeListView(const memory_pool_t* pool) noexcept : pool_(pool) {}
125
127 explicit FreeListView(Pool& pool) noexcept : pool_(pool.native_handle()) {}
128
130 [[nodiscard]] FreeListIterator begin() const noexcept {
131 return {pool_, ::memory_pool_debug_free_list_head(pool_)};
132 }
133
140 [[nodiscard]] FreeListIterator end() const noexcept {
141 return {pool_, nullptr};
142 }
143
144private:
145 const memory_pool_t* pool_;
146};
147
148} // namespace it::d4np::memorypool
149
150#endif // PBR_MEMORY_POOL_DIAGNOSTICS
151
152#endif // IT_D4NP_MEMORYPOOL_FREE_LIST_ITERATOR_HPP_
Read-only LegacyForwardIterator over the free-list slots.
const FreeListIterator operator++(int) noexcept
Post-increment; returns the pre-advance value.
FreeListIterator() noexcept=default
Construct the end sentinel.
bool operator!=(const FreeListIterator &rhs) const noexcept
Negation of operator==.
FreeListIterator & operator++() noexcept
Advance to the next free slot (ADR-0019 §2).
bool operator==(const FreeListIterator &rhs) const noexcept
Two iterators are equal iff they sit on the same slot (end == end).
Lightweight read-only range over a pool's free list (ADR-0019 §3).
FreeListIterator begin() const noexcept
FreeListIterator end() const noexcept
FreeListView(const memory_pool_t *pool) noexcept
View the free list of pool (a C handle), or an empty range if NULL.
FreeListView(Pool &pool) noexcept
View the free list of pool — ergonomic overload for the C++ wrapper.
Owning, non-copyable, move-only wrapper around a memory_pool_t*.
Public C API for the pbr-cpp-memory-pool fixed-block-size allocator.
struct memory_pool memory_pool_t
Opaque handle to a memory pool instance.
Definition memory_pool.h:82
const void * memory_pool_debug_free_list_head(const memory_pool_t *pool)
Diagnostics — return the head of pool's implicit free list (ADR-0019 §2).
const void * memory_pool_debug_free_list_next(const memory_pool_t *pool, const void *current)
Diagnostics — given a free slot current obtained from memory_pool_debug_free_list_head or a previous ...
C++17 RAII wrapper around the C memory pool.