From 048ab0903b8749717f4461a6abcfa023ceef0dd6 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Wed, 28 May 2025 00:47:52 +0200 Subject: WIP --- asl/base/meta.hpp | 26 +++++++++-- asl/base/meta_tests.cpp | 36 +++++++++++++++ asl/handle_pool/BUILD.bazel | 33 ++++++++++++++ asl/handle_pool/index_pool.hpp | 88 ++++++++++++++++++++++++++++++++++++ asl/handle_pool/index_pool_tests.cpp | 10 ++++ 5 files changed, 189 insertions(+), 4 deletions(-) create mode 100644 asl/handle_pool/BUILD.bazel create mode 100644 asl/handle_pool/index_pool.hpp create mode 100644 asl/handle_pool/index_pool_tests.cpp diff --git a/asl/base/meta.hpp b/asl/base/meta.hpp index b17d761..9f67e13 100644 --- a/asl/base/meta.hpp +++ b/asl/base/meta.hpp @@ -317,15 +317,33 @@ template concept is_integer = is_signed_integer || is_unsigned_in template using as_unsigned_integer = _integer_traits::as_unsigned; template using as_signed_integer = _integer_traits::as_signed; +template +struct smallest_unsigned_integer_type_for_width_helper { using type = void; }; + +template requires (N >= 1 and N <= 8) +struct smallest_unsigned_integer_type_for_width_helper { using type = uint8_t; }; + +template requires (N >= 9 and N <= 16) +struct smallest_unsigned_integer_type_for_width_helper { using type = uint16_t; }; + +template requires (N >= 17 and N <= 32) +struct smallest_unsigned_integer_type_for_width_helper { using type = uint32_t; }; + +template requires (N >= 33 and N <= 64) +struct smallest_unsigned_integer_type_for_width_helper { using type = uint64_t; }; + +template using smallest_unsigned_integer_type_for_width + = smallest_unsigned_integer_type_for_width_helper::type; + template concept is_enum = __is_enum(T); template using underlying_t = __underlying_type(T); -template struct is_uniquely_represented : false_type {}; -template struct is_uniquely_represented : true_type {}; -template struct is_uniquely_represented : true_type {}; +template struct is_uniquely_represented : false_type {}; +template struct is_uniquely_represented : true_type {}; +template struct is_uniquely_represented : true_type {}; template<> struct is_uniquely_represented : true_type {}; -template<> struct is_uniquely_represented : true_type {}; +template<> struct is_uniquely_represented : true_type {}; template concept uniquely_represented = is_uniquely_represented>::value; diff --git a/asl/base/meta_tests.cpp b/asl/base/meta_tests.cpp index 36b0429..fbbec15 100644 --- a/asl/base/meta_tests.cpp +++ b/asl/base/meta_tests.cpp @@ -389,3 +389,39 @@ enum EnumI64 : int64_t {}; static_assert(asl::same_as, uint8_t>); static_assert(asl::same_as, int64_t>); +static_assert(!asl::is_integer); +static_assert(!asl::is_integer); + +static_assert(asl::same_as, uint8_t>); +static_assert(asl::same_as, uint16_t>); +static_assert(asl::same_as, uint32_t>); +static_assert(asl::same_as, uint64_t>); + +static_assert(asl::same_as, uint8_t>); +static_assert(asl::same_as, uint16_t>); +static_assert(asl::same_as, uint32_t>); +static_assert(asl::same_as, uint64_t>); + +static_assert(asl::same_as, int8_t>); +static_assert(asl::same_as, int16_t>); +static_assert(asl::same_as, int32_t>); +static_assert(asl::same_as, int64_t>); + +static_assert(asl::same_as, int8_t>); +static_assert(asl::same_as, int16_t>); +static_assert(asl::same_as, int32_t>); +static_assert(asl::same_as, int64_t>); + +static_assert(asl::same_as, uint8_t>); +static_assert(asl::same_as, uint8_t>); +static_assert(asl::same_as, uint8_t>); +static_assert(asl::same_as, uint8_t>); +static_assert(asl::same_as, uint16_t>); +static_assert(asl::same_as, uint16_t>); +static_assert(asl::same_as, uint32_t>); +static_assert(asl::same_as, uint32_t>); +static_assert(asl::same_as, uint32_t>); +static_assert(asl::same_as, uint32_t>); +static_assert(asl::same_as, uint64_t>); +static_assert(asl::same_as, uint64_t>); +static_assert(asl::same_as, uint64_t>); diff --git a/asl/handle_pool/BUILD.bazel b/asl/handle_pool/BUILD.bazel new file mode 100644 index 0000000..e5048d3 --- /dev/null +++ b/asl/handle_pool/BUILD.bazel @@ -0,0 +1,33 @@ +# Copyright 2025 Steven Le Rouzic +# +# SPDX-License-Identifier: BSD-3-Clause + +package( + default_applicable_licenses = ["//:license"], +) + +cc_library( + name = "index_pool", + hdrs = [ + "index_pool.hpp", + ], + deps = [ + "//asl/memory", + "//asl/memory:allocator", + "//asl/base", + "//asl/containers:chunked_buffer", + ], + visibility = ["//visibility:public"], +) + +cc_test( + name = "index_pool_tests", + srcs = [ + "index_pool_tests.cpp", + ], + deps = [ + ":index_pool", + "//asl/tests:utils", + "//asl/testing", + ], +) diff --git a/asl/handle_pool/index_pool.hpp b/asl/handle_pool/index_pool.hpp new file mode 100644 index 0000000..377f6f4 --- /dev/null +++ b/asl/handle_pool/index_pool.hpp @@ -0,0 +1,88 @@ +// Copyright 2025 Steven Le Rouzic +// +// SPDX-License-Identifier: BSD-3-Clause + +#include "asl/base/integers.hpp" +#include "asl/base/meta.hpp" +#include "asl/containers/chunked_buffer.hpp" +#include "asl/memory/allocator.hpp" + +namespace asl +{ + +template< + int kIndexSize_, + int kGenSize_, + int kUserSize_ = 0 +> +requires ( + kUserSize_ >= 0 + && kGenSize_ > 0 + && kIndexSize_ > 0 + && (kUserSize_ + kGenSize_ + kIndexSize_ <= 63) +) +struct index_pool_config +{ + static constexpr int kUserSize = kUserSize_; + static constexpr int kGenSize = kGenSize_; + static constexpr int kIndexSize = kIndexSize_; + + static constexpr bool kHasUser = kUserSize > 0; + + using handle_type = smallest_unsigned_integer_type_for_width; + using user_type = select_t, empty>; +}; + + +template< + int kIndexSize_, + int kGenSize_, + typename UserType = empty, + int kUserSize_ = 0 +> +requires ( + same_as || (kUserSize_ <= size_of * 8 && trivially_copy_constructible) +) +class index_pool_handle_base +{ + static constexpr int kUserSizeComputed = + same_as ? 0 : (kUserSize_ == 0 ? size_of * 8 : kUserSize_); // NOLINT + +public: + using config = index_pool_config; + +private: + using handle_type = config::handle_type; + using user_type = config::user_type; + using user_type_external = UserType; + + static constexpr handle_type kHasValueMask = ~(~handle_type{0} >> 1); + + static constexpr handle_type kIndexMask = (handle_type{1} << config::kIndexSize) - 1; + + static constexpr int kGenShift = config::kIndexSize; + static constexpr handle_type kGenMask = ((handle_type{1} << config::kGenSize) - 1) << kGenShift; + + static constexpr int kUserShift = config::kIndexSize + config::kGenSize; + static constexpr handle_type kUserMask = ((handle_type{1} << config::kUserSize) - 1) << kUserShift; + + handle_type m_handle{}; + +public: + [[nodiscard]] constexpr bool has_value() const { return m_handle & kHasValueMask; } + + [[nodiscard]] constexpr user_type_external user() const + requires config::kHasUser + { + return bit_cast( + static_cast((m_handle & kUserMask) >> kUserShift) + ); + } +}; + +class IndexPool +{ +}; + +} // namespace asl + diff --git a/asl/handle_pool/index_pool_tests.cpp b/asl/handle_pool/index_pool_tests.cpp new file mode 100644 index 0000000..1dd2816 --- /dev/null +++ b/asl/handle_pool/index_pool_tests.cpp @@ -0,0 +1,10 @@ +// Copyright 2025 Steven Le Rouzic +// +// SPDX-License-Identifier: BSD-3-Clause + +#include "asl/testing/testing.hpp" + +ASL_TEST(test) +{ +} + -- cgit