libusual  0.1
Data Structures | Defines | Functions | Variables
usual/cxalloc.h File Reference

Context-based Memory Allocator. More...

Data Structures

struct  CxOps
 Ops for allocator that takes context. More...
struct  CxMem
 Memory allocation context. More...

Defines

#define USUAL_ALLOC
 Default allocator.

Functions

void * cx_alloc (CxMem *cx, size_t len) _MALLOC
 Allocate memory from context.
void * cx_realloc (CxMem *cx, void *ptr, size_t len)
 Change existing allocation.
void cx_free (CxMem *cx, const void *ptr)
 Free existing allocation.
void cx_destroy (CxMem *cx)
 Release all memory allocated in context.
void * cx_alloc0 (CxMem *cx, size_t len) _MALLOC
 Allocate and zero-fill memory.
void * cx_memdup (CxMem *cx, const void *src, size_t len) _MALLOC
 Allocate and copy.
void * cx_strdup (CxMem *cx, const char *str) _MALLOC
 Allocate and copy string.
int cx_asprintf (CxMem *cx, char **dst_p, const char *fmt,...) _PRINTF(3
 Print to allocated string, return length or -1 on error.
int int cx_vasprintf (CxMem *cx, char **dst_p, const char *fmt, va_list ap) _PRINTF(3
 Print to allocated string, return length or -1 on error.
int int char * cx_sprintf (CxMem *cx, const char *fmt,...) _PRINTF(2
 Print to allocated string, return new string or NULL on error.
int int char char * cx_vsprintf (CxMem *cx, const char *fmt, va_list ap) _PRINTF(2
 Print to allocated string, return new string or NULL on error.

Variables

int int char char CxMem cx_libc_allocator
 Allocator that uses libc malloc/realloc/free.

Detailed Description

Context-based Memory Allocator.

The idea is that each data structure is given a context to allocate from, and it can create subcontext for that which can be specific allocation pattern that matches the data structure.

It is slightly more work to use than palloc (PostgreSQL) or talloc (Samba), but it avoids the need to have big fully-featured framework that does everything at once.

Instead you have small task-specific allocators, and you can always fall back to raw malloc if you want to valgrind the code.

Potential variants:


Define Documentation

#define USUAL_ALLOC

Default allocator.


Function Documentation

void* cx_alloc ( CxMem cx,
size_t  len 
)

Allocate memory from context.

Returns NULL if no memory or len == 0.

void* cx_realloc ( CxMem cx,
void *  ptr,
size_t  len 
)

Change existing allocation.

If ptr is NULL it creates new allocation. If len is 0 it frees the memory.

void cx_free ( CxMem cx,
const void *  ptr 
)

Free existing allocation.

Does nothing if ptr is NULL.

Referenced by hashtab_destroy().

void cx_destroy ( CxMem cx)

Release all memory allocated in context.

Should be called only on contexts that support it.

void* cx_alloc0 ( CxMem cx,
size_t  len 
)

Allocate and zero-fill memory.

Referenced by hashtab_create().

void* cx_memdup ( CxMem cx,
const void *  src,
size_t  len 
)

Allocate and copy.

void* cx_strdup ( CxMem cx,
const char *  str 
)

Allocate and copy string.

int cx_asprintf ( CxMem cx,
char **  dst_p,
const char *  fmt,
  ... 
)

Print to allocated string, return length or -1 on error.

int int cx_vasprintf ( CxMem cx,
char **  dst_p,
const char *  fmt,
va_list  ap 
)

Print to allocated string, return length or -1 on error.

int int char* cx_sprintf ( CxMem cx,
const char *  fmt,
  ... 
)

Print to allocated string, return new string or NULL on error.

int int char char* cx_vsprintf ( CxMem cx,
const char *  fmt,
va_list  ap 
)

Print to allocated string, return new string or NULL on error.


Variable Documentation

int int char char CxMem cx_libc_allocator

Allocator that uses libc malloc/realloc/free.