e6e4cdd9614d1f45701dd8487c799cf7d827f8bb
Core “make C nicer” utilities
-
xmalloc/xcalloc/xrealloc/xstrdup(header:xalloc.h)- Teaches: error handling patterns,
errno, consistent OOM behavior.
- Teaches: error handling patterns,
-
Arena allocator (header:
arena.h)arena_init,arena_alloc,arena_reset,arena_free- Teaches: alignment, bump allocators, lifetime-based allocation.
-
Dynamic buffer / string builder (header:
buf.h)buf_reserve,buf_append,buf_appendf- Teaches: amortized growth,
vsnprintf, safe formatting.
-
Slice + spans (header:
span.h)typedef struct { uint8_t* p; size_t n; } span_u8;etc.- Teaches: safer APIs than raw pointers, length-carrying.
-
Hash map (header:
hashmap.h) (string→void* or string→u64)- Teaches: hashing, open addressing, tombstones, resizing.
-
Bitset (header:
bitset.h)bitset_set/clear/test,bitset_find_first_zero- Teaches: word operations, popcount/ctz if you want.
C11 correctness features
-
Compile-time assertions & type checks (header:
ctassert.h)- Wrap
_Static_assert, plus some_Generichelpers. - Teaches: compile-time constraints, “making invalid states unrepresentable”.
- Wrap
-
_Genericlogging macros (header:log.h)LOG_INFO("x=%d", x)with file/line, optional type-based formatting helpers.- Teaches: variadic macros,
_Generic, ergonomics.
-
Defer/cleanup pattern (header:
defer.h)- Macro that runs cleanup at scope end (via
gotopattern). - Teaches: structured cleanup in C, error paths.
- Macro that runs cleanup at scope end (via
-
Optional result type (header:
result.h)
typedef struct { int ok; int err; T value; } result_T;pattern.- Teaches: explicit error handling, no hidden global state.
Concurrency & atomics (C11’s “real” new power)
- Atomic reference counting (header:
refcnt.h)
ref_inc/ref_decwith destructor callback.- Teaches:
stdatomic.h, memory ordering (start with seq_cst, then learn relax/acq_rel).
- Lock-free SPSC ring buffer (header:
ring_spsc.h)
- Single-producer/single-consumer queue.
- Teaches: atomics, cache friendliness, correctness reasoning.
- Thread pool (header:
threadpool.h) usingthreads.h
tp_init,tp_submit,tp_join,tp_destroy- Teaches:
thrd_t,mtx_t,cnd_t, work queues.
- Once-init & singletons (header:
once.h)
- Use
once_flag/call_once(or implement if platform lacks). - Teaches: init races, safe global setup.
Parsing / CLI tools (you’ll actually use these)
- Argument parser (header:
argparse.h)
--long,-s, combined short flags,--key=value- Teaches: string parsing, API design, test cases.
- INI parser (header:
ini.h)
- Minimal: sections, key=value, comments.
- Teaches: parsing state machines, callbacks.
- CSV reader/writer (header:
csv.h)
- Correct quoting/escaping.
- Teaches: edge cases, streaming parsing.
- Path utilities (header:
path.h)
path_join,path_dirname,path_basename, normalize..and.- Teaches: portability pitfalls, careful string ops.
Systems-ish building blocks
- File mapping / buffered reader (header:
io.h)
- Portable-ish wrapper for “read whole file”, “iter lines”, “atomic write via temp+rename”.
- Teaches: robust file I/O patterns, error handling.
- Timing + profiling helpers (header:
timeutil.h)
now_ns(),stopwatch,scope_timermacro- Teaches:
timespec_get, measurement pitfalls, microbenchmark hygiene.
A suggested “crash course” order (so it builds)
1–3 (xalloc/arena/buf) → 7–10 (static assert, generic, cleanup, results) → 15 (argparse) → 19 (io) → 16–18 (parsers) → 11–14 (atomics/threads) → 5–6 (hashmap/bitset) → 20 (timing) → 12/13 (ring + threadpool, as capstone)
Two capstone tool ideas that use a lot of the above
logscan: fast log parser + filter + histogram (buf + argparse + csv + hashmap + timeutil)dedupe: file deduplicator by hashing chunks (io + hashmap + arena + threadpool)
Description
collection of library things I've written. My intention is to collect common functions I use here.
Languages
C
89.7%
Makefile
10.3%