]> git.ameliathe1st.gay Git - voyage-au-centre-des-fichiers.git/commitdiff
Simplified some getters and setters, with the new template definitions
authorAmelia Coutard <eliottulio.coutard@gmail.com>
Tue, 5 Dec 2023 16:35:07 +0000 (17:35 +0100)
committerAmelia Coutard <eliottulio.coutard@gmail.com>
Tue, 5 Dec 2023 16:35:07 +0000 (17:35 +0100)
kernel/src/paging.hpp

index d4ed443513200981e983a6d32d8c46a9bf9ed1b5..6955a7bbe3ab4ea701e56202735c1028e1ab7c3f 100644 (file)
@@ -170,59 +170,27 @@ static_assert(alignof(paging_entry<0>) == 8);
 static_assert(sizeof(paging_entry<0>) == 8);
 
 
-inline bool is_page(const PML4E __attribute__((unused))& PML4E) { return false; }
-inline bool is_page(const PDPE& PDPE) { return PDPE.page.one == 1; }
-inline bool is_page(const PDE& PDE) { return PDE.page.one == 1; }
-inline bool is_page(const PE __attribute__((unused))& PE) { return true; }
-
-inline phys_ptr<PDPT> get_base_address(const PML4E& PML4E) {
-       os::assert(!is_page(PML4E), "Tried to get non-page out of a page paging.");
-       return phys_ptr<PDPT>{PML4E.non_page.base_address * 0x1000ull};
-}
-inline phys_ptr<PDT> get_base_address(const PDPE& PDPE) {
-       os::assert(!is_page(PDPE), "Tried to get non-page out of a page paging.");
-       return phys_ptr<PDT>{PDPE.non_page.base_address * 0x1000ull};
-}
-inline phys_ptr<PT> get_base_address(const PDE& PDE) {
-       os::assert(!is_page(PDE), "Tried to get non-page out of a page paging.");
-       return phys_ptr<PT>{PDE.non_page.base_address * 0x1000ull};
-}
-inline phys_ptr<page<2>> get_page_base_address(const PDPE& PDPE) {
-       os::assert(is_page(PDPE), "Tried to get page out of a non-page paging.");
-       return phys_ptr<page<2>>{PDPE.page.base_address * sizeof(page<2>)};
-}
-inline phys_ptr<page<1>> get_page_base_address(const PDE& PDE) {
-       os::assert(is_page(PDE), "Tried to get page out of a non-page paging.");
-       return phys_ptr<page<1>>{PDE.page.base_address * sizeof(page<1>)};
+inline bool is_page(PML4E) { return false; }
+inline bool is_page(PDPE PDPE) { return PDPE.page.one == 1; }
+inline bool is_page(PDE PDE) { return PDE.page.one == 1; }
+inline bool is_page(PE) { return true; }
+
+template <size_t depth> phys_ptr<paging_table<depth - 1>> get_base_address(paging_entry<depth> entry) {
+       os::assert(!is_page(entry), "Tried to get non-page out of a page paging.");
+       return phys_ptr<paging_table<depth - 1>>{entry.non_page.base_address * 0x1000ull};
 }
-inline phys_ptr<page<0>> get_page_base_address(const PE& PE) {
-       os::assert(is_page(PE), "Tried to get page out of a non-page paging.");
-       return phys_ptr<page<0>>{PE.page.base_address * sizeof(page<0>)};
+template <size_t depth> phys_ptr<page<depth>> get_page_base_address(paging_entry<depth> entry) {
+       os::assert(is_page(entry), "Tried to get page out of a non-page paging.");
+       return phys_ptr<page<depth>>{entry.page.base_address * sizeof(page<depth>)};
 }
 
-inline void set_base_address(PML4E& PML4E, phys_ptr<PDPT> ptr) {
-       os::assert(!is_page(PML4E), "Tried to get non-page out of a page paging.");
-       PML4E.non_page.base_address = ptr.get_phys_addr() / 0x1000ull;
-}
-inline void set_base_address(PDPE& PDPE, phys_ptr<PDT> ptr) {
-       os::assert(!is_page(PDPE), "Tried to get non-page out of a page paging.");
-       PDPE.non_page.base_address = ptr.get_phys_addr() / 0x1000ull;
-}
-inline void set_base_address(PDE& PDE, phys_ptr<PT> ptr) {
-       os::assert(!is_page(PDE), "Tried to get non-page out of a page paging.");
-       PDE.non_page.base_address = ptr.get_phys_addr() / 0x1000ull;
-}
-inline void set_page_base_address(PDPE& PDPE, phys_ptr<page<2>> ptr) {
-       os::assert(is_page(PDPE), "Tried to get page out of a non-page paging.");
-       PDPE.page.base_address = ptr.get_phys_addr() / sizeof(*ptr);
-}
-inline void set_page_base_address(PDE& PDE, phys_ptr<page<1>> ptr) {
-       os::assert(is_page(PDE), "Tried to get page out of a non-page paging.");
-       PDE.page.base_address = ptr.get_phys_addr() / sizeof(*ptr);
+template <size_t depth> void set_base_address(paging_entry<depth>& entry, phys_ptr<paging_table<depth - 1>> ptr) {
+       os::assert(!is_page(entry), "Tried to get non-page out of a page paging.");
+       entry.non_page.base_address = ptr.get_phys_addr() / 0x1000ull;
 }
-inline void set_page_base_address(PE& PE, phys_ptr<page<0>> ptr) {
-       os::assert(is_page(PE), "Tried to get page out of a non-page paging.");
-       PE.page.base_address = ptr.get_phys_addr() / sizeof(*ptr);
+template <size_t depth> void set_page_base_address(paging_entry<depth>& entry, phys_ptr<page<depth>> ptr) {
+       os::assert(is_page(entry), "Tried to get page out of a non-page paging.");
+       entry.page.base_address = ptr.get_phys_addr() / sizeof(page<depth>);
 }
 
 struct page_table_indices {