Shmex v0.1.5 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
Concatenates two shared memory areas by copying the data from the second at the end of the first one
Reads the contents of shared memory and returns it as a binary
Reads cnt
bytes from the shared memory and returns it as a binary
Sets the capacity of shared memory area and updates the Shmex struct accordingly
Splits the contents of shared memory area into two by moving the data past the specified position into a new shared memory
Trims shared memory capacity to match its size
Drops bytes
bytes from the beggining of shared memory area and
trims it to match the new size
Writes the binary into the shared memory
Link to this section Types
try_t()
View Source
try_t() :: :ok | {:error, reason :: any()}
try_t() :: :ok | {:error, reason :: any()}
try_t(result)
View Source
try_t(result) :: {:ok, result} | {:error, reason :: any()}
try_t(result) :: {:ok, result} | {:error, reason :: any()}
Link to this section Functions
add_guard(shm) View Source
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
allocate(shm) View Source
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
concat(target, source) View Source
Concatenates two shared memory areas by copying the data from the second at the end of the first one.
The first shared memory is a target that will contain data from both shared memory areas. Its capacity will be set to the sum of sizes of both shared memory areas. The second one, the source, will remain unmodified.
read(shm) View Source
Reads the contents of shared memory and returns it as a binary.
read(shm, cnt)
View Source
read(shm :: Shmex.t(), cnt :: non_neg_integer()) :: try_t(binary())
read(shm :: Shmex.t(), cnt :: non_neg_integer()) :: try_t(binary())
Reads cnt
bytes from the shared memory and returns it as a binary.
cnt
should not be greater than shm.size
set_capacity(shm, capacity)
View Source
set_capacity(shm :: Shmex.t(), capacity :: pos_integer()) :: try_t(Shmex.t())
set_capacity(shm :: Shmex.t(), capacity :: pos_integer()) :: try_t(Shmex.t())
Sets the capacity of shared memory area and updates the Shmex struct accordingly.
split_at(shm, position)
View Source
split_at(shm :: Shmex.t(), position :: non_neg_integer()) ::
try_t({Shmex.t(), Shmex.t()})
split_at(shm :: Shmex.t(), position :: non_neg_integer()) :: try_t({Shmex.t(), Shmex.t()})
Splits the contents of shared memory area into two by moving the data past the specified position into a new shared memory.
shm
has to be an existing shared memory (obtained via allocate/1
).
It virtually trims the existing shared memory to position
bytes
by setting size
to position
(The actual data is still present)
and the overlapping data is copied into the new shared memory area.
trim(shm) View Source
Trims shared memory capacity to match its size.
trim(shm, bytes)
View Source
trim(shm :: Shmex.t(), bytes :: non_neg_integer()) :: try_t()
trim(shm :: Shmex.t(), bytes :: non_neg_integer()) :: try_t()
Drops bytes
bytes from the beggining of shared memory area and
trims it to match the new size.
write(shm, data) View Source
Writes the binary into the shared memory.
Overwrites the existing content. Increases the capacity of shared memory to fit the data.