aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/gpa.h210
-rw-r--r--src/gpa.c271
2 files changed, 481 insertions, 0 deletions
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 @@
1// gpa.h --- General Purpose Allocator -*- C -*-
2//
3// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
4//
5// SPDX-License-Identifier: MIT
6//
7// This file is part of COSMOS Codebase.
8//
9// Commentary:
10//
11// To disable specific features, define:
12// - GPA_NO_FEAT_CTX
13// - GPA_NO_FEAT_SYS
14// - GPA_NO_FEAT_DBG (auto-disabled with GPA_NO_FEAT_SYS or NDEBUG)
15// - GPA_NO_FEAT_UNW
16// - GPA_NO_FEAT_UNW_OOM (auto-disabled with GPA_NO_FEAT_UNW)
17// - GPA_NO_FEAT_UNW_INVAL (auto-disabled with GPA_NO_FEAT_UNW)
18//
19// Unwinding is handled centrally in `_gpa`, don't do unwinding in
20// actual Gaps.
21//
22// TODO(cmmm): add tracing support
23// TODO(cmmm): save used gpa into gpa's ctx if there was none
24//
25// Code:
26
27#ifndef INCLUDED_COSMOS_GPA_H
28#define INCLUDED_COSMOS_GPA_H
29
30#include "types.h"
31#include "macros.h"
32
33// TEMP(cmmm): actually implement FEAT_DBG
34#undef GPA_NO_FEAT_DBG
35#define GPA_NO_FEAT_DBG
36
37#if defined (NDEBUG) || defined (GPA_NO_FEAT_SYS)
38#undef GPA_NO_FEAT_DBG
39#define GPA_NO_FEAT_DBG
40#endif // defined (NDEBUG) || defined (GPA_NO_FEAT_SYS)
41
42#ifdef GPA_NO_FEAT_UNW
43#undef GPA_NO_FEAT_UNW_OOM
44#define GPA_NO_FEAT_UNW_OOM
45#undef GPA_NO_FEAT_UNW_INVAL
46#define GPA_NO_FEAT_UNW_INVAL
47#endif // GPA_NO_FEAT_UNW
48
49typedef void *(*gpa_fn) (void *ctx, void *ptr, usz old_sz, usz new_sz, usz spec);
50
51struct gpa {
52 gpa_fn _fun;
53 void *_ctx;
54};
55
56#define GPA_NULL ((struct gpa) { ._fun = NULL, ._ctx = NULL })
57
58struct gpa gpa_from (gpa_fn fun, void *ctx);
59
60#define GPA_GRAB( gpa, cnt, esz ) _gpa (gpa, NULL, 0, cnt, esz, 0 )
61#define GPA_GRAB_X( gpa, cnt, esz, ext) _gpa (gpa, NULL, 0, cnt, esz, (ext))
62#define GPA_GRAB_0( gpa, cnt, esz ) _gpa (gpa, NULL, 0, cnt, esz, GPA_ZI )
63#define GPA_GRAB_0X( gpa, cnt, esz, ext) _gpa (gpa, NULL, 0, cnt, esz, GPA_ZI | (ext))
64#define GPA_GRAB_T( gpa, T ) ((typeof (T)*) _gpa (gpa, NULL, 0, 1, sizeof ( T ), ALIGN_OF ( T ) - 1 ))
65#define GPA_GRAB_TX( gpa, T, ext) ((typeof (T)*) _gpa (gpa, NULL, 0, 1, sizeof ( T ), ALIGN_OF ( T ) - 1 | (ext)))
66#define GPA_GRAB_T0( gpa, T ) ((typeof (T)*) _gpa (gpa, NULL, 0, 1, sizeof ( T ), ALIGN_OF ( T ) - 1 | GPA_ZI ))
67#define GPA_GRAB_T0X(gpa, T, ext) ((typeof (T)*) _gpa (gpa, NULL, 0, 1, sizeof ( T ), ALIGN_OF ( T ) - 1 | GPA_ZI | (ext)))
68#define GPA_GRAB_N( gpa, cnt, T ) ((typeof (T)*) _gpa (gpa, NULL, 0, cnt, sizeof ( T ), ALIGN_OF ( T ) - 1 ))
69#define GPA_GRAB_NX( gpa, cnt, T, ext) ((typeof (T)*) _gpa (gpa, NULL, 0, cnt, sizeof ( T ), ALIGN_OF ( T ) - 1 | (ext)))
70#define GPA_GRAB_N0( gpa, cnt, T ) ((typeof (T)*) _gpa (gpa, NULL, 0, cnt, sizeof ( T ), ALIGN_OF ( T ) - 1 | GPA_ZI ))
71#define GPA_GRAB_N0X(gpa, cnt, T, ext) ((typeof (T)*) _gpa (gpa, NULL, 0, cnt, sizeof ( T ), ALIGN_OF ( T ) - 1 | GPA_ZI | (ext)))
72#define GPA_RESZ( gpa, ptr, old_cnt, new_cnt, esz ) _gpa (gpa, ptr, old_cnt, new_cnt, esz, 0 )
73#define GPA_RESZ_X( gpa, ptr, old_cnt, new_cnt, esz, ext) _gpa (gpa, ptr, old_cnt, new_cnt, esz, (ext))
74#define GPA_RESZ_0( gpa, ptr, old_cnt, new_cnt, esz ) _gpa (gpa, ptr, old_cnt, new_cnt, esz, GPA_ZI )
75#define GPA_RESZ_0X( gpa, ptr, old_cnt, new_cnt, esz, ext) _gpa (gpa, ptr, old_cnt, new_cnt, esz, GPA_ZI | (ext))
76#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 ))
77#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)))
78#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 ))
79#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)))
80#define GPA_DROP( gpa, ptr, cnt, esz ) ((void ) _gpa (gpa, ptr, cnt, 0, esz, 0 ))
81#define GPA_DROP_X( gpa, ptr, cnt, esz, ext) ((void ) _gpa (gpa, ptr, cnt, 0, esz, (ext)))
82#define GPA_DROP_T( gpa, ptr ) ((void ) _gpa (gpa, ptr, 1, 0, sizeof (*(ptr)), 0 ))
83#define GPA_DROP_TX( gpa, ptr, ext) ((void ) _gpa (gpa, ptr, 1, 0, sizeof (*(ptr)), (ext)))
84#define GPA_DROP_N( gpa, ptr, cnt ) ((void ) _gpa (gpa, ptr, cnt, 0, sizeof (*(ptr)), 0 ))
85#define GPA_DROP_NX( gpa, ptr, cnt, ext) ((void ) _gpa (gpa, ptr, cnt, 0, sizeof (*(ptr)), (ext)))
86
87#define GPA(gpa, ptr, old_cnt, new_cnt, esz, spec) \
88 _gpa (gpa, ptr, old_cnt, new_cnt, esz, spec)
89
90void *_gpa (struct gpa, void *ptr, usz old_cnt, usz new_cnt, usz esz, usz spec);
91
92#define GPA_ZI ((usz) 1 << (sizeof (usz)*8 - 1))
93
94#ifndef GPA_NO_FEAT_UNW
95#include "unw.h"
96UNW_DECLARE_REASON (OOM);
97#define GPA_NO_UNW ((usz) 1 << (sizeof (usz)*8 - 2))
98#else
99#define GPA_NO_UNW 0
100#endif // GPA_NO_FEAT_UNW
101
102#define GPA_ALIGN_MASK ~(GPA_ZI | GPA_NO_UNW)
103
104#ifndef GPA_NO_FEAT_SYS
105
106#define GPA_SYS ((struct gpa) { ._fun = gpa_fun_sys, ._ctx = NULL })
107void *gpa_fun_sys (void *ctx, void *ptr, usz old_sz, usz new_sz, usz spec);
108
109#else // GPA_NO_FEAT_SYS
110
111#define GPA_SYS GPA_NULL
112
113#endif // GPA_NO_FEAT_SYS
114
115#ifndef GPA_NO_FEAT_DBG
116
117// TODO(cmmm): move to CMK_META section
118#define DBG_GPA_WRAP_LIBC_LDFLAGS \
119 MV0 ("-Wl,--wrap=malloc" ), \
120 MV0 ("-Wl,--wrap=calloc" ), \
121 MV0 ("-Wl,--wrap=realloc" ), \
122 MV0 ("-Wl,--wrap=reallocarray" ), \
123 MV0 ("-Wl,--wrap=free" ), \
124 MV0 ("-Wl,--wrap=aligned_alloc" ), \
125 MV0 ("-Wl,--wrap=memalign" ), \
126 MV0 ("-Wl,--wrap=posix_memalign"), \
127 MV0 ("-Wl,--wrap=valloc" )
128
129struct dbg_gpa_call {
130 struct dbg_gpa_call *next_in_ctx;
131 struct dbg_gpa_call *next_temporal;
132 struct dbg_loc loc; // TODO(cmmm): replace with backtrace field
133 /* */ void *old_ptr;
134 /* */ void *new_ptr;
135 /* */ usz old_sz;
136 /* */ usz new_sz;
137 /* */ usz spec;
138};
139
140struct dbg_gpa_ctx {
141 struct dbg_gpa_ctx *next;
142 struct dbg_gpa_call *calls;
143 struct gpa orig;
144 struct dbg_loc loc; // TODO(cmmm): replace with backtrace field
145 const char *label;
146 /* */ usz active_allocs;
147 /* */ usz active_bytes;
148 /* */ usz peak_bytes;
149 /* */ usz total_allocs;
150};
151
152extern struct dbg_gpa_ctx *dbg_gpa_ctx_top;
153
154#define DBG_GPA(lbl, gpa) _dbg_gpa (lbl, gpa, DBG_LOC)
155struct gpa _dbg_gpa (const char *label, struct gpa, struct dbg_loc);
156
157#else // GPA_NO_FEAT_DBG
158
159#define DBG_GPA(sym, gpa) gpa
160
161#endif // GPA_NO_FEAT_DBG
162
163#ifndef GPA_NO_FEAT_CTX
164
165struct gpa_ctx_frame {
166 struct gpa_ctx_frame *next;
167 struct gpa gpa;
168};
169extern THREAD_LOCAL struct gpa_ctx_frame *_gpa_ctx_top;
170
171struct gpa_ctx_frame *gpa_ctx_beg (struct gpa);
172struct gpa gpa_ctx_end (void);
173void gpa_ctx_rollback (struct gpa_ctx_frame *);
174
175#define CTX_GRAB( ...) GPA_GRAB( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
176#define CTX_GRAB_X( ...) GPA_GRAB_X( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
177#define CTX_GRAB_0( ...) GPA_GRAB_0( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
178#define CTX_GRAB_0X( ...) GPA_GRAB_0X( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
179#define CTX_GRAB_T( ...) GPA_GRAB_T( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
180#define CTX_GRAB_TX( ...) GPA_GRAB_TX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
181#define CTX_GRAB_T0( ...) GPA_GRAB_T0( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
182#define CTX_GRAB_T0X(...) GPA_GRAB_T0X(_gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
183#define CTX_GRAB_N( ...) GPA_GRAB_N( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
184#define CTX_GRAB_NX( ...) GPA_GRAB_NX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
185#define CTX_GRAB_N0( ...) GPA_GRAB_N0( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
186#define CTX_GRAB_N0X(...) GPA_GRAB_N0X(_gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
187#define CTX_RESZ( ...) GPA_RESZ( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
188#define CTX_RESZ_X( ...) GPA_RESZ_X( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
189#define CTX_RESZ_0( ...) GPA_RESZ_0( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
190#define CTX_RESZ_0X( ...) GPA_RESZ_0X( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
191#define CTX_RESZ_T( ...) GPA_RESZ_T( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
192#define CTX_RESZ_TX( ...) GPA_RESZ_TX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
193#define CTX_RESZ_T0( ...) GPA_RESZ_T0( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
194#define CTX_RESZ_T0X(...) GPA_RESZ_T0X(_gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
195#define CTX_RESZ_N( ...) GPA_RESZ_N( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
196#define CTX_RESZ_NX( ...) GPA_RESZ_NX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
197#define CTX_RESZ_N0( ...) GPA_RESZ_N0( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
198#define CTX_RESZ_N0X(...) GPA_RESZ_N0X(_gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
199#define CTX_DROP( ...) GPA_DROP( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
200#define CTX_DROP_X( ...) GPA_DROP_X( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
201#define CTX_DROP_T( ...) GPA_DROP_T( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
202#define CTX_DROP_TX( ...) GPA_DROP_TX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
203#define CTX_DROP_N( ...) GPA_DROP_N( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
204#define CTX_DROP_NX( ...) GPA_DROP_NX( _gpa_ctx_top ? _gpa_ctx_top->gpa : GPA_SYS, __VA_ARGS__)
205
206#endif // GPA_NO_FEAT_CTX
207
208#endif // INCLUDED_COSMOS_GPA_H
209
210// 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 @@
1// gpa.c --- General Purpose Allocator -*- C -*-
2//
3// Copyright (c) 2026 Martin Michalec <martin@michalec.dev>
4//
5// SPDX-License-Identifier: MIT
6//
7// This file is part of COSMOS Codebase.
8//
9// Commentary:
10//
11// Code:
12
13#include "gpa.h"
14#include "dbg.h"
15
16struct gpa
17gpa_from (gpa_fn fun, void *ctx)
18{
19 DBG_ASSERT_NN (fun);
20 return (struct gpa) {
21 ._fun = fun,
22 ._ctx = ctx,
23 };
24}
25
26void *
27_gpa (struct gpa gpa, void *ptr, usz old_cnt, usz new_cnt, usz esz, usz spec)
28{
29 usz old_sz, new_sz;
30
31 if (usz_mul_overflow (old_cnt, esz, &old_sz)) {
32#ifndef GPA_NO_FEAT_UNW_INVAL
33 if (~spec & GPA_NO_UNW)
34 UNW (INVAL, 1, "old_cnt*esz overflowed!");
35#endif // GPA_NO_FEAT_UNW_INVAL
36 return NULL;
37 }
38
39 if (usz_mul_overflow (new_cnt, esz, &new_sz)) {
40#ifndef GPA_NO_FEAT_UNW_INVAL
41 if (~spec & GPA_NO_UNW)
42 UNW (INVAL, 2, "new_cnt*esz overflowed!");
43#endif // GPA_NO_FEAT_UNW_INVAL
44 return NULL;
45 }
46
47 if (!gpa._fun) {
48#ifndef GPA_NO_FEAT_CTX
49 if (_gpa_ctx_top) {
50 gpa = _gpa_ctx_top->gpa;
51 // DBG_BACKTRACE ("warning", "defaulted to gpa_ctx!");
52 } else
53#endif // GPA_NO_FEAT_CTX
54#ifndef GPA_NO_FEAT_SYS
55 if (1) {
56 gpa = GPA_SYS;
57 // DBG_BACKTRACE ("warning", "defaulted to gpa_sys!");
58 } else
59#endif // GPA_NO_FEAT_SYS
60#ifndef GPA_NO_FEAT_UNW_INVAL
61 if (~spec & GPA_NO_UNW) {
62 UNW (INVAL, 3, "no gpa to default to!");
63 } else
64#endif // GPA_NO_FEAT_UNW_INVAL
65 return NULL;
66 }
67
68 void *new_ptr = gpa._fun (gpa._ctx, ptr, old_sz, new_sz, spec);
69#ifndef GPA_NO_FEAT_UNW_OOM
70 if ((spec & GPA_NO_UNW) == 0 && new_sz && !new_ptr)
71 UNW (OOM, 3, "Allocation request couldn't be fulfilled!");
72#endif // GPA_NO_FEAT_UNW_OOM
73
74 return new_ptr;
75}
76
77#ifndef GPA_NO_FEAT_UNW
78UNW_DEFINE_REASON (OOM, "Out Of Memory");
79#endif // GPA_NO_FEAT_UNW
80
81#ifndef GPA_NO_FEAT_SYS
82#include <string.h> // TODO(cmmm): remove dep <string.h> (memset)
83
84#ifndef GPA_NO_FEAT_DBG
85void *__real_calloc (usz cnt, usz esz);
86void *__real_realloc (void *ptr, usz sz);
87void __real_free (void *ptr);
88#else // GPA_NO_FEAT_DBG
89#include <stdlib.h>
90#define __real_calloc calloc
91#define __real_realloc realloc
92#define __real_free free
93#endif // GPA_NO_FEAT_DBG
94
95// TODO(cmmm): support alignment (unw/return null if can't be met)
96void *
97gpa_fun_sys (void *ctx, void *ptr, usz old_sz, usz new_sz, usz spec)
98{
99 UNUSED (ctx);
100
101 if (new_sz == 0) {
102 __real_free (ptr);
103 return NULL;
104 }
105
106 if (!ptr && (spec & GPA_ZI))
107 return __real_calloc (new_sz, 1);
108
109 void *ret = __real_realloc (ptr, new_sz);
110
111 if (new_sz > old_sz && (spec & GPA_ZI))
112 memset (ret + old_sz, 0, new_sz - old_sz);
113
114 return ret;
115}
116#endif // GPA_NO_FEAT_SYS
117
118#ifndef GPA_NO_FEAT_DBG
119
120static void *
121dbg_gpa_fun (void *_ctx, void *ptr, usz old_sz, usz new_sz, usz spec)
122{
123 struct dbg_gpa_ctx *ctx = _ctx;
124
125 // TODO(cmmm): save backtrace
126 // struct dbg_gpa_call *call = DA_GRAB (&ctx->calls, 1);
127 // *call = (struct dbg_gpa_call) {
128 // .old_ptr = ptr,
129 // .new_ptr = ctx->orig._fun (ctx->orig._ctx, ptr, old_sz, new_sz, spec),
130 // .old_sz = old_sz,
131 // .new_sz = new_sz,
132 // .spec = spec,
133 // };
134
135 // return call->new_ptr;
136 return NULL;
137}
138
139struct dbg_gpa_ctx *dbg_gpa_ctx_top = NULL;
140
141struct gpa
142_dbg_gpa (const char *label, struct gpa orig, struct dbg_loc loc)
143{
144 struct dbg_gpa_ctx *ctx = GPA_GRAB_T (GPA_SYS, *ctx);
145
146 *ctx = (struct dbg_gpa_ctx) {
147 .next = dbg_gpa_ctx_top,
148 .loc = loc, // TODO(cmmm): save backtrace instead of loc
149 .label = label,
150 .orig = orig,
151 };
152
153 dbg_gpa_ctx_top = ctx;
154
155 return (struct gpa) {
156 ._ctx = ctx,
157 ._fun = dbg_gpa_fun,
158 };
159}
160
161static struct gpa bare;
162
163BEFORE_MAIN static void
164dbg_gpa_bare_init (void)
165{
166 bare = DBG_GPA ("bare", GPA_SYS);
167 // atexit (gpa_dbg_report_all);
168}
169
170// TODO(cmmm): split this of into a sub feature: DBG_BARE
171// TODO(cmmm): redo this with __real_*, just add logging and stuff...
172// HOLD(cmmm): wrap mmap and munmap
173// void *
174// __wrap_malloc (usz sz)
175// {
176// return GPA_GRAB_X (bare, 1, sz, ALIGN_OF (void *) - 1 | GPA_NO_UNW);
177// }
178//
179// void *
180// __wrap_calloc (usz sz)
181// {
182// return GPA_GRAB_0X (bare, 1, sz, ALIGN_OF (void *) - 1 | GPA_NO_UNW);
183// }
184//
185// void *
186// __wrap_realloc (void *ptr, usz sz)
187// {
188// return GPA_RESZ_X (bare, ptr, 0, 1, sz, ALIGN_OF (void *) - 1 | GPA_NO_UNW);
189// }
190//
191// void *
192// __wrap_reallocarray (void *ptr, usz cnt, usz esz)
193// {
194// return GPA_RESZ_X (bare, ptr, 0, cnt, esz, ALIGN_OF (void *) - 1 | GPA_NO_UNW);
195// }
196//
197// void
198// __wrap_free (void *ptr)
199// {
200// GPA_DROP (bare, ptr, 0, GPA_NO_UNW);
201// }
202//
203// void *
204// __wrap_aligned_alloc (usz align, usz sz)
205// {
206// DBG_WARNING ("TODO(cmmm): make sure it works");
207// return GPA_GRAB (bare, 1, sz, boundary - 1 | GPA_NO_UNW);
208// }
209//
210// void *
211// __wrap_memalign (usz boundary, usz sz)
212// {
213// DBG_WARNING ("TODO(cmmm): make sure it works");
214// return GPA_GRAB (bare, 1, sz, boundary - 1 | GPA_NO_UNW);
215// }
216//
217// int
218// __wrap_posix_memalign (void **memptr, usz align, usz sz)
219// {
220// DBG_WARNING ("TODO(cmmm): make sure it works");
221// void *ptr = GPA_GRAB (bare, 1, sz, boundary - 1 | GPA_NO_UNW);
222// if (ptr)
223// *memptr = ptr;
224// }
225//
226// void *
227// __wrap_valloc (usz sz)
228// {
229// DBG_WARNING ("TODO(cmmm): make sure it works");
230// return GPA_GRAB (bare, 1, sz, getpagesize () - 1 | GPA_NO_UNW);
231// }
232
233#endif // GPA_NO_FEAT_DBG
234
235#ifndef GPA_NO_FEAT_CTX
236#include "arena.h"
237
238// TODO(cmmm): this is exactly the same as lgr's ctx, extract it into ctx.h
239
240THREAD_LOCAL struct gpa_ctx_frame *_gpa_ctx_top = NULL;
241
242struct gpa_ctx_frame *
243gpa_ctx_beg (struct gpa gpa)
244{
245 struct gpa_ctx_frame *new = ARENA_GRAB_T (&scr, *new);
246 new->gpa = gpa;
247 new->next = _gpa_ctx_top;
248 _gpa_ctx_top = new;
249 return new;
250}
251
252struct gpa
253gpa_ctx_end (void)
254{
255 struct gpa gpa = _gpa_ctx_top->gpa;
256 _gpa_ctx_top = _gpa_ctx_top->next;
257 return gpa;
258}
259
260void
261gpa_ctx_rollback (struct gpa_ctx_frame *to)
262{
263 while (_gpa_ctx_top != to) {
264 DBG_ASSERT_NN (_gpa_ctx_top);
265 _gpa_ctx_top = _gpa_ctx_top->next;
266 }
267}
268
269#endif // GPA_NO_FEAT_CTX
270
271// gpa.c ends here