PLCnext API Documentation  22.9.0.33
SharedMemory.hpp
1 #pragma once
2 #include "Arp/System/Core/Arp.h"
3 #include "Arp/System/Core/TypeName.hxx"
4 #include "Arp/System/Core/AppDomainSingleton.hxx"
5 #include "Arp/System/Commons/Logging.h"
6 #include "Arp/System/Commons/Threading/Thread.hpp"
7 #include "boost/interprocess/managed_shared_memory.hpp"
8 #include <algorithm> // using std::replace
9 #include <new> // using std::new(p)
10 #include <iostream>
11 
12 namespace Arp { namespace System { namespace Commons { namespace Ipc
13 {
14 
17 {
18 public: // typedefs
19 
20  typedef boost::interprocess::managed_shared_memory SharedMemoryImpl;
21  typedef SharedMemoryImpl::segment_manager SegmentManager;
22  typedef boost::interprocess::permissions SharedMemoryPermission;
23 
25  template <class T>
26  using Ptr = boost::interprocess::offset_ptr<T>;
27 
28 public: // construction/destruction
29 
33  SharedMemory(const char* name);
34 
39  SharedMemory(const char* name, size_t memorySize);
40 
42  ~SharedMemory(void) = default;
43 
44 public: // static operations
45 
47  void Remove(void);
48 
49 public: // properties
50 
54  const Arp::String& GetName(void)const;
55 
59  size_t GetSize(void)const;
60 
61 public: // operation
62 
68  byte* Allocate(size_t size);
69 
73  void Deallocate(byte* pMemory);
74 
78  template<class T>
79  Ptr<T> Get(const char* name);
80 
87  template<class T, typename ...TArgs>
88  Ptr<T> GetOrConstruct(const char* name, TArgs... args);
89 
96  template<class T, typename ...TArgs>
97  Ptr<T> Construct(const char* name, TArgs... args);
98 
104  template<class T, typename ...TArgs>
105  Ptr<T> ConstructUnnamed(TArgs... args);
106 
110  template<class T>
111  void Destroy(const char* name);
112 
116  size_t GetFreeMemory(void)const;
117 
119  void ZeroFreeMemory(void);
120 
124  bool AllMemoryDeallocated(void);
125 
129  bool CheckSanity(void);
130 
131 private: // deleted copying/assignment
132  SharedMemory(const SharedMemory& arg) = delete;
133  SharedMemory& operator=(const SharedMemory& arg) = delete;
134 
135 protected: // fields
136 
139 
141  SharedMemoryImpl sharedMemoryImpl;
142 
143 private:
144 
145 };
146 
148 // inline methods of class SharedMemory
149 
150 inline SharedMemory::SharedMemory(const char* name)
151  : memoryName(name)
152 {
153  this->sharedMemoryImpl = SharedMemoryImpl(boost::interprocess::open_only, this->memoryName);
154 }
155 
156 inline const Arp::String& SharedMemory::GetName(void)const
157 {
158  return this->memoryName;
159 }
160 
161 inline size_t SharedMemory::GetSize(void)const
162 {
163  return this->sharedMemoryImpl.get_size();
164 }
165 
166 inline byte* SharedMemory::Allocate(size_t size)
167 {
168  return (byte*)this->sharedMemoryImpl.allocate(size);
169 }
170 
171 inline void SharedMemory::Deallocate(byte* pMemory)
172 {
173  this->sharedMemoryImpl.deallocate(pMemory);
174 }
175 
176 template<class T>
177 inline SharedMemory::Ptr<T> SharedMemory::Get(const char* name)
178 {
179  return sharedMemoryImpl.find<T>(name).first;
180 }
181 
182 template<class T, typename ...TArgs>
183 inline SharedMemory::Ptr<T> SharedMemory::GetOrConstruct(const char* name, TArgs... args)
184 {
185  //Ptr<T> result = sharedMemoryImpl.find<T>(name).first;
186  Ptr<T> result = Get<T>(name);
187  if (!result)
188  {
189  //return this->sharedMemoryImpl.construct<T>(name)(args...);
190  return Construct<T>(name, args...);
191  }
192  return result;
193 }
194 
195 template<class T, typename ...TArgs>
196 inline SharedMemory::Ptr<T> SharedMemory::Construct(const char* name, TArgs... args)
197 {
198  return this->sharedMemoryImpl.construct<T>(name)(args...);
199 }
200 
201 template<class T, typename ...TArgs>
203 {
204  // allocate memory
205  byte* pResult = (byte*)this->sharedMemoryImpl.allocate(sizeof(T));
206  // call ctor using placement new operator with allocated memory pointer
207  return new (pResult) T(args...);
208 }
209 
210 template<class T>
211 inline void SharedMemory::Destroy(const char* name)
212 {
213  Ptr<T> pValue = this->Get<T>(name);
214  if(pValue != nullptr)
215  {
216  // call dtor of object explicitly
217 // pValue->~T();
218  // free memory
219  this->sharedMemoryImpl.destroy<T>(name);
220  }
221 }
222 
223 inline void SharedMemory::Remove()
224 {
225  (void)boost::interprocess::shared_memory_object::remove(this->GetName());
226 }
227 
228 inline size_t SharedMemory::GetFreeMemory(void)const
229 {
230  return this->sharedMemoryImpl.get_free_memory();
231 }
232 
234 {
235  this->sharedMemoryImpl.zero_free_memory();
236 }
237 
239 {
240  return this->sharedMemoryImpl.all_memory_deallocated();
241 }
242 
243 inline bool SharedMemory::CheckSanity(void)
244 {
245  return this->sharedMemoryImpl.check_sanity();
246 }
247 
249 // Template specializations
250 
251 }}}} // end of namespace Arp::System::Commons::Ipc
API to manage and manipulate memory shared between different processes
Definition: SharedMemory.hpp:17
byte * Allocate(size_t size)
Tries to allocate size amount of bytes in the memory.
Definition: SharedMemory.hpp:166
Arp::String memoryName
Name of memory identifying the shared memory inside the operating system.
Definition: SharedMemory.hpp:138
void ZeroFreeMemory(void)
Writes zero in all bytes not yet allocated.
Definition: SharedMemory.hpp:233
Ptr< T > Get(const char *name)
Tries to find a previously allocated named object.
Definition: SharedMemory.hpp:177
SharedMemoryImpl sharedMemoryImpl
Actual implementation of the shared memory functionality.
Definition: SharedMemory.hpp:141
boost::interprocess::offset_ptr< T > Ptr
Returned pointer types are shared memory based offset pointer.
Definition: SharedMemory.hpp:26
Ptr< T > GetOrConstruct(const char *name, TArgs... args)
Tries to find object identified by name, creates a new one if the object does not exists yet.
Definition: SharedMemory.hpp:183
void Deallocate(byte *pMemory)
Marks the memory pointed to by pMemory available for new allocation requests.
Definition: SharedMemory.hpp:171
bool AllMemoryDeallocated(void)
Checks if all memory has been deallocated.
Definition: SharedMemory.hpp:238
SharedMemory(const char *name)
Opens an existing shared memory.
Definition: SharedMemory.hpp:150
size_t GetSize(void) const
Returns the capacity of the shared memory in bytes.
Definition: SharedMemory.hpp:161
~SharedMemory(void)=default
Deallocates the memory for this management object but not the memory shared between processes.
void Destroy(const char *name)
Deallocates the object in the shared memory, also calling the objects destructor.
Definition: SharedMemory.hpp:211
size_t GetFreeMemory(void) const
Obtain the number of free bytes in the shared memory.
Definition: SharedMemory.hpp:228
SharedMemory(const char *name, size_t memorySize)
Creates an non-existing instance, throws if yet exists.
const Arp::String & GetName(void) const
Returns the name of the memory object.
Definition: SharedMemory.hpp:156
void Remove(void)
Removes the shared memory from the system.
Definition: SharedMemory.hpp:223
Ptr< T > Construct(const char *name, TArgs... args)
Creates a named object in the shared memory.
Definition: SharedMemory.hpp:196
bool CheckSanity(void)
Performs a sanity check over the shared memory.
Definition: SharedMemory.hpp:243
Ptr< T > ConstructUnnamed(TArgs... args)
Creates an unnamed object in the shared memory.
Definition: SharedMemory.hpp:202
@ System
System components used by the System, Device, Plc or Io domains.
Root namespace for the PLCnext API