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