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 <christoffer@aegik.com>
This commit is contained in:
Zack Puhl
2026-01-25 10:12:44 -05:00
committed by GitHub
parent 6cffb888ea
commit 75d454b6a6
3 changed files with 33 additions and 0 deletions

View File

@@ -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.

View File

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

View File

@@ -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);
}