diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/gpa.h | 210 |
1 files changed, 210 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 | |||
| 49 | typedef void *(*gpa_fn) (void *ctx, void *ptr, usz old_sz, usz new_sz, usz spec); | ||
| 50 | |||
| 51 | struct gpa { | ||
| 52 | gpa_fn _fun; | ||
| 53 | void *_ctx; | ||
| 54 | }; | ||
| 55 | |||
| 56 | #define GPA_NULL ((struct gpa) { ._fun = NULL, ._ctx = NULL }) | ||
| 57 | |||
| 58 | struct 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 | |||
| 90 | void *_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" | ||
| 96 | UNW_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 }) | ||
| 107 | void *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 | |||
| 129 | struct 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 | |||
| 140 | struct 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 | |||
| 152 | extern struct dbg_gpa_ctx *dbg_gpa_ctx_top; | ||
| 153 | |||
| 154 | #define DBG_GPA(lbl, gpa) _dbg_gpa (lbl, gpa, DBG_LOC) | ||
| 155 | struct 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 | |||
| 165 | struct gpa_ctx_frame { | ||
| 166 | struct gpa_ctx_frame *next; | ||
| 167 | struct gpa gpa; | ||
| 168 | }; | ||
| 169 | extern THREAD_LOCAL struct gpa_ctx_frame *_gpa_ctx_top; | ||
| 170 | |||
| 171 | struct gpa_ctx_frame *gpa_ctx_beg (struct gpa); | ||
| 172 | struct gpa gpa_ctx_end (void); | ||
| 173 | void 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 | ||
