diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-07-12 22:14:44 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-07-14 13:04:41 +0200 |
commit | 19abcac32009d58450d80add39c5b1d5e7d8a993 (patch) | |
tree | eca43054b1614778fd9468f087b134f54f5e18dd | |
parent | eb469ff58365517515d73827e70a9519436b5858 (diff) |
w
-rw-r--r-- | src/handle-fwd.hpp | 11 | ||||
-rw-r--r-- | src/handle-page.hpp | 6 | ||||
-rw-r--r-- | src/handle-page.inl | 11 | ||||
-rw-r--r-- | src/handle-pool.hpp | 10 | ||||
-rw-r--r-- | src/handle-pool.inl | 4 | ||||
-rw-r--r-- | src/handle.cpp | 6 | ||||
-rw-r--r-- | src/handle.hpp | 16 | ||||
-rw-r--r-- | src/handle.inl | 21 | ||||
-rw-r--r-- | test/handle.cpp | 34 |
9 files changed, 69 insertions, 50 deletions
diff --git a/src/handle-fwd.hpp b/src/handle-fwd.hpp index 919461f5..79fa76e9 100644 --- a/src/handle-fwd.hpp +++ b/src/handle-fwd.hpp @@ -1,11 +1,14 @@ #pragma once -namespace floormat::Handle { +namespace floormat::impl_handle { +template<typename OBJ, uint32_t PAGE_SIZE> struct Handle; template<typename Obj, uint32_t PageSize> struct Item; +template<typename Obj, uint32_t PageSize> class Page; +template<typename Obj, uint32_t PageSize> class Pool; template<typename OBJ, uint32_t PAGE_SIZE> -struct Handle +struct Handle final { uint32_t index = (uint32_t)-1; uint32_t counter = 0; @@ -26,11 +29,11 @@ private: static Item<OBJ, PAGE_SIZE>& get_from_pool(uint32_t index, uint32_t counter); }; -} // namespace floormat::Handle +} // namespace floormat::impl_handle namespace floormat { template<typename Obj, uint32_t PageSize> -using handle = struct Handle::Handle<Obj, PageSize>; +using handle = struct impl_handle::Handle<Obj, PageSize>; } // namespace floormat diff --git a/src/handle-page.hpp b/src/handle-page.hpp index dee35a9e..c0397db7 100644 --- a/src/handle-page.hpp +++ b/src/handle-page.hpp @@ -4,7 +4,7 @@ #include <array> #include <cr/BitArray.h> -namespace floormat::Handle { +namespace floormat::impl_handle { template<typename Obj, uint32_t PageSize> struct Item @@ -17,7 +17,7 @@ struct Item template<typename Obj, uint32_t PageSize> class Page { - friend class Handle<Obj, PageSize>; + friend struct Handle<Obj, PageSize>; std::array<Item<Obj, PageSize>, PageSize> storage; BitArray used_map; // todo replace with a rewrite of std::bitset @@ -37,4 +37,4 @@ public: bool is_full(); }; -} // namespace floormat::Handle +} // namespace floormat::impl_handle diff --git a/src/handle-page.inl b/src/handle-page.inl index 7c9d5a39..09d351ac 100644 --- a/src/handle-page.inl +++ b/src/handle-page.inl @@ -3,7 +3,7 @@ #include "compat/assert.hpp" #include "compat/pretty-function.hpp" -namespace floormat::Handle { +namespace floormat::impl_handle { template<typename Obj, uint32_t PageSize> Page<Obj, PageSize>::~Page() noexcept @@ -17,7 +17,7 @@ Page<Obj, PageSize>::Page(uint32_t start_index): start_index{start_index}, first_free{start_index} { - fm_assert(size_t{start_index} + size_t{PageSize} <= 1<<31 && start_index + PageSize > start_index); + fm_assert(size_t{start_index} + size_t{PageSize} <= size_t{1u<<31} && start_index + PageSize > start_index); auto val = start_index; for (auto i = 0u; i < PageSize; i++) { @@ -50,9 +50,10 @@ void Page<Obj, PageSize>::deallocate(Handle<Obj, PageSize> obj) 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{0}) [[unlikely]] + auto ctr = item.handle.counter++; + if (ctr == (uint32_t)-1) [[unlikely]] Debug{} << "counter" << FM_PRETTY_FUNCTION << "overflowed"; + fm_assert(obj.counter == ctr); item.next = first_free; used_count--; first_free = obj.index; @@ -70,4 +71,4 @@ bool Page<Obj, PageSize>::is_full() return used_count == PageSize; } -} // namespace floormat::Handle +} // namespace floormat::impl_handle diff --git a/src/handle-pool.hpp b/src/handle-pool.hpp index da7a2538..2721f094 100644 --- a/src/handle-pool.hpp +++ b/src/handle-pool.hpp @@ -3,14 +3,12 @@ #include <cr/Array.h> #include <cr/Pointer.h> -namespace floormat::Handle { - -template<typename Obj, uint32_t PageSize> class Page; +namespace floormat::impl_handle { template<typename Obj, uint32_t PageSize> class Pool { - friend class Handle<Obj, PageSize>; + friend class impl_handle<Obj, PageSize>; static Pointer<Pool<Obj, PageSize>> make_pool(); static Page<Obj, PageSize>& find_page(); @@ -30,7 +28,7 @@ public: requires requires (Xs&&... xs) { Obj{forward<Xs>(xs)...}; } - static Handle<Obj, PageSize> make_object(Xs&&... xs); + static impl_handle<Obj, PageSize> make_object(Xs&&... xs); }; -} // namespace floormat::Handle +} // namespace floormat::impl_handle diff --git a/src/handle-pool.inl b/src/handle-pool.inl index 5fe63c88..1420bd7b 100644 --- a/src/handle-pool.inl +++ b/src/handle-pool.inl @@ -4,7 +4,7 @@ #include "compat/move.hpp" #include <cr/GrowableArray.h> -namespace floormat::Handle { +namespace floormat::impl_handle { template<typename Obj, uint32_t PageSize> template<typename... Xs> @@ -44,4 +44,4 @@ Pool<Obj, PageSize>::find_page() return P.pages.back(); } -} // namespace floormat::Handle +} // namespace floormat::impl_handle diff --git a/src/handle.cpp b/src/handle.cpp index af0220f6..ff9ed4db 100644 --- a/src/handle.cpp +++ b/src/handle.cpp @@ -1,10 +1,8 @@ #include "handle.inl" -namespace floormat::Handle { +namespace floormat::impl_handle { -static_assert(sizeof(Handle) == sizeof(HandleType)); - -} // namespace floormat::Handle +} // namespace floormat::impl_handle namespace floormat { diff --git a/src/handle.hpp b/src/handle.hpp index 7620e69b..89c1c480 100644 --- a/src/handle.hpp +++ b/src/handle.hpp @@ -2,11 +2,23 @@ #include "handle-fwd.hpp" #include "compat/assert.hpp" -namespace floormat::Handle { +namespace floormat::impl_handle { +template<typename Obj, uint32_t PageSize> +bool Handle<Obj, PageSize>::operator==(const Handle& other) const noexcept +{ + bool ret = index == other.index; + fm_debug_assert(!ret || counter == other.counter); + return ret; +} +template<typename Obj, uint32_t PageSize> +Handle<Obj, PageSize>::operator bool() const noexcept +{ + return index != (uint32_t)-1; +} -} // namespace floormat::Handle +} // namespace floormat::impl_handle namespace floormat { diff --git a/src/handle.inl b/src/handle.inl index 2f6daa20..f19ec216 100644 --- a/src/handle.inl +++ b/src/handle.inl @@ -2,25 +2,8 @@ #include "handle.hpp" //#include "handle-pool.inl" #include "compat/assert.hpp" -#include <cr/Pointer.h> -namespace floormat::Handle { - -template<typename Obj, uint32_t PageSize> class Pool; - -template<typename Obj, uint32_t PageSize> -bool Handle<Obj, PageSize>::operator==(const Handle& other) const noexcept -{ - bool ret = index == other.index; - fm_debug_assert(!ret || counter == other.counter); - return ret; -} - -template<typename Obj, uint32_t PageSize> -Handle<Obj, PageSize>::operator bool() const noexcept -{ - return index != (uint32_t)-1; -} +namespace floormat::impl_handle { template<typename Obj, uint32_t PageSize> const Obj& Handle<Obj, PageSize>::get() const @@ -48,7 +31,7 @@ Item<Obj, PageSize>& Handle<Obj, PageSize>::get_from_pool(uint32_t index, uint32 return item.object; } -} // namespace floormat::Handle +} // namespace floormat::impl_handle namespace floormat { diff --git a/test/handle.cpp b/test/handle.cpp index 79416c2c..2198281c 100644 --- a/test/handle.cpp +++ b/test/handle.cpp @@ -1,23 +1,47 @@ #include "app.hpp" -#include "src/handle.inl" +//#include "src/handle.inl" +#include "src/handle.hpp" #include "src/handle-page.inl" //#include "src/handle-pool.inl" +namespace floormat { +namespace { + +constexpr uint32_t start_index = 1024, page_size = 128; +struct Foo { int value; }; +using Page = impl_handle::Page<Foo, page_size>; +using Handle = impl_handle::Handle<Foo, page_size>; + +} // namespace +} // namespace floormat + +namespace floormat::impl_handle { + +template struct Handle<Foo, page_size>; +template struct Item<Foo, page_size>; // NOLINT(*-pro-type-member-init) +template class Page<Foo, page_size>; +//template class Pool<Foo, page_size>; + +} // namespace floormat::impl_handle + + namespace floormat::Test { namespace { -void test1() +void test_page1() { - + Page page{start_index}; + auto& item = page.allocate(); + fm_assert(item.handle == Handle{start_index}); + page.deallocate(item.handle); } - } // namespace void test_handle() { - test1(); + test_page1(); } } // namespace floormat::Test |