diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-05-09 15:17:35 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-05-09 15:17:35 +0200 |
commit | 7059e4e133b62f5ad3339d51966f226089532710 (patch) | |
tree | 94f3124e189d4329f7ee52ff69fd6619754bcafe /lib/alloc | |
parent | 6cd517eee582d797d766f3ed8dfbfb8c107eed7c (diff) |
Try to do better error handling with custom allocators
Diffstat (limited to 'lib/alloc')
-rw-r--r-- | lib/alloc/alloc_arena.c | 16 | ||||
-rw-r--r-- | lib/alloc/alloc_heap.c | 24 |
2 files changed, 33 insertions, 7 deletions
diff --git a/lib/alloc/alloc_arena.c b/lib/alloc/alloc_arena.c index 9ddc6e2..52d0df9 100644 --- a/lib/alloc/alloc_arena.c +++ b/lib/alloc/alloc_arena.c @@ -1,9 +1,21 @@ #include <stdlib.h> #include "alloc.h" +#include "errors.h" +#include "macros.h" void * -alloc_arena(void *ctx, void *ptr, size_t old, size_t new, size_t align) +alloc_arena(void *raw_ctx, void *ptr, size_t old, size_t new, size_t align) { - return arena_realloc(ctx, ptr, old, new, 1, align); + struct arena_ctx *ctx = raw_ctx; + ASSUME(ctx != nullptr); + ASSUME(ctx->a != nullptr); + + void *p = arena_realloc(ctx->a, ptr, old, new, 1, align); + if (new == 0 || p != nullptr) + return p; + + if (ctx->jmp != nullptr) + longjmp(*ctx->jmp, 1); + err("arena_realloc:"); } diff --git a/lib/alloc/alloc_heap.c b/lib/alloc/alloc_heap.c index 72cd7ad..46fbdc9 100644 --- a/lib/alloc/alloc_heap.c +++ b/lib/alloc/alloc_heap.c @@ -1,12 +1,26 @@ +#include <stddef.h> +#include <setjmp.h> #include <stdlib.h> #include "alloc.h" +#include "errors.h" void * -alloc_heap(void *, void *ptr, size_t, size_t new, size_t) +alloc_heap(void *raw_ctx, void *ptr, size_t, size_t new, size_t) { - if (new > 0) - return realloc(ptr, new); - free(ptr); - return nullptr; + if (new == 0) { + free(ptr); + return nullptr; + } + + void *p = realloc(ptr, new); + if (p != nullptr) + return p; + + struct heap_ctx *ctx = raw_ctx; + if (ctx == nullptr || ctx->jmp == nullptr) + err("realloc:"); + + longjmp(*ctx->jmp, 1); + unreachable(); } |