mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
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:
@@ -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.
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user