| | |
| | | #include "shm.h" |
| | | #include <atomic> |
| | | #include <boost/interprocess/offset_ptr.hpp> |
| | | #include <boost/uuid/uuid_generators.hpp> |
| | | #include <functional> |
| | | #include <stdint.h> |
| | | |
| | |
| | | // ShmMsg is safe to be stored in shared memory, so POD data or offset_ptr is required. |
| | | // message content layout: (meta) / header_size + header + data_size + data |
| | | |
| | | typedef boost::uuids::uuid MQId; |
| | | |
| | | class ShmMsg |
| | | class ShmMsg : private StaticDataRef<SharedMemory, ShmMsg> |
| | | { |
| | | private: |
| | | static inline SharedMemory &shm() { return GetData(); } |
| | | // store ref count, msgs shareing the same data should also hold a pointer of the same RefCount object. |
| | | class RefCount : private boost::noncopyable |
| | | { |
| | |
| | | { |
| | | static const Offset base = Addr(shm().get_address()); // cache value. |
| | | return base; |
| | | } |
| | | static inline SharedMemory &shm() |
| | | { |
| | | if (!pshm()) { throw std::string("Must set ShmMsg shm before use!"); } |
| | | return *pshm(); |
| | | } |
| | | static inline SharedMemory *&pshm() |
| | | { |
| | | static SharedMemory *pshm = 0; |
| | | return pshm; |
| | | } |
| | | |
| | | static const uint32_t kMsgTag = 0xf1e2d3c4; |
| | |
| | | T *get() const { return static_cast<T *>(Ptr(offset_ + BaseAddr())); } |
| | | |
| | | public: |
| | | static bool BindShm(SharedMemory &shm) |
| | | { |
| | | assert(!pshm()); |
| | | pshm() = &shm; |
| | | return true; |
| | | } |
| | | |
| | | static bool BindShm(SharedMemory &shm) { return SetData(shm); } |
| | | ShmMsg() : |
| | | ShmMsg(nullptr) {} |
| | | explicit ShmMsg(const size_t size) : |