libcamera v0.3.1 Supporting cameras in Linux since 2019

Loading...
Searching...
No Matches
shared_mem_object.h
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2023 Raspberry Pi Ltd
4 * Copyright (C) 2024 Andrei Konovalov
5 * Copyright (C) 2024 Dennis Bonke
6 *
7 * Helpers for shared memory allocations
8 */
9#pragma once
10
11#include <stddef.h>
12#include <stdint.h>
13#include <string>
14#include <sys/mman.h>
15#include <type_traits>
16#include <utility>
17
18#include <libcamera/base/class.h>
19#include <libcamera/base/shared_fd.h>
20#include <libcamera/base/span.h>
21
22 namespace libcamera {
23
24 class SharedMem
25{
26 public:
27 SharedMem();
28
29 SharedMem(const std::string &name, std::size_t size);
30 SharedMem(SharedMem &&rhs);
31
32 virtual ~SharedMem();
33
34 SharedMem &operator=(SharedMem &&rhs);
35
36 const SharedFD &fd() const
37 {
38 return fd_;
39 }
40
41 Span<uint8_t> mem() const
42 {
43 return mem_;
44 }
45
46 explicit operator bool() const
47 {
48 return !mem_.empty();
49 }
50
51 private:
52 LIBCAMERA_DISABLE_COPY(SharedMem)
53
54 SharedFD fd_;
55
56 Span<uint8_t> mem_;
57};
58
59template<class T, typename = std::enable_if_t<std::is_standard_layout<T>::value>>
60 class SharedMemObject : public SharedMem
61{
62 public:
63 static constexpr std::size_t kSize = sizeof(T);
64
65 SharedMemObject()
66 : SharedMem(), obj_(nullptr)
67 {
68 }
69
70 template<class... Args>
71 SharedMemObject(const std::string &name, Args &&...args)
72 : SharedMem(name, kSize), obj_(nullptr)
73 {
74 if (mem().empty())
75 return;
76
77 obj_ = new (mem().data()) T(std::forward<Args>(args)...);
78 }
79
80 SharedMemObject(SharedMemObject<T> &&rhs)
81 : SharedMem(std::move(rhs))
82 {
83 this->obj_ = rhs.obj_;
84 rhs.obj_ = nullptr;
85 }
86
87 ~SharedMemObject()
88 {
89 if (obj_)
90 obj_->~T();
91 }
92
93 SharedMemObject<T> &operator=(SharedMemObject<T> &&rhs)
94 {
95 SharedMem::operator=(std::move(rhs));
96 this->obj_ = rhs.obj_;
97 rhs.obj_ = nullptr;
98 return *this;
99 }
100
101 T *operator->()
102 {
103 return obj_;
104 }
105
106 const T *operator->() const
107 {
108 return obj_;
109 }
110
111 T &operator*()
112 {
113 return *obj_;
114 }
115
116 const T &operator*() const
117 {
118 return *obj_;
119 }
120
121 private:
122 LIBCAMERA_DISABLE_COPY(SharedMemObject)
123
124 T *obj_;
125};
126
127} /* namespace libcamera */
class.h
Utilities to help constructing class interfaces.
LIBCAMERA_DISABLE_COPY
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
Definition class.h:27
libcamera::SharedFD
RAII-style wrapper for file descriptors.
Definition shared_fd.h:17
libcamera::SharedMemObject
Helper class to allocate an object in shareable memory.
Definition shared_mem_object.h:61
libcamera::SharedMemObject::kSize
static constexpr std::size_t kSize
The size of the object stored in shared memory.
Definition shared_mem_object.h:63
libcamera::SharedMemObject::operator->
const T * operator->() const
Dereference the stored object.
Definition shared_mem_object.h:106
libcamera::SharedMemObject::SharedMemObject
SharedMemObject(const std::string &name, Args &&...args)
Construct a SharedMemObject.
Definition shared_mem_object.h:71
libcamera::SharedMemObject::operator*
const T & operator*() const
Dereference the stored object.
Definition shared_mem_object.h:116
libcamera::SharedMemObject::operator->
T * operator->()
Dereference the stored object.
Definition shared_mem_object.h:101
libcamera::SharedMemObject::SharedMemObject
SharedMemObject(SharedMemObject< T > &&rhs)
Move constructor for SharedMemObject.
Definition shared_mem_object.h:80
libcamera::SharedMemObject::operator*
T & operator*()
Dereference the stored object.
Definition shared_mem_object.h:111
libcamera::SharedMemObject::operator=
SharedMemObject< T > & operator=(SharedMemObject< T > &&rhs)
Move assignment operator for SharedMemObject.
Definition shared_mem_object.h:93
libcamera::SharedMemObject::~SharedMemObject
~SharedMemObject()
Destroy the SharedMemObject instance.
Definition shared_mem_object.h:87
libcamera::SharedMem
Helper class to allocate and manage memory shareable between processes.
Definition shared_mem_object.h:25
libcamera::SharedMem::~SharedMem
virtual ~SharedMem()
Destroy the SharedMem instance.
Definition shared_mem_object.cpp:111
libcamera::SharedMem::mem
Span< uint8_t > mem() const
Retrieve the underlying shared memory.
Definition shared_mem_object.h:41
libcamera::SharedMem::operator=
SharedMem & operator=(SharedMem &&rhs)
Move assignment operator for SharedMem.
Definition shared_mem_object.cpp:121
libcamera::SharedMem::fd
const SharedFD & fd() const
Retrieve the file descriptor for the underlying shared memory.
Definition shared_mem_object.h:36
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
shared_fd.h
File descriptor wrapper.