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
20#include <libcamera/base/span.h>
21
22namespace libcamera {
23
25{
26public:
27 SharedMem();
28
29 SharedMem(const std::string &name, std::size_t size);
30 SharedMem(SharedMem &&rhs);
31
32 virtual ~SharedMem();
33
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
51private:
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>>
61{
62public:
63 static constexpr std::size_t kSize = sizeof(T);
64
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
81 : SharedMem(std::move(rhs))
82 {
83 this->obj_ = rhs.obj_;
84 rhs.obj_ = nullptr;
85 }
86
88 {
89 if (obj_)
90 obj_->~T();
91 }
92
94 {
95 SharedMem::operator=(std::move(rhs));
96 this->obj_ = rhs.obj_;
97 rhs.obj_ = nullptr;
98 return *this;
99 }
100
102 {
103 return obj_;
104 }
105
106 const T *operator->() const
107 {
108 return obj_;
109 }
110
112 {
113 return *obj_;
114 }
115
116 const T &operator*() const
117 {
118 return *obj_;
119 }
120
121private:
123
124 T *obj_;
125};
126
127} /* 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:61
static constexpr std::size_t kSize
The size of the object stored in shared memory.
Definition shared_mem_object.h:63
const T * operator->() const
Dereference the stored object.
Definition shared_mem_object.h:106
SharedMemObject(const std::string &name, Args &&...args)
Construct a SharedMemObject.
Definition shared_mem_object.h:71
const T & operator*() const
Dereference the stored object.
Definition shared_mem_object.h:116
T * operator->()
Dereference the stored object.
Definition shared_mem_object.h:101
SharedMemObject(SharedMemObject< T > &&rhs)
Move constructor for SharedMemObject.
Definition shared_mem_object.h:80
T & operator*()
Dereference the stored object.
Definition shared_mem_object.h:111
SharedMemObject< T > & operator=(SharedMemObject< T > &&rhs)
Move assignment operator for SharedMemObject.
Definition shared_mem_object.h:93
~SharedMemObject()
Destroy the SharedMemObject instance.
Definition shared_mem_object.h:87
Helper class to allocate and manage memory shareable between processes.
Definition shared_mem_object.h:25
virtual ~SharedMem()
Destroy the SharedMem instance.
Definition shared_mem_object.cpp:111
Span< uint8_t > mem() const
Retrieve the underlying shared memory.
Definition shared_mem_object.h:41
SharedMem & operator=(SharedMem &&rhs)
Move assignment operator for SharedMem.
Definition shared_mem_object.cpp:121
const SharedFD & fd() const
Retrieve the file descriptor for the underlying shared memory.
Definition shared_mem_object.h:36
Top-level libcamera namespace.
Definition backtrace.h:17
File descriptor wrapper.