From a29e823c6ead21fa7c8f6445411d52f57c4518fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Sun, 27 Oct 2024 09:21:25 +0000 Subject: Attempt to start using C++20 algorithm calls. --- .../arch/memory/allocator/area_frame_allocator.hpp | 28 +++----------- .../include/arch/memory/multiboot/memory_map.hpp | 45 +++++++++++++++++++++- 2 files changed, 49 insertions(+), 24 deletions(-) (limited to 'arch/x86_64/include') diff --git a/arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp b/arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp index a1b771e..8e971f0 100644 --- a/arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp +++ b/arch/x86_64/include/arch/memory/allocator/area_frame_allocator.hpp @@ -45,22 +45,6 @@ namespace teachos::arch::memory::allocator */ auto deallocate_frame(physical_frame physical_frame) -> void; - /** - * @brief Returns the iterator pointing to the first element of the memory area. - * Allows using this class in the for each loop, because it follows the InputIterator template scheme. - * - * @return Iterator pointing to first element of the memory area. - */ - auto begin() -> multiboot::memory_area_iterator; - - /** - * @brief Returns the iterator pointing to one past the last element of the memory area. - * Allows using this class in the for each loop, because it follows the InputIterator template scheme. - * - * @return Iterator pointing to one past the last element of the memory area. - */ - auto end() -> multiboot::memory_area_iterator; - private: /** * @brief Find the next memory area and write it into current_area. @@ -69,12 +53,12 @@ namespace teachos::arch::memory::allocator physical_frame next_free_frame; ///< The physical_frame after the last allocated one. std::optional current_area; ///< The current memory area. - multiboot::memory_area_iterator area_begin; ///< Pointer to the first element of all memory areas. - multiboot::memory_area_iterator area_end; ///< Pointer to one pas the last element of all memory areas. - physical_frame const kernel_start; ///< The start address of the kernel code in memory. - physical_frame const kernel_end; ///< The end address of the kernel code in memory. - physical_frame const multiboot_start; ///< The start address of the multiboot code in memory. - physical_frame const multiboot_end; ///< The end address of the multiboot code in memory. + multiboot::memory_area_container const + memory_areas; ///< All memory areas in custom container allows to use std::ranges + physical_frame const kernel_start; ///< The start address of the kernel code in memory. + physical_frame const kernel_end; ///< The end address of the kernel code in memory. + physical_frame const multiboot_start; ///< The start address of the multiboot code in memory. + physical_frame const multiboot_end; ///< The end address of the multiboot code in memory. }; } // namespace teachos::arch::memory::allocator diff --git a/arch/x86_64/include/arch/memory/multiboot/memory_map.hpp b/arch/x86_64/include/arch/memory/multiboot/memory_map.hpp index e30d2c4..3801e57 100644 --- a/arch/x86_64/include/arch/memory/multiboot/memory_map.hpp +++ b/arch/x86_64/include/arch/memory/multiboot/memory_map.hpp @@ -48,10 +48,16 @@ namespace teachos::arch::memory::multiboot */ struct memory_area_iterator { + /** + * @brief Defaulted constructor. + */ + memory_area_iterator() = default; + /** * @brief Constructor. * - * @param p Underlying address the iterator should point too, ensure to not pass an invalid pointer. + * @param p Underlying address the iterator should point too, ensure to not pass an invalid pointer or the + * constructo will halt execution. */ explicit memory_area_iterator(memory_area * p); @@ -80,13 +86,48 @@ namespace teachos::arch::memory::multiboot * @brief Defaulted comparsion operator. Simply compares the memory address of both iterators. * * @param other Other iterator to compare to. - * @return Whether poith iterators point to the same underlying address in memory. + * @return Whether both iterators point to the same underlying address in memory. */ bool operator==(memory_area_iterator const & other) const = default; private: memory_area * ptr; ///< Underlying address the iterator is currently pointing too. }; + + /** + * @brief Read-only container for memory areas, that allow to easily use the memory_area_iterator in C++20 ranges + * calls. + */ + struct memory_area_container + { + /** + * @brief Constructor. + * + * @param begin Pointer to the first memory area, will be used to construct the begin iterator. + * @param size Amount of entries in the container we want to construct. + */ + memory_area_container(memory_area * begin, std::size_t size); + + /** + * @brief Returns the iterator pointing to the first element of the memory area. + * Allows using this class in the for each loop, because it follows the InputIterator template scheme. + * + * @return Iterator pointing to first element of the memory area. + */ + auto begin() const -> memory_area_iterator; + + /** + * @brief Returns the iterator pointing to one past the last element of the memory area. + * Allows using this class in the for each loop, because it follows the InputIterator template scheme. + * + * @return Iterator pointing to one past the last element of the memory area. + */ + auto end() const -> memory_area_iterator; + + private: + memory_area_iterator area_begin; ///< Pointer to the first element of all memory areas. + memory_area_iterator area_end; ///< Pointer to one pas the last element of all memory areas. + }; } // namespace teachos::arch::memory::multiboot #endif // TEACHOS_ARCH_X86_64_MEMORY_MULTIBOOT_MEMORY_MAP_HPP -- cgit v1.2.3