skip to content
==> include/tui/arena.h <==
1/* @ajax/tui-arena: a bump allocator. A terminal frame builds many short-lived strings, spans and
2 * lines; they all come from one arena and go with one tui_arena_reset (or tui_arena_free). Memory
3 * never fails visibly: an allocation failure aborts. */
4#ifndef TUI_ARENA_H
5#define TUI_ARENA_H
6
7#include <stdarg.h>
8#include <stddef.h>
9
10typedef struct tui_arena_chunk tui_arena_chunk;
11
12typedef struct {
13    tui_arena_chunk *head; /* the chunk being filled; older chunks follow */
14    size_t used;           /* bytes used in head */
15} tui_arena;
16
17/* Zeroed, 16-byte aligned memory that lives until the arena is reset or freed. */
18void *tui_arena_alloc(tui_arena *a, size_t n);
19/* Grows the last allocation (or copies when it isn't the last). old may be NULL. */
20void *tui_arena_grow(tui_arena *a, void *old, size_t old_n, size_t new_n);
21char *tui_arena_strdup(tui_arena *a, const char *s);
22char *tui_arena_strndup(tui_arena *a, const char *s, size_t n);
23char *tui_arena_fmt(tui_arena *a, const char *fmt, ...)
24#if defined(__GNUC__) || defined(__clang__)
25    __attribute__((format(printf, 2, 3)))
26#endif
27    ;
28char *tui_arena_vfmt(tui_arena *a, const char *fmt, va_list ap);
29/* Forgets every allocation but keeps the first chunk for reuse. */
30void tui_arena_reset(tui_arena *a);
31void tui_arena_free(tui_arena *a);
32
33#endif