diff --git a/std/mem/mem.hon b/std/mem/mem.hon index 1dc2c9f..7b0d41b 100644 --- a/std/mem/mem.hon +++ b/std/mem/mem.hon @@ -78,6 +78,7 @@ realloc proc($T type, allocator Allocator, memory []mut T, new_count usize) []mu old_memory = ptrcast!(u8, memory.ptr) old_size = memory.len * element_size } + resized := raw_realloc( allocator, old_memory, @@ -85,6 +86,7 @@ realloc proc($T type, allocator Allocator, memory []mut T, new_count usize) []mu new_count * element_size, alignof!(T), ) + if resized |bytes| { pointer *mut T :: ptrcast!(T, bytes) return pointer[..new_count] @@ -105,7 +107,10 @@ free proc($T type, allocator Allocator, memory []T) void { ) } -#! get an empty slice of type `T` with `count` elements. +#! create a slice backed by `empty_storage` without allocating. +#! precondition: `count` must be 0 or `sizeof!(T)` must be 0; otherwise the slice +#! exceeds its backing storage. +@hide:file empty_slice proc($T type, count usize) []mut T { pointer *mut T :: ptrcast!(T, (&empty_storage).ptr) return pointer[..count]