xtc_slab(3)
---xtc_slab(3)
slab + magazine allocator
| XTC_SLAB(3) | Library Functions Manual | XTC_SLAB(3) |
NAME
xtc_slab_create,
xtc_slab_destroy,
xtc_slab_alloc,
xtc_slab_free,
xtc_slab_reap,
xtc_slab_stat,
xtc_slab_offset,
xtc_slab_resolve,
xtc_slab_pressure_listen,
xtc_slab_pressure_listen_ex,
xtc_slab_pressure_stop,
xtc_slab_reaper_spawn — slab
+ magazine allocator
SYNOPSIS
#include <xtc.h>
#include <xtc_slab.h>
int
xtc_slab_create(const
xtc_slab_opts_t *opts,
xtc_slab_t **out);
void
xtc_slab_destroy(xtc_slab_t
*slab);
void *
xtc_slab_alloc(xtc_slab_t
*slab);
void
xtc_slab_free(xtc_slab_t
*slab, void
*obj);
int
xtc_slab_reap(xtc_slab_t
*slab);
int
xtc_slab_stat(const
xtc_slab_t *slab,
xtc_slab_stats_t
*out);
xtc_slab_off_t
xtc_slab_offset(const
xtc_slab_t *slab, const
void *p);
void *
xtc_slab_resolve(const
xtc_slab_t *slab,
xtc_slab_off_t off);
int
xtc_slab_pressure_listen(const
char *psi_path,
xtc_slab_pressure_fn fn,
void *user);
int
xtc_slab_pressure_listen_ex(const
char *psi_path,
xtc_slab_pressure_fn fn,
void *user,
xtc_slab_pressure_t
**out);
int
xtc_slab_pressure_stop(xtc_slab_pressure_t
*handle);
int
xtc_slab_reaper_spawn(xtc_loop_t
*loop, int64_t
interval_ns, xtc_pid_t
*out_pid);
DESCRIPTION
xtc_slab is a libumem-style slab allocator
with per-loop magazines. Every fixed-size hot-path allocation in xtc routes
through it (lockmgr lock entries, proc link/monitor entries, RCU retired)
(nodes, timer nodes); applications can use it directly for any pool of
same-sized objects.
A slab is a cache of objects of a single size. Objects are carved from chunks (mmap'd regions, default 64 KiB); each chunk holds many slots. Free objects are linked through their first sizeof(void *) bytes.
Magazines: each thread keeps a small per-cache cache (default 16 slots) of recently-freed objects. Allocs hit the magazine (lock-free, no atomics); on miss the cache lock is taken and the magazine is refilled from a partial chunk. Frees push to the magazine; when full, the magazine spills back to the cache under lock.
Constructor / destructor: optional callbacks run once per object lifetime. Constructor on the first time an object is pulled from a slot; destructor at cache destroy or chunk reap.
XTC_SLAB_PROCESS_LOCAL- Default. Chunks via mmap(2)
MAP_ANONYMOUS. XTC_SLAB_SHARED_MEMORY- Caller passes an mmap'd
MAP_SHAREDfd; the slab carves chunks from that region. Object handles are xtc_slab_off_t offsets that two processes can resolve to local pointers usingxtc_slab_resolve(). The shared header at the start of the region holds an atomic cursor so concurrent processes don't collide on chunk allocation.
XTC_SLAB_REDZONE- 8-byte magic before+after every object; checked on free.
XTC_SLAB_AUDIT- 64-event ring of recent (alloc, free) events.
XTC_SLAB_BACKTRACE- Backtrace each event (Linux/glibc only).
XTC_SLAB_NO_MAGAZINE- Always go through the cache lock. For diagnostic use.
OOM policies control behaviour when no chunk can be allocated:
XTC_SLAB_OOM_FAIL- Return NULL. Default.
XTC_SLAB_OOM_BACKOFF- Brief usleep + retry once, then NULL.
XTC_SLAB_OOM_ABORTabort() (debug builds).
xtc_slab_create()
allocates a cache. opts is required and must set
opts->name and
opts->obj_size.
xtc_slab_alloc()
returns a fresh object pointer or NULL on OOM.
xtc_slab_free()
returns it to the cache.
xtc_slab_reap()
drops magazines and returns empty chunks to the OS. Useful under memory
pressure.
xtc_slab_pressure_listen()
attaches a callback that fires when Linux PSI memory pressure is detected.
When triggered, all registered slab caches are reaped. Returns
XTC_E_NOSYS on platforms without PSI.
xtc_slab_reaper_spawn()
spawns an xtc_proc(3) that periodically calls
xtc_slab_reap_all()
(returns total objects reaped).
EXAMPLES
struct widget { int id; char name[32]; };
xtc_slab_opts_t opts = XTC_SLAB_OPTS_DEFAULT;
opts.name = "widgets";
opts.obj_size = sizeof(struct widget);
opts.flags = XTC_SLAB_REDZONE | XTC_SLAB_AUDIT;
xtc_slab_t *slab;
xtc_slab_create(&opts, &slab);
struct widget *w = xtc_slab_alloc(slab);
w->id = 42;
strcpy(w->name, "answer");
xtc_slab_free(slab, w);
xtc_slab_destroy(slab);
SEE ALSO
HISTORY
Modeled on libumem. Appeared in xtc 0.1.
| May 28, 2026 | Debian |