libcamera: libcamera::SharedMemObject< T, typename > Class Template Reference

libcamera v0.7.1 Supporting cameras in Linux since 2019

Loading...
Searching...
No Matches
libcamera::SharedMemObject< T, typename > Class Template Reference

Helper class to allocate an object in shareable memory. More...

Inheritance diagram for libcamera::SharedMemObject< T, typename >:
[legend]
Collaboration diagram for libcamera::SharedMemObject< T, typename >:
[legend]

## Public Member Functions
template<class... Args>
| SharedMemObject (const std::string &name, Args &&...args)
| Construct a SharedMemObject.
| SharedMemObject (SharedMemObject< T > &&rhs)
| Move constructor for SharedMemObject.
| ~SharedMemObject ()
| Destroy the SharedMemObject instance.
SharedMemObject< T > & | operator= (SharedMemObject< T > &&rhs)
| Move assignment operator for SharedMemObject.
T * | operator-> ()
| Dereference the stored object.
const T * | operator-> () const
| Dereference the stored object.
T & | operator* ()
| Dereference the stored object.
const T & | operator* () const
| Dereference the stored object.
Public Member Functions inherited from libcamera::SharedMem
| 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.

## Static Public Attributes
static constexpr std::size_t | kSize = sizeof(T)
| The size of the object stored in shared memory.

## Detailed Description

template<class T, typename = std::enable_if_t<std::is_standard_layout<T>::value>>
class libcamera::SharedMemObject< T, typename >

Helper class to allocate an object in shareable memory.

Template Parameters

  The | object type

The SharedMemObject class is a specialization of the SharedMem class that wraps an object of type T and constructs it in shareable memory. It uses the same underlying memory allocation and sharing mechanism as the SharedMem class.

The wrapped object is constructed at the same time as the SharedMemObject instance, by forwarding the arguments passed to the SharedMemObject constructor. The underlying memory allocation is sized to the object T size. The bool() operator should be used to check the allocation was successful. The object can be accessed using the dereference operators operator*() and operator->().

While no restriction on the type T is enforced, not all types are suitable for sharing between multiple processes. Most notably, any object type that contains pointer or reference members will likely cause issues. Even if those members refer to other members of the same object, the shared memory will be mapped at different addresses in different processes, and the pointers will not be valid.

A new anonymous file is created for every SharedMemObject instance. If there is a need to share a large number of small objects, these objects should be grouped into a single larger object to limit the number of file descriptors.

To share the object with other processes, see the SharedMem documentation.

## Constructor & Destructor Documentation

## ◆ SharedMemObject() [1/2]

template<class T, typename = std::enable_if_t<std::is_standard_layout<T>::value>>
template<class... Args>

libcamera::SharedMemObject< T, typename >::SharedMemObject | ( | const std::string & | name,
|  | Args &&... | args )
| inline

Construct a SharedMemObject.

Parameters

  [in] | name | Name of the SharedMemObject
  [in] | args | Arguments to pass to the constructor of the object T

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

## ◆ SharedMemObject() [2/2]

template<class T, typename = std::enable_if_t<std::is_standard_layout<T>::value>>

libcamera::SharedMemObject< T, typename >::SharedMemObject | ( | SharedMemObject< T > && | rhs | ) |
| inline

Move constructor for SharedMemObject.

Parameters

  [in] | rhs | The object to move

## ◆ ~SharedMemObject()

template<class T, typename = std::enable_if_t<std::is_standard_layout<T>::value>>

libcamera::SharedMemObject< T, typename >::~SharedMemObject | ( |  | ) |
| inline

Destroy the SharedMemObject instance.

Destroying a SharedMemObject calls the wrapped T object's destructor. While the underlying memory may not be freed immediately if other mappings have been created manually (see SharedMem::~SharedMem() for more information), the stored object may be modified. Depending on the ~T() destructor, accessing the object after destruction of the SharedMemObject causes undefined behaviour. It is the responsibility of the user of this class to synchronize with other users who have access to the shared object.

## Member Function Documentation

## ◆ operator*() [1/2]

template<class T, typename = std::enable_if_t<std::is_standard_layout<T>::value>>

T & libcamera::SharedMemObject< T, typename >::operator* | ( |  | ) |
| inline

Dereference the stored object.

Returns
  Reference to the stored object

## ◆ operator*() [2/2]

template<class T, typename = std::enable_if_t<std::is_standard_layout<T>::value>>

const T & libcamera::SharedMemObject< T, typename >::operator* | ( |  | ) | const
| inline

Dereference the stored object.

Returns
  Reference to the stored object

## ◆ operator->() [1/2]

template<class T, typename = std::enable_if_t<std::is_standard_layout<T>::value>>

T * libcamera::SharedMemObject< T, typename >::operator-> | ( |  | ) |
| inline

Dereference the stored object.

Returns
  Pointer to the stored object

## ◆ operator->() [2/2]

template<class T, typename = std::enable_if_t<std::is_standard_layout<T>::value>>

const T * libcamera::SharedMemObject< T, typename >::operator-> | ( |  | ) | const
| inline

Dereference the stored object.

Returns
  Pointer to the stored object

## ◆ operator=()

template<class T, typename = std::enable_if_t<std::is_standard_layout<T>::value>>

SharedMemObject< T > & libcamera::SharedMemObject< T, typename >::operator= | ( | SharedMemObject< T > && | rhs | ) |
| inline

Move assignment operator for SharedMemObject.

Parameters

  [in] | rhs | The SharedMemObject object to take the data from

Moving a SharedMemObject does not affect the stored object.

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

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