Browse the docs

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#

silk
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#

silk
pub struct OutOfMemoryError

The 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#

silk
impl Debug for OutOfMemoryError

Operation debug#

silk
effect fn debug(self: &Self, formatter: &mut Formatter) -> () ! WriterError ? &mut Writer

Writes OutOfMemoryError.

Allocator#

silk
pub service Allocator

A 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#

  • OutOfMemoryError when the allocator cannot supply the storage. A failed request never returns part of an allocation.

Operation allocate#

silk
effect fn allocate(layout: Layout) -> Intrinsic.Allocation ! OutOfMemoryError ? &mut Allocator

Returns one allocation for layout, or fails with no partial storage.

Implementation Allocator#

silk
impl Allocator

Associated function outOfMemory#

silk
pub effect fn outOfMemory() -> never ! OutOfMemoryError

Fails 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#

silk
pub fn systemAllocatorProvider() -> SystemAllocator

Returns 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#

silk
pub struct SystemAllocator

The process-heap provider for Allocator. It has no state.

Implementation SystemAllocator#

silk
impl SystemAllocator

Implementation Allocator for SystemAllocator#

silk
impl Allocator for SystemAllocator

Operation allocate#

silk
allocate: SystemAllocator.systemAllocate

Category: 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.

View the source of this page