include/Cnx/Allocators.h file

Allocators provides an abstraction to modularize custom memory allocators to make custom allocator use simple and configurable.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Typedefs

using allocate_function = void*(*)(CnxAllocator*restrict self, usize size_bytes)
A memory allocation function used with CnxAllocator must follow this signature.
using reallocate_function = void*(*)(CnxAllocator*restrict self, void*memory, usize new_size_bytes)
A memory reallocation function used with CnxAllocator must follow this signature.
using deallocate_function = void(*)(CnxAllocator*restrict self, void*memory)
A memory deallocation function used with CnxAllocator must follow this signature.

Functions

void* cnx_allocate(CnxAllocator*restrict self, usize size_bytes) cnx_disable_if(!self
Wrapper for malloc so it can be used in CnxAllocators Behavior matches that of malloc
void* cnx_reallocate(CnxAllocator*restrict self, void* memory, usize new_size_bytes) cnx_disable_if(!self
Wrapper for realloc so it can be used in CnxAllocators Behavior matches that of realloc
void cnx_deallocate(CnxAllocator*restrict self, void* memory) cnx_disable_if(!self
Wrapper for free so it can be used in CnxAllocators Behavior matches that of free
CnxAllocator cnx_allocator_new(void)
Creates a new CnxAllocator with the default allocation and default deallocation functions.
void* cnx_allocator_allocate(CnxAllocator allocator, usize size_bytes)
Allocates new memory with the given CnxAllocator
void* cnx_allocator_allocate_array(CnxAllocator allocator, usize num_elements, usize element_size_bytes)
Allocates new memory for an array of the given size with the given CnxAllocator
void* cnx_allocator_reallocate(CnxAllocator allocator, void* memory, usize old_size_bytes, usize new_size_bytes)
Allocates new memory large enough to store new_size_bytes bytes of data, and copies the old contents over.
void* cnx_allocator_reallocate_array(CnxAllocator allocator, void* memory, usize old_num_elements, usize new_num_elements, usize element_size_bytes)
Allocates new memory large enough to store new_num_elements elements of size element_size_bytes, and copies the old contents over.
void cnx_allocator_deallocate(CnxAllocator allocator, void* memory)
Deallocates (aka frees) the given memory with the given CnxAllocator

Variables

const CnxAllocator DEFAULT_ALLOCATOR
The default CnxAllocator

Defines

#define CNX_DEFAULT_ALLOCATOR_FUNCTION
The default CnxAllocator allocation function Define this to your desired function to override the default allocation function.
#define CNX_DEFAULT_DEALLOCATOR_FUNCTION
The default CnxAllocator deallocation (free) function Define this to your desired function to override the default deallocation function.
#define CNX_DEFAULT_REALLOCATOR_FUNCTION
The default CnxAllocator reallocation (realloc) function Define this to your desired function to override the default reallocation function.
#define CNX_ALLOCATOR_ABORT_ON_ALLOCATION_FAILURE
CnxAllocator allocation failure strategy. By default allocation failure will abort the process. Define this to false to disable this behavior
#define impl_cnx_allocator_for_custom_typed_allocator(SelfType, allocate, reallocate, deallocate)
Implements CnxAllocator for the given custom allocator type.
#define cnx_allocator_from_custom_typed_allocator(SelfType, self)
Creates a new CnxAllocator with the given custom allocator.
#define cnx_allocator_from_custom_stateless_allocator(allocate_function, reallocate_function, deallocate_function)
Creates a new "stateless" CnxAllocator with the given custom allocator functions.
#define CNX_DEFAULT_ALLOCATOR
The default CnxAllocator Define this to your desired CnxAllocator compound literal to override the default CnxAllocator
#define cnx_allocator_allocate_t(T, allocator)
Allocates enough new memory to store a type T with the given CnxAllocator
#define cnx_allocator_allocate_array_t(T, allocator, num_elements)
Allocates enough new memory to store an array of num_elements of type T with the given CnxAllocator
#define cnx_allocator_reallocate_t(T, allocator, memory_ptr)
Allocates new memory large enough to store a T, and copies the old contents over.
#define cnx_allocator_reallocate_array_t(T, allocator, memory_ptr, old_num_elements, new_num_elements)
Allocates new memory large enough to store new_num_elements elements of type T, and copies the old contents over.
#define cnx_memcpy(T, dest_ptr, src_ptr, num_elements)
Copies memory containing an array of type T from src_ptr to dst_ptr
#define cnx_memmove(T, dest_ptr, src_ptr, num_elements)
Moves memory containing an array of type T from src_ptr to dst_ptr
#define cnx_memset(T, dest_ptr, value, num_elements)
Sets the memory at dest_ptr, containing an array of type T, to the given value