libcamera: include/libcamera/internal/shared_mem_object.h Source File

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