Ocean
SharedMemory.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_BASE_SHARED_MEMORY_H
9 #define META_OCEAN_BASE_SHARED_MEMORY_H
10 
11 #include "ocean/base/Base.h"
12 #include "ocean/base/Lock.h"
13 
14 namespace Ocean
15 {
16 
17 /**
18  * This class implements a shared memory object allowing access to a shared buffer.
19  * @see SharedLock.
20  * @ingroup base
21  */
22 class OCEAN_BASE_EXPORT SharedMemory
23 {
24  public:
25 
26  /**
27  * Creates a new shared memory object.
28  */
29  SharedMemory() = default;
30 
31  /**
32  * Creates a new shared memory object with specified buffer size.
33  * @param name System wide unique name of the shared memory
34  * @param size Size of the buffer in bytes
35  */
36  SharedMemory(const std::wstring& name, const size_t size);
37 
38  /**
39  * Disabled copy constructor for a shared memory object.
40  * @param sharedMemory Shared memory object to be copied
41  */
42  SharedMemory(const SharedMemory& sharedMemory) = delete;
43 
44  /**
45  * Move constructor for a shared memory object.
46  * @param sharedMemory Shared memory object to be copied
47  */
48  SharedMemory(SharedMemory&& sharedMemory) noexcept;
49 
50  /**
51  * Destructs a shared memory object.
52  */
54 
55  /**
56  * Returns the system wide unique name of this shared memory
57  * @return Shared memory name
58  */
59  inline const std::wstring& name() const;
60 
61  /**
62  * Returns the size of the shared memory buffer in bytes.
63  * @return Buffer size
64  */
65  inline size_t size() const;
66 
67  /**
68  * Resizes the shared memory buffer.
69  * @param newSize New size of the shared buffer in bytes
70  * @return True, if succeeded
71  */
72  bool resize(const size_t newSize);
73 
74  /**
75  * Flushes the shared memory.
76  */
77  void flush();
78 
79  /**
80  * Releases the shared memory buffer.
81  */
82  void release();
83 
84  /**
85  * Returns a pointer to the shared memory buffer.
86  * @return Memory buffer
87  */
88  inline const void* constdata() const;
89 
90  /**
91  * Returns a pointer to the shared memory buffer.
92  * @return Memory buffer
93  */
94  inline void* data();
95 
96  /**
97  * Returns whether this shared memory object holds a valid shared buffer.
98  * @return True, if so
99  */
100  explicit inline operator bool() const;
101 
102  /**
103  * Disabled assign operator for shared memory objects.
104  * @param sharedMemory Shared memory object to be assigned
105  * @return Reference to this object
106  */
107  SharedMemory& operator=(const SharedMemory& sharedMemory) = delete;
108 
109  /**
110  * Move operator for shared memory objects.
111  * @param sharedMemory Shared memory object to be moved
112  * @return Reference to this object
113  */
114  SharedMemory& operator=(SharedMemory&& sharedMemory) noexcept;
115 
116  private:
117 
118  /**
119  * Requests a shared memory buffer.
120  * @param name Unique system wide memory name
121  * @param size Size of the requested memory and finally the resulting size of the created memory (or already existing memory)
122  * @param handle Resulting memory handle
123  * @param data Resulting buffer pointer
124  * @param existedAlready Optional resulting whether the buffer existed already
125  * @return True, if succeeded
126  */
127  static bool requestSharedMemory(const std::wstring& name, size_t& size, void*& handle, void*& data, bool* existedAlready = nullptr);
128 
129  private:
130 
131  /// System wide unique memory name.
132  std::wstring name_;
133 
134  /// Sized of the shared memory buffer in bytes.
135  size_t size_ = 0;
136 
137  /// Pointer to the shared memory.
138  void* data_ = nullptr;
139 
140  /// Shared memory handle.
141  void* handle_ = nullptr;
142 };
143 
144 inline const std::wstring& SharedMemory::name() const
145 {
146  return name_;
147 }
148 
149 inline size_t SharedMemory::size() const
150 {
151  return size_;
152 }
153 
154 inline const void* SharedMemory::constdata() const
155 {
156  return data_;
157 }
158 
159 inline void* SharedMemory::data()
160 {
161  return data_;
162 }
163 
164 inline SharedMemory::operator bool() const
165 {
166  return data_ != nullptr;
167 }
168 
169 }
170 
171 #endif // META_OCEAN_BASE_SHARED_MEMORY_H
This class implements a shared memory object allowing access to a shared buffer.
Definition: SharedMemory.h:23
void flush()
Flushes the shared memory.
SharedMemory()=default
Creates a new shared memory object.
size_t size_
Sized of the shared memory buffer in bytes.
Definition: SharedMemory.h:135
size_t size() const
Returns the size of the shared memory buffer in bytes.
Definition: SharedMemory.h:149
std::wstring name_
System wide unique memory name.
Definition: SharedMemory.h:132
SharedMemory(const std::wstring &name, const size_t size)
Creates a new shared memory object with specified buffer size.
SharedMemory & operator=(const SharedMemory &sharedMemory)=delete
Disabled assign operator for shared memory objects.
void * data_
Pointer to the shared memory.
Definition: SharedMemory.h:138
static bool requestSharedMemory(const std::wstring &name, size_t &size, void *&handle, void *&data, bool *existedAlready=nullptr)
Requests a shared memory buffer.
SharedMemory(const SharedMemory &sharedMemory)=delete
Disabled copy constructor for a shared memory object.
SharedMemory & operator=(SharedMemory &&sharedMemory) noexcept
Move operator for shared memory objects.
~SharedMemory()
Destructs a shared memory object.
void * data()
Returns a pointer to the shared memory buffer.
Definition: SharedMemory.h:159
void release()
Releases the shared memory buffer.
const std::wstring & name() const
Returns the system wide unique name of this shared memory.
Definition: SharedMemory.h:144
SharedMemory(SharedMemory &&sharedMemory) noexcept
Move constructor for a shared memory object.
bool resize(const size_t newSize)
Resizes the shared memory buffer.
const void * constdata() const
Returns a pointer to the shared memory buffer.
Definition: SharedMemory.h:154
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15