libcamera  v0.5.0+45-f3a12332
Supporting cameras in Linux since 2019
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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>
19 #include <libcamera/base/span.h>
20 
21 namespace libcamera {
22 
23 class SharedMem
24 {
25 public:
26  SharedMem();
27 
28  SharedMem(const std::string &name, std::size_t size);
29  SharedMem(SharedMem &&rhs);
30 
31  virtual ~SharedMem();
32 
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 
50 private:
52 
53  SharedFD fd_;
54 
55  Span<uint8_t> mem_;
56 };
57 
58 template<class T, typename = std::enable_if_t<std::is_standard_layout<T>::value>>
59 class SharedMemObject : public SharedMem
60 {
61 public:
62  static constexpr std::size_t kSize = sizeof(T);
63 
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 
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 
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 
120 private:
122 
123  T *obj_;
124 };
125 
126 } /* namespace libcamera */
RAII-style wrapper for file descriptors.
Definition: shared_fd.h:16
T & operator*()
Dereference the stored object.
Definition: shared_mem_object.h:110
Utilities to help constructing class interfaces.
File descriptor wrapper.
SharedMemObject(SharedMemObject< T > &&rhs)
Move constructor for SharedMemObject.
Definition: shared_mem_object.h:79
Top-level libcamera namespace.
Definition: backtrace.h:17
T * operator->()
Dereference the stored object.
Definition: shared_mem_object.h:100
SharedMemObject(const std::string &name, Args &&...args)
Construct a SharedMemObject.
Definition: shared_mem_object.h:70
Span< uint8_t > mem() const
Retrieve the underlying shared memory.
Definition: shared_mem_object.h:40
Definition: v4l2_pixelformat.h:62
const T & operator*() const
Dereference the stored object.
Definition: shared_mem_object.h:115
virtual ~SharedMem()
Destroy the SharedMem instance.
Definition: shared_mem_object.cpp:100
Helper class to allocate an object in shareable memory.
Definition: shared_mem_object.h:59
~SharedMemObject()
Destroy the SharedMemObject instance.
Definition: shared_mem_object.h:86
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
const T * operator->() const
Dereference the stored object.
Definition: shared_mem_object.h:105
Helper class to allocate and manage memory shareable between processes.
Definition: shared_mem_object.h:23
const SharedFD & fd() const
Retrieve the file descriptor for the underlying shared memory.
Definition: shared_mem_object.h:35
SharedMem & operator=(SharedMem &&rhs)
Move assignment operator for SharedMem.
Definition: shared_mem_object.cpp:110
SharedMemObject< T > & operator=(SharedMemObject< T > &&rhs)
Move assignment operator for SharedMemObject.
Definition: shared_mem_object.h:92