Silk

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 OutOfMemoryError

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

outOfMemory

pub effect 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.

Allocator

pub service Allocator

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

Operation allocate

effect fn allocate(layout: LayoutValue) -> Allocation ! OutOfMemoryError ? &mut Allocator

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

SystemAllocator

pub struct SystemAllocator

A process-backed provider for Allocator.

systemAllocatorProvider

pub fn systemAllocatorProvider() -> SystemAllocator

Creates a process-backed allocator provider without allocating storage.

Implementation Allocator for SystemAllocator

impl Allocator for SystemAllocator

Operation allocate

allocate = SystemAllocator.systemAllocate

On this page