TypedAllocation
Acquires raw storage for `Count` not-yet-live `T` objects.
Module std.memory
import std.memory;Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.
Allocate
pub unsafe fn Allocate<T, A>(
Value: mut A&,
Count: usize
) -> Result<mut T*, AllocationError>
where A: AllocatorAcquires raw storage for Count not-yet-live T objects. T: Complete sized slot type whose lifetime is not started here. A: Concrete allocator satisfying the canonical structural protocol. Value: Writable allocator instance borrow retained only for the call. Count: Number of raw slots, including zero. Return: A distinct nonnull aligned base pointer or allocation/layout failure. Warning: The caller must establish each object lifetime before typed access.
Example
module example.typed_allocation;
from std.memory import Allocate, DefaultAllocator, Free;
fn Main() -> i32 {
var Heap = default(DefaultAllocator);
unsafe {
switch Allocate<u64, DefaultAllocator>(Heap, 4) {
case .Ok { Value = Storage } => {
Free<u64, DefaultAllocator>(Heap, Storage, 4);
return 0;
}
case .Err { Error = _ } => return 1;
}
}
}Free
pub unsafe fn Free<T, A>(
Value: mut A&,
Storage: mut T*,
Count: usize
) -> void
where A: AllocatorReleases raw storage previously acquired for exactly Count T slots. T: Exact complete slot type used for allocation. A: Compatible concrete allocator type. Value: Writable compatible allocator borrow retained only for the call. Storage: Exact base pointer, or null only when Count is zero. Count: Exact acquired slot count. Return: No value after allocation lifetime ends. Warning: No T object may remain live and no alias may be used afterward.