| | |
| | | perm.set_unrestricted(); |
| | | return perm; |
| | | } |
| | | void Swap(SharedMemory &a); |
| | | public: |
| | | static bool Remove(const std::string &name) { |
| | | return shared_memory_object::remove(name.c_str()); |
| | | } |
| | | SharedMemory(const std::string &name, const uint64_t size): |
| | | mshm_t(open_or_create, name.c_str(), size, 0, AllowAll()), |
| | | name_(name) |
| | | {} |
| | | SharedMemory(const std::string &name, const uint64_t size); |
| | | ~SharedMemory(); |
| | | std::string name() const { return name_; } |
| | | bool Remove() { return Remove(name()); } |
| | | |
| | | void *Alloc(const size_t size) { return allocate(size, std::nothrow); } |
| | | void Dealloc(void *p) { if(p) { deallocate(p); } } |
| | | template<class T> void Dealloc(offset_ptr<T> ptr) { return Dealloc(ptr.get()); } |
| | | |
| | | template <class T, class ...Params> T * New(Params const&...params) { return construct<T>(anonymous_instance, std::nothrow)(params...); } |
| | | template <class T> void Delete(T *p) { if (p) { destroy_ptr<T>(p); }; } |
| | | template <class T> void Delete(offset_ptr<T> p) { Delete(p.get()); } |
| | |
| | | |
| | | // ShmObject manages an object in shared memory, but ShmObject itself is not in shared memory. |
| | | // works like a smart pointer of an object in shared memory. |
| | | // TODO handshake with center, and can be removed if killed. |
| | | template <class T> |
| | | class ShmObject : private boost::noncopyable { |
| | | static std::string ObjName(const std::string &name) { return "obj" + name; } |
| | |
| | | |
| | | bool IsOk() const { return pdata_; } |
| | | protected: |
| | | ShmType &shm() { return shm_; } |
| | | ShmType &shm() const { return shm_; } |
| | | public: |
| | | template <class...Params> |
| | | ShmObject(ShmType &segment, const std::string &name, Params&&...t): |
| | |
| | | { |
| | | pdata_ = shm_.find_or_construct<Data>(ObjName(name_).c_str(), std::nothrow)(t...); |
| | | if (!IsOk()) { |
| | | throw("shm error: " + name_); |
| | | throw("Error: Not enough memory, can not allocate \"" + name_ + "\""); |
| | | } |
| | | } |
| | | Data *find(const std::string &name) { return shm_.find<Data>(ObjName(name).c_str()).first; } |
| | |
| | | const Data* data() const { return pdata_; } |
| | | Data* operator->() { return data(); } |
| | | const Data* operator->() const { return data(); } |
| | | virtual bool Remove() { return shm_.destroy<Data>(ObjName(name_).c_str()); } |
| | | bool Remove() { return shm_.destroy<Data>(ObjName(name_).c_str()); } |
| | | }; |
| | | |
| | | template <class D> using Allocator = allocator<D, SharedMemory::segment_manager>; |