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
22
25template<typename T_>
26concept hxfree_list_concept_ = requires(T_& x_) {
27 sizeof(T_);
28 { x_.~T_() };
29};
30#else
31#define hxfree_list_concept_ typename
32#endif
33
44template<hxfree_list_concept_ T_, hxsize_t capacity_=hxallocator_dynamic_capacity>
45class hxfree_list : private hxallocator<T_, capacity_> {
46public:
49 class deleter_t {
50 public:
53 explicit deleter_t(hxfree_list& owner_) noexcept : m_owner_(&owner_) { }
54
58 void operator()(T_* p_) noexcept { if(p_ != hxnull) { m_owner_->release(p_); } }
59
61 operator bool(void) const { return true; }
62
63 private:
64 hxfree_list* m_owner_;
65 };
66
68 using value_t = T_;
69
72
75 explicit hxfree_list(void) noexcept;
76
80 hxfree_list(hxfree_list&& x_) noexcept;
81
83 ~hxfree_list(void) noexcept;
84
88 template<typename... args_t_>
89 hxattr_nodiscard hxptr<T_, deleter_t> allocate(args_t_&&... args_) noexcept;
90
93
95 deleter_t deleter(void) noexcept { return deleter_t(*this); }
96
98 hxattr_nodiscard bool empty(void) const { return m_free_head_ == hxnull; }
99
102 hxattr_nodiscard bool is_allocator(const T_* p_) const noexcept;
103
106 template<typename deleter_t_>
107 hxattr_nodiscard bool is_allocator(const hxptr<T_, deleter_t_>& ptr_) const noexcept;
108
110 hxattr_nodiscard hxsize_t max_size(void) const { return this->capacity(); }
111
114 void release(T_* p_) noexcept hxattr_nonnull(2);
115
118 template<typename deleter_t_>
119 void release(hxptr<T_, deleter_t_>&& ptr_) noexcept;
120
128 void reserve(hxsize_t size_,
130 hxalignment_t alignment_=hxalignment) noexcept;
131
133 hxattr_nodiscard hxsize_t size(void) const { return m_size_; }
134
138 template<typename... args_t_>
140
141private:
142 hxfree_list(const hxfree_list&) = delete;
143 void operator=(const hxfree_list&) = delete;
144
145 union slot_ {
146 slot_* next_;
147 T_ value_;
148 slot_(void) { }
149 ~slot_(void) { }
150 };
151
152 void enqueue_all_(hxsize_t count_) noexcept;
153
154 slot_* m_free_head_;
155 hxsize_t m_size_;
156};
157
158#include "detail/hxfree_list.inl"
159HX_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:49
deleter_t(hxfree_list &owner_) noexcept
Constructs a deleter associated with owner.
Definition hxfree_list.hpp:53
void operator()(T *p_) noexcept
Destructs *p and returns its slot to the owning hxfree_list.
Definition hxfree_list.hpp:58
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:68
bool empty(void) const
Returns true if no unallocated T remains.
Definition hxfree_list.hpp:98
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:133
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:71
hxsize_t max_size(void) const
Returns the capacity of the pool.
Definition hxfree_list.hpp:110
deleter_t deleter(void) noexcept
Returns a deleter_t associated with this hxfree_list.
Definition hxfree_list.hpp:95
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 when capacity is hxallocator_dynamic_capacity.
void release(hxptr< T, deleter_t_ > &&ptr_) noexcept
Destructs the object owned by ptr 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.
void release(T *p_) noexcept
Destructs *p and returns its slot to the free list.
hxfree_list(hxfree_list &&x_) noexcept
Move constructs from a temporary hxfree_list.
~hxfree_list(void) noexcept
All T must have been released before destruction.
bool is_allocator(const T *p_) const noexcept
Returns true if p points into this free list's storage.
hxptr<T, deleter_t> - A unique owning pointer.
Definition hxptr.hpp:30
hxfree_list_concept - Concept smoke testing the hxfree_list element.
Definition hxfree_list.hpp:26
Similar to std::allocator.
@ hxsystem_allocator_current
hxsystem_allocator_current - Use current allocation scope.
Definition hxmemory_manager.h:82
int hxsystem_allocator_t
hxsystem_allocator_t - This is extendable by the application.
Definition hxmemory_manager.h:77
unsigned int hxalignment_t
hxalignment_t - A positive integer power of 2 for aligning allocations.
Definition hxmemory_manager.h:64
constexpr hxalignment_t hxalignment
hxalignment - The default alignment.
Definition hxmemory_manager.h:69
A unique owning pointer.
#define hxattr_nonnull(...)
hxattr_nonnull - Indicates that a function has args that should not be null.
Definition hxsettings.h:97
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:89
Provides core macros, memory management and feature detection.
#define hxnull
hxnull - The null pointer value for a given pointer type represented by the numeric constant 0.
Definition libhatchet.h:277
ptrdiff_t hxsize_t
hxsize_t - A signed size type, same as ssize_t or ptrdiff_t.
Definition libhatchet.h:209