From f7f8fbc85c48b003d1e3094a94a3528001977977 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期二, 01 六月 2021 15:15:55 +0800
Subject: [PATCH] remove length in ShmSocket ctor, not used.

---
 src/shm.h              |   34 ++++++++++------
 src/shm_socket.cpp     |    7 ---
 src/shm_socket.h       |    8 ++-
 utest/simple_tests.cpp |    4 +-
 src/shm_msg_queue.h    |    3 -
 utest/robust_test.cpp  |    2 
 box/node_center.cpp    |    4 +-
 src/shm_msg_queue.cpp  |   13 +-----
 src/defs.cpp           |    2 
 9 files changed, 35 insertions(+), 42 deletions(-)

diff --git a/box/node_center.cpp b/box/node_center.cpp
index 662b2c0..b285c9f 100644
--- a/box/node_center.cpp
+++ b/box/node_center.cpp
@@ -164,7 +164,7 @@
 
 		// create sockets.
 		try {
-			ShmSocket tmp(shm, true, ssn, 16);
+			ShmSocket tmp(shm, ssn, eCreate);
 			node->addrs_.emplace(ssn, tmp.AbsAddr());
 			return true;
 		} catch (...) {
@@ -333,7 +333,7 @@
 	auto &node = pos->second;
 	try {
 		for (int i = 0; i < msg.extra_mq_num(); ++i) {
-			ShmSocket tmp(node->shm_, true, head.ssn_id() + i + 1, 16);
+			ShmSocket tmp(node->shm_, head.ssn_id() + i + 1, eCreate);
 			node->addrs_.emplace(tmp.id(), tmp.AbsAddr());
 			auto addr = reply.add_extra_mqs();
 			addr->set_mq_id(tmp.id());
diff --git a/src/defs.cpp b/src/defs.cpp
index db0ae61..57df1bc 100644
--- a/src/defs.cpp
+++ b/src/defs.cpp
@@ -145,7 +145,7 @@
 
 			auto InitMQ = [&](auto &mq, auto &&id) {
 				mq.id_ = id;
-				ShmSocket tmp(shm, id, 16);
+				ShmSocket tmp(shm, id, eOpenOrCreate);
 				mq.offset_ = tmp.AbsAddr();
 			};
 
diff --git a/src/shm.h b/src/shm.h
index b5ec2ea..5013893 100644
--- a/src/shm.h
+++ b/src/shm.h
@@ -162,6 +162,12 @@
 template <class D>
 using SharedPtr = shared_ptr<D, Allocator<void>, Deleter<D>>;
 
+enum Mode {
+	eOpen = 0,
+	eCreate = 1,
+	eOpenOrCreate = 2
+};
+
 // ShmObject manages an object in shared memory, but ShmObject itself is not in shared memory.
 template <class T>
 class ShmObject : private boost::noncopyable
@@ -174,24 +180,26 @@
 	ShmType &shm() const { return shm_; }
 
 	template <class... Params>
-	ShmObject(ShmType &segment, const std::string &name, Params &&...t) :
+	ShmObject(ShmType &segment, const std::string &name, Mode mode, Params &&...t) :
 	    shm_(segment), name_(name)
 	{
-		pdata_ = shm_.FindOrCreate<Data>(ObjName(name_), std::forward<decltype(t)>(t)...);
-		if (!IsOk()) {
-			throw("Error: Not enough memory, can not allocate \"" + name_ + "\"");
-		}
-	}
-	template <class... Params>
-	ShmObject(ShmType &segment, const bool create_or_else_find, const std::string &name, Params &&...t) :
-	    shm_(segment), name_(name)
-	{
-		if (create_or_else_find) {
-			pdata_ = shm_.Create<Data>(ObjName(name_), std::forward<decltype(t)>(t)...);
-		} else {
+		switch (mode) {
+		case eOpen:
 			pdata_ = shm_.Find<Data>(ObjName(name_));
+			break;
+		case eCreate:
+			pdata_ = shm_.Create<Data>(ObjName(name_), std::forward<decltype(t)>(t)...);
+			break;
+		case eOpenOrCreate:
+			pdata_ = shm_.FindOrCreate<Data>(ObjName(name_), std::forward<decltype(t)>(t)...);
+			break;
+		default: break;
+		}
+		if (!IsOk()) {
+			throw("Error: shm can not create/open \"" + name_ + "\"");
 		}
 	}
+
 	ShmObject(const int64_t offset, ShmType &segment, const std::string &name) :
 	    shm_(segment), name_(name)
 	{
diff --git a/src/shm_msg_queue.cpp b/src/shm_msg_queue.cpp
index 9db4c6b..056daec 100644
--- a/src/shm_msg_queue.cpp
+++ b/src/shm_msg_queue.cpp
@@ -31,19 +31,10 @@
 
 } // namespace
 
-ShmMsgQueue::ShmMsgQueue(ShmType &segment, const MQId id, const int len) :
+ShmMsgQueue::ShmMsgQueue(ShmType &segment, const MQId id, Mode mode) :
     id_(id),
-    queue_(segment, MsgQIdToName(id_), len, segment.get_segment_manager())
+    queue_(segment, MsgQIdToName(id_), mode)
 {
-}
-
-ShmMsgQueue::ShmMsgQueue(ShmType &segment, const bool create_or_else_find, const MQId id, const int len) :
-    id_(id),
-    queue_(segment, create_or_else_find, MsgQIdToName(id_), len, segment.get_segment_manager())
-{
-	if (!queue_.IsOk()) {
-		throw("error create/find msgq " + std::to_string(id_));
-	}
 }
 ShmMsgQueue::ShmMsgQueue(const int64_t abs_addr, ShmType &segment, const MQId id) :
     id_(id), queue_(abs_addr, segment, MsgQIdToName(id_))
diff --git a/src/shm_msg_queue.h b/src/shm_msg_queue.h
index db5414b..e6c104b 100644
--- a/src/shm_msg_queue.h
+++ b/src/shm_msg_queue.h
@@ -34,8 +34,7 @@
 	typedef Shmq::ShmType ShmType;
 	typedef uint64_t MQId;
 
-	ShmMsgQueue(ShmType &segment, const MQId id, const int len);
-	ShmMsgQueue(ShmType &segment, const bool create_or_else_find, const MQId id, const int len);
+	ShmMsgQueue(ShmType &segment, const MQId id, Mode mode);
 	ShmMsgQueue(const int64_t abs_addr, ShmType &segment, const MQId id);
 	~ShmMsgQueue();
 	static bool Remove(ShmType &shm, const MQId id);
diff --git a/src/shm_socket.cpp b/src/shm_socket.cpp
index 2bdccc2..fa10dd3 100644
--- a/src/shm_socket.cpp
+++ b/src/shm_socket.cpp
@@ -29,13 +29,6 @@
 using namespace bhome_msg;
 using namespace bhome_shm;
 
-ShmSocket::ShmSocket(Shm &shm, const MQId id, const int len) :
-    run_(false), mq_(shm, id, len), alloc_id_(0), send_buffer_(shm) { Start(); }
-ShmSocket::ShmSocket(Shm &shm, const bool create_or_else_find, const MQId id, const int len) :
-    run_(false), mq_(shm, create_or_else_find, id, len), alloc_id_(0), send_buffer_(shm) { Start(); }
-ShmSocket::ShmSocket(int64_t abs_addr, Shm &shm, const MQId id) :
-    run_(false), mq_(abs_addr, shm, id), alloc_id_(0), send_buffer_(shm) { Start(); }
-
 ShmSocket::~ShmSocket() { Stop(); }
 
 bool ShmSocket::Start(int nworker, const RecvCB &onData, const RawRecvCB &onRaw, const IdleCB &onIdle)
diff --git a/src/shm_socket.h b/src/shm_socket.h
index 9dfdd6b..9bb65fc 100644
--- a/src/shm_socket.h
+++ b/src/shm_socket.h
@@ -47,9 +47,11 @@
 	typedef std::function<bool(ShmSocket &sock, MsgI &imsg, BHMsgHead &head)> PartialRecvCB;
 	typedef std::function<void(ShmSocket &sock)> IdleCB;
 
-	ShmSocket(Shm &shm, const MQId id, const int len);
-	ShmSocket(Shm &shm, const bool create_or_else_find, const MQId id, const int len);
-	ShmSocket(int64_t offset, Shm &shm, const MQId id);
+	ShmSocket(Shm &shm, const MQId id, Mode mode) :
+	    run_(false), mq_(shm, id, mode), alloc_id_(0), send_buffer_(shm) { Start(); }
+	ShmSocket(int64_t abs_addr, Shm &shm, const MQId id) :
+	    run_(false), mq_(abs_addr, shm, id), alloc_id_(0), send_buffer_(shm) { Start(); }
+
 	~ShmSocket();
 	static bool Remove(SharedMemory &shm, const MQId id) { return Queue::Remove(shm, id); }
 	bool Remove() { return Remove(shm(), id()); }
diff --git a/utest/robust_test.cpp b/utest/robust_test.cpp
index a744a61..b15e2e8 100644
--- a/utest/robust_test.cpp
+++ b/utest/robust_test.cpp
@@ -75,7 +75,7 @@
 	int64_t d;
 	BOOST_CHECK(tmp.pop(d));
 
-	ShmObject<Rcb> rcb(shm, "test_rcb");
+	ShmObject<Rcb> rcb(shm, "test_rcb", eOpenOrCreate);
 	bool try_more = true;
 
 	auto Writer = [&]() {
diff --git a/utest/simple_tests.cpp b/utest/simple_tests.cpp
index 6d3c7a2..87933ac 100644
--- a/utest/simple_tests.cpp
+++ b/utest/simple_tests.cpp
@@ -50,12 +50,12 @@
 			typedef ShmObject<s1000> Int;
 			std::string name = std::to_string(id);
 			auto a0 = Avail();
-			Int i1(shm, name);
+			Int i1(shm, name, eOpenOrCreate);
 			auto a1 = Avail();
 			BOOST_CHECK_LT(a1, a0);
 			printf("s1000 size: %ld\n", a0 - a1);
 			i1->a[0] = 5;
-			Int i2(shm, name);
+			Int i2(shm, name, eOpenOrCreate);
 			auto a2 = Avail();
 			BOOST_CHECK_EQUAL(a1, a2);
 			BOOST_CHECK_EQUAL(i1.data(), i2.data());

--
Gitblit v1.8.0