31#ifndef MTCORE_ALLOC_HPP
32#define MTCORE_ALLOC_HPP
35#include <unordered_map>
61 namespace impl::alloc {
115 void *(*alloc)(
void *
state,
size_t bytes);
121 void *(*realloc)(
void *
state,
void *ptr,
size_t bytes);
172 ensure(count,
"CANNOT ALLOC 0 ELEMENTS!");
173 ensure(
vtable,
"DEREFERENCING NULL VTABLE! ENSURE ALLOCATOR HAS NOT BEEN "
175 ensure(
vtable->alloc,
"VTABLE CORRUPTED, MISSING ALLOC");
178 for (
size_t i = 0; i < count; ++i) {
195 template<
typename T,
typename... Args>
197 ensure(
vtable,
"DEREFERENCING NULL VTABLE! ENSURE ALLOCATOR HAS NOT BEEN "
199 ensure(
vtable->alloc,
"VTABLE CORRUPTED, MISSING ALLOC");
200 auto res =
static_cast<T *
>(
vtable->alloc(
state,
sizeof(T)));
202 if constexpr (std::is_copy_assignable<T>::value) {
205 else if constexpr (std::is_move_assignable<T>::value) {
206 *res = std::move(T{args...});
208 else if constexpr (std::is_trivially_constructible_v<T> &&
is_initable<T, Args...>) {
213 new (res) T(args...);
250 ensure(
vtable,
"DEREFERENCING NULL VTABLE! ENSURE ALLOCATOR HAS NOT BEEN "
254 if (ptr ==
nullptr) {
259 mtcore_warn(
"NO FREE ON ALLOCATOR! FREE IS A NO-OP!");
315#ifndef MTCORE_NO_STACKTRACE
316 std::unordered_map<i64, cpptrace::stacktrace>
trace = {};
438 size_t elementCountHint) noexcept;
440 namespace impl::alloc {
444 template<AllocatorDeinit T>
446 void operator()(
Allocator &alloc, T &t) { t.deinit(alloc); }
449 template<DefaultDeinit T>
451 void operator()(
Allocator &, T &t) { t.deinit(); }
461 (Deiniter<T>{})(alloc, t);
467 if (ptr ==
nullptr) {
472 impl::alloc::deinit(*
this, *ptr);
481 if (s.
head ==
nullptr || s.
len == 0) {
486 for (
size_t i = 0; i < s.
len; ++i) {
487 impl::alloc::deinit(*
this, s[i]);
#define mtcore_warn(...)
Prints a warning message and stacktrace in debug builds Does nothing in release builds`.
ThreadLocalFixedBufferAllocator fixed_buff_thread_local(Slice< u8 > bytes) noexcept
NOT THREAD SAFE!
ThreadLocalFixedArenaAllocator fixed_arena_thread_local(Slice< u8 > bytes) noexcept
NOT THREAD SAFE!
MallocAllocator malloc_alloc() noexcept
Creates a memory allocator using the standard's malloc under the hood Does no memory tracking Ideal f...
ThreadLocalDebugAllocator debug_alloc_thread_local() noexcept
NOT THREAD SAFE!
ThreadLocalDynamicArenaAllocator dynamic_arena_thread_local(Allocator *root, size_t elementSizeHint, size_t elementCountHint) noexcept
NOT THREAD SAFE!
AllocatorError
Error indicating allocator internal state failed (e.g.
AllocationError
Error indicating failed allocation.
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
Success< void > success()
Creates a successful void Result object.
Error< Underlying > error(Underlying err)
Creates an error.
constexpr bool is_initable
Checks if a type has an init with the param types.
int64_t i64
Alias for 64-bit ints.
uint8_t u8
Alias for 8-bit unsigned ints.
Core library for C++ with Zig-related functionality.
V-Table for implementing your own allocator Each allocator has state, some of which is standardized (...
Result< void, AllocatorError >(* deinit)(void *state)
Function pointer to deinit method (Optional, if not provided will default to a no-op)
void(* free)(void *state, void *ptr)
Function pointer to free method (Optional, if not provided will default to no-op)
Represents a memory allocator Exact behavior depends on the underlying VTable used Should use the a_*...
void free(T *&ptr)
Marks a block of memory as "free-able" May or may not immediately deallocate memory,...
const VTable * vtable
VTable to allocation-related methods.
void * state
Internal state for the allocator.
void destroy_many(Slice< T > s)
Destroys objects allocated by this allocator by calling the destructors and freeing memory.
Result< void *, AllocationError > realloc(void *ptr, size_t newBytes)
Attempts to reallocate a block of memory to either shrink it or grow it.
Result< void, AllocatorError > deinit()
De-initializes a memory allocator.
Result< T *, AllocationError > create(Args... args)
Allocates some block of memory with an allocator with a known type Will pass arguments to the constru...
Result< void *, AllocationError > alloc(size_t bytes)
Allocates some block of memory with an allocator May fail.
Result< Slice< T >, AllocationError > create_many(size_t count=1)
Allocates some block of memory with an allocator with a known type Will call default constructor on t...
void destroy(T *&ptr)
Destroys an object allocated by this allocator by calling the destructor and freeing memory.
State variable for malloc-based allocator.
Allocator allocator() noexcept
Represents a Result that may have an error (error code) or a success value A type of "void" means the...
A Slice which is just a pointer + length Accessing elements through the array operator will do bounds...
State variable for thread local debug allocator Must live longer (and at the same memory address) tha...
Allocator allocator() noexcept
std::unordered_map< i64, cpptrace::stacktrace > trace
State variable for thread local dynamic arena allocator Must live longer (and at the same memory addr...
Allocator allocator() noexcept
State variable for thread local fixed arena allocator Must live longer (and at the same memory addres...
Allocator allocator() noexcept
State variable for thread local fixed buffer allocator Must live longer (and at the same memory addre...
Allocator allocator() noexcept