libcamera  v0.2.0+150-2031e2f2
Supporting cameras in Linux since 2019
Public Member Functions | List of all members
libcamera::SharedMem Class Reference

Helper class to allocate and manage memory shareable between processes. More...

Inheritance diagram for libcamera::SharedMem:
Inheritance graph
[legend]

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...
 
SharedMemoperator= (SharedMem &&rhs)
 Move assignment operator for SharedMem. More...
 
const SharedFDfd () 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...
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ SharedMem() [1/2]

libcamera::SharedMem::SharedMem ( const std::string &  name,
std::size_t  size 
)

Construct a SharedMem with memory of the given size.

Parameters
[in]nameName of the SharedMem
[in]sizeSize of the shared memory to allocate and map

The name is used for debugging purpose only. Multiple SharedMem instances can have the same name.

◆ SharedMem() [2/2]

libcamera::SharedMem::SharedMem ( SharedMem &&  rhs)

Move constructor for SharedMem.

Parameters
[in]rhsThe object to move

◆ ~SharedMem()

libcamera::SharedMem::~SharedMem ( )
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.

Member Function Documentation

◆ fd()

const SharedFD & libcamera::SharedMem::fd ( ) const
inline

Retrieve the file descriptor for the underlying shared memory.

Returns
The file descriptor, or an invalid SharedFD if allocation failed

◆ mem()

Span< uint8_t > libcamera::SharedMem::mem ( ) const
inline

Retrieve the underlying shared memory.

Returns
The memory buffer, or an empty Span if allocation failed

◆ operator bool()

libcamera::SharedMem::operator bool ( ) const
inlineexplicit

Check if the shared memory allocation succeeded.

Returns
True if allocation of the shared memory succeeded, false otherwise

◆ operator=()

SharedMem & libcamera::SharedMem::operator= ( SharedMem &&  rhs)

Move assignment operator for SharedMem.

Parameters
[in]rhsThe object to move

The documentation for this class was generated from the following files: