MT Core (C++)
Core library for replacing C++ standard in project usage
|
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>
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, AllocatorError > | deinit () |
De-initializes a memory allocator. | |
Result< void *, AllocationError > | alloc (size_t bytes) |
Allocates some block of memory with an allocator May fail. | |
template<typename T> | |
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 the memory block. | |
template<typename T, typename... Args> | |
Result< T *, AllocationError > | create (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 *, AllocationError > | realloc (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 VTable * | vtable = nullptr |
VTable to allocation-related methods. | |
void * | state = nullptr |
Internal state for the allocator. | |
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.
|
nodiscard |
Allocates some block of memory with an allocator May fail.
On failure, AllocRes.type
will be FAIL, and ptr will be nullptr
bytes | Number of bytes to allocate |
|
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
T | Type of object to allocate (MUST be default constructible) |
args | Constructor arguments |
Definition at line 196 of file core/mtcore/alloc.hpp.
|
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
T | Type of object to allocate (MUST be default constructible) |
count | Number of objects to allocate |
Definition at line 171 of file core/mtcore/alloc.hpp.
|
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
void mtcore::Allocator::destroy | ( | T *& | ptr | ) |
Destroys an object allocated by this allocator by calling the destructor and freeing memory.
T | Type of element to destroy, will call destructor |
ptr | Pointer to element to destroy, will call destructor |
Definition at line 466 of file core/mtcore/alloc.hpp.
void mtcore::Allocator::destroy_many | ( | Slice< T > | s | ) |
Destroys objects allocated by this allocator by calling the destructors and freeing memory.
T | Type of element to destroy, will call destructor |
s | Slice of elements to destroy, will call destructor on each |
Definition at line 480 of file core/mtcore/alloc.hpp.
|
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;.
ptr | Pointer to memory to free |
Definition at line 249 of file core/mtcore/alloc.hpp.
|
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
ptr | Pointer to existing memory allocated by this allocator |
newBytes | Number of new bytes to have for the allocator (bigger = grow, smaller = shrink) |
void* mtcore::Allocator::state = nullptr |
Internal state for the allocator.
Definition at line 140 of file core/mtcore/alloc.hpp.
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.