From 75d454b6a63d701c557d88773525d07e67083ae3 Mon Sep 17 00:00:00 2001 From: Zack Puhl Date: Sun, 25 Jan 2026 10:12:44 -0500 Subject: [PATCH] Add mem_allocator `realloc_array` Macros (#2760) * Add mem_allocator `realloc_array` Macros * test, ensure `realloc_array` to 0 size returns `null` * update release notes --------- Co-authored-by: Christoffer Lerno --- lib/std/core/mem_allocator.c3 | 25 +++++++++++++++++++++++++ releasenotes.md | 1 + test/unit/stdlib/core/mem_allocator.c3 | 7 +++++++ 3 files changed, 33 insertions(+) diff --git a/lib/std/core/mem_allocator.c3 b/lib/std/core/mem_allocator.c3 index eb56626f3..a1afcd57d 100644 --- a/lib/std/core/mem_allocator.c3 +++ b/lib/std/core/mem_allocator.c3 @@ -304,6 +304,31 @@ macro alloc_array_try(Allocator allocator, $Type, usz elements) @nodiscard return (($Type*)malloc_try(allocator, $Type.sizeof * elements))[:elements]; } +<* + @require $Type.alignof <= mem::DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'alloc_array_aligned' instead" +*> +macro realloc_array(Allocator allocator, void* ptr, $Type, usz elements) @nodiscard +{ + return realloc_array_try(allocator, ptr, $Type, elements)!!; +} + +<* + Allocate using an aligned allocation. This is necessary for types with a default memory alignment + exceeding DEFAULT_MEM_ALIGNMENT. IMPORTANT! It must be freed using free_aligned. +*> +macro realloc_array_aligned(Allocator allocator, void* ptr, $Type, usz elements) @nodiscard +{ + return (($Type*)realloc_aligned(allocator, ptr, $Type.sizeof * elements, $Type.alignof))[:elements]!!; +} + +<* + @require $Type.alignof <= mem::DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'alloc_array_aligned' instead" +*> +macro realloc_array_try(Allocator allocator, void* ptr, $Type, usz elements) @nodiscard +{ + return (($Type*)realloc_try(allocator, ptr, $Type.sizeof * elements))[:elements]; +} + <* Clone a value. diff --git a/releasenotes.md b/releasenotes.md index c3067ff59..959b6187b 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -159,6 +159,7 @@ - Add `mem::store` and `mem::load` which may combine both aligned and volatile operations. - Deprecated `EMPTY_MACRO_SLOT` and its related uses, in favor of `optional_param = ...` named macro arguments. #2805 - Add tracking of peak memory usage in the tracking allocator. +- Added `realloc_array`, `realloc_array_aligned`, and `realloc_array_try` to `allocator::`. #2760 ## 0.7.8 Change list diff --git a/test/unit/stdlib/core/mem_allocator.c3 b/test/unit/stdlib/core/mem_allocator.c3 index ee1f65fa4..fe23457ce 100644 --- a/test/unit/stdlib/core/mem_allocator.c3 +++ b/test/unit/stdlib/core/mem_allocator.c3 @@ -23,3 +23,10 @@ fn void test_new_aligned_compiles() @test Bar* bar = allocator::new_aligned(mem, Bar, {.x = 1, .y = 2, .foos = {}})!!; allocator::free_aligned(mem, bar); } + +fn void test_realloc_array_to_zero() @test +{ + usz* x = mem::talloc_array(usz, 24); + x = allocator::realloc_array(tmem, x, usz, 0); + test::@check(x == null, "Zero-sized reallocation's pointer is not null: %p", x); +}