aboutsummaryrefslogtreecommitdiff
path: root/kapi/include/kapi/memory/page.hpp
blob: 2b8e52a44731cde42c580427871d2cc7ca79da78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef TEACHOS_KAPI_MEMORY_PAGE_HPP
#define TEACHOS_KAPI_MEMORY_PAGE_HPP

#include "kapi/memory/address.hpp"

#include <compare>
#include <cstddef>

namespace teachos::memory
{

  extern std::size_t const PLATFORM_PAGE_SIZE;

  struct page
  {
    constexpr page() = default;

    explicit constexpr page(std::size_t number)
        : m_number{number}
    {}

    /**
     * @brief Returns the virtual page the given address is contained in.
     *
     * @param address Linear address we want to get the corresponding virtual page for.
     * @return Page the given address is contained in.
     */
    constexpr auto static containing(linear_address address) noexcept -> page
    {
      return page{address.raw() / PLATFORM_PAGE_SIZE};
    }

    /**
     * @brief Get the start address of this virtual page.
     *
     * @return Start address of the virtual page.
     */
    [[nodiscard]] constexpr auto start_address() const noexcept -> linear_address
    {
      return linear_address{m_number * PLATFORM_PAGE_SIZE};
    }

    /**
     * @brief Check if this page refers to the same page as @p other.
     */
    constexpr auto operator==(page const & other) const noexcept -> bool = default;

    /**
     * @brief Lexicographically compare this page to @p other.
     */
    constexpr auto operator<=>(page const & other) const noexcept -> std::strong_ordering = default;

  private:
    std::size_t m_number{};
  };

}  // namespace teachos::memory

#endif