// 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