Allocation

July 1, 2023 ยท View on GitHub

Allocation utilities. By default aether-game-utils uses system allocations (malloc / free), which may be fine for your use case. If not, it's advised that you implement your own ae::Allocator with dlmalloc or similar and then call ae::SetGlobalAllocator() with your allocator at program start. All allocations are tagged, (@TODO) they can be inspected through the current ae::Allocator with ae::GetGlobalAllocator().

Members

public void SetGlobalAllocator(Allocator * alloc)

The given ae::Allocator is used for all memory allocations.

You must call ae::SetGlobalAllocator() before any allocations are made or else a default allocator which uses malloc / free will be used. The set value can be retrieved with ae::GetGlobalAllocator().

public Allocator * GetGlobalAllocator()

Get the custom allocator set with ae::SetGlobalAllocator().

If no custom allocator is set before the first allocation is made, this will return a default ae::Allocator which uses malloc / free. If ae::SetGlobalAllocator() has never been called and no allocations have been made, this will return nullptr.

public template<>
T *
NewArray(ae::Tag tag,uint32_t count)

Allocates and constructs an array of 'count' elements of type T.

an ae::Tag must be specifed and should represent the allocation type. Type T must have a default constructor. All arrays allocated with this function should be freed with ae::Delete(). Uses ae::GetGlobalAllocator() and ae::Allocator::Allocate() internally.

public template<>
T *
New(ae::Tag tag,Args ... args)

Allocates and constructs a single element of type T.

an ae::Tag must be specified and should represent the allocation type. All 'args' are passed to the constructor of T. All allocations should be freed with ae::Delete(). Uses ae::GetGlobalAllocator() and ae::Allocator::Allocate() internally.

public template<>
void
Delete(T * obj)

Should be called to destruct and free all allocations made with ae::New() and ae::NewArray().

Uses ae::GetGlobalAllocator() and ae::Allocator::Free() internally.

public inline void * Allocate(ae::Tag tag,uint32_t bytes,uint32_t alignment)

public inline void * Reallocate(void * data,uint32_t bytes,uint32_t alignment)

public inline void Free(void * data)



ae::Allocator class

public virtual ~Allocator()

public void * Allocate(ae::Tag tag,uint32_t bytes,uint32_t alignment)

Should return 'bytes' with minimum alignment of 'alignment'.

Optionally, a tag should be used to select a pool of memory, or for diagnostics/debugging.

public void * Reallocate(void * data,uint32_t bytes,uint32_t alignment)

Should attempt to expand or contract allocations made with Allocate() to match size 'bytes'.

On failure this function should return nullptr.

public void Free(void * data)

Free memory allocated with ae::Allocator::Allocate() or ae::Allocator::Reallocate().

public bool IsThreadSafe() const

Used for safety checks.