pub struct SharedAllocator(/* private fields */);
Expand description
An allocator implementing std::alloc::Allocator
which allocates items in linux shared
memory.
Implementations§
Sourcepub fn new(shmid_path: &str, size: usize) -> Self
pub fn new(shmid_path: &str, size: usize) -> Self
Construct an alloctor storing the shared memory id in a file at shmid_path
.
Constructing multiple allocators with the same shmid_path
will use the same shared memory.
After constructing the first allocator of a given shmid_path
constructing new allocators
with the same shmid_path
is the same as cloning the original allocator.
Allocators with the same shmid_path
across processes access the same memory although are
unaware of the presence of items created with allocators from other processes.
If 2 or more processes are allocating items in the same shared memory it is likely memory
will be corrupted.
When constructing an allocator with the same shmid_path
as an existing allocator the value
of size
will not be used for allocating shared memory but rather attaching shared memory.
As such if the value shoould not be larger than the shared memory initially allocated (by
the first allocator constructed with the shmid_path
)
This library does not currently implement a mechanism for communicating the layout of the
shared memory between allocators in different processes.
You have to do this manually, in the example of the simplest use case, 1 process stores a
large object in shared memory, when this process wishes to handoff to a newer process it
sends the address of this object in the shared memory over a
std::os::unix::net::UnixDatagram
to the new process, which can pickup this object more
quickly than if it had to be serialized and sent over a std::os::unix::net::UnixStream
for example
§Panics
For a whole lot of reasons. This is not a production ready library, it is a toy, treat it as such.
Trait Implementations§
Source§fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
allocator_api
)Source§unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
allocator_api
)ptr
. Read moreSource§fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
allocator_api
)allocate
, but also ensures that the returned memory is zero-initialized. Read moreSource§unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
allocator_api
)Source§unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow_zeroed( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
allocator_api
)grow
, but also ensures that the new contents are set to zero before being
returned. Read moreSource§fn clone(&self) -> SharedAllocator
fn clone(&self) -> SharedAllocator
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more