libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
xtc_mctx.h
1/*-
2 * Copyright (c) 2026, The XTC Project
3 * Use of this source code is governed by the ISC License.
4 *
5 * src/inc/xtc_mctx.h
6 * Memory contexts: hierarchical allocation pools with parent-
7 * tracked lifetime and bulk reset/destroy.
8 *
9 * A context owns:
10 * - all allocations made within it (chained on a free list);
11 * - zero or more child contexts (siblings of each other);
12 * - optional "before-destroy" callbacks for non-memory cleanup.
13 *
14 * Destroying a context destroys its children first, runs the
15 * cleanup callbacks bottom-up, then frees all chunks. Reset
16 * frees chunks but keeps children alive.
17 *
18 * M11 deliberately ships a simple-but-correct allocator first
19 * (every allocation = malloc + chain). M11.5 will swap in slab
20 * caches for fixed-size hot paths and arena-free for large
21 * short-lived contexts. The public API doesn't change.
22 *
23 * Thread-safety: each context carries an optional pthread mutex.
24 * The default factory makes contexts unlocked for the per-loop
25 * (single-thread) common case; a flag enables locking for
26 * contexts that legitimately span threads.
27 */
28
29#ifndef XTC_MCTX_H
30#define XTC_MCTX_H
31
32#include <stddef.h>
33#include <stdint.h>
34
35#include "xtc.h"
36
37typedef struct xtc_mctx xtc_mctx_t;
38
39typedef enum xtc_mctx_flags {
40 XTC_MCTX_DEFAULT = 0,
41 XTC_MCTX_THREAD_SAFE = 1u << 0 /* internally locked */
42} xtc_mctx_flags_t;
43
44typedef void (*xtc_mctx_cleanup_fn)(void *user);
45
46/*
47 * PUBLIC: int xtc_mctx_create __P((xtc_mctx_t *, const char *, unsigned, xtc_mctx_t **));
48 * PUBLIC: void xtc_mctx_destroy __P((xtc_mctx_t *));
49 * PUBLIC: void xtc_mctx_reset __P((xtc_mctx_t *));
50 *
51 * PUBLIC: void *xtc_mctx_alloc __P((xtc_mctx_t *, size_t));
52 * PUBLIC: void *xtc_mctx_calloc __P((xtc_mctx_t *, size_t, size_t));
53 * PUBLIC: void *xtc_mctx_strdup __P((xtc_mctx_t *, const char *));
54 * PUBLIC: void xtc_mctx_free __P((xtc_mctx_t *, void *));
55 *
56 * PUBLIC: int xtc_mctx_register_cleanup __P((xtc_mctx_t *, xtc_mctx_cleanup_fn, void *));
57 *
58 * PUBLIC: const char *xtc_mctx_name __P((const xtc_mctx_t *));
59 * PUBLIC: size_t xtc_mctx_total_bytes __P((const xtc_mctx_t *));
60 * PUBLIC: size_t xtc_mctx_total_chunks __P((const xtc_mctx_t *));
61 */
62
63/* Create a child context. parent==NULL produces a root context.
64 * name is copied for diagnostics. flags = bitmask of XTC_MCTX_*. */
65int xtc_mctx_create(xtc_mctx_t *parent, const char *name,
66 unsigned flags, xtc_mctx_t **out);
67
68/* Destroy a context: recursively destroys children, runs cleanups
69 * bottom-up, frees all chunks. Detaches from parent. */
70void xtc_mctx_destroy(xtc_mctx_t *m);
71
72/* Free all allocations and run cleanups, but keep the context (and
73 * its children) usable. Useful for per-iteration scratch contexts. */
74void xtc_mctx_reset(xtc_mctx_t *m);
75
76/* Allocate within the context. Returns NULL on failure. */
77void *xtc_mctx_alloc(xtc_mctx_t *m, size_t size);
78void *xtc_mctx_calloc(xtc_mctx_t *m, size_t n, size_t size);
79
80/* Strdup into the context. Lives until reset/destroy. */
81void *xtc_mctx_strdup(xtc_mctx_t *m, const char *s);
82
83/* Free a single allocation early. Optional -- most code just lets
84 * destroy/reset reclaim. */
85void xtc_mctx_free(xtc_mctx_t *m, void *p);
86
87/* Register a cleanup callback. Runs at destroy/reset time, before
88 * the chunks are freed. Multiple callbacks run in LIFO order. */
89int xtc_mctx_register_cleanup(xtc_mctx_t *m,
90 xtc_mctx_cleanup_fn fn, void *user);
91
92const char *xtc_mctx_name(const xtc_mctx_t *m);
93size_t xtc_mctx_total_bytes(const xtc_mctx_t *m);
94size_t xtc_mctx_total_chunks(const xtc_mctx_t *m);
95
96#endif /* XTC_MCTX_H */