libcamera
v0.3.1+12-19bbca3c
Supporting cameras in Linux since 2019
|
Helper class to allocate and manage memory shareable between processes. More...
Public Member Functions | |
SharedMem (const std::string &name, std::size_t size) | |
Construct a SharedMem with memory of the given size. More... | |
SharedMem (SharedMem &&rhs) | |
Move constructor for SharedMem. More... | |
virtual | ~SharedMem () |
Destroy the SharedMem instance. More... | |
SharedMem & | operator= (SharedMem &&rhs) |
Move assignment operator for SharedMem. More... | |
const SharedFD & | fd () const |
Retrieve the file descriptor for the underlying shared memory. More... | |
Span< uint8_t > | mem () const |
Retrieve the underlying shared memory. More... | |
operator bool () const | |
Check if the shared memory allocation succeeded. More... | |
Helper class to allocate and manage memory shareable between processes.
SharedMem manages memory suitable for sharing between processes. When an instance is constructed, it allocates a memory buffer of the requested size backed by an anonymous file, using the memfd API.
The allocated memory is exposed by the mem() function. If memory allocation fails, the function returns an empty Span. This can be also checked using the bool() operator.
The file descriptor for the backing file is exposed as a SharedFD by the fd() function. It can be shared with other processes across IPC boundaries, which can then map the memory with mmap().
A single memfd is created for every SharedMem. If there is a need to allocate a large number of objects in shared memory, these objects should be grouped together and use the shared memory allocated by a single SharedMem object if possible. This will help to minimize the number of created memfd's.
libcamera::SharedMem::SharedMem | ( | const std::string & | name, |
std::size_t | size | ||
) |
libcamera::SharedMem::SharedMem | ( | SharedMem && | rhs | ) |
Move constructor for SharedMem.
[in] | rhs | The object to move |
|
virtual |
Destroy the SharedMem instance.
Destroying an instance invalidates the memory mapping exposed with mem(). Other mappings of the backing file, created in this or other processes with mmap(), remain valid.
Similarly, other references to the backing file descriptor created by copying the SharedFD returned by fd() remain valid. The underlying memory will be freed only when all file descriptors that reference the anonymous file get closed.
|
inline |
Retrieve the file descriptor for the underlying shared memory.
|
inline |
Retrieve the underlying shared memory.
|
inlineexplicit |
Check if the shared memory allocation succeeded.
Move assignment operator for SharedMem.
[in] | rhs | The object to move |