PLCnext API Documentation 25.0.2.69
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
15class SharedMemory : private Loggable<SharedMemory>
16{
17public: // usings
18 using SharedMemoryImpl = boost::interprocess::managed_shared_memory;
19 using SegmentManager = SharedMemoryImpl::segment_manager;
20 using SharedMemoryPermission = boost::interprocess::permissions;
21
23 template <class T>
24 using Ptr = boost::interprocess::offset_ptr<T>;
25
26public: // construction/destruction
27 SharedMemory(const char* name);
28 SharedMemory(const char* name, size_t memorySize);
29 SharedMemory(const SharedMemory& arg) = delete;
30 SharedMemory(SharedMemory&& arg)noexcept = delete;
31 SharedMemory& operator=(const SharedMemory& arg) = delete;
32 SharedMemory& operator=(SharedMemory&& arg)noexcept = delete;
33 ~SharedMemory(void);
34
35public: // operations
36 void Dispose(void);
37
38public: // properties
39 bool IsOwner(void)const;
40 const String& GetName(void)const;
41 size_t GetSize(void)const;
42
43public: // operation
44 byte* Allocate(size_t size);
45 void Deallocate(byte* pMemory);
46
47 template<class T>
48 Ptr<T> Get(const char* name);
49
50 template<class T, typename ...TArgs>
51 Ptr<T> GetOrConstruct(const char* name, TArgs... args);
52
53 template<class T, typename ...TArgs>
54 Ptr<T> Construct(const char* name, TArgs... args);
55
56 template<class T>
57 void Destroy(const char* name);
58
59 template<class T, typename ...TArgs>
60 Ptr<T> ConstructUnnamed(TArgs... args);
61
62 template<class T>
64
65 size_t GetFreeMemory(void)const;
66 void ZeroFreeMemory(void);
67 bool AllMemoryDeallocated(void);
68 bool CheckSanity(void);
69
70protected: // fields
74 SharedMemoryImpl sharedMemoryImpl;
75
76private: // methods
77 void Create(void);
78 bool TryCreate(void);
79 void Open(void);
80 bool TryOpen(void);
81 void Remove(void);
82 bool TryRemove(void);
83
84private: // fields
85 size_t memorySize = 0;
86};
87
89// inline methods of class SharedMemory
90
93template<class T>
95{
96 return sharedMemoryImpl.find<T>(name).first;
97}
98
103template<class T, typename ...TArgs>
104inline SharedMemory::Ptr<T> SharedMemory::GetOrConstruct(const char* name, TArgs... args)
105{
106 Ptr<T> result = Get<T>(name);
107 if (!result)
108 {
109 return Construct<T>(name, args...);
110 }
111 return result;
112}
113
118template<class T, typename ...TArgs>
119inline SharedMemory::Ptr<T> SharedMemory::Construct(const char* name, TArgs... args)
120{
121 return this->sharedMemoryImpl.construct<T>(name)(args...);
122}
123
126template<class T>
127inline void SharedMemory::Destroy(const char* name)
128{
129 Ptr<T> pValue = this->Get<T>(name);
130 if (pValue != nullptr)
131 {
132 // free memory
133 this->sharedMemoryImpl.destroy<T>(name);
134 }
135}
136
140template<class T, typename ...TArgs>
142{
143 // allocate memory
144 void* pResult = this->sharedMemoryImpl.allocate(sizeof(T));
145 // call ctor using placement new operator with allocated memory pointer
146 return new (pResult) T(args...);
147}
148
151template<class T>
153{
154 if (ptr != nullptr)
155 {
156 // call dtor of object explicitly
157 ptr->~T();
158 // free memory
159 this->sharedMemoryImpl.deallocate(ptr.get());
160 }
161}
162
163}}}} // end of namespace Arp::System::Commons::Ipc
This class represents the Arp String. The implementation is based on std::string.
Definition: String.hpp:39
Derive from this class to inherit logging functionality.
Definition: Loggable.hxx:28
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:152
byte * Allocate(size_t size)
Tries to allocate size amount of bytes in the memory.
Definition: SharedMemory.cpp:155
bool IsOwner(void) const
Determines if this instance is the woner of the shared memory, i.e. ths shared memory was created by ...
Definition: SharedMemory.cpp:133
void ZeroFreeMemory(void)
Writes zero in all bytes not yet allocated.
Definition: SharedMemory.cpp:184
Ptr< T > Get(const char *name)
Tries to find a previously allocated named object.
Definition: SharedMemory.hpp:94
SharedMemoryImpl sharedMemoryImpl
Actual implementation of the shared memory functionality.
Definition: SharedMemory.hpp:74
boost::interprocess::offset_ptr< T > Ptr
Returned pointer types are shared memory based offset pointer.
Definition: SharedMemory.hpp:24
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:104
void Deallocate(byte *pMemory)
Marks the memory pointed to by pMemory available for new allocation requests.
Definition: SharedMemory.cpp:162
bool AllMemoryDeallocated(void)
Checks if all memory has been deallocated.
Definition: SharedMemory.cpp:191
String memoryName
Name of memory identifying the shared memory inside the operating system.
Definition: SharedMemory.hpp:72
SharedMemory(const char *name)
Opens an existing shared memory.
Definition: SharedMemory.cpp:116
size_t GetSize(void) const
Returns the capacity of the shared memory in bytes.
Definition: SharedMemory.cpp:147
void Destroy(const char *name)
Deallocates the object in the shared memory, also calling the objects destructor.
Definition: SharedMemory.hpp:127
size_t GetFreeMemory(void) const
Obtain the number of free bytes in the shared memory.
Definition: SharedMemory.cpp:178
const String & GetName(void) const
Returns the name of the memory object.
Definition: SharedMemory.cpp:140
~SharedMemory(void)
Deallocates the memory for this management object but not the memory shared between processes.
Definition: SharedMemory.cpp:123
void Dispose(void)
Removes the shared memory from the system. if this instance owns it.
Definition: SharedMemory.cpp:168
Ptr< T > Construct(const char *name, TArgs... args)
Creates a named object in the shared memory.
Definition: SharedMemory.hpp:119
bool CheckSanity(void)
Performs a sanity check over the shared memory.
Definition: SharedMemory.cpp:198
Ptr< T > ConstructUnnamed(TArgs... args)
Creates an unnamed object in the shared memory.
Definition: SharedMemory.hpp:141
Root namespace for the PLCnext API