libhatchet
Loading...
Searching...
No Matches
hxmemory_manager.h
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
50
51#if !LIBHATCHET_VER
52#error #include <hx/libhatchet.h> instead.
53#endif
54
55#if HX_CPLUSPLUS && (HX_USE_LIBCXX)
56#include <new>
57#endif
58
59HX_C_BEGIN_
60
62typedef unsigned int hxalignment_t;
63
64#if HX_CPLUSPLUS >= 202002L
67inline constexpr hxalignment_t hxalignment = static_cast<hxalignment_t>(alignof(max_align_t ));
68#elif HX_CPLUSPLUS
69#define hxalignment static_cast<hxalignment_t>(alignof(max_align_t ))
70#else
71#define hxalignment (hxalignment_t)_Alignof(max_align_t )
72#endif
73
76
77enum {
89};
90
95
109void* hxmalloc(size_t size_);
110
120void* hxmalloc_ext(size_t size_, hxsystem_allocator_t allocator_,
121 hxalignment_t alignment_/*=hxalignment*/);
122
130char* hxstring_duplicate(const char* string_,
131 hxsystem_allocator_t allocator_ /*=hxsystem_allocator_current*/);
132
133HX_C_END_
134#if HX_CPLUSPLUS
135
141 return hxmalloc_ext(size_, allocator_, alignment_);
142}
143
147hxinline char* hxstring_duplicate(const char* s_) {
149}
150
151// Memory Manager C++ API
152
153#if !(HX_USE_LIBCXX)
154// Declare placement new. These are not built into the compiler.
155// HX_PROVIDE_NEW_DELETE is concerned with the other versions of new and delete.
156#if HX_CPLUSPLUS >= 202002L
157constexpr void* operator new(size_t, void* ptr_) noexcept { return ptr_; }
158constexpr void* operator new[](size_t, void* ptr_) noexcept { return ptr_; }
159#else
160hxinline void* operator new(size_t, void* ptr_) noexcept { return ptr_; }
161hxinline void* operator new[](size_t, void* ptr_) noexcept { return ptr_; }
162#endif
163
164#if (HX_PROVIDE_NEW_DELETE) != -1
165// Provide these for constant evaluation/consteval. Link errors may still result.
166// Use HX_PROVIDE_NEW_DELETE=-1 when compiling a module to prevent declaration.
167void* operator new(size_t size_) hxattr_nodiscard;
168void* operator new[](size_t size_) hxattr_nodiscard;
169void operator delete(void* ptr_) noexcept;
170void operator delete[](void* ptr_) noexcept;
171#endif
172#endif
173
174HX_NS_BEGIN_
175
185{
186public:
191
195
201
206
209 return m_initial_allocation_count_;
210 }
211
214 return m_initial_bytes_allocated_;
215 }
216
217private:
218 // The hxsystem_allocator_* classes are responsible for setting
219 // m_initial_allocation_count_ and m_initial_bytes_allocated_.
220 // This avoids a number of potential cache misses.
222 size_t allocation_count_, size_t bytes_allocated_);
223
224 // Deleted copy constructor to prevent copying.
226
227 // Deleted assignment operator to prevent copying.
228 void operator=(const hxsystem_allocator_scope&) = delete;
229
230 hxsystem_allocator_t m_this_allocator_;
231 hxsystem_allocator_t m_initial_allocator_;
232 size_t m_initial_allocation_count_;
233 size_t m_initial_bytes_allocated_;
234};
235
244 hxalignment_t align_=hxalignment, typename... Args_>
245T_* hxnew(Args_&&... args_) noexcept {
246 // Implements hxforward.
247 return ::new(hxmalloc_ext(sizeof(T_), allocator_, align_)) T_(static_cast<Args_&&>(args_)...);
248}
249
253template <typename T_>
254void hxdelete(T_* t_) noexcept {
255 if(t_) {
256 t_->~T_();
257 hxfree(const_cast<void*>(static_cast<const void*>(t_)));
258 }
259}
260
266public:
268 template <typename T_>
269 void operator()(T_* t_) const { hxdelete(t_); }
270
272 operator bool(void) const { return true; }
273};
274
279public:
281 template <typename T_>
282 void operator()(T_*) const { }
283
285 operator bool(void) const { return false; }
286};
287
288#if HX_CPLUSPLUS >= 201402L
293public:
294 // GCOVR_EXCL_START. No coverage at compile time.
296 template <typename T_>
297 constexpr void operator()(T_* t_) const { ::delete t_; }
298
300 constexpr operator bool(void) const { return true; }
301 // GCOVR_EXCL_STOP
302};
303#endif
305
306// `hxmemory_manager_init_` - WARNING: Not intended for direct use. This is
307// called by hxinit(). Initializes the memory manager. Must be called before
308// using any memory manager functions.
309void hxmemory_manager_init_(void) hxattr_cold;
310
311// `hxmemory_manager_shut_down_` - WARNING: Not intended for direct use. Shuts
312// down the memory manager. Frees any remaining resources.
313void hxmemory_manager_shut_down_(void) hxattr_cold;
315
322void hxmemory_manager_allocate_stacks(const size_t* sizes_, size_t stack_count_) hxattr_cold;
323
325template<size_t stack_count_>
326void hxmemory_manager_allocate_stacks(const size_t(&list_)[stack_count_]) {
327 hxmemory_manager_allocate_stacks(list_, stack_count_);
328}
329
338
344
345HX_NS_END_
346#endif // HX_CPLUSPLUS
hxconsteval_delete - A constexpr-compatible deleter that uses delete.
Definition hxmemory_manager.h:292
constexpr void operator()(T *t_) const
Deletes the object using delete.
Definition hxmemory_manager.h:297
hxdefault_delete - A callable that deletes objects of type T using hxdelete.
Definition hxmemory_manager.h:265
void operator()(T *t_) const
Deletes the object using hxdelete.
Definition hxmemory_manager.h:269
hxdo_not_delete - A version of hxdefault_delete that does not delete the object.
Definition hxmemory_manager.h:278
void operator()(T *) const
Does not delete the object.
Definition hxmemory_manager.h:282
hxmemory_manager_stats - The utilization statistics reported by hxmemory_manager_utilization.
Definition hxmemory_manager.h:332
size_t allocator_overflows
Definition hxmemory_manager.h:336
size_t allocations_outstanding
Definition hxmemory_manager.h:334
size_t bytes_outstanding
Definition hxmemory_manager.h:335
friend hxinline void hxsystem_allocator_scope_init_(hxsystem_allocator_scope *scope_, size_t allocation_count_, size_t bytes_allocated_)
hxattr_nodiscard size_t get_initial_bytes_allocated(void) const
Gets the number of bytes allocated when this scope was entered.
Definition hxmemory_manager.h:213
hxattr_nodiscard size_t get_current_bytes_allocated(void) const
Gets the total number of bytes allocated outstanding for this memory allocator.
hxattr_nodiscard size_t get_initial_allocation_count(void) const
Gets the number of allocations made when this scope was entered.
Definition hxmemory_manager.h:208
hxattr_nodiscard size_t get_current_allocation_count(void) const
Gets the total number of allocations outstanding for this memory allocator.
hxsystem_allocator_scope(hxsystem_allocator_t allocator_) hxattr_noexcept
Constructor: Sets the current memory allocator to the specified ID.
~hxsystem_allocator_scope(void) hxattr_noexcept
Destructor restores the stored previous memory manager allocator ID.
@ hxsystem_allocator_stack_0
hxsystem_allocator_stack_0 - Temporary stacks.
Definition hxmemory_manager.h:88
@ hxsystem_allocator_heap
hxsystem_allocator_heap - OS heap with alignment.
Definition hxmemory_manager.h:82
@ hxsystem_allocator_current
hxsystem_allocator_current - Use current allocation scope.
Definition hxmemory_manager.h:80
@ hxsystem_allocator_permanent
hxsystem_allocator_permanent - Contiguous allocations that must not be freed.
Definition hxmemory_manager.h:85
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
hxinline char * hxstring_duplicate(const char *s_)
hxstring_duplicate - Add default args to C interface.
Definition hxmemory_manager.h:147
HX_C_END_ hxinline void * hxmalloc(size_t size_, hxsystem_allocator_t allocator_, hxalignment_t alignment_=hxalignment)
hxmalloc - Add hxmalloc_ext args to hxmalloc C interface.
Definition hxmemory_manager.h:139
void hxfree(void *ptr_) hxattr_noexcept hxattr_hot
hxfree - Frees memory previously allocated with hxmalloc or hxmalloc_ext.
void hxdelete(T *t_) noexcept
hxdelete - Deletes an object of type T and frees its memory using the memory manager.
Definition hxmemory_manager.h:254
hxattr_noexcept hxsystem_allocator_t allocator_
Definition hxmemory_manager.h:120
T * hxnew(Args_ &&... args_) noexcept
hxnew<T, allocator, align>(...) - Allocates and constructs an object of type T using an optional memo...
Definition hxmemory_manager.h:245
hxattr_noexcept hxsystem_allocator_t hxalignment_t alignment_
Definition hxmemory_manager.h:121
void hxmemory_manager_allocate_stacks(const size_t *sizes_, size_t stack_count_) hxattr_cold
hxmemory_manager_allocate_stacks - Allocates the runtime temporary stacks.
constexpr hxalignment_t hxalignment
hxalignment - The default alignment.
Definition hxmemory_manager.h:67
hxmemory_manager_stats hxmemory_manager_utilization(bool stacks_only_, bool log_) hxattr_cold
hxmemory_manager_utilization - Returns the utilization statistics of the memory manager.
#define hxinline
hxinline - Force a function to be inlined into its callers.
Definition hxsettings.h:125
#define hxattr_noexcept
hxattr_noexcept - Use gcc/clang nothrow attribute.
Definition hxsettings.h:102
#define hxattr_allocator(...)
hxattr_allocator - Mark allocator/deallocator pairs for static analysis.
Definition hxsettings.h:79
#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
#define hxattr_cold
hxattr_cold - Optimize a function for size.
Definition hxsettings.h:86
#define hxattr_hot
hxattr_hot - Optimize a function more aggressively.
Definition hxsettings.h:94