MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::Allocator Struct Reference

Represents a memory allocator Exact behavior depends on the underlying VTable used Should use the a_* methods for working with an allocator. More...

#include <alloc.hpp>

Collaboration diagram for mtcore::Allocator:

Classes

struct  VTable
 V-Table for implementing your own allocator Each allocator has state, some of which is standardized (e.g. More...
 

Public Member Functions

Result< void, AllocatorErrordeinit ()
 De-initializes a memory allocator.
 
Result< void *, AllocationErroralloc (size_t bytes)
 Allocates some block of memory with an allocator May fail.
 
template<typename T>
Result< Slice< T >, AllocationErrorcreate_many (size_t count=1)
 Allocates some block of memory with an allocator with a known type Will call default constructor on the memory block.
 
template<typename T, typename... Args>
Result< T *, AllocationErrorcreate (Args... args)
 Allocates some block of memory with an allocator with a known type Will pass arguments to the constructor call for the element May fail.
 
Result< void *, AllocationErrorrealloc (void *ptr, size_t newBytes)
 Attempts to reallocate a block of memory to either shrink it or grow it.
 
template<typename T>
void free (T *&ptr)
 Marks a block of memory as "free-able" May or may not immediately deallocate memory, depending on what the allocator does Also sets the passed in pointer to nullptr;.
 
template<typename T>
void destroy (T *&ptr)
 Destroys an object allocated by this allocator by calling the destructor and freeing memory.
 
template<typename T>
void destroy_many (Slice< T > s)
 Destroys objects allocated by this allocator by calling the destructors and freeing memory.
 

Public Attributes

const VTablevtable = nullptr
 VTable to allocation-related methods.
 
void * state = nullptr
 Internal state for the allocator.
 

Detailed Description

Represents a memory allocator Exact behavior depends on the underlying VTable used Should use the a_* methods for working with an allocator.

Definition at line 72 of file core/mtcore/alloc.hpp.

Member Function Documentation

◆ alloc()

Result< void *, AllocationError > mtcore::Allocator::alloc ( size_t bytes)
nodiscard

Allocates some block of memory with an allocator May fail.

On failure, AllocRes.type will be FAIL, and ptr will be nullptr

Parameters
bytesNumber of bytes to allocate
Returns
Returns an allocation Result containing allocated bytes

◆ create()

template<typename T, typename... Args>
Result< T *, AllocationError > mtcore::Allocator::create ( Args... args)
inlinenodiscard

Allocates some block of memory with an allocator with a known type Will pass arguments to the constructor call for the element May fail.

On failure, AllocRes.type will be FAIL, and Slice will be empty

Template Parameters
TType of object to allocate (MUST be default constructible)
Parameters
argsConstructor arguments
Returns
A typed allocation Result containing a Slice of allocated objects

Definition at line 196 of file core/mtcore/alloc.hpp.

196 {
197 ensure(vtable, "DEREFERENCING NULL VTABLE! ENSURE ALLOCATOR HAS NOT BEEN "
198 "DEINITIALIZED!");
199 ensure(vtable->alloc, "VTABLE CORRUPTED, MISSING ALLOC");
200 auto res = static_cast<T *>(vtable->alloc(state, sizeof(T)));
201 if (res) {
202 if constexpr (std::is_copy_assignable<T>::value) {
203 *res = T{args...};
204 }
205 else if constexpr (std::is_move_assignable<T>::value) {
206 *res = std::move(T{args...});
207 }
208 else if constexpr (std::is_trivially_constructible_v<T> && is_initable<T, Args...>) {
209 *res = T{};
210 res->init(args...);
211 }
212 else {
213 new (res) T(args...);
214 }
215 return success(res);
216 }
218 }
#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.
Definition result.hpp:398
Error< Underlying > error(Underlying err)
Creates an error.
Definition result.hpp:425
constexpr bool is_initable
Checks if a type has an init with the param types.
const VTable * vtable
VTable to allocation-related methods.
void * state
Internal state for the allocator.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_many()

template<typename T>
Result< Slice< T >, AllocationError > mtcore::Allocator::create_many ( size_t count = 1)
inlinenodiscard

Allocates some block of memory with an allocator with a known type Will call default constructor on the memory block.

May fail. On failure, AllocRes.type will be FAIL, and Slice will be empty

Template Parameters
TType of object to allocate (MUST be default constructible)
Parameters
countNumber of objects to allocate
Returns
A typed allocation Result containing a Slice of allocated objects

Definition at line 171 of file core/mtcore/alloc.hpp.

171 {
172 ensure(count, "CANNOT ALLOC 0 ELEMENTS!");
173 ensure(vtable, "DEREFERENCING NULL VTABLE! ENSURE ALLOCATOR HAS NOT BEEN "
174 "DEINITIALIZED!");
175 ensure(vtable->alloc, "VTABLE CORRUPTED, MISSING ALLOC");
176 auto s = Slice<T>{static_cast<T *>(vtable->alloc(state, sizeof(T) * count)), count};
177 if (s.head) {
178 for (size_t i = 0; i < count; ++i) {
179 s[i] = T{};
180 }
181 return success(s);
182 }
184 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ deinit()

Result< void, AllocatorError > mtcore::Allocator::deinit ( )
nodiscard

De-initializes a memory allocator.

Assume that it makes the allocator invalid to use for future use (unless otherwise stated by the creator)

Can check for memory leaks and report them via ErrCode

Returns
Error code if anything goes wrong during initialization

◆ destroy()

template<typename T>
void mtcore::Allocator::destroy ( T *& ptr)

Destroys an object allocated by this allocator by calling the destructor and freeing memory.

Template Parameters
TType of element to destroy, will call destructor
Parameters
ptrPointer to element to destroy, will call destructor

Definition at line 466 of file core/mtcore/alloc.hpp.

466 {
467 if (ptr == nullptr) {
468 return;
469 }
470
471 // call deinit if it's there
472 impl::alloc::deinit(*this, *ptr);
473
474 // call destructor
475 ptr->~T();
476 this->free(ptr);
477 }
PtrIter< T > ptr(T &r)
Generic pointer iterator that uses the operator[] and incrementing indexes to iterate over a collecti...
Definition iter.hpp:101
void free(T *&ptr)
Marks a block of memory as "free-able" May or may not immediately deallocate memory,...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ destroy_many()

template<typename T>
void mtcore::Allocator::destroy_many ( Slice< T > s)

Destroys objects allocated by this allocator by calling the destructors and freeing memory.

Template Parameters
TType of element to destroy, will call destructor
Parameters
sSlice of elements to destroy, will call destructor on each

Definition at line 480 of file core/mtcore/alloc.hpp.

480 {
481 if (s.head == nullptr || s.len == 0) {
482 return;
483 }
484
485 // call destructors & deinit
486 for (size_t i = 0; i < s.len; ++i) {
487 impl::alloc::deinit(*this, s[i]);
488 s[i].~T();
489 }
490 this->free(s.head);
491 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ free()

template<typename T>
void mtcore::Allocator::free ( T *& ptr)
inline

Marks a block of memory as "free-able" May or may not immediately deallocate memory, depending on what the allocator does Also sets the passed in pointer to nullptr;.

Parameters
ptrPointer to memory to free

Definition at line 249 of file core/mtcore/alloc.hpp.

249 {
250 ensure(vtable, "DEREFERENCING NULL VTABLE! ENSURE ALLOCATOR HAS NOT BEEN "
251 "DEINITIALIZED!");
252
253 // no need to free nullptr
254 if (ptr == nullptr) {
255 return;
256 }
257
258 if (!vtable->free) {
259 mtcore_warn("NO FREE ON ALLOCATOR! FREE IS A NO-OP!");
260 return;
261 }
262 vtable->free(state, ptr);
263
264 ptr = nullptr;
265 }
#define mtcore_warn(...)
Prints a warning message and stacktrace in debug builds Does nothing in release builds`.
Here is the caller graph for this function:

◆ realloc()

Result< void *, AllocationError > mtcore::Allocator::realloc ( void * ptr,
size_t newBytes )
nodiscard

Attempts to reallocate a block of memory to either shrink it or grow it.

If the operation cannot be done in place, the allocator may allocate a new block of memory and copy the bytes over May fail. On failure, AllocRes.type will be FAIL and ptr will be nullptr

NOTE: A failure here does NOT mean there's no more memory available, only that the allocator cannot reallocate blocks This may happen if, for example, the allocator does NOT track individual allocations and therefore cannot reallocate blocks This may happen with simple arena allocators, for example

Parameters
ptrPointer to existing memory allocated by this allocator
newBytesNumber of new bytes to have for the allocator (bigger = grow, smaller = shrink)
Returns
On success, pointer to new memory, on failure nullptr. On success, old memory is freed, on failure old memory is preserved.

Member Data Documentation

◆ state

void* mtcore::Allocator::state = nullptr

Internal state for the allocator.

Definition at line 140 of file core/mtcore/alloc.hpp.

◆ vtable

const VTable* mtcore::Allocator::vtable = nullptr

VTable to allocation-related methods.

Do not use directly

Definition at line 137 of file core/mtcore/alloc.hpp.


The documentation for this struct was generated from the following file: