libcamera v0.2.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 *
5 * shared_mem_object.h - Helper class for shared memory allocations
6 */
7#pragma once
8
9#include <cstddef>
10#include <string>
11#include <sys/mman.h>
12#include <utility>
13
16
17namespace libcamera {
18
20{
21public:
22 SharedMem()
23 : mem_(nullptr)
24 {
25 }
26
27 SharedMem(const std::string &name, std::size_t size);
28
30 {
31 this->name_ = std::move(rhs.name_);
32 this->fd_ = std::move(rhs.fd_);
33 this->mem_ = rhs.mem_;
34 rhs.mem_ = nullptr;
35 }
36
38 {
39 if (mem_)
40 munmap(mem_, size_);
41 }
42
43 /* Make SharedMem non-copyable for now. */
45
46 SharedMem &operator=(SharedMem &&rhs)
47 {
48 this->name_ = std::move(rhs.name_);
49 this->fd_ = std::move(rhs.fd_);
50 this->mem_ = rhs.mem_;
51 rhs.mem_ = nullptr;
52 return *this;
53 }
54
55 const SharedFD &fd() const
56 {
57 return fd_;
58 }
59
60 void *mem() const
61 {
62 return mem_;
63 }
64
65private:
66 std::string name_;
67 SharedFD fd_;
68 size_t size_;
69 void *mem_;
70};
71
72template<class T>
74{
75public:
76 static constexpr std::size_t SIZE = sizeof(T);
77
79 : obj_(nullptr)
80 {
81 }
82
83 template<class... Args>
84 SharedMemObject(const std::string &name, Args &&...args)
85 : shMem_(name, SIZE), obj_(nullptr)
86 {
87 void *mem = shMem_.mem();
88
89 if (mem == nullptr)
90 return;
91
92 obj_ = new (mem) T(std::forward<Args>(args)...);
93 }
94
96 {
97 this->shMem_ = std::move(rhs.shMem_);
98 this->obj_ = rhs.obj_;
99 rhs.obj_ = nullptr;
100 }
101
103 {
104 if (obj_)
105 obj_->~T();
106 }
107
108 /* Make SharedMemObject non-copyable for now. */
110
111 SharedMemObject<T> &operator=(SharedMemObject<T> &&rhs)
112 {
113 this->shMem_ = std::move(rhs.shMem_);
114 this->obj_ = rhs.obj_;
115 rhs.obj_ = nullptr;
116 return *this;
117 }
118
120 {
121 return obj_;
122 }
123
124 const T *operator->() const
125 {
126 return obj_;
127 }
128
130 {
131 return *obj_;
132 }
133
134 const T &operator*() const
135 {
136 return *obj_;
137 }
138
139 const SharedFD &fd() const
140 {
141 return shMem_.fd();
142 }
143
144 explicit operator bool() const
145 {
146 return !!obj_;
147 }
148
149private:
150 SharedMem shMem_;
151 T *obj_;
152};
153
154} /* 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 for allocating objects in shared memory.
Definition shared_mem_object.h:74
static constexpr std::size_t SIZE
The size of the object that is going to be stored here.
Definition shared_mem_object.h:76
const T * operator->() const
Operator-> for SharedMemObject.
Definition shared_mem_object.h:124
SharedMemObject(SharedMemObject< T > &&rhs)
Move constructor for SharedMemObject.
Definition shared_mem_object.h:95
const SharedFD & fd() const
Gets the file descriptor for the underlaying storage file.
Definition shared_mem_object.h:139
SharedMemObject(const std::string &name, Args &&...args)
Contstructor for the SharedMemObject.
Definition shared_mem_object.h:84
T & operator*()
Operator* for SharedMemObject.
Definition shared_mem_object.h:129
~SharedMemObject()
SharedMemObject destructor.
Definition shared_mem_object.h:102
T * operator->()
Operator-> for SharedMemObject.
Definition shared_mem_object.h:119
const T & operator*() const
Operator* for SharedMemObject.
Definition shared_mem_object.h:134
Helper class for allocating shared memory.
Definition shared_mem_object.h:20
SharedMem(SharedMem &&rhs)
Move constructor for SharedMem.
Definition shared_mem_object.h:29
~SharedMem()
SharedMem destructor.
Definition shared_mem_object.h:37
void * mem() const
Gets the pointer to the underlaying shared memory.
Definition shared_mem_object.h:60
const SharedFD & fd() const
Gets the file descriptor for the underlaying shared memory.
Definition shared_mem_object.h:55
Top-level libcamera namespace.
Definition backtrace.h:17
File descriptor wrapper.