aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/include
diff options
context:
space:
mode:
authorMatteo Gmür <matteo.gmuer1@ost.ch>2024-10-27 11:20:46 +0000
committerMatteo Gmür <matteo.gmuer1@ost.ch>2024-10-27 11:20:46 +0000
commit3fb2780f665eb3514ef07d2d4a83820653e35b52 (patch)
tree10e075235775b35cc91e6d755b3dcc47fd890193 /arch/x86_64/include
parenta29e823c6ead21fa7c8f6445411d52f57c4518fb (diff)
downloadteachos-3fb2780f665eb3514ef07d2d4a83820653e35b52.tar.xz
teachos-3fb2780f665eb3514ef07d2d4a83820653e35b52.zip
Use container and C++20 range algorithms for allocator.
Diffstat (limited to 'arch/x86_64/include')
-rw-r--r--arch/x86_64/include/arch/memory/multiboot/memory_map.hpp113
1 files changed, 100 insertions, 13 deletions
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 3801e57..f9c902a 100644
--- a/arch/x86_64/include/arch/memory/multiboot/memory_map.hpp
+++ b/arch/x86_64/include/arch/memory/multiboot/memory_map.hpp
@@ -3,6 +3,7 @@
#include "info.hpp"
#include <cstdint>
+#include <iterator>
namespace teachos::arch::memory::multiboot
{
@@ -44,10 +45,14 @@ namespace teachos::arch::memory::multiboot
};
/**
- * @brief Iterator for memory areas.
+ * @brief Random access iterator for memory areas.
*/
struct memory_area_iterator
{
+ using iterator_category = std::random_access_iterator_tag; ///< Iterator category of this type.
+ using difference_type = std::ptrdiff_t; ///< Type when diving one instance of this iterator by another.
+ using value_type = memory_area; ///< Underlying value pointed to by this iterator.
+
/**
* @brief Defaulted constructor.
*/
@@ -57,30 +62,86 @@ namespace teachos::arch::memory::multiboot
* @brief Constructor.
*
* @param p Underlying address the iterator should point too, ensure to not pass an invalid pointer or the
- * constructo will halt execution.
+ * constructor will halt execution.
*/
- explicit memory_area_iterator(memory_area * p);
+ explicit memory_area_iterator(value_type * p);
/**
* @brief Dereferences the initally given pointer to its value.
*
* @return Reference to the value.
*/
- memory_area & operator*() const;
+ auto operator*() const -> value_type &;
+
+ /**
+ * @brief Get underlying value, which is the intially passed pointer.
+ *
+ * @return Underlying value passed intially.
+ */
+ auto operator->() const -> value_type *;
/**
* @brief Post increment operator. Returns a copy of the value.
*
* @return Copy of the incremented underlying address.
*/
- memory_area_iterator operator++(int);
+ auto operator++(int) -> memory_area_iterator;
/**
* @brief Pre increment operator. Returns a reference to the changed value.
*
* @return Reference to the incremented underlying address.
*/
- memory_area_iterator & operator++();
+ auto operator++() -> memory_area_iterator &;
+
+ /**
+ * @brief Addition assignment operator. Returns a reference to the changed value.
+ *
+ * @param value Value we want to add to the underlying address.
+ * @return Reference to the changed underlying address.
+ */
+ auto operator+=(difference_type value) -> memory_area_iterator &;
+
+ /**
+ * @brief Subtraction assignment operator. Returns a reference to the changed value.
+ *
+ * @param value Value we want to subtract from the underlying address.
+ * @return Reference to the changed underlying address.
+ */
+ auto operator-=(difference_type value) -> memory_area_iterator &;
+
+ /**
+ * @brief Addition operator. Returns the changed value.
+ *
+ * @param value Value we want to add to a copy of the underlying address.
+ * @return Copy of underlying address incremented by the given value.
+ */
+ auto operator+(difference_type value) const -> memory_area_iterator;
+
+ /**
+ * @brief Subtraction operator. Returns the changed value.
+ *
+ * @param value Value we want to subtrcat from a copy of the underlying address.
+ * @return Copy of underlying address decremented by the given value.
+ */
+ auto operator-(difference_type value) const -> memory_area_iterator;
+
+ /**
+ * @brief Subtraction operator. Returns the size difference between two iterators.
+ *
+ * @param other Other iterator we want to substract the underlying address with ours.
+ * @return Size difference between the underlying address of this instance and the given iterator.
+ */
+ auto operator-(const memory_area_iterator & other) const -> difference_type;
+
+ /**
+ * @brief Index operator overload. Returns a reference to the value at the given index. Simply returns the
+ * dereferenced underlying pointer incremented by the given index.
+ *
+ * @param index Index we want to access and get the value from.
+ * @return Reference to the value at the given index.
+ */
+ auto operator[](difference_type index) const -> value_type &;
/**
* @brief Defaulted comparsion operator. Simply compares the memory address of both iterators.
@@ -88,10 +149,18 @@ namespace teachos::arch::memory::multiboot
* @param other Other iterator to compare to.
* @return Whether both iterators point to the same underlying address in memory.
*/
- bool operator==(memory_area_iterator const & other) const = default;
+ auto operator==(memory_area_iterator const & other) const -> bool = default;
+
+ /**
+ * @brief Defaulted threeway comparsion operator. Simply compares the memory address of both iterators.
+ *
+ * @param other Other iterator to compare to.
+ * @return Whether the given iterator is smaller or larger than this iterator.
+ */
+ auto operator<=>(memory_area_iterator const & other) const -> std::strong_ordering = default;
private:
- memory_area * ptr; ///< Underlying address the iterator is currently pointing too.
+ value_type * ptr; ///< Underlying address the iterator is currently pointing too.
};
/**
@@ -100,13 +169,16 @@ namespace teachos::arch::memory::multiboot
*/
struct memory_area_container
{
+ using iterator = memory_area_iterator; ///< Iterators used by this container.
+ using size_type = std::size_t; ///< Maximum size of this 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);
+ memory_area_container(memory_area_iterator::value_type * begin, size_type size);
/**
* @brief Returns the iterator pointing to the first element of the memory area.
@@ -114,7 +186,7 @@ namespace teachos::arch::memory::multiboot
*
* @return Iterator pointing to first element of the memory area.
*/
- auto begin() const -> memory_area_iterator;
+ auto begin() const -> iterator;
/**
* @brief Returns the iterator pointing to one past the last element of the memory area.
@@ -122,11 +194,26 @@ namespace teachos::arch::memory::multiboot
*
* @return Iterator pointing to one past the last element of the memory area.
*/
- auto end() const -> memory_area_iterator;
+ auto end() const -> iterator;
+
+ /**
+ * @brief Calculates the size of this container, simply subtracts the iterator pointing to the first element by the
+ * last.
+ *
+ * @return Actual size of this container.
+ */
+ auto size() const -> size_type;
+
+ /**
+ * @brief Calcualtes the size and returns true if the size is 0 and the container therefore emtpy.
+ *
+ * @return Whether the container is empty, size being 0 or not
+ */
+ auto empty() const -> bool;
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.
+ iterator area_begin; ///< Pointer to the first element of all memory areas.
+ iterator area_end; ///< Pointer to one pas the last element of all memory areas.
};
} // namespace teachos::arch::memory::multiboot