From d0657d8cbc8439bfa2de4eca4afa6062f1ef7222 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Fri, 6 Nov 2026 05:49:55 +0300 Subject: feat(gpa): add general purpose allocator --- include/gpa.h | 210 +++++++++++++++++++++++++++++++++++++++++++++ src/gpa.c | 271 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 481 insertions(+) create mode 100644 include/gpa.h create mode 100644 src/gpa.c diff --git a/include/gpa.h b/include/gpa.h new file mode 100644 index 0000000..13642aa --- /dev/null +++ b/include/gpa.h @@ -0,0 +1,210 @@ +// gpa.h --- General Purpose Allocator -*- C -*- +// +// Copyright (c) 2026 Martin Michalec +// +// SPDX-License-Identifier: MIT +// +// This file is part of COSMOS Codebase. +// +// Commentary: +// +// To disable specific features, define: +// - GPA_NO_FEAT_CTX +// - GPA_NO_FEAT_SYS +// - GPA_NO_FEAT_DBG (auto-disabled with GPA_NO_FEAT_SYS or NDEBUG) +// - GPA_NO_FEAT_UNW +// - GPA_NO_FEAT_UNW_OOM (auto-disabled with GPA_NO_FEAT_UNW) +// - GPA_NO_FEAT_UNW_INVAL (auto-disabled with GPA_NO_FEAT_UNW) +// +// Unwinding is handled centrally in `_gpa`, don't do unwinding in +// actual Gaps. +// +// TODO(cmmm): add tracing support +// TODO(cmmm): save used gpa into gpa's ctx if there was none +// +// Code: + +#ifndef INCLUDED_COSMOS_GPA_H +#define INCLUDED_COSMOS_GPA_H + +#include "types.h" +#include "macros.h" + +// TEMP(cmmm): actually implement FEAT_DBG +#undef GPA_NO_FEAT_DBG +#define GPA_NO_FEAT_DBG + +#if defined (NDEBUG) || defined (GPA_NO_FEAT_SYS) +#undef GPA_NO_FEAT_DBG +#define GPA_NO_FEAT_DBG +#endif // defined (NDEBUG) || defined (GPA_NO_FEAT_SYS) + +#ifdef GPA_NO_FEAT_UNW +#undef GPA_NO_FEAT_UNW_OOM +#define GPA_NO_FEAT_UNW_OOM +#undef GPA_NO_FEAT_UNW_INVAL +#define GPA_NO_FEAT_UNW_INVAL +#endif // GPA_NO_FEAT_UNW + +typedef void *(*gpa_fn) (void *ctx, void *ptr, usz old_sz, usz new_sz, usz spec); + +struct gpa { + gpa_fn _fun; + void *_ctx; +}; + +#define GPA_NULL ((struct gpa) { ._fun = NULL, ._ctx = NULL }) + +struct gpa gpa_from (gpa_fn fun, void *ctx); + +#define GPA_GRAB( gpa, cnt, esz ) _gpa (gpa, NULL, 0, cnt, esz, 0 ) +#define GPA_GRAB_X( gpa, cnt, esz, ext) _gpa (gpa, NULL, 0, cnt, esz, (ext)) +#define GPA_GRAB_0( gpa, cnt, esz ) _gpa (gpa, NULL, 0, cnt, esz, GPA_ZI ) +#define GPA_GRAB_0X( gpa, cnt, esz, ext) _gpa (gpa, NULL, 0, cnt, esz, GPA_ZI | (ext)) +#define GPA_GRAB_T( gpa, T ) ((typeof (T)*) _gpa (gpa, NULL, 0, 1, sizeof ( T ), ALIGN_OF ( T ) - 1 )) +#define GPA_GRAB_TX( gpa, T, ext) ((typeof (T)*) _gpa (gpa, NULL, 0, 1, sizeof ( T ), ALIGN_OF ( T ) - 1 | (ext))) +#define GPA_GRAB_T0( gpa, T ) ((typeof (T)*) _gpa (gpa, NULL, 0, 1, sizeof ( T ), ALIGN_OF ( T ) - 1 | GPA_ZI )) +#define GPA_GRAB_T0X(gpa, T, ext) ((typeof (T)*) _gpa (gpa, NULL, 0, 1, sizeof ( T ), ALIGN_OF ( T ) - 1 | GPA_ZI | (ext))) +#define GPA_GRAB_N( gpa, cnt, T ) ((typeof (T)*) _gpa (gpa, NULL, 0, cnt, sizeof ( T ), ALIGN_OF ( T ) - 1 )) +#define GPA_GRAB_NX( gpa, cnt, T, ext) ((typeof (T)*) _gpa (gpa, NULL, 0, cnt, sizeof ( T ), ALIGN_OF ( T ) - 1 | (ext))) +#define GPA_GRAB_N0( gpa, cnt, T ) ((typeof (T)*) _gpa (gpa, NULL, 0, cnt, sizeof ( T ), ALIGN_OF ( T ) - 1 | GPA_ZI )) +#define GPA_GRAB_N0X(gpa, cnt, T, ext) ((typeof (T)*) _gpa (gpa, NULL, 0, cnt, sizeof ( T ), ALIGN_OF ( T ) - 1 | GPA_ZI | (ext))) +#define GPA_RESZ( gpa, ptr, old_cnt, new_cnt, esz ) _gpa (gpa, ptr, old_cnt, new_cnt, esz, 0 ) +#define GPA_RESZ_X( gpa, ptr, old_cnt, new_cnt, esz, ext) _gpa (gpa, ptr, old_cnt, new_cnt, esz, (ext)) +#define GPA_RESZ_0( gpa, ptr, old_cnt, new_cnt, esz ) _gpa (gpa, ptr, old_cnt, new_cnt, esz, GPA_ZI ) +#define GPA_RESZ_0X( gpa, ptr, old_cnt, new_cnt, esz, ext) _gpa (gpa, ptr, old_cnt, new_cnt, esz, GPA_ZI | (ext)) +#define GPA_RESZ_N( gpa, ptr, old_cnt, new_cnt ) ((typeof (ptr)) _gpa (gpa, ptr, old_cnt, new_cnt, sizeof (*(ptr)), ALIGN_OF (*(ptr)) - 1 )) +#define GPA_RESZ_NX( gpa, ptr, old_cnt, new_cnt, ext) ((typeof (ptr)) _gpa (gpa, ptr, old_cnt, new_cnt, sizeof (*(ptr)), ALIGN_OF (*(ptr)) - 1 | (ext))) +#define GPA_RESZ_N0( gpa, ptr, old_cnt, new_cnt ) ((typeof (ptr)) _gpa (gpa, ptr, old_cnt, new_cnt, sizeof (*(ptr)), ALIGN_OF (*(ptr)) - 1 | GPA_ZI )) +#define GPA_RESZ_N0X(gpa, ptr, old_cnt, new_cnt, ext) ((typeof (ptr)) _gpa (gpa, ptr, old_cnt, new_cnt, sizeof (*(ptr)), ALIGN_OF (*(ptr)) - 1 | GPA_ZI | (ext))) +#define GPA_DROP( gpa, ptr, cnt, esz ) ((void ) _gpa (gpa, ptr, cnt, 0, esz, 0 )) +#define GPA_DROP_X( gpa, ptr, cnt, esz, ext) ((void ) _gpa (gpa, ptr, cnt, 0, esz, (ext))) +#define GPA_DROP_T( gpa, ptr ) ((void ) _gpa (gpa, ptr, 1, 0, sizeof (*(ptr)), 0 )) +#define GPA_DROP_TX( gpa, ptr, ext) ((void ) _gpa (gpa, ptr, 1, 0, sizeof (*(ptr)), (ext))) +#define GPA_DROP_N( gpa, ptr, cnt ) ((void ) _gpa (gpa, ptr, cnt, 0, sizeof (*(ptr)), 0 )) +#define GPA_DROP_NX( gpa, ptr, cnt, ext) ((void ) _gpa (gpa, ptr, cnt, 0, sizeof (*(ptr)), (ext))) + +#define GPA(gpa, ptr, old_cnt, new_cnt, esz, spec) \ + _gpa (gpa, ptr, old_cnt, new_cnt, esz, spec) + +void *_gpa (struct gpa, void *ptr, usz old_cnt, usz new_cnt, usz esz, usz spec); + +#define GPA_ZI ((usz) 1 << (sizeof (usz)*8 - 1)) + +#ifndef GPA_NO_FEAT_UNW +#include "unw.h" +UNW_DECLARE_REASON (OOM); +#define GPA_NO_UNW ((usz) 1 << (sizeof (usz)*8 - 2)) +#else +#define GPA_NO_UNW 0 +#endif // GPA_NO_FEAT_UNW + +#define GPA_ALIGN_MASK ~(GPA_ZI | GPA_NO_UNW) + +#ifndef GPA_NO_FEAT_SYS + +#define GPA_SYS ((struct gpa) { ._fun = gpa_fun_sys, ._ctx = NULL }) +void *gpa_fun_sys (void *ctx, void *ptr, usz old_sz, usz new_sz, usz spec); + +#else // GPA_NO_FEAT_SYS + +#define GPA_SYS GPA_NULL + +#endif // GPA_NO_FEAT_SYS + +#ifndef GPA_NO_FEAT_DBG + +// TODO(cmmm): move to CMK_META section +#define DBG_GPA_WRAP_LIBC_LDFLAGS \ + MV0 ("-Wl,--wrap=malloc" ), \ + MV0 ("-Wl,--wrap=calloc" ), \ + MV0 ("-Wl,--wrap=realloc" ), \ + MV0 ("-Wl,--wrap=reallocarray" ), \ + MV0 ("-Wl,--wrap=free" ), \ + MV0 ("-Wl,--wrap=aligned_alloc" ), \ + MV0 ("-Wl,--wrap=memalign" ), \ + MV0 ("-Wl,--wrap=posix_memalign"), \ + MV0 ("-Wl,--wrap=valloc" ) + +struct dbg_gpa_call { + struct dbg_gpa_call *next_in_ctx; + struct dbg_gpa_call *next_temporal; + struct dbg_loc loc; // TODO(cmmm): replace with backtrace field + /* */ void *old_ptr; + /* */ void *new_ptr; + /* */ usz old_sz; + /* */ usz new_sz; + /* */ usz spec; +}; + +struct dbg_gpa_ctx { + struct dbg_gpa_ctx *next; + struct dbg_gpa_call *calls; + struct gpa orig; + struct dbg_loc loc; // TODO(cmmm): replace with backtrace field + const char *label; + /* */ usz active_allocs; + /* */ usz active_bytes; + /* */ usz peak_bytes; + /* */ usz total_allocs; +}; + +extern struct dbg_gpa_ctx *dbg_gpa_ctx_top; + +#define DBG_GPA(lbl, gpa) _dbg_gpa (lbl, gpa, DBG_LOC) +struct gpa _dbg_gpa (const char *label, struct gpa, struct dbg_loc); + +#else // GPA_NO_FEAT_DBG + +#define DBG_GPA(sym, gpa) gpa + +#endif // GPA_NO_FEAT_DBG + +#ifndef GPA_NO_FEAT_CTX + +struct gpa_ctx_frame { + struct gpa_ctx_frame *next; + struct gpa gpa; +}; +extern THREAD_LOCAL struct gpa_ctx_frame *_gpa_ctx_top; + +struct gpa_ctx_frame *gpa_ctx_beg (struct gpa); +struct gpa gpa_ctx_end (void); +void gpa_ctx_rollback (struct gpa_ctx_frame *); + +#define CTX_GRAB( ...) GPA_GRAB( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_GRAB_X( ...) GPA_GRAB_X( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_GRAB_0( ...) GPA_GRAB_0( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_GRAB_0X( ...) GPA_GRAB_0X( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_GRAB_T( ...) GPA_GRAB_T( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_GRAB_TX( ...) GPA_GRAB_TX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_GRAB_T0( ...) GPA_GRAB_T0( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_GRAB_T0X(...) GPA_GRAB_T0X(_gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_GRAB_N( ...) GPA_GRAB_N( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_GRAB_NX( ...) GPA_GRAB_NX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_GRAB_N0( ...) GPA_GRAB_N0( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_GRAB_N0X(...) GPA_GRAB_N0X(_gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_RESZ( ...) GPA_RESZ( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_RESZ_X( ...) GPA_RESZ_X( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_RESZ_0( ...) GPA_RESZ_0( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_RESZ_0X( ...) GPA_RESZ_0X( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_RESZ_T( ...) GPA_RESZ_T( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_RESZ_TX( ...) GPA_RESZ_TX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_RESZ_T0( ...) GPA_RESZ_T0( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_RESZ_T0X(...) GPA_RESZ_T0X(_gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_RESZ_N( ...) GPA_RESZ_N( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_RESZ_NX( ...) GPA_RESZ_NX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_RESZ_N0( ...) GPA_RESZ_N0( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_RESZ_N0X(...) GPA_RESZ_N0X(_gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_DROP( ...) GPA_DROP( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_DROP_X( ...) GPA_DROP_X( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_DROP_T( ...) GPA_DROP_T( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_DROP_TX( ...) GPA_DROP_TX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_DROP_N( ...) GPA_DROP_N( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) +#define CTX_DROP_NX( ...) GPA_DROP_NX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__) + +#endif // GPA_NO_FEAT_CTX + +#endif // INCLUDED_COSMOS_GPA_H + +// gpa.h ends here diff --git a/src/gpa.c b/src/gpa.c new file mode 100644 index 0000000..48ff723 --- /dev/null +++ b/src/gpa.c @@ -0,0 +1,271 @@ +// gpa.c --- General Purpose Allocator -*- C -*- +// +// Copyright (c) 2026 Martin Michalec +// +// SPDX-License-Identifier: MIT +// +// This file is part of COSMOS Codebase. +// +// Commentary: +// +// Code: + +#include "gpa.h" +#include "dbg.h" + +struct gpa +gpa_from (gpa_fn fun, void *ctx) +{ + DBG_ASSERT_NN (fun); + return (struct gpa) { + ._fun = fun, + ._ctx = ctx, + }; +} + +void * +_gpa (struct gpa gpa, void *ptr, usz old_cnt, usz new_cnt, usz esz, usz spec) +{ + usz old_sz, new_sz; + + if (usz_mul_overflow (old_cnt, esz, &old_sz)) { +#ifndef GPA_NO_FEAT_UNW_INVAL + if (~spec & GPA_NO_UNW) + UNW (INVAL, 1, "old_cnt*esz overflowed!"); +#endif // GPA_NO_FEAT_UNW_INVAL + return NULL; + } + + if (usz_mul_overflow (new_cnt, esz, &new_sz)) { +#ifndef GPA_NO_FEAT_UNW_INVAL + if (~spec & GPA_NO_UNW) + UNW (INVAL, 2, "new_cnt*esz overflowed!"); +#endif // GPA_NO_FEAT_UNW_INVAL + return NULL; + } + + if (!gpa._fun) { +#ifndef GPA_NO_FEAT_CTX + if (_gpa_ctx_top) { + gpa = _gpa_ctx_top->gpa; + // DBG_BACKTRACE ("warning", "defaulted to gpa_ctx!"); + } else +#endif // GPA_NO_FEAT_CTX +#ifndef GPA_NO_FEAT_SYS + if (1) { + gpa = GPA_SYS; + // DBG_BACKTRACE ("warning", "defaulted to gpa_sys!"); + } else +#endif // GPA_NO_FEAT_SYS +#ifndef GPA_NO_FEAT_UNW_INVAL + if (~spec & GPA_NO_UNW) { + UNW (INVAL, 3, "no gpa to default to!"); + } else +#endif // GPA_NO_FEAT_UNW_INVAL + return NULL; + } + + void *new_ptr = gpa._fun (gpa._ctx, ptr, old_sz, new_sz, spec); +#ifndef GPA_NO_FEAT_UNW_OOM + if ((spec & GPA_NO_UNW) == 0 && new_sz && !new_ptr) + UNW (OOM, 3, "Allocation request couldn't be fulfilled!"); +#endif // GPA_NO_FEAT_UNW_OOM + + return new_ptr; +} + +#ifndef GPA_NO_FEAT_UNW +UNW_DEFINE_REASON (OOM, "Out Of Memory"); +#endif // GPA_NO_FEAT_UNW + +#ifndef GPA_NO_FEAT_SYS +#include // TODO(cmmm): remove dep (memset) + +#ifndef GPA_NO_FEAT_DBG +void *__real_calloc (usz cnt, usz esz); +void *__real_realloc (void *ptr, usz sz); +void __real_free (void *ptr); +#else // GPA_NO_FEAT_DBG +#include +#define __real_calloc calloc +#define __real_realloc realloc +#define __real_free free +#endif // GPA_NO_FEAT_DBG + +// TODO(cmmm): support alignment (unw/return null if can't be met) +void * +gpa_fun_sys (void *ctx, void *ptr, usz old_sz, usz new_sz, usz spec) +{ + UNUSED (ctx); + + if (new_sz == 0) { + __real_free (ptr); + return NULL; + } + + if (!ptr && (spec & GPA_ZI)) + return __real_calloc (new_sz, 1); + + void *ret = __real_realloc (ptr, new_sz); + + if (new_sz > old_sz && (spec & GPA_ZI)) + memset (ret + old_sz, 0, new_sz - old_sz); + + return ret; +} +#endif // GPA_NO_FEAT_SYS + +#ifndef GPA_NO_FEAT_DBG + +static void * +dbg_gpa_fun (void *_ctx, void *ptr, usz old_sz, usz new_sz, usz spec) +{ + struct dbg_gpa_ctx *ctx = _ctx; + + // TODO(cmmm): save backtrace + // struct dbg_gpa_call *call = DA_GRAB (&ctx->calls, 1); + // *call = (struct dbg_gpa_call) { + // .old_ptr = ptr, + // .new_ptr = ctx->orig._fun (ctx->orig._ctx, ptr, old_sz, new_sz, spec), + // .old_sz = old_sz, + // .new_sz = new_sz, + // .spec = spec, + // }; + + // return call->new_ptr; + return NULL; +} + +struct dbg_gpa_ctx *dbg_gpa_ctx_top = NULL; + +struct gpa +_dbg_gpa (const char *label, struct gpa orig, struct dbg_loc loc) +{ + struct dbg_gpa_ctx *ctx = GPA_GRAB_T (GPA_SYS, *ctx); + + *ctx = (struct dbg_gpa_ctx) { + .next = dbg_gpa_ctx_top, + .loc = loc, // TODO(cmmm): save backtrace instead of loc + .label = label, + .orig = orig, + }; + + dbg_gpa_ctx_top = ctx; + + return (struct gpa) { + ._ctx = ctx, + ._fun = dbg_gpa_fun, + }; +} + +static struct gpa bare; + +BEFORE_MAIN static void +dbg_gpa_bare_init (void) +{ + bare = DBG_GPA ("bare", GPA_SYS); + // atexit (gpa_dbg_report_all); +} + +// TODO(cmmm): split this of into a sub feature: DBG_BARE +// TODO(cmmm): redo this with __real_*, just add logging and stuff... +// HOLD(cmmm): wrap mmap and munmap +// void * +// __wrap_malloc (usz sz) +// { +// return GPA_GRAB_X (bare, 1, sz, ALIGN_OF (void *) - 1 | GPA_NO_UNW); +// } +// +// void * +// __wrap_calloc (usz sz) +// { +// return GPA_GRAB_0X (bare, 1, sz, ALIGN_OF (void *) - 1 | GPA_NO_UNW); +// } +// +// void * +// __wrap_realloc (void *ptr, usz sz) +// { +// return GPA_RESZ_X (bare, ptr, 0, 1, sz, ALIGN_OF (void *) - 1 | GPA_NO_UNW); +// } +// +// void * +// __wrap_reallocarray (void *ptr, usz cnt, usz esz) +// { +// return GPA_RESZ_X (bare, ptr, 0, cnt, esz, ALIGN_OF (void *) - 1 | GPA_NO_UNW); +// } +// +// void +// __wrap_free (void *ptr) +// { +// GPA_DROP (bare, ptr, 0, GPA_NO_UNW); +// } +// +// void * +// __wrap_aligned_alloc (usz align, usz sz) +// { +// DBG_WARNING ("TODO(cmmm): make sure it works"); +// return GPA_GRAB (bare, 1, sz, boundary - 1 | GPA_NO_UNW); +// } +// +// void * +// __wrap_memalign (usz boundary, usz sz) +// { +// DBG_WARNING ("TODO(cmmm): make sure it works"); +// return GPA_GRAB (bare, 1, sz, boundary - 1 | GPA_NO_UNW); +// } +// +// int +// __wrap_posix_memalign (void **memptr, usz align, usz sz) +// { +// DBG_WARNING ("TODO(cmmm): make sure it works"); +// void *ptr = GPA_GRAB (bare, 1, sz, boundary - 1 | GPA_NO_UNW); +// if (ptr) +// *memptr = ptr; +// } +// +// void * +// __wrap_valloc (usz sz) +// { +// DBG_WARNING ("TODO(cmmm): make sure it works"); +// return GPA_GRAB (bare, 1, sz, getpagesize () - 1 | GPA_NO_UNW); +// } + +#endif // GPA_NO_FEAT_DBG + +#ifndef GPA_NO_FEAT_CTX +#include "arena.h" + +// TODO(cmmm): this is exactly the same as lgr's ctx, extract it into ctx.h + +THREAD_LOCAL struct gpa_ctx_frame *_gpa_ctx_top = NULL; + +struct gpa_ctx_frame * +gpa_ctx_beg (struct gpa gpa) +{ + struct gpa_ctx_frame *new = ARENA_GRAB_T (&scr, *new); + new->gpa = gpa; + new->next = _gpa_ctx_top; + _gpa_ctx_top = new; + return new; +} + +struct gpa +gpa_ctx_end (void) +{ + struct gpa gpa = _gpa_ctx_top->gpa; + _gpa_ctx_top = _gpa_ctx_top->next; + return gpa; +} + +void +gpa_ctx_rollback (struct gpa_ctx_frame *to) +{ + while (_gpa_ctx_top != to) { + DBG_ASSERT_NN (_gpa_ctx_top); + _gpa_ctx_top = _gpa_ctx_top->next; + } +} + +#endif // GPA_NO_FEAT_CTX + +// gpa.c ends here -- cgit v1.3.1