3#include "Arp/System/Core/TypeName.hxx"
4#include "Arp/System/Commons/Logging.h"
5#include "Arp/System/Commons/Threading/Thread.hpp"
6#include "Arp/System/Commons/Exceptions/InvalidOperationException.hpp"
7#include "boost/interprocess/managed_shared_memory.hpp"
12namespace Arp {
namespace System {
namespace Commons {
namespace Ipc
19 using SharedMemoryImpl = boost::interprocess::managed_shared_memory;
20 using SegmentManager = SharedMemoryImpl::segment_manager;
21 using SharedMemoryPermission = boost::interprocess::permissions;
25 using Ptr = boost::interprocess::offset_ptr<T>;
56 template<
class T,
typename ...TArgs>
59 template<
class T,
typename ...TArgs>
65 template<
class T,
typename ...TArgs>
84 size_t memorySize = 0;
87 template<
class T>
class AlignmentProxy
90 template<
typename ...Args>
91 explicit AlignmentProxy(Args&& ... args);
92 AlignmentProxy(
const AlignmentProxy& arg) =
delete;
93 AlignmentProxy(AlignmentProxy&& arg)
noexcept =
delete;
94 AlignmentProxy& operator=(
const AlignmentProxy& arg) =
delete;
95 AlignmentProxy& operator=(AlignmentProxy&& arg)
noexcept =
delete;
96 ~AlignmentProxy(
void);
99 T* GetAlignedObjectPtr(
void);
102 static constexpr size_t alignment =
static_cast<size_t>(std::max(
static_cast<int>(
alignof(T)) -
static_cast<int>(
alignof(
void*)), 0));
103 static constexpr size_t alignedSize =
sizeof(T) + alignment;
104 std::array<byte, alignedSize> buffer{};
122 return proxyPtr.get()->GetAlignedObjectPtr();
129template<
class T,
typename ...TArgs>
132 Ptr<T> result = Get<T>(name);
135 return Construct<T>(name, std::forward<TArgs>(args)...);
144template<
class T,
typename ...TArgs>
147 return this->sharedMemoryImpl.construct<AlignmentProxy<T>>(name)(std::forward<TArgs>(args)...)->GetAlignedObjectPtr();
157 if (proxyPtr !=
nullptr)
160 this->sharedMemoryImpl.destroy<AlignmentProxy<T>>(name);
167template<
class T,
typename ...TArgs>
171 void* pResult = this->sharedMemoryImpl.allocate(
sizeof(T));
173 return new (pResult) T(std::forward<TArgs>(args)...);
186 this->sharedMemoryImpl.deallocate(ptr.get());
194template<
typename ...Args>
195inline SharedMemory::AlignmentProxy<T>::AlignmentProxy(Args&& ... args)
197 new (this->GetAlignedObjectPtr()) T(std::forward<Args>(args)...);
201inline SharedMemory::AlignmentProxy<T>::~AlignmentProxy()
203 this->GetAlignedObjectPtr()->~T();
207inline T* SharedMemory::AlignmentProxy<T>::GetAlignedObjectPtr()
209 size_t alignedSpace = alignedSize;
210 void* pBuffer = buffer.data();
212 void* pResult = std::align(
alignof(T),
sizeof(T), pBuffer, alignedSpace);
213 if (pResult ==
nullptr)
219 return reinterpret_cast<T*
>(pResult);
static InvalidOperationException Create(const String &message)
Creates an InvalidOperationException using an additional message.
Definition: InvalidOperationException.cpp:96
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 several processes
Definition: SharedMemory.hpp:17
void DestroyUnnamed(SharedMemory::Ptr< T > ptr)
Deallocates the unnamed object in the shared memory, also calling the objects destructor.
Definition: SharedMemory.hpp:179
byte * Allocate(size_t size)
Tries to allocate size amount of bytes in the memory.
Definition: SharedMemory.cpp:178
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:156
void ZeroFreeMemory(void)
Writes zero in all bytes not yet allocated.
Definition: SharedMemory.cpp:207
Ptr< T > Get(const char *name)
Tries to find a previously allocated named object.
Definition: SharedMemory.hpp:114
SharedMemoryImpl sharedMemoryImpl
Actual implementation of the shared memory functionality.
Definition: SharedMemory.hpp:72
boost::interprocess::offset_ptr< T > Ptr
Returned pointer types are shared memory based offset pointer.
Definition: SharedMemory.hpp:25
void Deallocate(byte *pMemory)
Marks the memory pointed to by pMemory available for new allocation requests.
Definition: SharedMemory.cpp:185
bool AllMemoryDeallocated(void)
Checks if all memory has been deallocated.
Definition: SharedMemory.cpp:214
SharedMemory(const char *name)
Opens an existing shared memory.
Definition: SharedMemory.cpp:17
size_t GetSize(void) const
Returns the capacity of the shared memory in bytes.
Definition: SharedMemory.cpp:170
void Destroy(const char *name)
Deallocates the object in the shared memory, also calling the objects destructor.
Definition: SharedMemory.hpp:153
size_t GetFreeMemory(void) const
Obtain the number of free bytes in the shared memory.
Definition: SharedMemory.cpp:201
const String & GetName(void) const
Returns the name of the memory object.
Definition: SharedMemory.cpp:163
~SharedMemory(void)
Deallocates the memory for this management object but not the memory shared between processes.
Definition: SharedMemory.cpp:46
void Dispose(void)
Removes the shared memory from the system. if this instance owns it.
Definition: SharedMemory.cpp:191
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:130
Ptr< T > Construct(const char *name, TArgs &&... args)
Creates a named object in the shared memory.
Definition: SharedMemory.hpp:145
Ptr< T > ConstructUnnamed(TArgs &&... args)
Creates an unnamed object in the shared memory.
Definition: SharedMemory.hpp:168
bool CheckSanity(void)
Performs a sanity check over the shared memory.
Definition: SharedMemory.cpp:221
Root namespace for the PLCnext API