From ef1edc3fb580d1b3c4b6b2876e63ca9d801a74c6 Mon Sep 17 00:00:00 2001
From: Amelia Coutard <eliottulio.coutard@gmail.com>
Date: Tue, 5 Dec 2023 17:35:07 +0100
Subject: [PATCH] Simplified some getters and setters, with the new template
 definitions

---
 kernel/src/paging.hpp | 66 +++++++++++--------------------------------
 1 file changed, 17 insertions(+), 49 deletions(-)

diff --git a/kernel/src/paging.hpp b/kernel/src/paging.hpp
index d4ed443..6955a7b 100644
--- a/kernel/src/paging.hpp
+++ b/kernel/src/paging.hpp
@@ -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 {
-- 
2.46.0