Added missing @clone_aligned and add checks to @tclone

This commit is contained in:
Christoffer Lerno
2025-04-20 18:31:52 +02:00
parent 6ab7953706
commit f778e75757
4 changed files with 61 additions and 0 deletions

View File

@@ -627,16 +627,44 @@ macro TrackingEnv* get_tracking_env()
$endif
}
<*
@param value : "The value to clone"
@return "A pointer to the cloned value"
@require $alignof(value) <= mem::DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'clone_aligned' instead"
*>
macro @clone(value) @builtin @nodiscard
{
return allocator::clone(mem, value);
}
<*
@param value : "The value to clone"
@return "A pointer to the cloned value, which must be released using free_aligned"
*>
macro @clone_aligned(value) @builtin @nodiscard
{
return allocator::clone_aligned(mem, value);
}
<*
@param value : "The value to clone"
@return "A pointer to the cloned value"
@require $alignof(value) <= mem::DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'tclone_aligned' instead"
*>
macro @tclone(value) @builtin @nodiscard
{
return tnew($typeof(value), value);
}
<*
@param value : "The value to clone"
@return "A pointer to the cloned value"
*>
macro @tclone_aligned(value) @builtin @nodiscard
{
return allocator::clone_aligned(tmem, value);
}
fn void* malloc(usz size) @builtin @inline @nodiscard
{
return allocator::malloc(mem, size);

View File

@@ -283,11 +283,31 @@ macro alloc_array_try(Allocator allocator, $Type, usz elements) @nodiscard
return (($Type*)malloc_try(allocator, $Type.sizeof * elements))[:elements];
}
<*
Clone a value.
@param [&inout] allocator : "The allocator to use to clone"
@param value : "The value to clone"
@return "A pointer to the cloned value"
@require $alignof(value) <= mem::DEFAULT_MEM_ALIGNMENT : "Types with alignment exceeding the default must use 'clone_aligned' instead"
*>
macro clone(Allocator allocator, value) @nodiscard
{
return new(allocator, $typeof(value), value);
}
<*
Clone overaligned values. Must be released using free_aligned.
@param [&inout] allocator : "The allocator to use to clone"
@param value : "The value to clone"
@return "A pointer to the cloned value"
*>
macro clone_aligned(Allocator allocator, value) @nodiscard
{
return new_aligned(allocator, $typeof(value), value)!!;
}
fn any clone_any(Allocator allocator, any value) @nodiscard
{
usz size = value.type.sizeof;

View File

@@ -29,6 +29,7 @@
- Fix broken enum inline -> bool conversions #2094.
- `@if` was ignored on attrdef, regression 0.7 #2093.
- `@ensure` was not included when the function doesn't return a value #2098.
- Added missing `@clone_aligned` and add checks to `@tclone`
### Stdlib changes
- Hash functions for integer vectors and arrays.

View File

@@ -0,0 +1,12 @@
module test;
fn void clone_test() @test
{
double x;
void *a = @clone(x);
free(a);
(void)@tclone(x);
a = @clone_aligned(x);
free_aligned(a);
(void)@tclone_aligned(x);
}