MT Core (C++)
Core library for replacing C++ standard in project usage
|
V-Table for implementing your own allocator Each allocator has state, some of which is standardized (e.g. More...
#include <alloc.hpp>
Public Attributes | |
void *(* | alloc )(void *state, size_t bytes) |
Function pointer to memory allocation method (required) | |
void *(* | realloc )(void *state, void *ptr, size_t bytes) |
Function pointer to memory reallocation method (Optional, if not provided realloc will always fail) | |
void(* | free )(void *state, void *ptr) |
Function pointer to free method (Optional, if not provided will default to no-op) | |
Result< void, AllocatorError >(* | deinit )(void *state) |
Function pointer to deinit method (Optional, if not provided will default to a no-op) | |
V-Table for implementing your own allocator Each allocator has state, some of which is standardized (e.g.
mut_u8s as a buffer of bytes, size_t as a s1, etc.) But there is also the option to add your own state with void*
It is up to the developer to choose how to use the available state options and to maintain storage lifetime Some state can be global/shared (e.g. mutex locks, global scratch buffer, etc.) while others can be allocated per instance
When void* is the return type, a NULL indicates failure and a non-null is a pointer to the new memory region.
Summary of what each method should do:
bytes
sizebytes
size (if possible) or allocates a new region of memory and copies old region overIf a method is invalid, then it should always return the error code. For instance, the provided arena allocators don't track allocations, and so they are never able to reallocate. In that case, they always return NULL. Also, arena allocators have a no-op free, opting to "free" on a deinit.
No additional constraints exist on whether zeroing memory is required, the time complexity, etc.
Note: At least one of either free or deinit should be implemented (e.g. deinit for arena, free for malloc)
Definition at line 111 of file core/mtcore/alloc.hpp.
void *(* mtcore::Allocator::VTable::alloc) (void *state, size_t bytes) |
Function pointer to memory allocation method (required)
Definition at line 115 of file core/mtcore/alloc.hpp.
Result< void, AllocatorError >(* mtcore::Allocator::VTable::deinit) (void *state) |
Function pointer to deinit method (Optional, if not provided will default to a no-op)
Definition at line 127 of file core/mtcore/alloc.hpp.
void(* mtcore::Allocator::VTable::free) (void *state, void *ptr) |
Function pointer to free method (Optional, if not provided will default to no-op)
Definition at line 127 of file core/mtcore/alloc.hpp.
void *(* mtcore::Allocator::VTable::realloc) (void *state, void *ptr, size_t bytes) |
Function pointer to memory reallocation method (Optional, if not provided realloc will always fail)
Definition at line 121 of file core/mtcore/alloc.hpp.