nqsetal(3)
NAME
nqsetal — set the
allocator of the capsule and parser layers
LIBRARY
libnoqtis, -lnoqtis
SYNOPSIS
#include
<noqtis.h>
typedef void *(*nqmalfn)(size_t size, void
*user);
typedef void (*nqfreefn)(void *ptr, size_t size, void
*user);
int
nqsetal(nqmalfn
mal, nqfreefn fre,
void *user);
DESCRIPTION
The algorithm code of noqtis never allocates; the capsule layer
(nqseal(3),
nqchk(3)) and its
parsers do, through one hook.
nqsetal()
replaces it: mal is called with the size wanted and
user, and returns the memory or
NULL; fre is called with a
pointer mal returned, the size that was asked for, and
user. The library wipes memory before it gives it to
fre, so a hook need not. The size makes pool and arena
allocators and allocation accounting easy.
mal and fre both
NULL restore the default,
malloc(3) and
free(3).
The hook is global and
nqsetal()
is not thread safe: call it before any other function of the library, and
never while another thread uses the library.
RETURN VALUES
NQOK, or NQEARG
when exactly one of mal and fre
is NULL (the hook is then unchanged). A hook that
returns NULL makes the calling function return
NQEMEM.
EXAMPLES
Count the bytes in use:
static size_t used;
static void *mal(size_t n, void *u) {
void *p = malloc(n);
(void)u;
if (p != NULL)
used += n;
return p;
}
static void fre(void *p, size_t n, void *u) {
(void)u;
used -= n;
free(p);
}
/* at start-up */
nqsetal(mal, fre, NULL);