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