mirror of
https://github.com/Ed94/Odin.git
synced 2026-06-13 01:21:38 -07:00
Add mem.make_over_aligned
This commit is contained in:
@@ -888,6 +888,34 @@ make_aligned :: proc(
|
||||
return runtime.make_aligned(T, len, alignment, allocator, loc)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Allocate a new slice with alignment for allocators that might not support the
|
||||
specified alignment requirement.
|
||||
|
||||
This procedure allocates a new slice of type `T` with length `len`, aligned
|
||||
on a boundary specified by `alignment` from an allocator specified by
|
||||
`allocator`, and returns the allocated slice.
|
||||
|
||||
The user should `delete` the return `original_data` slice not the typed `slice`.
|
||||
*/
|
||||
@(require_results)
|
||||
make_over_aligned :: proc(
|
||||
$T: typeid/[]$E,
|
||||
#any_int len: int,
|
||||
alignment: int,
|
||||
allocator: runtime.Allocator,
|
||||
loc := #caller_location,
|
||||
) -> (slice: T, original_data: []byte, err: Allocator_Error) {
|
||||
size := size_of(E)*len + alignment-1
|
||||
original_data, err = runtime.make([]byte, size, allocator, loc)
|
||||
if err == nil {
|
||||
ptr := align_forward(raw_data(original_data), uintptr(alignment))
|
||||
slice = ([^]E)(ptr)[:len]
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
/*
|
||||
Allocate a new slice.
|
||||
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
#package mem
|
||||
package mem
|
||||
|
||||
import "base:runtime"
|
||||
import "base:intrinsics"
|
||||
|
||||
Reference in New Issue
Block a user