ucx
UAP Common Extensions
Data Structures | Macros | Typedefs | Functions
allocator.h File Reference

Allocator for custom memory management. More...

#include "ucx.h"

Go to the source code of this file.

Data Structures

struct  UcxAllocator
 UCX allocator data structure containing memory management functions. More...
 

Macros

#define almalloc(allocator, n)   ((allocator)->malloc((allocator)->pool, n))
 Shorthand for calling an allocators malloc function. More...
 
#define alcalloc(allocator, n, size)   ((allocator)->calloc((allocator)->pool, n, size))
 Shorthand for calling an allocators calloc function. More...
 
#define alrealloc(allocator, ptr, n)   ((allocator)->realloc((allocator)->pool, ptr, n))
 Shorthand for calling an allocators realloc function. More...
 
#define alfree(allocator, ptr)   ((allocator)->free((allocator)->pool, ptr))
 Shorthand for calling an allocators free function. More...
 
#define UCX_ALLOCATOR_DEFAULT
 Convenient macro for a default allocator struct definition. More...
 

Typedefs

typedef void *(* ucx_allocator_malloc) (void *pool, size_t n)
 A function pointer to the allocators malloc() function. More...
 
typedef void *(* ucx_allocator_calloc) (void *pool, size_t n, size_t size)
 A function pointer to the allocators calloc() function. More...
 
typedef void *(* ucx_allocator_realloc) (void *pool, void *data, size_t n)
 A function pointer to the allocators realloc() function. More...
 
typedef void(* ucx_allocator_free) (void *pool, void *data)
 A function pointer to the allocators free() function. More...
 

Functions

UcxAllocatorucx_default_allocator ()
 Returns a pointer to the default allocator. More...
 
void * ucx_default_malloc (void *ignore, size_t n)
 A wrapper for the standard libc malloc() function. More...
 
void * ucx_default_calloc (void *ignore, size_t n, size_t size)
 A wrapper for the standard libc calloc() function. More...
 
void * ucx_default_realloc (void *ignore, void *data, size_t n)
 A wrapper for the standard libc realloc() function. More...
 
void ucx_default_free (void *ignore, void *data)
 A wrapper for the standard libc free() function. More...
 

Detailed Description

Allocator for custom memory management.

A UCX allocator consists of a pointer to the memory area / pool and four function pointers to memory management functions operating on this memory area / pool. These functions shall behave equivalent to the standard libc functions malloc(), calloc(), realloc() and free().

The signature of the memory management functions is based on the signature of the respective libc function but each of them takes the pointer to the memory area / pool as first argument.

As the pointer to the memory area / pool can be arbitrarily chosen, any data can be provided to the memory management functions. A UcxMempool is just one example.

See also
mempool.h
UcxMap
Author
Mike Becker
Olaf Wintermann

Macro Definition Documentation

◆ alcalloc

#define alcalloc (   allocator,
  n,
  size 
)    ((allocator)->calloc((allocator)->pool, n, size))

Shorthand for calling an allocators calloc function.

Parameters
allocatorthe allocator to use
nthe count of elements the space should be allocated for
sizethe size of each element
Returns
a pointer to the allocated memory area

◆ alfree

#define alfree (   allocator,
  ptr 
)    ((allocator)->free((allocator)->pool, ptr))

Shorthand for calling an allocators free function.

Parameters
allocatorthe allocator to use
ptrthe pointer to the memory area that shall be freed

◆ almalloc

#define almalloc (   allocator,
 
)    ((allocator)->malloc((allocator)->pool, n))

Shorthand for calling an allocators malloc function.

Parameters
allocatorthe allocator to use
nsize of space to allocate
Returns
a pointer to the allocated memory area

◆ alrealloc

#define alrealloc (   allocator,
  ptr,
 
)    ((allocator)->realloc((allocator)->pool, ptr, n))

Shorthand for calling an allocators realloc function.

Parameters
allocatorthe allocator to use
ptrthe pointer to the memory area that shall be reallocated
nthe new size of the allocated memory area
Returns
a pointer to the reallocated memory area

◆ UCX_ALLOCATOR_DEFAULT

#define UCX_ALLOCATOR_DEFAULT
Value:
{NULL, \
ucx_default_malloc, ucx_default_calloc, ucx_default_realloc, \
ucx_default_free }
void * ucx_default_calloc(void *ignore, size_t n, size_t size)
A wrapper for the standard libc calloc() function.
Definition: allocator.c:50
void * ucx_default_realloc(void *ignore, void *data, size_t n)
A wrapper for the standard libc realloc() function.
Definition: allocator.c:54

Convenient macro for a default allocator struct definition.

Typedef Documentation

◆ ucx_allocator_calloc

typedef void*(* ucx_allocator_calloc) (void *pool, size_t n, size_t size)

A function pointer to the allocators calloc() function.

See also
UcxAllocator

◆ ucx_allocator_free

typedef void(* ucx_allocator_free) (void *pool, void *data)

A function pointer to the allocators free() function.

See also
UcxAllocator

◆ ucx_allocator_malloc

typedef void*(* ucx_allocator_malloc) (void *pool, size_t n)

A function pointer to the allocators malloc() function.

See also
UcxAllocator

◆ ucx_allocator_realloc

typedef void*(* ucx_allocator_realloc) (void *pool, void *data, size_t n)

A function pointer to the allocators realloc() function.

See also
UcxAllocator

Function Documentation

◆ ucx_default_allocator()

UcxAllocator* ucx_default_allocator ( )

Returns a pointer to the default allocator.

The default allocator contains wrappers to the standard libc memory management functions. Use this function to get a pointer to a globally available allocator. You may also define an own UcxAllocator by assigning UCX_ALLOCATOR_DEFAULT to a variable and pass the address of this variable to any function that takes a UcxAllocator as argument. Note that using this function is the recommended way of passing a default allocator, thus it never runs out of scope.

Returns
a pointer to the default allocator
See also
UCX_ALLOCATOR_DEFAULT

◆ ucx_default_calloc()

void* ucx_default_calloc ( void *  ignore,
size_t  n,
size_t  size 
)

A wrapper for the standard libc calloc() function.

Parameters
ignoreignored (may be used by allocators for pooled memory)
nargument passed to calloc()
sizeargument passed to calloc()
Returns
return value of calloc()

◆ ucx_default_free()

void ucx_default_free ( void *  ignore,
void *  data 
)

A wrapper for the standard libc free() function.

Parameters
ignoreignored (may be used by allocators for pooled memory)
dataargument passed to free()

◆ ucx_default_malloc()

void* ucx_default_malloc ( void *  ignore,
size_t  n 
)

A wrapper for the standard libc malloc() function.

Parameters
ignoreignored (may be used by allocators for pooled memory)
nargument passed to malloc()
Returns
return value of malloc()

◆ ucx_default_realloc()

void* ucx_default_realloc ( void *  ignore,
void *  data,
size_t  n 
)

A wrapper for the standard libc realloc() function.

Parameters
ignoreignored (may be used by allocators for pooled memory)
dataargumend passed to realloc()
nargument passed to realloc()
Returns
return value of realloc()