ucx
UAP Common Extensions
|
Allocator for custom memory management. More...
#include "ucx.h"
Go to the source code of this file.
Data Structures | |
struct | UcxAllocator |
UCX allocator data structure containing memory management functions. More... | |
Macros | |
#define | almalloc(allocator, n) ((allocator)->malloc((allocator)->pool, n)) |
Shorthand for calling an allocators malloc function. More... | |
#define | alcalloc(allocator, n, size) ((allocator)->calloc((allocator)->pool, n, size)) |
Shorthand for calling an allocators calloc function. More... | |
#define | alrealloc(allocator, ptr, n) ((allocator)->realloc((allocator)->pool, ptr, n)) |
Shorthand for calling an allocators realloc function. More... | |
#define | alfree(allocator, ptr) ((allocator)->free((allocator)->pool, ptr)) |
Shorthand for calling an allocators free function. More... | |
#define | UCX_ALLOCATOR_DEFAULT |
Convenient macro for a default allocator struct definition. More... | |
Typedefs | |
typedef void *(* | ucx_allocator_malloc) (void *pool, size_t n) |
A function pointer to the allocators malloc() function. More... | |
typedef void *(* | ucx_allocator_calloc) (void *pool, size_t n, size_t size) |
A function pointer to the allocators calloc() function. More... | |
typedef void *(* | ucx_allocator_realloc) (void *pool, void *data, size_t n) |
A function pointer to the allocators realloc() function. More... | |
typedef void(* | ucx_allocator_free) (void *pool, void *data) |
A function pointer to the allocators free() function. More... | |
Functions | |
UcxAllocator * | ucx_default_allocator () |
Returns a pointer to the default allocator. More... | |
void * | ucx_default_malloc (void *ignore, size_t n) |
A wrapper for the standard libc malloc() function. More... | |
void * | ucx_default_calloc (void *ignore, size_t n, size_t size) |
A wrapper for the standard libc calloc() function. More... | |
void * | ucx_default_realloc (void *ignore, void *data, size_t n) |
A wrapper for the standard libc realloc() function. More... | |
void | ucx_default_free (void *ignore, void *data) |
A wrapper for the standard libc free() function. More... | |
Allocator for custom memory management.
A UCX allocator consists of a pointer to the memory area / pool and four function pointers to memory management functions operating on this memory area / pool. These functions shall behave equivalent to the standard libc functions malloc(), calloc(), realloc()
and free()
.
The signature of the memory management functions is based on the signature of the respective libc function but each of them takes the pointer to the memory area / pool as first argument.
As the pointer to the memory area / pool can be arbitrarily chosen, any data can be provided to the memory management functions. A UcxMempool is just one example.
#define alcalloc | ( | allocator, | |
n, | |||
size | |||
) | ((allocator)->calloc((allocator)->pool, n, size)) |
Shorthand for calling an allocators calloc function.
allocator | the allocator to use |
n | the count of elements the space should be allocated for |
size | the size of each element |
#define alfree | ( | allocator, | |
ptr | |||
) | ((allocator)->free((allocator)->pool, ptr)) |
Shorthand for calling an allocators free function.
allocator | the allocator to use |
ptr | the pointer to the memory area that shall be freed |
#define almalloc | ( | allocator, | |
n | |||
) | ((allocator)->malloc((allocator)->pool, n)) |
Shorthand for calling an allocators malloc function.
allocator | the allocator to use |
n | size of space to allocate |
#define alrealloc | ( | allocator, | |
ptr, | |||
n | |||
) | ((allocator)->realloc((allocator)->pool, ptr, n)) |
Shorthand for calling an allocators realloc function.
allocator | the allocator to use |
ptr | the pointer to the memory area that shall be reallocated |
n | the new size of the allocated memory area |
#define UCX_ALLOCATOR_DEFAULT |
Convenient macro for a default allocator struct
definition.
typedef void*(* ucx_allocator_calloc) (void *pool, size_t n, size_t size) |
A function pointer to the allocators calloc()
function.
typedef void(* ucx_allocator_free) (void *pool, void *data) |
A function pointer to the allocators free()
function.
typedef void*(* ucx_allocator_malloc) (void *pool, size_t n) |
A function pointer to the allocators malloc()
function.
typedef void*(* ucx_allocator_realloc) (void *pool, void *data, size_t n) |
A function pointer to the allocators realloc()
function.
UcxAllocator* ucx_default_allocator | ( | ) |
Returns a pointer to the default allocator.
The default allocator contains wrappers to the standard libc memory management functions. Use this function to get a pointer to a globally available allocator. You may also define an own UcxAllocator by assigning UCX_ALLOCATOR_DEFAULT to a variable and pass the address of this variable to any function that takes a UcxAllocator as argument. Note that using this function is the recommended way of passing a default allocator, thus it never runs out of scope.
void* ucx_default_calloc | ( | void * | ignore, |
size_t | n, | ||
size_t | size | ||
) |
A wrapper for the standard libc calloc()
function.
ignore | ignored (may be used by allocators for pooled memory) |
n | argument passed to calloc() |
size | argument passed to calloc() |
calloc()
void ucx_default_free | ( | void * | ignore, |
void * | data | ||
) |
A wrapper for the standard libc free()
function.
ignore | ignored (may be used by allocators for pooled memory) |
data | argument passed to free() |
void* ucx_default_malloc | ( | void * | ignore, |
size_t | n | ||
) |
A wrapper for the standard libc malloc()
function.
ignore | ignored (may be used by allocators for pooled memory) |
n | argument passed to malloc() |
malloc()
void* ucx_default_realloc | ( | void * | ignore, |
void * | data, | ||
size_t | n | ||
) |
A wrapper for the standard libc realloc()
function.
ignore | ignored (may be used by allocators for pooled memory) |
data | argumend passed to realloc() |
n | argument passed to realloc() |
realloc()