Silk

silk/allocator

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc.

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, OutOfMemoryError }

import silk.bytes { Bytes }

import silk.effect { Effect }

import silk.usize

effect fn copyMessage() -> i32
! 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: OutOfMemoryError) -> i32 {
  return 0
}

pub fn main() -> i32 {
  return run Effect.catchAll(copyMessage(), recoverAllocation)
}

Import as Allocator with import silk.allocator { Allocator }.

Public declarations: 3.

OutOfMemoryError

pub struct OutOfMemoryError

A failure that reports that an allocator cannot satisfy a storage request.

Allocator

pub service Allocator

A mutable service that acquires owned storage described by a layout.

Operation allocate

effect<'static> fn allocate(layout: Layout) -> Allocation ! OutOfMemoryError ? &mut Allocator

Acquires one allocation for layout, or fails without returning partial storage.

Associated function Allocator.outOfMemory

pub effect<'static> fn outOfMemory() -> never ! OutOfMemoryError

Fails immediately with OutOfMemoryError.

When to use

Use this function to translate a checked size failure into the standard allocation failure.

Associated function Allocator.systemAllocatorProvider

pub fn systemAllocatorProvider() -> SystemAllocator

Creates a process-backed allocator provider without allocating storage.

SystemAllocator

pub struct SystemAllocator

A process-backed provider for Allocator.

Implementation Allocator for SystemAllocator

impl Allocator for SystemAllocator

Operation allocate

allocate = SystemAllocator.systemAllocate

On this page