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 "xtc_export.h"
33
34#include <stddef.h>
35#include <stdint.h>
36
37#include "xtc.h"
38
39typedef struct xtc_mctx xtc_mctx_t;
40
41typedef enum xtc_mctx_flags {
42 XTC_MCTX_DEFAULT = 0,
43 XTC_MCTX_THREAD_SAFE = 1u << 0 /* internally locked */
44} xtc_mctx_flags_t;
45
46typedef void (*xtc_mctx_cleanup_fn)(void *user);
47
48/*
49 * PUBLIC: int xtc_mctx_create __P((xtc_mctx_t *, const char *, unsigned, xtc_mctx_t **));
50 * PUBLIC: void xtc_mctx_destroy __P((xtc_mctx_t *));
51 * PUBLIC: void xtc_mctx_reset __P((xtc_mctx_t *));
52 *
53 * PUBLIC: void *xtc_mctx_alloc __P((xtc_mctx_t *, size_t));
54 * PUBLIC: void *xtc_mctx_calloc __P((xtc_mctx_t *, size_t, size_t));
55 * PUBLIC: void *xtc_mctx_strdup __P((xtc_mctx_t *, const char *));
56 * PUBLIC: void xtc_mctx_free __P((xtc_mctx_t *, void *));
57 *
58 * PUBLIC: int xtc_mctx_register_cleanup __P((xtc_mctx_t *, xtc_mctx_cleanup_fn, void *));
59 *
60 * PUBLIC: const char *xtc_mctx_name __P((const xtc_mctx_t *));
61 * PUBLIC: size_t xtc_mctx_total_bytes __P((const xtc_mctx_t *));
62 * PUBLIC: size_t xtc_mctx_total_chunks __P((const xtc_mctx_t *));
63 */
64
65/* Create a child context. parent==NULL produces a root context.
66 * name is copied for diagnostics. flags = bitmask of XTC_MCTX_*. */
67XTC_API int xtc_mctx_create(xtc_mctx_t *parent, const char *name,
68 unsigned flags, xtc_mctx_t **out);
69
70/* Destroy a context: recursively destroys children, runs cleanups
71 * bottom-up, frees all chunks. Detaches from parent. */
72XTC_API void xtc_mctx_destroy(xtc_mctx_t *m);
73
74/* Free all allocations and run cleanups, but keep the context (and
75 * its children) usable. Useful for per-iteration scratch contexts. */
76XTC_API void xtc_mctx_reset(xtc_mctx_t *m);
77
78/* Allocate within the context. Returns NULL on failure. */
79XTC_API void *xtc_mctx_alloc(xtc_mctx_t *m, size_t size);
80XTC_API void *xtc_mctx_calloc(xtc_mctx_t *m, size_t n, size_t size);
81
82/* Strdup into the context. Lives until reset/destroy. */
83XTC_API void *xtc_mctx_strdup(xtc_mctx_t *m, const char *s);
84
85/* Free a single allocation early. Optional -- most code just lets
86 * destroy/reset reclaim. */
87XTC_API void xtc_mctx_free(xtc_mctx_t *m, void *p);
88
89/* Register a cleanup callback. Runs at destroy/reset time, before
90 * the chunks are freed. Multiple callbacks run in LIFO order. */
91XTC_API int xtc_mctx_register_cleanup(xtc_mctx_t *m,
92 xtc_mctx_cleanup_fn fn, void *user);
93
94XTC_API const char *xtc_mctx_name(const xtc_mctx_t *m);
95XTC_API size_t xtc_mctx_total_bytes(const xtc_mctx_t *m);
96XTC_API size_t xtc_mctx_total_chunks(const xtc_mctx_t *m);
97
98#endif /* XTC_MCTX_H */