wasm_storage_memory (wasm_storage v0.1.0)

View Source

A device held in memory.

The bytes live in a public ETS table as fixed-size blocks, so any process can read the device: a test can kill the process that was using it and open it again, the way a restart would reopen a file. The device is volatile: sync/2 succeeds, and the data lasts as long as the table.

Two ways to get one:

%% A device of its own, deleted when the handle closes or its owner exits:
{ok, H} = wasm_storage:open({wasm_storage_memory, new}, #{}).

%% A device that outlives its handles, owned by the calling process:
{ok, Dev} = wasm_storage_memory:new(#{}),
{ok, H1} = wasm_storage:open({wasm_storage_memory, Dev}, #{}),
ok = wasm_storage:close(H1),
{ok, H2} = wasm_storage:open({wasm_storage_memory, Dev}, #{}).

Options of new/1 (and of open/2 with new):

  • block_size: bytes per block, a power of two, default 4096.
  • max_size: writes or resizes past this many bytes fail with enospc. Default infinity.

Blocks never written read as zeros and take no memory.

Summary

Functions

The whole content of a device, for tests and crash images.

Free a device made by new/1. Open handles then return {error, closed}.

A new device holding Bin.

Create a device owned by the calling process.

Types

device()

-opaque device()

Functions

contents/1

-spec contents(device()) -> binary().

The whole content of a device, for tests and crash images.

delete/1

-spec delete(device()) -> ok.

Free a device made by new/1. Open handles then return {error, closed}.

from_binary(Bin, Opts)

-spec from_binary(binary(), map()) -> {ok, device()} | {error, einval}.

A new device holding Bin.

new(Opts)

-spec new(map()) -> {ok, device()} | {error, einval}.

Create a device owned by the calling process.