diff options
-rw-r--r-- | include/alloc.h | 10 | ||||
-rw-r--r-- | lib/alloc/alloc_arena.c | 16 | ||||
-rw-r--r-- | lib/alloc/alloc_heap.c | 24 | ||||
-rw-r--r-- | lib/unicode/string/u8casefold.c | 2 | ||||
-rw-r--r-- | lib/unicode/string/u8lower.c | 2 | ||||
-rw-r--r-- | lib/unicode/string/u8title.c | 2 | ||||
-rw-r--r-- | lib/unicode/string/u8upper.c | 2 | ||||
-rw-r--r-- | test/_case-test.h | 9 |
8 files changed, 46 insertions, 21 deletions
diff --git a/include/alloc.h b/include/alloc.h index 966361e..b9e03e2 100644 --- a/include/alloc.h +++ b/include/alloc.h @@ -1,6 +1,7 @@ #ifndef MLIB_ALLOC_H #define MLIB_ALLOC_H +#include <setjmp.h> #include <stddef.h> #include "_attrs.h" @@ -44,6 +45,15 @@ void arena_free(arena *); #define arena_resz(a, T, p, n) \ ((T *)arena_realloc((a), (p), (n), sizeof(T), alignof(T))) +/* Memory allocator callbacks for memory-allocating functions */ +struct arena_ctx { + arena *a; + jmp_buf *jmp; +}; +struct heap_ctx { + jmp_buf *jmp; +}; + [[nodiscard]] void *alloc_arena(void *, void *, size_t, size_t, size_t); [[nodiscard]] void *alloc_heap(void *, void *, size_t, size_t, size_t); 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(); } diff --git a/lib/unicode/string/u8casefold.c b/lib/unicode/string/u8casefold.c index eff1e48..b4afe50 100644 --- a/lib/unicode/string/u8casefold.c +++ b/lib/unicode/string/u8casefold.c @@ -20,8 +20,6 @@ u8casefold(size_t *dstn, struct u8view sv, enum caseflags flags, alloc_fn alloc, } char8_t *dst = alloc(alloc_ctx, nullptr, 0, bufsz, alignof(char8_t)); - if (dst == nullptr) - return nullptr; rune ch; size_t n = 0; diff --git a/lib/unicode/string/u8lower.c b/lib/unicode/string/u8lower.c index d5eb58c..45309b6 100644 --- a/lib/unicode/string/u8lower.c +++ b/lib/unicode/string/u8lower.c @@ -47,8 +47,6 @@ u8lower(size_t *dstn, struct u8view sv, enum caseflags flags, alloc_fn alloc, } char8_t *dst = alloc(alloc_ctx, nullptr, 0, bufsz, alignof(char8_t)); - if (dst == nullptr) - return nullptr; while (u8next(&ch, &sv)) { rune next = 0; diff --git a/lib/unicode/string/u8title.c b/lib/unicode/string/u8title.c index 0c3620e..5710920 100644 --- a/lib/unicode/string/u8title.c +++ b/lib/unicode/string/u8title.c @@ -53,8 +53,6 @@ u8title(size_t *dstn, struct u8view sv, enum caseflags flags, alloc_fn alloc, } char8_t *dst = alloc(alloc_ctx, nullptr, 0, bufsz, alignof(char8_t)); - if (dst == nullptr) - return nullptr; while (u8next(&ch, &sv)) { if (sv.p > word.p + word.len) { diff --git a/lib/unicode/string/u8upper.c b/lib/unicode/string/u8upper.c index df25ee7..91ae679 100644 --- a/lib/unicode/string/u8upper.c +++ b/lib/unicode/string/u8upper.c @@ -26,8 +26,6 @@ u8upper(size_t *dstn, struct u8view sv, enum caseflags flags, alloc_fn alloc, } char8_t *dst = alloc(alloc_ctx, nullptr, 0, bufsz, alignof(char8_t)); - if (dst == nullptr) - return nullptr; rune ch; size_t n = 0; diff --git a/test/_case-test.h b/test/_case-test.h index 684c6ec..ea05a86 100644 --- a/test/_case-test.h +++ b/test/_case-test.h @@ -65,12 +65,9 @@ test(const char8_t *line, int id) : 0; arena a = mkarena(0); - mapped.p = FUNC(&mapped.len, before, cf, alloc_arena, &a); - - if (mapped.p == nullptr) { - warn("case %d: got null %s buffer", id, STR(CASETYPE_VERB)); - return false; - } + mapped.p = FUNC(&mapped.len, before, cf, alloc_arena, &(struct arena_ctx){ + .a = &a, + }); if (!u8eq(mapped, after)) { warn("case %d: expected ā%.*sā but got ā%.*sā", id, SV_PRI_ARGS(after), |