Shmex v0.1.0 Shmex.Native View Source

This module provides natively implemented functions allowing low-level operations on Posix shared memory. Use with caution!

Link to this section Summary

Functions

Creates guard for existing shared memory

Creates shared memory segment and adds a guard for it

Reads the contents of SHM and returns as binary

Reads cnt bytes from SHM and returns as binary

Sets the capacity of SHM and updates the struct accordingly

Splits the contents of SHM into 2 by moving part of the data into a new SHM

Writes the binary into the SHM

Link to this section Functions

Creates guard for existing shared memory.

This function should be only used when Shmex struct was created by some other NIF and even though the SHM exists, it’s guard field is set to nil. Trying to use it with SHM obtained via allocate/1 will result in error.

See also docs for allocate/1

Link to this function allocate(payload) View Source
allocate(payload :: Shmex.t()) :: Bunch.Type.try_t(Shmex.t())

Creates shared memory segment and adds a guard for it.

The guard associated with this memory segment is placed in returned Shmex struct. When the guard resource is deallocated by BEAM, the shared memory is unlinked and will disappear from the system when last process using it unmaps it

Link to this function concat(left, right) View Source
concat(left :: Shmex.t(), right :: Shmex.t()) :: Bunch.Type.try_t(Shmex.t())

Reads the contents of SHM and returns as binary

Link to this function read(payload, cnt) View Source
read(payload :: Shmex.t(), cnt :: non_neg_integer()) ::
  Bunch.Type.try_t(binary())

Reads cnt bytes from SHM and returns as binary

cnt should not be greater than payload.size

Link to this function set_capacity(payload, capacity) View Source
set_capacity(payload :: Shmex.t(), capacity :: pos_integer()) ::
  Bunch.Type.try_t(Shmex.t())

Sets the capacity of SHM and updates the struct accordingly

Link to this function split_at(payload, position) View Source
split_at(payload :: Shmex.t(), position :: non_neg_integer()) ::
  Bunch.Type.try_t({Shmex.t(), Shmex.t()})

Splits the contents of SHM into 2 by moving part of the data into a new SHM

payload has to be an existing shm (obtained via allocate/1).

It virtually trims the existing SHM to position bytes by setting size to position (The actual data is still present) and the overlapping data is copied into the new SHM.

Link to this function trim(payload, bytes) View Source
trim(payload :: Shmex.t(), bytes :: non_neg_integer()) :: Bunch.Type.try_t()
Link to this function write(payload, data) View Source
write(payload :: Shmex.t(), data :: binary()) :: Bunch.Type.try_t(Shmex.t())

Writes the binary into the SHM.

Overwrites existing content. Increases capacity to fit the data.