Libecoli 0.5.0
Extensible COmmand LIne library
 
Loading...
Searching...
No Matches
Allocation

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
 

Detailed Description

Helpers to allocate and free memory.

Interface to configure the allocator used by libecoli. By default, the standard allocation functions from libc are used.

Macro Definition Documentation

◆ ec_malloc

#define ec_malloc ( size)
Value:
({ \
void *ret_; \
if (ec_malloc_handler.malloc == NULL) \
ret_ = malloc(size); \
else \
ret_ = __ec_malloc(size, __FILE__, __LINE__); \
ret_; \
})

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().

Parameters
sizeThe size of the area to allocate in bytes.
Returns
The pointer to the allocated memory, or NULL on error (errno is set).

Definition at line 117 of file ecoli_malloc.h.

◆ ec_free

#define ec_free ( ptr)
Value:
({ \
if (ec_malloc_handler.free == NULL) \
free(ptr); \
else \
__ec_free(ptr, __FILE__, __LINE__); \
})

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.

Parameters
ptrThe pointer to the memory area.

Definition at line 144 of file ecoli_malloc.h.

◆ ec_realloc

#define ec_realloc ( ptr,
size )
Value:
({ \
void *ret_; \
if (ec_malloc_handler.realloc == NULL) \
ret_ = realloc(ptr, size); \
else \
ret_ = __ec_realloc(ptr, size, __FILE__, __LINE__); \
ret_; \
})

Resize an allocated memory area.

Parameters
ptrThe pointer to the previously allocated memory area, or NULL.
sizeThe new size of the memory area.
Returns
A pointer to the newly allocated memory, or NULL if the request fails. In that case, the original area is left untouched.

Definition at line 170 of file ecoli_malloc.h.

◆ ec_calloc

#define ec_calloc ( n,
size )
Value:
({ \
void *ret_; \
if (ec_malloc_handler.malloc == NULL) \
ret_ = calloc(n, size); \
else \
ret_ = __ec_calloc(n, size, __FILE__, __LINE__); \
ret_; \
})

Allocate and initialize an array of elements.

Parameters
nThe number of elements.
sizeThe size of each element.
Returns
The pointer to the allocated memory, or NULL on error (errno is set).

Definition at line 197 of file ecoli_malloc.h.

◆ ec_strdup

#define ec_strdup ( s)
Value:
({ \
void *ret_; \
if (ec_malloc_handler.malloc == NULL) \
ret_ = strdup(s); \
else \
ret_ = __ec_strdup(s, __FILE__, __LINE__); \
ret_; \
})

Duplicate a string.

Memory for the new string is obtained with ec_malloc(), and can be freed with ec_free().

Parameters
sThe string to be duplicated.
Returns
The pointer to the duplicated string, or NULL on error (errno is set).

Definition at line 217 of file ecoli_malloc.h.

◆ ec_strndup

#define ec_strndup ( s,
n )
Value:
({ \
void *ret_; \
if (ec_malloc_handler.malloc == NULL) \
ret_ = strndup(s, n); \
else \
ret_ = __ec_strndup(s, n, __FILE__, __LINE__); \
ret_; \
})

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.

Parameters
sThe string to be duplicated.
nThe maximum length of the new string.
Returns
The pointer to the duplicated string, or NULL on error (errno is set).

Definition at line 240 of file ecoli_malloc.h.

Typedef Documentation

◆ ec_malloc_t

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.

Parameters
sizeThe size of the memory area to allocate.
fileThe path to the file that invoked the malloc.
lineThe line in the file that invoked the malloc.
Returns
A pointer to the allocated memory area, or NULL on error (errno is set).

Definition at line 38 of file ecoli_malloc.h.

◆ ec_free_t

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.

Parameters
ptrThe pointer to the memory area to be freed.
fileThe path to the file that invoked the malloc.
lineThe line in the file that invoked the malloc.

Definition at line 53 of file ecoli_malloc.h.

◆ ec_realloc_t

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.

Parameters
ptrThe pointer to the memory area to be reallocated.
fileThe path to the file that invoked the malloc.
lineThe line in the file that invoked the malloc.
Returns
A pointer to the allocated memory area, or NULL on error (errno is set).

Definition at line 71 of file ecoli_malloc.h.

Function Documentation

◆ ec_malloc_register()

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().

Parameters
usr_mallocA user-defined malloc function.
usr_freeA user-defined free function.
usr_reallocA user-defined realloc function.
Returns
0 on success, or -1 on error (errno is set).

◆ ec_malloc_func()

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.

◆ ec_free_func()

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.

◆ ec_realloc_func()

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.