From 280cc81fa28399432a94fd5ba8aa1254d591f823 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Fri, 22 Mar 2019 22:16:53 +0800 Subject: [PATCH] remove useless codes --- include/def.h | 3 -- include/ipc.h | 12 ----- src/channel.cpp | 115 ---------------------------------------- src/id_pool.h | 127 --------------------------------------------- test/test_circ.cpp | 2 +- 5 files changed, 1 insertion(+), 258 deletions(-) delete mode 100644 src/channel.cpp delete mode 100644 src/id_pool.h diff --git a/include/def.h b/include/def.h index 4e704ef..fd3a608 100644 --- a/include/def.h +++ b/include/def.h @@ -46,9 +46,6 @@ enum class trans { // transmission template struct wr {}; -// implement with multi routes -struct wr_routes {}; - // concept helpers template diff --git a/include/ipc.h b/include/ipc.h index 7e4d0a9..80ca8a2 100644 --- a/include/ipc.h +++ b/include/ipc.h @@ -25,18 +25,6 @@ struct IPC_EXPORT chan_impl { static buff_t recv(handle_t h); }; -template <> -struct IPC_EXPORT chan_impl { - static handle_t connect (char const * name); - static void disconnect(handle_t h); - - static std::size_t recv_count(handle_t h); - static bool wait_for_recv(handle_t h, std::size_t r_count); - - static bool send(handle_t h, void const * data, std::size_t size); - static buff_t recv(handle_t h); -}; - template class chan_wrapper { private: diff --git a/src/channel.cpp b/src/channel.cpp deleted file mode 100644 index 3287143..0000000 --- a/src/channel.cpp +++ /dev/null @@ -1,115 +0,0 @@ -#include -#include -#include - -#include "ipc.h" -#include "shm.h" -#include "rw_lock.h" -#include "id_pool.h" - -#include "platform/detail.h" - -namespace { - -using namespace ipc; - -struct ch_info_t { - rw_lock lc_; - id_pool<> ch_acc_; // only support 255 channels with one name -}; - -struct ch_multi_routes { - shm::handle h_; - route r_; - - std::size_t id_; - bool marked_ = false; - - std::array rts_; - - ch_info_t& info() { - return *static_cast(h_.get()); - } - - auto& acc() { - return info().ch_acc_; - } - - void mark_id() { - if (marked_) return; - marked_ = true; - IPC_UNUSED_ auto guard = ipc::detail::unique_lock(info().lc_); - acc().mark_acquired(id_); - } - - auto& sender() { - mark_id(); - return r_; - } - - bool valid() const { - return h_.valid() && r_.valid(); - } - - bool connect(char const * name) { - if (name == nullptr || name[0] == '\0') { - return false; - } - disconnect(); - if (!h_.acquire((std::string { name } + "_").c_str(), sizeof(ch_info_t))) { - return false; - } - { - IPC_UNUSED_ auto guard = ipc::detail::unique_lock(info().lc_); - if (acc().invalid()) acc().init(); - id_ = acc().acquire(); - } - if (id_ == invalid_value) { - return false; - } - r_.connect((name + std::to_string(id_)).c_str()); - return valid(); - } - - void disconnect() { - if (!valid()) return; - { - IPC_UNUSED_ auto guard = ipc::detail::unique_lock(info().lc_); - acc().release(id_); - } - for (auto& rt : rts_) { - rt.disconnect(); - } - r_.disconnect(); - h_.release(); - } -}; - -} // internal-linkage - -namespace ipc { - -ipc::handle_t chan_impl::connect(char const * /*name*/) { - return nullptr; -} - -void chan_impl::disconnect(ipc::handle_t /*h*/) { -} - -std::size_t chan_impl::recv_count(ipc::handle_t /*h*/) { - return 0; -} - -bool chan_impl::wait_for_recv(ipc::handle_t /*h*/, std::size_t /*r_count*/) { - return false; -} - -bool chan_impl::send(ipc::handle_t /*h*/, void const * /*data*/, std::size_t /*size*/) { - return false; -} - -buff_t chan_impl::recv(ipc::handle_t /*h*/) { - return {}; -} - -} // namespace ipc diff --git a/src/id_pool.h b/src/id_pool.h deleted file mode 100644 index 16b2611..0000000 --- a/src/id_pool.h +++ /dev/null @@ -1,127 +0,0 @@ -#pragma once - -#include -#include - -#include "def.h" - -namespace ipc { - -template -struct id_type { - uint_t<8> id_; - alignas(AlignSize) byte_t data_[DataSize] {}; - - id_type& operator=(uint_t<8> val) { - id_ = val; - return (*this); - } - - operator uint_t<8>() const { - return id_; - } -}; - -template -struct id_type<0, AlignSize> { - uint_t<8> id_; - - id_type& operator=(uint_t<8> val) { - id_ = val; - return (*this); - } - - operator uint_t<8>() const { - return id_; - } -}; - -template = 201703L - std::size_t AlignSize = (std::min)(DataSize, alignof(std::size_t))> -#else /*__cplusplus < 201703L*/ - std::size_t AlignSize = (alignof(std::size_t) < DataSize) ? alignof(std::size_t) : DataSize> -#endif/*__cplusplus < 201703L*/ -class id_pool { -public: - enum : std::size_t { - max_count = (std::numeric_limits>::max)() // 255 - }; - -private: - id_type next_[max_count]; - - uint_t<8> acquir_ = 0; - uint_t<8> cursor_ = 0; - -public: - void init() { - acquir_ = max_count; - for (std::size_t i = 0; i < max_count;) { - i = next_[i] = static_cast>(i + 1); - } - } - - bool invalid() const { - static id_pool inv; - return std::memcmp(this, &inv, sizeof(id_pool)) == 0; - } - - bool empty() const { - return cursor_ == max_count; - } - - std::size_t acquire() { - if (empty()) { - return invalid_value; - } - std::size_t id = cursor_; - cursor_ = next_[id]; // point to next - return id; - } - - void mark_acquired(std::size_t id) { - next_[id] = acquir_; - acquir_ = static_cast>(id); // put it in acquired list - } - - bool release(std::size_t id) { - if (id == invalid_value) return false; - if (acquir_ == max_count) return false; - if (acquir_ == id) { - acquir_ = next_[id]; // point to next - } - else { - uint_t<8> a = next_[acquir_], l = acquir_; - while (1) { - if (a == max_count) { - return false; // found nothing - } - if (a == id) { - next_[l] = next_[a]; - break; - } - l = a; - a = next_[a]; - } - } - next_[id] = cursor_; - cursor_ = static_cast>(id); // put it back - return true; - } - - template - void for_acquired(F&& fr) { - auto a = acquir_; - while (a != max_count) { - if (!fr(a)) return; - a = next_[a]; - } - } - - void* at(std::size_t id) { - return next_[id].data_; - } -}; - -} // namespace ipc diff --git a/test/test_circ.cpp b/test/test_circ.cpp index 63e87d3..df1a493 100644 --- a/test/test_circ.cpp +++ b/test/test_circ.cpp @@ -241,7 +241,7 @@ private slots: #include "test_circ.moc" -constexpr int LoopCount = 10000000; +constexpr int LoopCount = 1000000; //constexpr int LoopCount = 1000/*0000*/; void Unit::initTestCase() {