From 67122f04e8b3f4bd6ced95a34d94509479d62e9e Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Fri, 10 May 2024 23:26:01 +0200 Subject: Accept an element size argument to allocators --- lib/alloc/alloc_arena.c | 5 +++-- lib/alloc/alloc_heap.c | 15 ++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) (limited to 'lib/alloc') diff --git a/lib/alloc/alloc_arena.c b/lib/alloc/alloc_arena.c index 52d0df9..8d2aa11 100644 --- a/lib/alloc/alloc_arena.c +++ b/lib/alloc/alloc_arena.c @@ -5,13 +5,14 @@ #include "macros.h" void * -alloc_arena(void *raw_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 elemsz, + size_t 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); + void *p = arena_realloc(ctx->a, ptr, old, new, elemsz, align); if (new == 0 || p != nullptr) return p; diff --git a/lib/alloc/alloc_heap.c b/lib/alloc/alloc_heap.c index 46fbdc9..fccfcfc 100644 --- a/lib/alloc/alloc_heap.c +++ b/lib/alloc/alloc_heap.c @@ -1,21 +1,26 @@ -#include +#include #include +#include +#include #include #include "alloc.h" #include "errors.h" void * -alloc_heap(void *raw_ctx, void *ptr, size_t, size_t new, size_t) +alloc_heap(void *raw_ctx, void *ptr, size_t, size_t new, size_t elemsz, size_t) { if (new == 0) { free(ptr); return nullptr; } - void *p = realloc(ptr, new); - if (p != nullptr) - return p; + if (!ckd_mul(&new, new, elemsz)) { + void *p = realloc(ptr, new); + if (p != nullptr) + return p; + } else + errno = EOVERFLOW; struct heap_ctx *ctx = raw_ctx; if (ctx == nullptr || ctx->jmp == nullptr) -- cgit v1.2.3