Memory module
Cnx provides abstractions and ergonomics features for dealing with memory allocation, deallocation, and handling custom allocators
Modules
- module CnxSharedPtr
- module SmartPtrs
- module CnxUniquePtr
Classes
- struct CnxAllocator
- A
CnxAllocatoris a convenient abstraction for wrapping memory allocation and deallocation, making it simple and easy to write allocator-configurable and/or allocator aware software.
Typedefs
- using allocate_function = void*(*)(CnxAllocator*restrict self, usize size_bytes)
- A memory allocation function used with
CnxAllocatormust follow this signature. - using reallocate_function = void*(*)(CnxAllocator*restrict self, void*memory, usize new_size_bytes)
- A memory reallocation function used with
CnxAllocatormust follow this signature. - using deallocate_function = void(*)(CnxAllocator*restrict self, void*memory)
- A memory deallocation function used with
CnxAllocatormust follow this signature.
Functions
- void* cnx_allocate(CnxAllocator*restrict self, usize size_bytes) cnx_disable_if(!self
- Wrapper for
mallocso it can be used inCnxAllocators Behavior matches that ofmalloc - void* cnx_reallocate(CnxAllocator*restrict self, void* memory, usize new_size_bytes) cnx_disable_if(!self
- Wrapper for
reallocso it can be used inCnxAllocators Behavior matches that ofrealloc - void cnx_deallocate(CnxAllocator*restrict self, void* memory) cnx_disable_if(!self
- Wrapper for
freeso it can be used inCnxAllocators Behavior matches that offree - CnxAllocator cnx_allocator_new(void)
- Creates a new
CnxAllocatorwith 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_bytesbytes 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_elementselements of sizeelement_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
CnxAllocatorallocation function Define this to your desired function to override the default allocation function. - #define CNX_DEFAULT_DEALLOCATOR_FUNCTION
- The default
CnxAllocatordeallocation (free) function Define this to your desired function to override the default deallocation function. - #define CNX_ALLOCATOR_ABORT_ON_ALLOCATION_FAILURE
CnxAllocatorallocation 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
CnxAllocatorfor the given custom allocator type. - #define cnx_allocator_from_custom_typed_allocator(SelfType, self)
- Creates a new
CnxAllocatorwith the given custom allocator. - #define cnx_allocator_from_custom_stateless_allocator(allocate_function, reallocate_function, deallocate_function)
- Creates a new "stateless"
CnxAllocatorwith the given custom allocator functions. - #define CNX_DEFAULT_ALLOCATOR
- The default
CnxAllocatorDefine this to your desiredCnxAllocatorcompound literal to override the defaultCnxAllocator - #define cnx_allocator_allocate_t(T, allocator)
- Allocates enough new memory to store a type
Twith the givenCnxAllocator - #define cnx_allocator_allocate_array_t(T, allocator, num_elements)
- Allocates enough new memory to store an array of
num_elementsof typeTwith the givenCnxAllocator - #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_elementselements of typeT, and copies the old contents over. - #define cnx_memcpy(T, dest_ptr, src_ptr, num_elements)
- Copies memory containing an array of type
Tfromsrc_ptrtodst_ptr - #define cnx_memmove(T, dest_ptr, src_ptr, num_elements)
- Moves memory containing an array of type
Tfromsrc_ptrtodst_ptr - #define cnx_memset(T, dest_ptr, value, num_elements)
- Sets the memory at
dest_ptr, containing an array of typeT, to the givenvalue
Typedef documentation
typedef void*(*allocate_function)(CnxAllocator*restrict self, usize size_bytes)
#include <include/Cnx/Allocators.h>
A memory allocation function used with CnxAllocator must follow this signature.
typedef void*(*reallocate_function)(CnxAllocator*restrict self, void*memory, usize new_size_bytes)
#include <include/Cnx/Allocators.h>
A memory reallocation function used with CnxAllocator must follow this signature.
typedef void(*deallocate_function)(CnxAllocator*restrict self, void*memory)
#include <include/Cnx/Allocators.h>
A memory deallocation function used with CnxAllocator must follow this signature.
Function documentation
void* cnx_allocate(CnxAllocator*restrict self,
usize size_bytes) cnx_disable_if(!self
#include <include/Cnx/Allocators.h>
Wrapper for malloc so it can be used in CnxAllocators Behavior matches that of malloc
| Parameters | |
|---|---|
| self | - The state of the allocator (unused) |
| size_bytes | - The number of bytes to allocate |
| Returns | the allocated memory |
void* cnx_reallocate(CnxAllocator*restrict self,
void* memory,
usize new_size_bytes) cnx_disable_if(!self
#include <include/Cnx/Allocators.h>
Wrapper for realloc so it can be used in CnxAllocators Behavior matches that of realloc
| Parameters | |
|---|---|
| self | - The state of the allocator (unused) |
| memory | - The memory allocation to shrink or grow |
| new_size_bytes | - The number of bytes to allocate |
| Returns | the allocated memory |
void cnx_deallocate(CnxAllocator*restrict self,
void* memory) cnx_disable_if(!self
#include <include/Cnx/Allocators.h>
Wrapper for free so it can be used in CnxAllocators Behavior matches that of free
| Parameters | |
|---|---|
| self | - The state of the allocator (unused) |
| memory | - The memory allocation to deallocate |
CnxAllocator cnx_allocator_new(void)
#include <include/Cnx/Allocators.h>
Creates a new CnxAllocator with the default allocation and default deallocation functions.
| Returns | a default CnxAllocator |
|---|
void* cnx_allocator_allocate(CnxAllocator allocator,
usize size_bytes)
#include <include/Cnx/Allocators.h>
Allocates new memory with the given CnxAllocator
| Parameters | |
|---|---|
| allocator | - The allocator to allocate with |
| size_bytes | - The size to allocate in bytes |
| Returns | newly allocated memory |
void* cnx_allocator_allocate_array(CnxAllocator allocator,
usize num_elements,
usize element_size_bytes)
#include <include/Cnx/Allocators.h>
Allocates new memory for an array of the given size with the given CnxAllocator
| Parameters | |
|---|---|
| allocator | - The allocator to allocate with |
| num_elements | - The number of elements in the array |
| element_size_bytes | - The size of an array element, in bytes |
| Returns | newly allocated memory |
void* cnx_allocator_reallocate(CnxAllocator allocator,
void* memory,
usize old_size_bytes,
usize new_size_bytes)
#include <include/Cnx/Allocators.h>
Allocates new memory large enough to store new_size_bytes bytes of data, and copies the old contents over.
| Parameters | |
|---|---|
| allocator | - The allocator to allocate memory with |
| memory | - The memory to be reallocated |
| old_size_bytes | - The old size of the memory, in bytes |
| new_size_bytes | - The desired new size of the memory, in bytes |
| Returns | reallocated memory |
void* cnx_allocator_reallocate_array(CnxAllocator allocator,
void* memory,
usize old_num_elements,
usize new_num_elements,
usize element_size_bytes)
#include <include/Cnx/Allocators.h>
Allocates new memory large enough to store new_num_elements elements of size element_size_bytes, and copies the old contents over.
| Parameters | |
|---|---|
| allocator | - The allocator to allocate memory with |
| memory | - The memory to be reallocated |
| old_num_elements | - The old number of elements in the memory |
| new_num_elements | - The desired new number of elements in the memory |
| element_size_bytes | - The size of a single element, in bytes |
| Returns | reallocated memory |
void cnx_allocator_deallocate(CnxAllocator allocator,
void* memory)
#include <include/Cnx/Allocators.h>
Deallocates (aka frees) the given memory with the given CnxAllocator
| Parameters | |
|---|---|
| allocator | - The allocator to deallocate with |
| memory | - The memory to deallocate |
Variable documentation
const CnxAllocator DEFAULT_ALLOCATOR
#include <include/Cnx/Allocators.h>
The default CnxAllocator
Define documentation
#define CNX_DEFAULT_ALLOCATOR_FUNCTION
#include <include/Cnx/Allocators.h>
The default CnxAllocator allocation function Define this to your desired function to override the default allocation function.
#define CNX_DEFAULT_DEALLOCATOR_FUNCTION
#include <include/Cnx/Allocators.h>
The default CnxAllocator deallocation (free) function Define this to your desired function to override the default deallocation function.
#define CNX_ALLOCATOR_ABORT_ON_ALLOCATION_FAILURE
#include <include/Cnx/Allocators.h>
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)
#include <include/Cnx/Allocators.h>
Implements CnxAllocator for the given custom allocator type.
| Parameters | |
|---|---|
| SelfType | - The type of self |
| allocate | - The function to allocate memory with |
| reallocate | - The function to reallocate memory with |
| deallocate | - The function to deallocate memory with |
#define cnx_allocator_from_custom_typed_allocator(SelfType,
self)
#include <include/Cnx/Allocators.h>
Creates a new CnxAllocator with the given custom allocator.
| Parameters | |
|---|---|
| SelfType | - The type of self |
| self | - The allocator's state |
| Returns | a custom CnxAllocator |
#define cnx_allocator_from_custom_stateless_allocator(allocate_function,
reallocate_function,
deallocate_function)
#include <include/Cnx/Allocators.h>
Creates a new "stateless" CnxAllocator with the given custom allocator functions.
| Parameters | |
|---|---|
| allocate_function | - The function to allocate memory with |
| reallocate_function | - The function to reallocate memory with |
| deallocate_function | - The function to deallocate memory with |
| Returns | a custom CnxAllocator |
#define CNX_DEFAULT_ALLOCATOR
#include <include/Cnx/Allocators.h>
The default CnxAllocator Define this to your desired CnxAllocator compound literal to override the default CnxAllocator
#define cnx_allocator_allocate_t(T,
allocator)
#include <include/Cnx/Allocators.h>
Allocates enough new memory to store a type T with the given CnxAllocator
| Parameters | |
|---|---|
| T | - The type to allocate memory for |
| allocator | - The CnxAllocator to allocate with |
| Returns | newly allocated memory |
#define cnx_allocator_allocate_array_t(T,
allocator,
num_elements)
#include <include/Cnx/Allocators.h>
Allocates enough new memory to store an array of num_elements of type T with the given CnxAllocator
| Parameters | |
|---|---|
| T | - The type to allocate memory for |
| allocator | - The `CnxAllocator to allocate with |
| num_elements | - The number of elements in the array |
| Returns | newly allocated memory |
#define cnx_allocator_reallocate_t(T,
allocator,
memory_ptr)
#include <include/Cnx/Allocators.h>
Allocates new memory large enough to store a T, and copies the old contents over.
| Parameters | |
|---|---|
| T | - The type to allocate memory for |
| allocator | - The CnxAllocator to allocate memory with |
| memory_ptr | - The memory to be reallocated |
| Returns | reallocated memory |
#define cnx_allocator_reallocate_array_t(T,
allocator,
memory_ptr,
old_num_elements,
new_num_elements)
#include <include/Cnx/Allocators.h>
Allocates new memory large enough to store new_num_elements elements of type T, and copies the old contents over.
| Parameters | |
|---|---|
| T | - The type to allocate memory for |
| allocator | - The CnxAllocator to allocate memory with |
| memory_ptr | - The memory to be reallocated |
| old_num_elements | - The previous number of elements in the memory |
| new_num_elements | - The desired new number of elements in the memory |
| Returns | reallocated memory |
#define cnx_memcpy(T,
dest_ptr,
src_ptr,
num_elements)
#include <include/Cnx/Allocators.h>
Copies memory containing an array of type T from src_ptr to dst_ptr
| Parameters | |
|---|---|
| T | - The type contained in the array |
| dest_ptr | - The destination of the copied data |
| src_ptr | - The source of the data to copy |
| num_elements | - The number of elements in the source array |
#define cnx_memmove(T,
dest_ptr,
src_ptr,
num_elements)
#include <include/Cnx/Allocators.h>
Moves memory containing an array of type T from src_ptr to dst_ptr
| Parameters | |
|---|---|
| T | - The type contained in the array |
| dest_ptr | - The destination of the moved data |
| src_ptr | - The source of the data to move |
| num_elements | - The number of elements in the source array |
#define cnx_memset(T,
dest_ptr,
value,
num_elements)
#include <include/Cnx/Allocators.h>
Sets the memory at dest_ptr, containing an array of type T, to the given value
| Parameters | |
|---|---|
| T | - The type contained in the array |
| dest_ptr | - The location of the memory to set the value of |
| value | - The value to set the memory located at dest_ptr to |
| num_elements | - The number of elements in the destination array |