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

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

#include <alloc.hpp>

Collaboration diagram for mtcore::Allocator::VTable:

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)
 

Detailed Description

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*

See also
AllocatorInternalState for what the built-in state looks like

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:

  • alloc
    • Allocates a new block of memory of at least bytes size
  • realloc
    • Grows (or shrinks) current allocation to at least bytes size (if possible) or allocates a new region of memory and copies old region over
  • free
    • Deallocates a region of memory (if applicable)
  • deinit
    • Deinitializes the allocator. If there is an error (e.g. detected memory leak), return a non-zero value. Otherwise, return 0 on success

If 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.

Member Data Documentation

◆ alloc

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.

◆ deinit

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.

◆ free

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.

◆ realloc

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.


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