From 755bea8112eb50d223823c35364f7474b9a8434b Mon Sep 17 00:00:00 2001 From: zhangyi Date: Thu, 28 Mar 2019 16:23:31 +0800 Subject: [PATCH] add attach/detach for shm --- include/shm.h | 4 +++- src/shm.cpp | 11 +++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/include/shm.h b/include/shm.h index b5206c6..a20731e 100644 --- a/include/shm.h +++ b/include/shm.h @@ -38,7 +38,9 @@ public: void release(); void* get() const; - id_t dismiss(); + + void attach(id_t); + id_t detach(); private: class handle_; diff --git a/src/shm.cpp b/src/shm.cpp index 3b035a7..71e3a2a 100644 --- a/src/shm.cpp +++ b/src/shm.cpp @@ -66,7 +66,7 @@ bool handle::acquire(char const * name, std::size_t size, unsigned mode) { void handle::release() { if (!valid()) return; - shm::release(dismiss()); + shm::release(detach()); } void* handle::get() const { @@ -74,7 +74,14 @@ void* handle::get() const { return impl(p_)->m_; } -id_t handle::dismiss() { +void handle::attach(id_t id) { + if (id == nullptr) return; + release(); + impl(p_)->id_ = id; + impl(p_)->m_ = shm::get_mem(impl(p_)->id_, &(impl(p_)->s_)); +} + +id_t handle::detach() { if (!valid()) return nullptr; auto old = impl(p_)->id_; impl(p_)->id_ = nullptr;