libhatchet
Loading...
Searching...
No Matches
hxfree_list.hpp
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: © 2017-2026 Adrian Johnston.
3// SPDX-License-Identifier: MIT
4// This file is licensed under the MIT license found in the LICENSE.md file.
5
8
9#include "libhatchet.h"
10
11// HX_USE_MACROS_WITH_MODULE allows including macros alongside the module.
12#if HX_USE_MACROS_WITH_MODULE
13#error Header does not provide macros alone.
14#endif
15
16#include "hxallocator.hpp"
17#include "hxptr.hpp"
18
19HX_NS_BEGIN_
20
21#if HX_CPLUSPLUS >= 202002L
23template<typename T_>
24concept hxfree_list_concept_ = requires(T_& x_) {
25 sizeof(T_);
26 x_.~T_();
27};
29#else
30#define hxfree_list_concept_ typename
31#endif
32
43template<hxfree_list_concept_ T_, hxsize_t capacity_=hxallocator_dynamic_capacity>
44class hxfree_list : private hxallocator<T_, capacity_> {
45public:
48 class deleter_t {
49 public:
52 explicit deleter_t(hxfree_list& owner_) noexcept : m_owner_(&owner_) { }
53
57 void operator()(T_* ptr_) noexcept { if(ptr_ != hxnull) { m_owner_->release(ptr_); } }
58
60 operator bool(void) const { return true; }
61
62 private:
63 hxfree_list* m_owner_;
64 };
65
67 using value_t = T_;
68
71
74 explicit hxfree_list(void) noexcept;
75
79 hxfree_list(hxfree_list&& x_) noexcept;
80
82 ~hxfree_list(void) noexcept;
83
87 template<typename... args_t_>
88 hxattr_nodiscard hxptr<T_, deleter_t> allocate(args_t_&&... args_) noexcept;
89
92
94 deleter_t deleter(void) noexcept { return deleter_t(*this); }
95
97 hxattr_nodiscard bool empty(void) const { return m_free_head_ == hxnull; }
98
101 hxattr_nodiscard bool is_allocator(const T_* ptr_) const noexcept;
102
105 template<typename deleter_t_>
106 hxattr_nodiscard bool is_allocator(const hxptr<T_, deleter_t_>& ptr_) const noexcept;
107
109 hxattr_nodiscard hxsize_t max_size(void) const { return this->capacity(); }
110
113 void release(T_* ptr_) noexcept hxattr_nonnull(2);
114
117 template<typename deleter_t_>
118 void release(hxptr<T_, deleter_t_>&& ptr_) noexcept;
119
126 void reserve(hxsize_t size_,
129
131 hxattr_nodiscard hxsize_t size(void) const { return m_size_; }
132
136 template<typename... args_t_>
138
139private:
140 hxfree_list(const hxfree_list&) = delete;
141 void operator=(const hxfree_list&) = delete;
142
143 union slot_ {
144 slot_* next_;
145 T_ value_;
146 slot_(void) { }
147 ~slot_(void) { }
148 };
149
150 void enqueue_all_(hxsize_t count_) noexcept;
151
152 slot_* m_free_head_;
153 hxsize_t m_size_;
154};
155
156#include "detail/hxfree_list.inl"
157HX_NS_END_
hxallocator(void)
Does not allocate until reserve_storage is called.
Definition hxallocator.hpp:78
deleter_t - A callable deleter that releases a T back to this hxfree_list.
Definition hxfree_list.hpp:48
void operator()(T *ptr_) noexcept
Destructs *p and returns its slot to the owning hxfree_list.
Definition hxfree_list.hpp:57
deleter_t(hxfree_list &owner_) noexcept
Constructs a deleter associated with owner.
Definition hxfree_list.hpp:52
hxsize_t capacity(void) const
Returns the capacity of the pool or 0 if unallocated.
hxfree_list(void) noexcept
When capacity is hxallocator_dynamic_capacity storage must be allocated via reserve before use.
T value_t
value_t - Publishes the value type.
Definition hxfree_list.hpp:67
bool empty(void) const
Returns true if no unallocated T remains.
Definition hxfree_list.hpp:97
hxptr< T, deleter_t > try_allocate(args_t_ &&... args_) noexcept
Constructs a T in a free slot and returns an hxptr owning it.
hxsize_t size(void) const
Returns the number of unallocated T available.
Definition hxfree_list.hpp:131
bool is_allocator(const hxptr< T, deleter_t_ > &ptr_) const noexcept
Returns true if ptr points into this free list's storage.
hxptr< T, deleter_t > ptr_t
ptr_t - Publish the hxptr type.
Definition hxfree_list.hpp:70
hxsize_t max_size(void) const
Returns the capacity of the pool.
Definition hxfree_list.hpp:109
deleter_t deleter(void) noexcept
Returns a deleter_t associated with this hxfree_list.
Definition hxfree_list.hpp:94
void reserve(hxsize_t size_, hxsystem_allocator_t allocator_=hxsystem_allocator_current, hxalignment_t alignment_=hxalignment) noexcept
Allocates storage for size slots and enqueues them.
void release(hxptr< T, deleter_t_ > &&ptr_) noexcept
Destructs *p and returns its slot to the free list.
hxptr< T, deleter_t > allocate(args_t_ &&... args_) noexcept
Constructs a T in a free slot and returns an hxptr owning it.
hxfree_list(hxfree_list &&x_) noexcept
Move constructs from a temporary hxfree_list.
bool is_allocator(const T *ptr_) const noexcept
Returns true if p points into this free list's storage.
~hxfree_list(void) noexcept
All T must have been released before destruction.
void release(T *ptr_) noexcept
hxptr<T, deleter_t> - A unique owning pointer.
Definition hxptr.hpp:30
Similar to std::allocator.
@ hxsystem_allocator_current
hxsystem_allocator_current - Use current allocation scope.
Definition hxmemory_manager.h:80
HX_C_BEGIN_ typedef unsigned int hxalignment_t
hxalignment_t - A positive integer power of 2 for aligning allocations.
Definition hxmemory_manager.h:62
int hxsystem_allocator_t
hxsystem_allocator_t - This is extendable by the application.
Definition hxmemory_manager.h:75
hxattr_noexcept hxsystem_allocator_t allocator_
Definition hxmemory_manager.h:120
hxattr_noexcept hxsystem_allocator_t hxalignment_t alignment_
Definition hxmemory_manager.h:121
constexpr hxalignment_t hxalignment
hxalignment - The default alignment.
Definition hxmemory_manager.h:67
A unique owning pointer.
#define hxattr_nonnull(...)
hxattr_nonnull - Indicates that a function has args that should not be null.
Definition hxsettings.h:106
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:98
Provides core macros, memory management and feature detection.
#define hxnull
hxnull - The null pointer value for a given pointer type represented by the numeric literal 0.
Definition libhatchet.h:280
ptrdiff_t hxsize_t
hxsize_t - A signed size type, same as ssize_t or ptrdiff_t.
Definition libhatchet.h:207