libxtc 0.4.0
Async concurrency for C: Tokio + Seastar + BEAM, in one library
Loading...
Searching...
No Matches
xtc_pdict.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_pdict.h
6 * Per-process dictionary -- string-keyed kv store local to each
7 * xtc_proc. Models Erlang's `put/2`, `get/1`, `erase/1`,
8 * `get_keys/0`. Used for:
9 * - per-proc tracing / debug names
10 * - request context (correlation ids, span ids) carried with
11 * a proc as it processes work
12 * - test-time per-proc state injection
13 *
14 * Implementation: tiny linked list inside `struct xtc_proc`.
15 * Linear lookup; entries usually <16 per proc. M11.5 swaps in
16 * a small hash table once we have one.
17 *
18 * All entries live in process memory and are freed at proc exit.
19 * Values are caller-owned `void *`; the store stores the pointer
20 * verbatim (no deep copy). If a value's lifetime should match
21 * the proc, register a destructor via `xtc_pdict_put_with_dtor`.
22 */
23
24#ifndef XTC_PDICT_H
25#define XTC_PDICT_H
26
27#include "xtc_export.h"
28
29#include <stddef.h>
30
31#include "xtc.h"
32
33typedef void (*xtc_pdict_dtor_fn)(void *value);
34
35/*
36 * PUBLIC: int xtc_pdict_put __P((const char *, void *));
37 * PUBLIC: int xtc_pdict_put_with_dtor __P((const char *, void *, xtc_pdict_dtor_fn));
38 * PUBLIC: int xtc_pdict_get __P((const char *, void **));
39 * PUBLIC: int xtc_pdict_erase __P((const char *));
40 * PUBLIC: int xtc_pdict_count __P((void));
41 * PUBLIC: int xtc_pdict_clear __P((void));
42 */
43
44/* All operations apply to the calling proc's dict. When called
45 * outside any proc, all return XTC_E_INVAL. */
46
47XTC_API int xtc_pdict_put(const char *key, void *value);
48XTC_API int xtc_pdict_put_with_dtor(const char *key, void *value,
49 xtc_pdict_dtor_fn dtor);
50
51/* Retrieve. Sets *value if found; returns XTC_E_INVAL if no entry. */
52XTC_API int xtc_pdict_get(const char *key, void **value);
53
54/* Remove (and run destructor if any). Returns XTC_OK if erased,
55 * XTC_E_INVAL if absent. */
56XTC_API int xtc_pdict_erase(const char *key);
57
58XTC_API int xtc_pdict_count(void);
59XTC_API int xtc_pdict_clear(void);
60
61#endif /* XTC_PDICT_H */