libcamera: libcamera::SharedMem Class Reference

libcamera v0.5.0 Supporting cameras in Linux since 2019 |

Loading...
Searching...
No Matches
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:
[legend]

## Public Member Functions
| SharedMem (const std::string &name, std::size_t size)
| Construct a SharedMem with memory of the given size.
| SharedMem (SharedMem &&rhs)
| Move constructor for SharedMem.
virtual | ~SharedMem ()
| Destroy the SharedMem instance.
SharedMem & | operator= (SharedMem &&rhs)
| Move assignment operator for SharedMem.
const SharedFD & | fd () const
| Retrieve the file descriptor for the underlying shared memory.
Span< uint8_t > | mem () const
| Retrieve the underlying shared memory.
| operator bool () const
| Check if the shared memory allocation succeeded.

## 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] | name | Name of the SharedMem
  [in] | size | Size 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] | rhs | The 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] | rhs | The object to move

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

- include/libcamera/internal/shared_mem_object.h
- src/libcamera/shared_mem_object.cpp