diff options
| -rw-r--r-- | kapi/kapi/memory/layout.hpp | 4 | ||||
| -rw-r--r-- | kernel/kapi/memory.cpp | 2 | ||||
| -rw-r--r-- | kernel/kernel/filesystem/ext2/filesystem.cpp | 10 | ||||
| -rw-r--r-- | kernel/kernel/filesystem/ext2/filesystem.tests.cpp | 6 | ||||
| -rw-r--r-- | kernel/kernel/test_support/filesystem/ext2.cpp | 9 | ||||
| -rw-r--r-- | libs/acpi/acpi/common/table_header.tests.cpp | 2 | ||||
| -rw-r--r-- | libs/kstd/kstd/bits/basic_unit.hpp | 117 | ||||
| -rw-r--r-- | libs/kstd/kstd/units.hpp | 110 |
8 files changed, 151 insertions, 109 deletions
diff --git a/kapi/kapi/memory/layout.hpp b/kapi/kapi/memory/layout.hpp index 5f720a51..a39997ac 100644 --- a/kapi/kapi/memory/layout.hpp +++ b/kapi/kapi/memory/layout.hpp @@ -13,12 +13,12 @@ namespace kapi::memory //! The size of a single page of virtual memory. //! //! Platforms that use different sizes of pages are expected to emulate 4 KiB pages towards the kernel. - constexpr auto page_size = kstd::units::KiB(4); + constexpr auto page_size = kstd::units::KiB(4u); //! The size of a single frame of physical memory. //! //! Platforms that use different sizes of frames are expected to emulate 4 KiB pages towards the kernel. - constexpr auto frame_size = kstd::units::KiB(4); + constexpr auto frame_size = kstd::units::KiB(4u); //! The linear base address of the higher-half direct map. //! diff --git a/kernel/kapi/memory.cpp b/kernel/kapi/memory.cpp index 91a6c2c7..4cba7238 100644 --- a/kernel/kapi/memory.cpp +++ b/kernel/kapi/memory.cpp @@ -140,7 +140,7 @@ namespace kapi::memory }); auto bitmap = - std::span{bitmap_ptr, (bitmap_bytes + kstd::type_size<std::uint64_t> - 1_B) / kstd::type_size<std::uint64_t>}; + std::span{bitmap_ptr, (bitmap_bytes + kstd::size_of<std::uint64_t>() - 1_B) / kstd::size_of<std::uint64_t>()}; allocator.emplace(bitmap, frame_count); diff --git a/kernel/kernel/filesystem/ext2/filesystem.cpp b/kernel/kernel/filesystem/ext2/filesystem.cpp index c7d8aee4..08f827a6 100644 --- a/kernel/kernel/filesystem/ext2/filesystem.cpp +++ b/kernel/kernel/filesystem/ext2/filesystem.cpp @@ -471,7 +471,7 @@ namespace kernel::filesystem::ext2 if (auto write_result = m_backing_inode->write(as_bytes(std::span{&block_group_descriptor, 1}), block_group_descriptor_table_offset() + - block_group_descriptor_index * kstd::object_size(block_group_descriptor)); + block_group_descriptor_index * kstd::size_of(block_group_descriptor)); !write_result) { return kstd::failure(write_result.error()); @@ -609,7 +609,7 @@ namespace kernel::filesystem::ext2 uint32_t block_number_buffer = 0; auto const block_start_offset = block_number * block_size(); - auto const number_start_address = block_start_offset + index * kstd::type_size<uint32_t>; + auto const number_start_address = block_start_offset + index * kstd::size_of<uint32_t>(); if (auto read_result = m_backing_inode->read(as_writable_bytes(std::span{&block_number_buffer, 1}), number_start_address); !read_result) @@ -671,7 +671,7 @@ namespace kernel::filesystem::ext2 } } - auto const byte_offset = block_number * block_size() + idx * kstd::type_size<uint32_t>; + auto const byte_offset = block_number * block_size() + idx * kstd::size_of<uint32_t>(); if (stride == 1) { @@ -718,7 +718,7 @@ namespace kernel::filesystem::ext2 return m_backing_inode ->write(as_bytes(std::span{&block_group_descriptor, 1}), block_group_descriptor_table_offset() + - block_group_descriptor_index * kstd::object_size(block_group_descriptor)) + block_group_descriptor_index * kstd::size_of(block_group_descriptor)) .transform([](auto) {}); } @@ -766,7 +766,7 @@ namespace kernel::filesystem::ext2 auto filesystem::block_numbers_per_block() const -> size_t { - return block_size() / kstd::type_size<uint32_t>; + return block_size() / kstd::size_of<uint32_t>(); } auto filesystem::block_numbers_per_singly_indirect_block() const -> size_t diff --git a/kernel/kernel/filesystem/ext2/filesystem.tests.cpp b/kernel/kernel/filesystem/ext2/filesystem.tests.cpp index 08232aa4..6bd17188 100644 --- a/kernel/kernel/filesystem/ext2/filesystem.tests.cpp +++ b/kernel/kernel/filesystem/ext2/filesystem.tests.cpp @@ -219,7 +219,7 @@ SCENARIO("Ext2 block mapping includes direct and all indirect levels", "[filesys kernel::tests::filesystem::ext2::write_u32(*device, 51 * block_size, 52); kernel::tests::filesystem::ext2::write_u32(*device, 52 * block_size, 53); - auto const numbers_per_block = static_cast<uint32_t>(block_size / kstd::type_size<uint32_t>); + auto const numbers_per_block = static_cast<uint32_t>(block_size / kstd::size_of<uint32_t>()); auto const singly_start = static_cast<uint32_t>(kernel::filesystem::ext2::constants::direct_block_count); auto const doubly_start = singly_start + numbers_per_block; auto const triply_start = doubly_start + numbers_per_block * numbers_per_block; @@ -284,7 +284,7 @@ SCENARIO("Ext2 block writing includes direct and all indirect levels", "[filesys auto inode_data = kernel::filesystem::ext2::inode_data{}; - auto const numbers_per_block = static_cast<uint32_t>(block_size / kstd::type_size<uint32_t>); + auto const numbers_per_block = static_cast<uint32_t>(block_size / kstd::size_of<uint32_t>()); auto const singly_start = static_cast<uint32_t>(kernel::filesystem::ext2::constants::direct_block_count); auto const doubly_start = singly_start + numbers_per_block; auto const triply_start = doubly_start + numbers_per_block * numbers_per_block; @@ -307,7 +307,7 @@ SCENARIO("Ext2 block writing includes direct and all indirect levels", "[filesys REQUIRE(fs.write_global_block_number_to_inode_block_index(singly_start + 1, inode_data, singly_global_block_number + 1)); REQUIRE(read_u32(static_cast<kstd::units::bytes>(inode_data.block[12]) * block_size.value + - 1 * kstd::type_size<uint32_t>) == singly_global_block_number + 1); + 1 * kstd::size_of<uint32_t>()) == singly_global_block_number + 1); REQUIRE(fs.write_global_block_number_to_inode_block_index(doubly_start, inode_data, doubly_global_block_number)); REQUIRE(inode_data.block[13] == 17); diff --git a/kernel/kernel/test_support/filesystem/ext2.cpp b/kernel/kernel/test_support/filesystem/ext2.cpp index ff01cbb2..6ea41bab 100644 --- a/kernel/kernel/test_support/filesystem/ext2.cpp +++ b/kernel/kernel/test_support/filesystem/ext2.cpp @@ -34,7 +34,7 @@ namespace kernel::tests::filesystem::ext2 auto write_u32(kernel::tests::devices::block_device & device, kstd::units::bytes offset, uint32_t value) -> void { - write_bytes(device, offset, &value, kstd::object_size(value)); + write_bytes(device, offset, &value, kstd::size_of(value)); } auto setup_mock_ext2_layout(kernel::tests::devices::block_device & device) -> void @@ -63,9 +63,8 @@ namespace kernel::tests::filesystem::ext2 kernel::filesystem::ext2::superblock const & superblock, kernel::filesystem::ext2::block_group_descriptor const & group_descriptor) -> void { - write_bytes(device, kernel::filesystem::ext2::constants::superblock_offset, &superblock, - kstd::object_size(superblock)); - write_bytes(device, 2048_B, &group_descriptor, kstd::object_size(group_descriptor)); + write_bytes(device, kernel::filesystem::ext2::constants::superblock_offset, &superblock, kstd::size_of(superblock)); + write_bytes(device, 2048_B, &group_descriptor, kstd::size_of(group_descriptor)); auto root_inode_data = kernel::filesystem::ext2::inode_data{}; root_inode_data.mode = kernel::filesystem::ext2::constants::mode_directory; @@ -75,6 +74,6 @@ namespace kernel::tests::filesystem::ext2 auto const root_inode_offset = static_cast<size_t>(group_descriptor.inode_table) * kernel::filesystem::ext2::constants::base_block_size + (kernel::filesystem::ext2::constants::root_inode_number - 1) * kstd::units::bytes{superblock.inode_size}; - write_bytes(device, root_inode_offset, &root_inode_data, kstd::object_size(root_inode_data)); + write_bytes(device, root_inode_offset, &root_inode_data, kstd::size_of(root_inode_data)); } } // namespace kernel::tests::filesystem::ext2
\ No newline at end of file diff --git a/libs/acpi/acpi/common/table_header.tests.cpp b/libs/acpi/acpi/common/table_header.tests.cpp index bbd42bd6..971c715c 100644 --- a/libs/acpi/acpi/common/table_header.tests.cpp +++ b/libs/acpi/acpi/common/table_header.tests.cpp @@ -28,7 +28,7 @@ SCENARIO("Common table header parsing", "[common_table_header]") THEN("the length is correct") { - REQUIRE(header->length() == kstd::type_size<acpi::table_header>); + REQUIRE(header->length() == kstd::size_of<acpi::table_header>()); } THEN("the oem id is correct") diff --git a/libs/kstd/kstd/bits/basic_unit.hpp b/libs/kstd/kstd/bits/basic_unit.hpp new file mode 100644 index 00000000..c7f794ea --- /dev/null +++ b/libs/kstd/kstd/bits/basic_unit.hpp @@ -0,0 +1,117 @@ +#ifndef KSTD_BITS_BASIC_UNITS_HPP +#define KSTD_BITS_BASIC_UNITS_HPP + +#include <concepts> +#include <utility> + +namespace kstd +{ + + //! A basic template for strongly typed units. + template<typename ValueType, typename Tags> + struct basic_unit + { + using value_type = ValueType; + + constexpr basic_unit() noexcept + : value{} + {} + + explicit constexpr basic_unit(value_type value) noexcept + : value{value} + {} + + template<std::integral T> + explicit constexpr operator T() const noexcept + { + return value; + } + + constexpr auto friend operator+=(basic_unit & lhs, basic_unit const & rhs) noexcept -> basic_unit & + { + lhs.value += rhs.value; + return lhs; + } + + constexpr auto friend operator+(basic_unit lhs, basic_unit const & rhs) noexcept -> basic_unit + { + return lhs += rhs; + } + + constexpr auto friend operator-=(basic_unit & lhs, basic_unit const & rhs) noexcept -> basic_unit & + { + lhs.value -= rhs.value; + return lhs; + } + + constexpr auto friend operator-(basic_unit lhs, basic_unit const & rhs) noexcept -> basic_unit + { + return lhs -= rhs; + } + + constexpr auto friend operator*=(basic_unit & lhs, std::integral auto factor) noexcept -> basic_unit & + { + lhs.value *= factor; + return lhs; + } + + constexpr auto friend operator*(basic_unit lhs, std::integral auto factor) noexcept -> basic_unit + { + return lhs *= factor; + } + + constexpr auto friend operator*(std::integral auto factor, basic_unit rhs) noexcept -> basic_unit + { + return rhs *= factor; + } + + constexpr auto friend operator/=(basic_unit & lhs, std::integral auto divisor) noexcept -> basic_unit & + { + lhs.value /= divisor; + return lhs; + } + + constexpr auto friend operator/(basic_unit lhs, std::integral auto divisor) noexcept -> basic_unit + { + return lhs /= divisor; + } + + constexpr auto friend operator%=(basic_unit & lhs, std::integral auto divisor) noexcept -> basic_unit & + { + lhs.value %= divisor; + return lhs; + } + + constexpr auto friend operator%(basic_unit lhs, std::integral auto divisor) noexcept -> basic_unit + { + return lhs %= divisor; + } + + constexpr auto friend operator/(basic_unit const & lhs, basic_unit const & rhs) noexcept + -> decltype(std::declval<value_type>() / std::declval<value_type>()) + { + return lhs.value / rhs.value; + } + + constexpr auto friend operator%(basic_unit const & lhs, basic_unit const & rhs) noexcept -> basic_unit + { + return basic_unit{lhs.value % rhs.value}; + } + + constexpr auto friend operator-(basic_unit const & unit) noexcept -> basic_unit + requires(std::is_signed_v<value_type>) + { + return basic_unit{-unit.value}; + } + + constexpr auto friend operator<=>(basic_unit const &, basic_unit const &) noexcept + -> decltype(std::declval<value_type const &>() <=> std::declval<value_type const &>()) = default; + + constexpr auto friend operator==(basic_unit const &, basic_unit const &) noexcept -> bool = default; + + value_type value; + }; + +} // namespace kstd + +#endif diff --git a/libs/kstd/kstd/units.hpp b/libs/kstd/kstd/units.hpp index 6e9ea1e2..cb33af21 100644 --- a/libs/kstd/kstd/units.hpp +++ b/libs/kstd/kstd/units.hpp @@ -1,112 +1,35 @@ #ifndef KSTD_UNITS_HPP #define KSTD_UNITS_HPP -#include <compare> +#include <kstd/bits/basic_unit.hpp> + #include <concepts> #include <cstddef> namespace kstd { - //! A basic template for strongly typed units. - template<typename ValueType, typename Tag> - struct basic_unit - { - using value_type = ValueType; - - constexpr basic_unit() noexcept - : value{} - {} - - explicit constexpr basic_unit(value_type value) noexcept - : value{value} - {} - - template<std::integral T> - explicit constexpr operator T() const noexcept - { - return value; - } - - constexpr auto operator+(basic_unit const & other) const noexcept -> basic_unit - { - return basic_unit{value + other.value}; - } - - constexpr auto operator+=(basic_unit const & other) noexcept -> basic_unit & - { - return *this = *this + other; - } - - constexpr auto operator-(basic_unit const & other) const noexcept -> basic_unit - { - return basic_unit{value - other.value}; - } - - constexpr auto operator-=(basic_unit const & other) noexcept -> basic_unit & - { - return *this = *this - other; - } - - constexpr auto operator*(std::integral auto factor) const noexcept -> basic_unit - { - return basic_unit{value * factor}; - } - - constexpr auto operator*=(std::integral auto factor) noexcept -> basic_unit - { - return *this = *this * factor; - } - - constexpr auto operator/(std::integral auto divisor) const noexcept -> basic_unit - { - return basic_unit{value / divisor}; - } - - constexpr auto operator/=(std::integral auto divisor) noexcept -> basic_unit - { - return *this = *this / divisor; - } - - constexpr auto operator/(basic_unit const & other) const noexcept - { - return value / other.value; - } - - constexpr auto operator%(basic_unit const & other) const noexcept -> basic_unit - { - return basic_unit{value % other.value}; - } - - constexpr auto operator<=>(basic_unit const & other) const noexcept -> std::strong_ordering = default; - - value_type value; - }; - - template<std::integral Factor, typename ValueType, typename Tag> - constexpr auto operator*(Factor factor, basic_unit<ValueType, Tag> const & unit) noexcept - -> basic_unit<ValueType, Tag> - { - return basic_unit<ValueType, Tag>{unit.value * factor}; - } - namespace units { + using bytes = basic_unit<std::size_t, struct bytes_tag>; - constexpr auto KiB(std::size_t value) noexcept -> bytes + // NOLINTNEXTLINE(readability-identifier-naming) + constexpr auto KiB(std::unsigned_integral auto value) noexcept -> bytes { - return bytes{value * 1024}; + return bytes{static_cast<bytes::value_type>(value) * 1024}; } - constexpr auto MiB(std::size_t value) noexcept -> bytes + // NOLINTNEXTLINE(readability-identifier-naming) + constexpr auto MiB(std::unsigned_integral auto value) noexcept -> bytes { - return bytes{value * 1024 * 1024}; + return bytes{static_cast<bytes::value_type>(value) * 1024 * 1024}; } - constexpr auto GiB(std::size_t value) noexcept -> bytes + // NOLINTNEXTLINE(readability-identifier-naming) + constexpr auto GiB(std::unsigned_integral auto value) noexcept -> bytes { - return bytes{value * 1024 * 1024 * 1024}; + return bytes{static_cast<bytes::value_type>(value) * 1024 * 1024 * 1024}; } template<typename ValueType> @@ -142,13 +65,16 @@ namespace kstd } // namespace units_literals template<typename ValueType> - constexpr auto object_size(ValueType const &) -> units::bytes + consteval auto size_of(ValueType const &) noexcept -> units::bytes { return units::bytes{sizeof(ValueType)}; } - template<typename T> - constexpr auto type_size = units::bytes{sizeof(T)}; + template<typename ValueType> + consteval auto size_of() noexcept -> units::bytes + { + return units::bytes{sizeof(ValueType)}; + } } // namespace kstd |
