Libecoli 0.3.0
Extensible COmmand LIne library
|
Helpers to allocate and free memory. More...
Data Structures | |
struct | ec_malloc_handler |
Macros | |
#define | ec_malloc(size) |
#define | ec_free(ptr) |
#define | ec_realloc(ptr, size) |
#define | ec_calloc(n, size) |
#define | ec_strdup(s) |
#define | ec_strndup(s, n) |
Typedefs | |
typedef void *(* | ec_malloc_t) (size_t size, const char *file, unsigned int line) |
typedef void(* | ec_free_t) (void *ptr, const char *file, unsigned int line) |
typedef void *(* | ec_realloc_t) (void *ptr, size_t size, const char *file, unsigned int line) |
Functions | |
int | ec_malloc_register (ec_malloc_t usr_malloc, ec_free_t usr_free, ec_realloc_t usr_realloc) |
void * | ec_malloc_func (size_t size) |
void | ec_free_func (void *ptr) |
void | ec_realloc_func (void *ptr, size_t size) |
Variables | |
struct ec_malloc_handler | ec_malloc_handler |
Helpers to allocate and free memory.
Interface to configure the allocator used by libecoli. By default, the standard allocation functions from libc are used.
#define ec_malloc | ( | size | ) |
Allocate a memory area.
Like malloc(), ec_malloc() allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized. The memory is freed with ec_free().
size | The size of the area to allocate in bytes. |
Definition at line 117 of file ecoli_malloc.h.
#define ec_free | ( | ptr | ) |
Free a memory area.
Like free(), ec_free() frees the area pointed by ptr, which must have been returned by a previous call to ec_malloc() or any other allocation function of this file.
ptr | The pointer to the memory area. |
Definition at line 144 of file ecoli_malloc.h.
#define ec_realloc | ( | ptr, | |
size ) |
Resize an allocated memory area.
ptr | The pointer to the previously allocated memory area, or NULL. |
size | The new size of the memory area. |
Definition at line 170 of file ecoli_malloc.h.
#define ec_calloc | ( | n, | |
size ) |
Allocate and initialize an array of elements.
n | The number of elements. |
size | The size of each element. |
Definition at line 197 of file ecoli_malloc.h.
#define ec_strdup | ( | s | ) |
Duplicate a string.
Memory for the new string is obtained with ec_malloc(), and can be freed with ec_free().
s | The string to be duplicated. |
Definition at line 217 of file ecoli_malloc.h.
#define ec_strndup | ( | s, | |
n ) |
Duplicate at most n bytes of a string.
This function is similar to ec_strdup(), except that it copies at most n bytes. If s is longer than n, only n bytes are copied, and a terminating null byte ('\0') is added.
s | The string to be duplicated. |
n | The maximum length of the new string. |
Definition at line 240 of file ecoli_malloc.h.
typedef void *(* ec_malloc_t) (size_t size, const char *file, unsigned int line) |
Function type of malloc, passed to ec_malloc_register().
The API is the same than malloc(), excepted the file and line arguments.
size | The size of the memory area to allocate. |
file | The path to the file that invoked the malloc. |
line | The line in the file that invoked the malloc. |
Definition at line 38 of file ecoli_malloc.h.
typedef void(* ec_free_t) (void *ptr, const char *file, unsigned int line) |
Function type of free, passed to ec_malloc_register().
The API is the same than free(), excepted the file and line arguments.
ptr | The pointer to the memory area to be freed. |
file | The path to the file that invoked the malloc. |
line | The line in the file that invoked the malloc. |
Definition at line 53 of file ecoli_malloc.h.
typedef void *(* ec_realloc_t) (void *ptr, size_t size, const char *file, unsigned int line) |
Function type of realloc, passed to ec_malloc_register().
The API is the same than realloc(), excepted the file and line arguments.
ptr | The pointer to the memory area to be reallocated. |
file | The path to the file that invoked the malloc. |
line | The line in the file that invoked the malloc. |
Definition at line 71 of file ecoli_malloc.h.
int ec_malloc_register | ( | ec_malloc_t | usr_malloc, |
ec_free_t | usr_free, | ||
ec_realloc_t | usr_realloc ) |
Register allocation functions.
This function can be use to register another allocator to be used by libecoli. By default, ec_malloc(), ec_free() and ec_realloc() use the standard libc allocator. Another handler can be used for debug purposes or when running in a specific environment.
This function must be called before ec_init().
usr_malloc | A user-defined malloc function. |
usr_free | A user-defined free function. |
usr_realloc | A user-defined realloc function. |
void * ec_malloc_func | ( | size_t | size | ) |
Ecoli malloc function.
Use this function when the macro ec_malloc() cannot be used, for instance when it is passed as a callback pointer.
void ec_free_func | ( | void * | ptr | ) |
Ecoli free function.
Use this function when the macro ec_free() cannot be used, for instance when it is passed as a callback pointer.
void ec_realloc_func | ( | void * | ptr, |
size_t | size ) |
Ecoli realloc function.
Use this function when the macro ec_realloc() cannot be used, for instance when it is passed as a callback pointer.