silk.mem.allocator.
The Allocator service: the source of owned heap storage in the standard library.
When to use#
Each operation that makes owned storage (such as Bytes.copy, List.push or Arc.make)
requires &mut Allocator. Provide it one time, near the top of the program, with
Allocator.systemAllocatorProvider.
Details#
Allocator is a normal service. The requirement stays in the Effect until a caller provides
it. Thus a test or an application can use a different allocator for one part of the code.
Errors#
OutOfMemoryError when the allocator cannot supply the storage. A failed request never
returns part of an allocation.
Examples#
Provide the system allocator#
import silk.mem.allocator { Allocator, OutOfMemoryError }
import silk.collections.bytes { Bytes }
import silk.core.effect { Effect }
// The requirement `? &mut Allocator` stays open until a caller provides it.
effect fn greeting() -> Bytes
! OutOfMemoryError
? &mut Allocator {
return run Bytes.copy(b"Silk")
}
effect fn program() -> i32
! OutOfMemoryError {
let mut allocator = Allocator.systemAllocatorProvider()
let bytes = run greeting() |> Effect.provideMut<Allocator>(&mut allocator)
if Bytes.length(&bytes) != 4 {
return 1
}
return 0
}
effect fn outOfMemory(error: OutOfMemoryError) -> i32 {
return 1
}
pub fn main() -> i32 {
return run Effect.catchAll(program(), outOfMemory)
}Import as Allocator with import silk.mem.allocator { Allocator }.
Public declarations: 3.
OutOfMemoryError#
pub struct OutOfMemoryErrorThe failure when an allocator cannot supply the requested storage.
Details#
A failed request never returns part of an allocation. Each operation that makes owned storage,
such as List.push or Box.make, can fail with this error.
Implementation Debug for OutOfMemoryError#
impl Debug for OutOfMemoryErrorOperation debug#
effect fn debug(self: &Self, formatter: &mut Formatter) -> () ! WriterError ? &mut WriterWrites OutOfMemoryError.
Allocator#
pub service AllocatorA mutable service that supplies owned storage for a Layout.
When to use#
Each operation that makes owned storage (such as Bytes.copy, List.push or Arc.make)
requires &mut Allocator. Provide it one time, near the top of the program, with
Allocator.systemAllocatorProvider.
Details#
Allocator is a normal service. The requirement stays in the Effect until a caller provides
it. Thus a test or an application can use a different allocator, such as an arena, for one
part of the code.
Errors#
OutOfMemoryErrorwhen the allocator cannot supply the storage. A failed request never returns part of an allocation.
Operation allocate#
effect fn allocate(layout: Layout) -> Intrinsic.Allocation ! OutOfMemoryError ? &mut AllocatorReturns one allocation for layout, or fails with no partial storage.
Implementation Allocator#
impl AllocatorAssociated function outOfMemory#
pub effect fn outOfMemory() -> never ! OutOfMemoryErrorFails at once with OutOfMemoryError.
When to use#
Use it when a size calculation overflows, so that the caller sees the normal allocation failure.
Associated function systemAllocatorProvider#
pub fn systemAllocatorProvider() -> SystemAllocatorReturns a provider that uses the process heap. It does not allocate.
When to use#
Provide it one time, near the top of the program, to satisfy the &mut Allocator
requirement of each operation that makes owned storage.
SystemAllocator#
pub struct SystemAllocatorThe process-heap provider for Allocator. It has no state.
Implementation SystemAllocator#
impl SystemAllocatorImplementation Allocator for SystemAllocator#
impl Allocator for SystemAllocatorOperation allocate#
allocate: SystemAllocator.systemAllocateCategory: Memory.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.