libcamera v0.2.0+3-70b69666-nvm
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 <fcntl.h>
11#include <string>
12#include <sys/mman.h>
13#include <sys/stat.h>
14#include <unistd.h>
15#include <utility>
16
19
20namespace libcamera {
21
29template<class T>
31{
32public:
36 static constexpr std::size_t SIZE = sizeof(T);
37
39 : obj_(nullptr)
40 {
41 }
42
48 template<class... Args>
49 SharedMemObject(const std::string &name, Args &&...args)
50 : name_(name), obj_(nullptr)
51 {
52 void *mem;
53 int ret;
54
55 ret = memfd_create(name_.c_str(), MFD_CLOEXEC);
56 if (ret < 0)
57 return;
58
59 fd_ = SharedFD(std::move(ret));
60 if (!fd_.isValid())
61 return;
62
63 ret = ftruncate(fd_.get(), SIZE);
64 if (ret < 0)
65 return;
66
67 mem = mmap(nullptr, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
68 fd_.get(), 0);
69 if (mem == MAP_FAILED)
70 return;
71
72 obj_ = new (mem) T(std::forward<Args>(args)...);
73 }
74
80 {
81 this->name_ = std::move(rhs.name_);
82 this->fd_ = std::move(rhs.fd_);
83 this->obj_ = rhs.obj_;
84 rhs.obj_ = nullptr;
85 }
86
88 {
89 if (obj_) {
90 obj_->~T();
91 munmap(obj_, SIZE);
92 }
93 }
94
95 /* Make SharedMemObject non-copyable for now. */
96 LIBCAMERA_DISABLE_COPY(SharedMemObject)
97
98
102 SharedMemObject<T> &operator=(SharedMemObject<T> &&rhs)
103 {
104 this->name_ = std::move(rhs.name_);
105 this->fd_ = std::move(rhs.fd_);
106 this->obj_ = rhs.obj_;
107 rhs.obj_ = nullptr;
108 return *this;
109 }
110
117 {
118 return obj_;
119 }
120
126 const T *operator->() const
127 {
128 return obj_;
129 }
130
137 {
138 return *obj_;
139 }
140
146 const T &operator*() const
147 {
148 return *obj_;
149 }
150
156 const SharedFD &fd() const
157 {
158 return fd_;
159 }
160
166 explicit operator bool() const
167 {
168 return !!obj_;
169 }
170
171private:
172 std::string name_;
173 SharedFD fd_;
174 T *obj_;
175};
176
177} /* 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
int get() const
Retrieve the numerical file descriptor.
Definition shared_fd.h:30
bool isValid() const
Check if the SharedFD instance is valid.
Definition shared_fd.h:29
Helper class for shared memory allocations.
Definition shared_mem_object.h:31
const T * operator->() const
Operator-> for SharedMemObject.
Definition shared_mem_object.h:126
SharedMemObject(SharedMemObject< T > &&rhs)
Move constructor for SharedMemObject.
Definition shared_mem_object.h:79
SharedMemObject(const std::string &name, Args &&...args)
Contstructor for the SharedMemObject.
Definition shared_mem_object.h:49
const SharedFD & fd() const
Gets the file descriptor for the underlaying storage file.
Definition shared_mem_object.h:156
T * operator->()
Operator-> for SharedMemObject.
Definition shared_mem_object.h:116
const T & operator*() const
Operator* for SharedMemObject.
Definition shared_mem_object.h:146
static constexpr std::size_t SIZE
The size of the object that is going to be stored here.
Definition shared_mem_object.h:36
T & operator*()
Operator* for SharedMemObject.
Definition shared_mem_object.h:136
Top-level libcamera namespace.
Definition backtrace.h:17
File descriptor wrapper.