rcutils
master
C API providing common utilities and data structures.
|
Encapsulation of an allocator. More...
#include <allocator.h>
Public Attributes | |
void *(* | allocate )(size_t size, void *state) |
Allocate memory, given a size and the state pointer. More... | |
void(* | deallocate )(void *pointer, void *state) |
Deallocate previously allocated memory, mimicking free(). More... | |
void *(* | reallocate )(void *pointer, size_t size, void *state) |
Reallocate if possible, otherwise it deallocates and allocates. More... | |
void *(* | zero_allocate )(size_t number_of_elements, size_t size_of_element, void *state) |
Allocate memory with all elements set to zero, given a number of elemets and their size. More... | |
void * | state |
Implementation defined state storage. More... | |
Encapsulation of an allocator.
The default allocator uses malloc(), free(), calloc(), and realloc(). It can be obtained using rcutils_get_default_allocator().
The allocator should be trivially copyable. Meaning that the struct should continue to work after being assignment copied into a new struct. Specifically the object pointed to by the state pointer should remain valid until all uses of the allocator have been made. Particular care should be taken when giving an allocator to functions like rcutils_*_init() where it is stored within another object and used later. Developers should note that, while the fields of a const-qualified allocator struct cannot be modified, the state of the allocator can be modified.
void*(* rcutils_allocator_t::allocate) (size_t size, void *state) |
Allocate memory, given a size and the state
pointer.
An error should be indicated by returning NULL
.
void(* rcutils_allocator_t::deallocate) (void *pointer, void *state) |
Deallocate previously allocated memory, mimicking free().
Also takes the state
pointer.
void*(* rcutils_allocator_t::reallocate) (void *pointer, size_t size, void *state) |
Reallocate if possible, otherwise it deallocates and allocates.
Also takes the state
pointer.
If unsupported then do deallocate and then allocate. This should behave as realloc() does, as opposed to posix's reallocf, i.e. the memory given by pointer will not be free'd automatically if realloc() fails. For reallocf-like behavior use rcutils_reallocf(). This function must be able to take an input pointer of NULL
and succeed.
void*(* rcutils_allocator_t::zero_allocate) (size_t number_of_elements, size_t size_of_element, void *state) |
Allocate memory with all elements set to zero, given a number of elemets and their size.
An error should be indicated by returning NULL
.
void* rcutils_allocator_t::state |
Implementation defined state storage.
This is passed as the final parameter to other allocator functions. Note that the contents of the state can be modified even in const-qualified allocator objects.