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
19#include <libcamera/base/span.h>
20
21namespace libcamera {
22
23class 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:
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>>
59class 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
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
101 {
102 return obj_;
103 }
104
105 const T *operator->() const
106 {
107 return obj_;
108 }
109
111 {
112 return *obj_;
113 }
114
115 const T &operator*() const
116 {
117 return *obj_;
118 }
119
120private:
122
123 T *obj_;
124};
125
126} /* namespace libcamera */
Utilities to help constructing class interfaces.
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
Definition class.h:27
RAII-style wrapper for file descriptors.
Definition shared_fd.h:17
Helper class to allocate an object in shareable memory.
Definition shared_mem_object.h:60
static constexpr std::size_t kSize
The size of the object stored in shared memory.
Definition shared_mem_object.h:62
const T * operator->() const
Dereference the stored object.
Definition shared_mem_object.h:105
SharedMemObject(const std::string &name, Args &&...args)
Construct a SharedMemObject.
Definition shared_mem_object.h:70
const T & operator*() const
Dereference the stored object.
Definition shared_mem_object.h:115
T * operator->()
Dereference the stored object.
Definition shared_mem_object.h:100
SharedMemObject(SharedMemObject< T > &&rhs)
Move constructor for SharedMemObject.
Definition shared_mem_object.h:79
T & operator*()
Dereference the stored object.
Definition shared_mem_object.h:110
SharedMemObject< T > & operator=(SharedMemObject< T > &&rhs)
Move assignment operator for SharedMemObject.
Definition shared_mem_object.h:92
~SharedMemObject()
Destroy the SharedMemObject instance.
Definition shared_mem_object.h:86
Helper class to allocate and manage memory shareable between processes.
Definition shared_mem_object.h:24
virtual ~SharedMem()
Destroy the SharedMem instance.
Definition shared_mem_object.cpp:100
Span< uint8_t > mem() const
Retrieve the underlying shared memory.
Definition shared_mem_object.h:40
SharedMem & operator=(SharedMem &&rhs)
Move assignment operator for SharedMem.
Definition shared_mem_object.cpp:110
const SharedFD & fd() const
Retrieve the file descriptor for the underlying shared memory.
Definition shared_mem_object.h:35
Top-level libcamera namespace.
Definition backtrace.h:17
File descriptor wrapper.