|
CAEN Utility
2.0.2
Utilities for CAEN projects
|
Utilities to handle memory allocation in C minimizing memory leaks It provides a malloc-like interface, but keeps an internal list of the allocated pointers. A function can be used to free them all at once, and a function to free all pointers and return a given value is provided. This allows a calling function to quickly allocate resources and return a value free-ing them. More...
Data Structures | |
| struct | c_allocator_t |
Macros | |
| #define | c_allocator_freeall_and_return(A, R) (c_allocator_freeall(A), R) |
Typedefs | |
| typedef struct c_allocator_t | c_allocator_t |
| typedef void(* | c_resource_destroy_function) (void *) |
Functions | |
| c_allocator_t | c_allocator_create () |
| void * | c_allocator_resource_create (c_allocator_t *allocator, c_resource_destroy_function df, void *resource) |
| void * | c_allocator_malloc (c_allocator_t *allocator, size_t size) |
| void * | c_allocator_calloc (c_allocator_t *allocator, size_t nmemb, size_t size) |
| int32_t | c_allocator_error_count (const c_allocator_t *allocator) |
| void | c_allocator_release (c_allocator_t *allocator, void *ptr) |
| void | c_allocator_clear (c_allocator_t *allocator) |
| void | c_allocator_free (c_allocator_t *allocator, void *ptr) |
| void | c_allocator_freeall (c_allocator_t *allocator) |
Utilities to handle memory allocation in C minimizing memory leaks It provides a malloc-like interface, but keeps an internal list of the allocated pointers. A function can be used to free them all at once, and a function to free all pointers and return a given value is provided. This allows a calling function to quickly allocate resources and return a value free-ing them.
Utilities for resource creation/destruction in C.
| #define c_allocator_freeall_and_return | ( | A, | |
| R | |||
| ) | (c_allocator_freeall(A), R) |
Free-es the entire list of allocated pointers and evaluates to the given value. To be used with return like:
| [in] | A | pointer to the allocator struct |
| [in] | R | the value to return |
Definition at line 153 of file CAENAllocator.h.
| typedef struct c_allocator_t c_allocator_t |
| typedef void(* c_resource_destroy_function) (void *) |
Definition at line 59 of file CAENAllocatorTypes.h.
| c_allocator_t c_allocator_create | ( | ) |
Creates a new empty c_allocator_t
Definition at line 42 of file CAENAllocator.c.
| void* c_allocator_resource_create | ( | c_allocator_t * | allocator, |
| c_resource_destroy_function | df, | ||
| void * | resource | ||
| ) |
Adds the allocated 'resource' to its internal list, using function 'df' to destroy it when needed.
| [in] | allocator | pointer to the allocator struct |
| [in] | df | function used to destroy the resource |
| [in] | resource | the allocated resource itself (for example the result 'malloc' or 'fopen', etc |
Definition at line 54 of file CAENAllocator.c.
| void* c_allocator_malloc | ( | c_allocator_t * | allocator, |
| size_t | size | ||
| ) |
Malloc-s a new element of the given size and return it
| [in] | allocator | pointer to the allocator struct |
| [in] | size | the size of memory to allocate |
Definition at line 86 of file CAENAllocator.c.
| void* c_allocator_calloc | ( | c_allocator_t * | allocator, |
| size_t | nmemb, | ||
| size_t | size | ||
| ) |
Calloc-s a new element of the given size, sets it to zero and return it.
| [in] | allocator | pointer to the allocator struct |
| [in] | nmemb | the number of elements to allocate |
| [in] | size | the size of a single element |
Definition at line 90 of file CAENAllocator.c.
| int32_t c_allocator_error_count | ( | const c_allocator_t * | allocator | ) |
Return the number of allocation errors that happened trying to allocate memory with this allocator
| [in] | allocator | pointer to the allocator struct |
Definition at line 94 of file CAENAllocator.c.
| void c_allocator_release | ( | c_allocator_t * | allocator, |
| void * | ptr | ||
| ) |
Removes the given pointer from the internal list. This is useful if you need to keep allocated the memory while you need to free other pointers.
| [in] | allocator | pointer to the allocator struct |
| [in] | ptr | pointer to the resource to release |
Definition at line 112 of file CAENAllocator.c.
| void c_allocator_clear | ( | c_allocator_t * | allocator | ) |
Clears the list of pointer without free-ing them
| [in] | allocator | pointer to the allocator struct |
Definition at line 116 of file CAENAllocator.c.
| void c_allocator_free | ( | c_allocator_t * | allocator, |
| void * | ptr | ||
| ) |
Free-es the given pointer using its destroy function and removes it from the internal list. This is useful if you want to destroy just one of the allocated resources.
| [in] | allocator | pointer to the allocator struct |
| [in] | allocator | pointer to the resource to be destroyed |
Definition at line 124 of file CAENAllocator.c.
| void c_allocator_freeall | ( | c_allocator_t * | allocator | ) |
Free-es the entire list of allocated pointers
| [in] | allocator | pointer to the allocator struct |
Definition at line 128 of file CAENAllocator.c.