From: Amelia Coutard <eliottulio.coutard@gmail.com>
Date: Thu, 26 May 2022 18:21:29 +0000 (+0200)
Subject: Changed nullptr to be ~0 instead of 0 for phys_ptr<T> and fixed a missing line in... 
X-Git-Url: https://git.ameliathe1st.gay/?a=commitdiff_plain;h=694848ebcd51f2a60acbe5f4deab1292f3cac092;p=voyage-au-centre-des-fichiers.git

Changed nullptr to be ~0 instead of 0 for phys_ptr<T> and fixed a missing line in page_allocator

The goal is to allow usage of physical address 0. Removed the changing
of address <0x1000 to 0x1000 in the initial memory detection phase.
---

diff --git a/src/kernel.cpp b/src/kernel.cpp
index 3ca6bd4..cb01bc1 100644
--- a/src/kernel.cpp
+++ b/src/kernel.cpp
@@ -58,11 +58,7 @@ extern "C" void kmain(unsigned long magic, os::phys_ptr<const multiboot2::info_s
 		case multiboot2::info::type_t::memory_map:
 			for (std::size_t i = 0; i < multiboot2::memory_map_number_of_entries(it); i++) {
 				if (multiboot2::memory_map_type(it, i) == 1) {
-					const os::phys_ptr<os::paging::page> s{
-						multiboot2::memory_map_base_addr(it, i) < 0x1000
-							? 0x1000
-							: (multiboot2::memory_map_base_addr(it, i) + 0x1000 - 1) / 0x1000 * 0x1000
-					};
+					const os::phys_ptr<os::paging::page> s{(multiboot2::memory_map_base_addr(it, i) + 0x1000 - 1) / 0x1000 * 0x1000};
 					const os::phys_ptr<os::paging::page> e{multiboot2::memory_map_base_addr(it, i) + multiboot2::memory_map_length(it, i) / 0x1000 * 0x1000};
 					if (s < e) {
 						remove_some_mem(
diff --git a/src/paging.cpp b/src/paging.cpp
index 7b96ef4..e73cfb2 100644
--- a/src/paging.cpp
+++ b/src/paging.cpp
@@ -36,6 +36,7 @@ os::paging::page_allocator::block os::paging::page_allocator::allocate(std::uint
 	return { .ptr = nullptr, .size = count };
 }
 void os::paging::page_allocator::deallocate(block b) {
+	if (b.ptr == nullptr) { return; }
 	if (b.size == 0) { return; }
 	const phys_ptr<page> b_it{b.ptr.get_phys_addr()};
 	auto it = begin();
diff --git a/src/phys_ptr.hpp b/src/phys_ptr.hpp
index 72ffc79..256cd5d 100644
--- a/src/phys_ptr.hpp
+++ b/src/phys_ptr.hpp
@@ -9,7 +9,7 @@ template <typename T>
 class phys_ptr {
 public:
 	constexpr explicit phys_ptr(std::uintptr_t phys_addr): phys_addr(phys_addr) {}
-	constexpr phys_ptr(std::nullptr_t): phys_addr(0) {}
+	constexpr phys_ptr(std::nullptr_t): phys_addr(~0ull) {}
 
 	T& operator[](std::size_t i) const {
 		return get_virt_addr()[i];
@@ -64,7 +64,7 @@ public:
 	friend constexpr auto operator<=>(phys_ptr<T> a, phys_ptr<T> b) = default;
 
 private:
-	T* get_virt_addr() const {
+	constexpr T* get_virt_addr() const {
 		return reinterpret_cast<T*>(phys_addr + 0xFFFF800000000000);
 	}