diff options
| author | 2026-11-06 02:49:55 +0000 | |
|---|---|---|
| committer | 2026-11-06 07:09:22 +0000 | |
| commit | d0657d8cbc8439bfa2de4eca4afa6062f1ef7222 (patch) | |
| tree | 703124d38d8263ed59dbfb1c21b1a051eebcb736 /src | |
| parent | feat(unw): add stack unwinding (diff) | |
| download | codebase-d0657d8cbc8439bfa2de4eca4afa6062f1ef7222.tar.gz | |
feat(gpa): add general purpose allocator
Diffstat (limited to 'src')
| -rw-r--r-- | src/gpa.c | 271 |
1 files changed, 271 insertions, 0 deletions
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 | |||
| 16 | struct gpa | ||
| 17 | gpa_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 | |||
| 26 | void * | ||
| 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 | ||
| 78 | UNW_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 | ||
| 85 | void *__real_calloc (usz cnt, usz esz); | ||
| 86 | void *__real_realloc (void *ptr, usz sz); | ||
| 87 | void __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) | ||
| 96 | void * | ||
| 97 | gpa_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 | |||
| 120 | static void * | ||
| 121 | dbg_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 | |||
| 139 | struct dbg_gpa_ctx *dbg_gpa_ctx_top = NULL; | ||
| 140 | |||
| 141 | struct 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 | |||
| 161 | static struct gpa bare; | ||
| 162 | |||
| 163 | BEFORE_MAIN static void | ||
| 164 | dbg_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 | |||
| 240 | THREAD_LOCAL struct gpa_ctx_frame *_gpa_ctx_top = NULL; | ||
| 241 | |||
| 242 | struct gpa_ctx_frame * | ||
| 243 | gpa_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 | |||
| 252 | struct gpa | ||
| 253 | gpa_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 | |||
| 260 | void | ||
| 261 | gpa_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 | ||
