libcamera  v0.3.1+12-19bbca3c
Supporting cameras in Linux since 2019
Public Member Functions | Static Public Attributes | List of all members
libcamera::SharedMemObject< T, typename > Class Template Reference

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

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

Public Member Functions

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

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
Theobject 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]nameName of the SharedMemObject
[in]argsArguments 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]rhsThe 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>>
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>>
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>>
libcamera::SharedMemObject< T, typename >::operator= ( SharedMemObject< T > &&  rhs)
inline

Move assignment operator for SharedMemObject.

Parameters
[in]rhsThe 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: