CAEN Utility  2.0.2
Utilities for CAEN projects
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. 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)
 

Detailed Description

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.

Author
Francesco Pepe
Date
2023

Macro Definition Documentation

◆ c_allocator_freeall_and_return

#define c_allocator_freeall_and_return (   A,
 
)    (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:

return c_allocator_freeall_and_return(&allocator, ret);
Parameters
[in]Apointer to the allocator struct
[in]Rthe value to return

Definition at line 153 of file CAENAllocator.h.

Typedef Documentation

◆ c_allocator_t

typedef struct c_allocator_t c_allocator_t

◆ c_resource_destroy_function

typedef void(* c_resource_destroy_function) (void *)

Definition at line 59 of file CAENAllocatorTypes.h.

Function Documentation

◆ c_allocator_create()

c_allocator_t c_allocator_create ( )

Creates a new empty c_allocator_t

Returns
the new c_allocator_t

Definition at line 42 of file CAENAllocator.c.

Here is the call graph for this function:

◆ c_allocator_resource_create()

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.

Parameters
[in]allocatorpointer to the allocator struct
[in]dffunction used to destroy the resource
[in]resourcethe allocated resource itself (for example the result 'malloc' or 'fopen', etc
Returns
return 'resource' in case of success or NULL otherwhise.
Note
if resource is NULL, the function fails. If an internal error occurs, 'resource' is automatically cleaned using 'df'.

Definition at line 54 of file CAENAllocator.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ c_allocator_malloc()

void* c_allocator_malloc ( c_allocator_t allocator,
size_t  size 
)

Malloc-s a new element of the given size and return it

Parameters
[in]allocatorpointer to the allocator struct
[in]sizethe size of memory to allocate
Returns
the pointer to allocated memory

Definition at line 86 of file CAENAllocator.c.

Here is the call graph for this function:

◆ c_allocator_calloc()

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.

Parameters
[in]allocatorpointer to the allocator struct
[in]nmembthe number of elements to allocate
[in]sizethe size of a single element
Returns
the pointer to allocated memory

Definition at line 90 of file CAENAllocator.c.

Here is the call graph for this function:

◆ c_allocator_error_count()

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

Parameters
[in]allocatorpointer to the allocator struct
Returns
the number of allocation errors

Definition at line 94 of file CAENAllocator.c.

◆ c_allocator_release()

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.

Note
to slightly boost performance, it would be advisable to allocate such resources after all other, because any newly allocated resource is added at the beginning of the internal linkedlist, and the list is iterated from first element to last for element find.
Parameters
[in]allocatorpointer to the allocator struct
[in]ptrpointer to the resource to release

Definition at line 112 of file CAENAllocator.c.

Here is the call graph for this function:

◆ c_allocator_clear()

void c_allocator_clear ( c_allocator_t allocator)

Clears the list of pointer without free-ing them

Parameters
[in]allocatorpointer to the allocator struct

Definition at line 116 of file CAENAllocator.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ c_allocator_free()

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.

Note
to slightly boost performance, it would be advisable to allocate such resources after all other, because any newly allocated resource is added at the beginning of the internal linkedlist, and the list is iterated from first element to last for element find.
Parameters
[in]allocatorpointer to the allocator struct
[in]allocatorpointer to the resource to be destroyed

Definition at line 124 of file CAENAllocator.c.

Here is the call graph for this function:

◆ c_allocator_freeall()

void c_allocator_freeall ( c_allocator_t allocator)

Free-es the entire list of allocated pointers

Parameters
[in]allocatorpointer to the allocator struct

Definition at line 128 of file CAENAllocator.c.

Here is the call graph for this function: