From 11a43e25d778f063ddb4173119de3cb4dafccac6 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Fri, 16 Aug 2019 00:59:30 +0300 Subject: [PATCH] typeindices.hpp - start index from 0 Starting it from 1 makes it more error-prone to use. --- Utilities/typemap.h | 43 +++++++++----------------------------- rpcs3/util/typeindices.hpp | 8 +++---- 2 files changed, 14 insertions(+), 37 deletions(-) diff --git a/Utilities/typemap.h b/Utilities/typemap.h index 36cdbdab04..c0b4ec41a7 100644 --- a/Utilities/typemap.h +++ b/Utilities/typemap.h @@ -117,12 +117,6 @@ namespace utils } }; - template - uint get_typeid() noexcept - { - return stx::type_counter::type>.index(); - } - // Internal, control block for a particular object class typemap_block { @@ -448,7 +442,7 @@ namespace utils static_assert(std::is_same_v); // Set type; zero value shall not be observed in the case of recreation - if (m_block->m_type.exchange(type_index()) != 0) + if (m_block->m_type.exchange(1) != 0) { // Destroy object if it exists m_block->get_ptr()->~T(); @@ -492,17 +486,6 @@ namespace utils return static_cast(quot) * step + bias; } - // Get current type - uint get_type() const - { - return m_block->m_type; - } - - static uint type_index() - { - return get_typeid(); - } - static constexpr bool type_const() { return std::is_const_v>; @@ -529,7 +512,7 @@ namespace utils template typemap_head* get_head() const { - return &m_map[get_typeid() - 1]; + return &m_map[stx::type_counter::type>.index()]; } public: @@ -585,9 +568,9 @@ namespace utils { const auto block = reinterpret_cast(m_map[i].m_ptr + j * m_map[i].m_ssize); - if (const uint type_id = block->m_type) + if (block->m_type) { - m_map[type_id - 1].clean(block); + m_map[i].clean(block); } } @@ -666,8 +649,6 @@ namespace utils return {}; } - const uint type_id = get_typeid(); - using id_tag = std::decay_t; typemap_head* head = get_head(); @@ -735,11 +716,11 @@ namespace utils { block = reinterpret_cast(head->m_ptr + j * head->m_ssize); - if (block->m_type == type_id) + if (block->m_type) { std::lock_guard lock(block->m_mutex); - if (block->m_type == type_id) + if (block->m_type) { if (std::invoke(std::forward(id), std::as_const(*block->get_ptr()))) { @@ -786,8 +767,6 @@ namespace utils { using id_tag = std::decay_t; - const uint type_id = get_typeid(); - if constexpr (std::is_same_v) { // No action for id_new @@ -800,7 +779,7 @@ namespace utils } else if constexpr (std::is_same_v) { - if (block->m_type == 0 && block->m_type.compare_and_swap_test(0, type_id)) + if (block->m_type == 0 && block->m_type.compare_and_swap_test(0, 1)) { // Initialize object if necessary static_assert(!std::is_const_v>); @@ -817,7 +796,7 @@ namespace utils return; } - if (LIKELY(block->m_type == type_id)) + if (LIKELY(block->m_type)) { if (std::invoke(std::forward(id), std::as_const(*block->get_ptr()))) { @@ -1026,8 +1005,6 @@ namespace utils static_assert(!std::is_array_v); static_assert(!std::is_void_v); - const uint type_id = get_typeid>(); - typemap_head* head = get_head>(); const ullong ix = head->m_create_count; @@ -1036,11 +1013,11 @@ namespace utils { const auto block = reinterpret_cast(head->m_ptr + j * head->m_ssize); - if (block->m_type == type_id) + if (block->m_type) { std::lock_guard lock(block->m_mutex); - if (block->m_type == type_id) + if (block->m_type) { std::invoke(std::forward(func), *block->get_ptr>()); } diff --git a/rpcs3/util/typeindices.hpp b/rpcs3/util/typeindices.hpp index ceb9142a50..8bb4d44ae1 100644 --- a/rpcs3/util/typeindices.hpp +++ b/rpcs3/util/typeindices.hpp @@ -9,8 +9,8 @@ namespace stx template class type_info final : public Info { - // Current type id (non-zero) - unsigned type = 0; + // Current type id (starts from 0) + unsigned type = 0u - 1; // Next typeinfo in linked list type_info* next = nullptr; @@ -45,7 +45,7 @@ namespace stx unsigned count() const { - return next->index(); + return next->index() + 1; } class const_iterator @@ -114,7 +114,7 @@ namespace stx template type_info::type_info(Info info, decltype(sizeof(int))) noexcept : Info(info) - , type(typeinfo_v.count() + 1) + , type(typeinfo_v.count()) { // Update linked list typeinfo_v.next->next = this;