ecoli_alloc - Man Page

Name

ecoli_alloc — Allocation

— Helpers to allocate and free memory.  

Synopsis

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

#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

size The 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.

#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

ptr The pointer to the memory area.

Definition at line 144 of file ecoli_malloc.h.

#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

ptr The pointer to the previously allocated memory area, or NULL.
size The 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.

#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

n The number of elements.
size The 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.

#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

s The 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.

#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

s The string to be duplicated.
n The 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

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

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.

Returns

A pointer to the allocated memory area, or NULL on error (errno is set).

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.

Parameters

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.

Parameters

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.

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

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_malloc A user-defined malloc function.
usr_free A user-defined free function.
usr_realloc A user-defined realloc function.

Returns

0 on success, or -1 on error (errno is set).

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.

Author

Generated automatically by Doxygen for Libecoli from the source code.

Referenced By

The man pages ec_calloc(3), ec_free(3), ec_free_func(3), ec_free_t(3), ec_malloc(3), ec_malloc_func(3), ec_malloc_register(3), ec_malloc_t(3), ec_realloc(3), ec_realloc_func(3), ec_realloc_t(3), ec_strdup(3) and ec_strndup(3) are aliases of ecoli_alloc(3).

Version 0.3.0 Libecoli