silk/allocator
Foundational allocation service used by higher standard-library actors.
When to use
Provide Allocator to operations that may create owned storage, normally with
SystemAllocator.
Details
Allocator is an ordinary source-declared service. Its requirement remains in an Effect
until a caller supplies a provider, so tests and applications can replace the process-backed
implementation lexically. Allocation failure is typed as OutOfMemoryError.
Examples
Provide the process allocator
import silk.allocator { Allocator }
import silk.bytes as Bytes
import silk.effect { Effect }
import silk.usize as usize
effect fn copyMessage() -> i32
! Allocator.OutOfMemoryError {
let mut allocator = Allocator.systemAllocatorProvider()
let source = b"Silk"
let copying = Bytes.copy(&source)
|> Effect.provideMut<Allocator>(&mut allocator)
let bytes = run copying
return Bytes.length(&bytes)
|> usize.toI32
}
effect fn recoverAllocation(error: Allocator.OutOfMemoryError) -> i32 {
return 0
}
pub fn main() -> i32 {
return run Effect.catchAll(copyMessage(), recoverAllocation)
}Import as Allocator with import silk.allocator.
Public declarations: 5.
OutOfMemoryError
pub struct OutOfMemoryErrorA failure that reports that an allocator cannot satisfy a storage request.
outOfMemory
pub effect fn outOfMemory() -> never ! OutOfMemoryErrorFails immediately with OutOfMemoryError.
When to use
Use this function to translate a checked size failure into the standard allocation failure.
Allocator
pub service AllocatorA mutable service that acquires owned storage described by a layout.
Operation allocate
effect fn allocate(layout: LayoutValue) -> Allocation ! OutOfMemoryError ? &mut AllocatorAcquires one allocation for layout, or fails without returning partial storage.
SystemAllocator
pub struct SystemAllocatorA process-backed provider for Allocator.
systemAllocatorProvider
pub fn systemAllocatorProvider() -> SystemAllocatorCreates a process-backed allocator provider without allocating storage.
Implementation Allocator for SystemAllocator
impl Allocator for SystemAllocatorOperation allocate
allocate = SystemAllocator.systemAllocate