From 3eb7e8d5a8b75a645d9994152451ce213e164887 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 14 Jul 2024 09:43:59 +0200 Subject: zz --- src/handle-fwd.hpp | 32 ------------- src/handle-page.hpp | 53 --------------------- src/handle-page.inl | 131 ---------------------------------------------------- src/handle-pool.hpp | 34 -------------- src/handle-pool.inl | 47 ------------------- src/handle.cpp | 12 ----- src/handle.hpp | 25 ---------- src/handle.inl | 40 ---------------- 8 files changed, 374 deletions(-) delete mode 100644 src/handle-fwd.hpp delete mode 100644 src/handle-page.hpp delete mode 100644 src/handle-page.inl delete mode 100644 src/handle-pool.hpp delete mode 100644 src/handle-pool.inl delete mode 100644 src/handle.cpp delete mode 100644 src/handle.hpp delete mode 100644 src/handle.inl (limited to 'src') diff --git a/src/handle-fwd.hpp b/src/handle-fwd.hpp deleted file mode 100644 index 3e010f6d..00000000 --- a/src/handle-fwd.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -namespace floormat::impl_handle { - -template struct Handle; -template struct Item; -template class Page; -template class Pool; - -template -struct Handle final -{ - uint32_t index = (uint32_t)-1; - uint32_t counter = 0; - - const OBJ& get() const; - OBJ& get(); - bool operator==(const Handle& other) const noexcept; - explicit operator bool() const noexcept; - -private: - static Item& get_from_pool(uint32_t index, uint32_t counter); -}; - -} // namespace floormat::impl_handle - -namespace floormat { - -template -using handle = struct impl_handle::Handle; - -} // namespace floormat diff --git a/src/handle-page.hpp b/src/handle-page.hpp deleted file mode 100644 index 7afad5f8..00000000 --- a/src/handle-page.hpp +++ /dev/null @@ -1,53 +0,0 @@ -#pragma once -#include "handle-fwd.hpp" -#include "compat/defs.hpp" -#include -#include - -namespace floormat::impl_handle { - -template -struct Item -{ - fm_DECLARE_DELETED_COPY_MOVE_ASSIGNMENTS(Item); - Item(); - ~Item() noexcept; - - union { char empty = {}; Obj object; }; - Handle handle; - uint32_t next; -}; - -template -class Page -{ - friend struct Handle; - - std::array, PageSize> storage; - BitArray used_map; // todo replace with a rewrite of std::bitset - uint32_t start_index; - uint32_t used_count; - uint32_t first_free; - bool locked; - - static void do_deallocate(Item& item); - -public: - fm_DECLARE_DELETED_COPY_MOVE_ASSIGNMENTS(Page); - - explicit Page(uint32_t start_index); - ~Page() noexcept; - - template - requires requires (Xs&&... xs) { - Obj{forward(xs)...}; - } - [[nodiscard]] Item& allocate(Xs&&... xs); - void deallocate(Handle obj); - void deallocate_all(); - bool is_empty(); - bool is_full(); - uint32_t use_count() const; -}; - -} // namespace floormat::impl_handle diff --git a/src/handle-page.inl b/src/handle-page.inl deleted file mode 100644 index 5eae88de..00000000 --- a/src/handle-page.inl +++ /dev/null @@ -1,131 +0,0 @@ -#pragma once -#include "handle-page.hpp" -#include "compat/assert.hpp" -#include "compat/pretty-function.hpp" - -namespace floormat::impl_handle { - -template -Item::Item() = default; - -template -Item::~Item() noexcept -{} - -template -void Page::do_deallocate(Item& item) -{ - item.object.~Obj(); -} - -template -Page::~Page() noexcept -{ - fm_assert(used_count == 0); -} - -template -Page::Page(uint32_t start_index): - used_map{ValueInit, PageSize}, - start_index{start_index}, - used_count{0}, - first_free{start_index}, - locked{false} -{ - fm_assert(start_index + PageSize > start_index); - auto val = start_index; - for (auto i = 0u; i < PageSize; i++) - { - auto& o = storage.data()[i]; - o.handle = {val++, 0}; - o.next = val; - } -} - -template -template -requires requires (Xs&&... xs) { - Obj{forward(xs)...}; -} -Item& Page::allocate(Xs&&... xs) -{ - fm_assert(!locked); - locked = true; - fm_assert(used_count < PageSize); - auto first_freeʹ = first_free - start_index; - fm_debug_assert(first_freeʹ < PageSize); - fm_debug_assert(!used_map[first_freeʹ]); - auto& item = storage[first_freeʹ]; - first_free = item.next; - used_count++; - used_map.set(first_freeʹ, true); - new (&item.object) Obj{ forward(xs)... }; - locked = false; - return item; -} - -template -void Page::deallocate(Handle obj) -{ - fm_assert(!locked); - locked = true; - auto index = obj.index - start_index; - fm_debug_assert(index < PageSize); - fm_assert(used_map[index]); - fm_debug_assert(used_count > 0); - fm_debug_assert(first_free != (uint32_t)-1); - auto& item = storage[index]; - auto ctr = item.handle.counter++; - if (ctr == (uint32_t)-1) [[unlikely]] - fm_debug("counter overflowed: %s", FM_PRETTY_FUNCTION); - fm_assert(obj.counter == ctr); - item.next = first_free; - first_free = obj.index; - used_count--; - used_map.set(index, false); - do_deallocate(item); - locked = false; -} - -template -void Page::deallocate_all() -{ - fm_assert(!locked); - locked = true; - for (auto i = 0u; i < PageSize; i++) - if (used_map[i]) - do_deallocate(storage[i]); - auto val = start_index; - for (auto i = 0u; i < PageSize; i++) - { - auto& o = storage.data()[i]; - bool used = used_map[i]; - uint32_t new_ctr = used ? 1 : 0; - o.handle = {val++, o.handle.counter + new_ctr}; - o.next = val; - } - first_free = start_index; - used_count = 0; - used_map = BitArray{ValueInit, PageSize}; - locked = false; -} - -template -bool Page::is_empty() -{ - return used_count == 0; -} - -template -bool Page::is_full() -{ - return used_count == PageSize; -} - -template -uint32_t Page::use_count() const -{ - return used_count; -} - -} // namespace floormat::impl_handle diff --git a/src/handle-pool.hpp b/src/handle-pool.hpp deleted file mode 100644 index 2721f094..00000000 --- a/src/handle-pool.hpp +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once -#include "compat/defs.hpp" -#include -#include - -namespace floormat::impl_handle { - -template -class Pool -{ - friend class impl_handle; - - static Pointer> make_pool(); - static Page& find_page(); - - Array>> pages; - size_t next_page_offset = 0; - - static Pointer> pool = make_pool(); - -public: - fm_DECLARE_DELETED_COPY_MOVE_ASSIGNMENTS(Pool); - - explicit Pool(); - ~Pool() noexcept; - - template - requires requires (Xs&&... xs) { - Obj{forward(xs)...}; - } - static impl_handle make_object(Xs&&... xs); -}; - -} // namespace floormat::impl_handle diff --git a/src/handle-pool.inl b/src/handle-pool.inl deleted file mode 100644 index 1420bd7b..00000000 --- a/src/handle-pool.inl +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once -#include "handle-pool.hpp" -#include "handle-page.hpp" -#include "compat/move.hpp" -#include - -namespace floormat::impl_handle { - -template -template -requires requires (Xs&&... xs) { - Obj{forward(xs)...}; -} -Handle Pool::make_object(Xs&&... xs) -{ - auto ret = std::shared_ptr(new Obj{forward(xs)...}); - do_make_object(std::static_pointer_cast(ret)); - return ret; -} - -template -Pointer> -Pool::make_pool() -{ - auto pool = Pointer>{InPlace}; - arrayReserve(pool->pages, 16); - return pool; -} - -template -Page& -Pool::find_page() -{ - auto& P = *pool; - auto sz = P.pages.size(); - for (auto i = 0uz; i < sz; i++) - { - auto& p = *P.pages.data()[i]; - if (!p.is_full()) - return p; - } - arrayAppend(P.pages, InPlace, InPlace, P.next_page_offset); - P.next_page_offset += PageSize; - return P.pages.back(); -} - -} // namespace floormat::impl_handle diff --git a/src/handle.cpp b/src/handle.cpp deleted file mode 100644 index ff9ed4db..00000000 --- a/src/handle.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "handle.inl" - -namespace floormat::impl_handle { - -} // namespace floormat::impl_handle - - -namespace floormat { - - - -} // namespace floormat diff --git a/src/handle.hpp b/src/handle.hpp deleted file mode 100644 index 89c1c480..00000000 --- a/src/handle.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include "handle-fwd.hpp" -#include "compat/assert.hpp" - -namespace floormat::impl_handle { - -template -bool Handle::operator==(const Handle& other) const noexcept -{ - bool ret = index == other.index; - fm_debug_assert(!ret || counter == other.counter); - return ret; -} - -template -Handle::operator bool() const noexcept -{ - return index != (uint32_t)-1; -} - -} // namespace floormat::impl_handle - -namespace floormat { - -} // namespace floormat diff --git a/src/handle.inl b/src/handle.inl deleted file mode 100644 index f19ec216..00000000 --- a/src/handle.inl +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once -#include "handle.hpp" -//#include "handle-pool.inl" -#include "compat/assert.hpp" - -namespace floormat::impl_handle { - -template -const Obj& Handle::get() const -{ - return get_from_pool(index, counter).object; -} - -template -Obj& Handle::get() -{ - return get_from_pool(index, counter).object; -} - -template -Item& Handle::get_from_pool(uint32_t index, uint32_t counter) -{ - auto page_idx = index / PageSize; - auto obj_idx = index % PageSize; - fm_assert(Pool::pages.size() < page_idx); - auto& page = Pool::pages.data()[page_idx]; - auto& item = page->storage[obj_idx]; - fm_debug_assert(page.used_map[obj_idx]); - fm_assert(item.handle.counter == counter); - fm_debug_assert(item.handle == index + page.start_index); - return item.object; -} - -} // namespace floormat::impl_handle - -namespace floormat { - - - -} // namespace floormat -- cgit v1.2.3