libhatchet
Loading...
Searching...
No Matches
hxallocator.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
16HX_NS_BEGIN_
17
21
24template<typename T_, hxsize_t fixed_capacity_>
26public:
28 using value_t = T_;
29
30 // Template specialization below should have been selected.
31 static_assert(fixed_capacity_ > hxallocator_dynamic_capacity, "Fixed capacity must be > 0");
32
36#if (HX_HARDENING_MODE) == HX_HARDENING_MODE_DEBUG
37 ::memset(m_data_, 0xab, sizeof m_data_);
38#endif
39 }
40
42 hxattr_nodiscard hxsize_t capacity(void) const { return fixed_capacity_; }
43
45 const T_* data(void) const { return reinterpret_cast<const T_*>(m_data_); }
46
48 T_* data(void) { return reinterpret_cast<T_*>(m_data_); }
49
57 hxalignment_t alignment_=hxalignment) {
58 (void)allocator_; (void)alignment_;
59 hxassert_always(size_ == fixed_capacity_, "reallocation_disallowed");
60 }
61
62private:
63 hxallocator(const hxallocator&) = delete;
64 void operator=(const hxallocator&) = delete;
65
66 alignas(T_) char m_data_[fixed_capacity_ * hxsizeof<T_>()];
67};
68
71template<typename T_>
73public:
75 using value_t = T_;
76
79 m_data_ = hxnull;
80 m_capacity_ = 0;
81 }
82
85 if(m_data_) {
86 m_capacity_ = 0;
87 hxfree(m_data_);
88 m_data_ = hxnull;
89 }
90 }
91
93 hxattr_nodiscard hxsize_t capacity(void) const { return m_capacity_; }
94
96 const T_* data(void) const { return m_data_; }
97
99 T_* data(void) { return m_data_; }
100
107 hxalignment_t alignment_=hxalignment) {
108 if(size_ == m_capacity_) {
109 return;
110 }
111 hxassert_always(m_capacity_ == 0, "reallocation_disallowed");
112 m_data_ = static_cast<T_*>(hxmalloc_ext(sizeof(T_) * static_cast<size_t>(size_), allocator_, alignment_));
113 m_capacity_ = size_;
114 }
115
116private:
117 hxallocator(const hxallocator&) = delete;
118 void operator=(const hxallocator&) = delete;
119
120 hxsize_t m_capacity_;
121 T_* m_data_;
122};
123
124HX_NS_END_
void reserve_storage(hxsize_t size_, hxsystem_allocator_t allocator_=hxsystem_allocator_current, hxalignment_t alignment_=hxalignment)
Capacity is set by first call to reserve_storage and may not be modified.
Definition hxallocator.hpp:105
T * data(void)
Returns a pointer to a potentially uninitialized array of T.
Definition hxallocator.hpp:99
const T * data(void) const
Returns a pointer to a const and potentially uninitialized array of T.
Definition hxallocator.hpp:96
T value_t
value_t - Publishes the value type.
Definition hxallocator.hpp:75
hxallocator(void)
Does not allocate until reserve_storage is called.
Definition hxallocator.hpp:78
hxsize_t capacity(void) const
Returns the number of elements of T allocated.
Definition hxallocator.hpp:93
~hxallocator(void)
Calls hxfree with any allocated memory.
Definition hxallocator.hpp:84
hxallocator<1+> - Provides static allocation when capacity is greater than zero.
Definition hxallocator.hpp:25
hxallocator(void)
Initializes memory to 0xab when HX_HARDENING_MODE == HX_HARDENING_MODE_DEBUG.
Definition hxallocator.hpp:35
const T * data(void) const
Returns a pointer to a const and potentially uninitialized array of T.
Definition hxallocator.hpp:45
T value_t
value_t - Publishes the value type.
Definition hxallocator.hpp:28
void reserve_storage(hxsize_t size_, hxsystem_allocator_t allocator_=hxsystem_allocator_current, hxalignment_t alignment_=hxalignment)
Ensures capacity.
Definition hxallocator.hpp:55
hxsize_t capacity(void) const
Returns the number of elements of T allocated.
Definition hxallocator.hpp:42
T * data(void)
Returns a pointer to a potentially uninitialized array of T.
Definition hxallocator.hpp:48
HX_NS_BEGIN_ constexpr hxsize_t hxallocator_dynamic_capacity
hxallocator_dynamic_capacity - A capacity value that allows for dynamic allocation.
Definition hxallocator.hpp:20
@ 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
void hxfree(void *ptr_) hxattr_noexcept hxattr_hot
hxfree - Frees memory previously allocated with hxmalloc or hxmalloc_ext.
unsigned int hxalignment_t
hxalignment_t - A positive integer power of 2 for aligning allocations.
Definition hxmemory_manager.h:64
void * hxmalloc_ext(size_t size_, hxsystem_allocator_t allocator_, hxalignment_t alignment_) hxattr_noexcept hxattr_allocator(hxfree) hxattr_hot
hxmalloc_ext - Allocates memory of the specified size with a specific memory manager and alignment.
constexpr hxalignment_t hxalignment
hxalignment - The default alignment.
Definition hxmemory_manager.h:69
#define hxinline_constexpr
hxinline_constexpr - Enables C++17 compatable "inline constexpr" usage for variables so they can be e...
Definition hxsettings.h:272
#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
constexpr hxsize_t hxsizeof(void)
hxsizeof - Returns the size of a type or expression as hxsize_t.
Definition libhatchet.h:288
#define hxassert_always(x_,...)
hxassert_always(bool x, ...) - Logs an error and terminates execution if x is false.
Definition libhatchet.h:121
ptrdiff_t hxsize_t
hxsize_t - A signed size type, same as ssize_t or ptrdiff_t.
Definition libhatchet.h:209