From: Amelia Coutard <eliottulio.coutard@gmail.com> Date: Mon, 9 May 2022 09:29:54 +0000 (+0200) Subject: Added operator- to phys_ptr<T>s, which allowed for simplifying a bit of the code X-Git-Url: https://git.ameliathe1st.gay/?a=commitdiff_plain;h=5907f488af24c0e1f5d39518accc719a80a53e79;p=voyage-au-centre-des-fichiers.git Added operator- to phys_ptr<T>s, which allowed for simplifying a bit of the code --- diff --git a/src/kernel.cpp b/src/kernel.cpp index 32ec362..9512d14 100644 --- a/src/kernel.cpp +++ b/src/kernel.cpp @@ -91,7 +91,7 @@ extern "C" void kmain(unsigned long magic, os::phys_ptr<const multiboot2::info_s remove_some_mem( s, e, info_start, info_end, [] (auto s, auto e) { - page_allocator.deallocate({.ptr = s, .size = (e.get_phys_addr() - s.get_phys_addr()) / 0x1000}); + page_allocator.deallocate({.ptr = s, .size = std::uint64_t(e - s)}); } ); } @@ -114,10 +114,10 @@ extern "C" void kmain(unsigned long magic, os::phys_ptr<const multiboot2::info_s // Deallocate multiboot info structure: not usable anymore. if (info_pages_wholly_usable) { - page_allocator.deallocate({.ptr = info_start, .size = (info_end.get_phys_addr() - info_start.get_phys_addr()) / 0x1000}); + page_allocator.deallocate({.ptr = info_start, .size = std::uint64_t(info_end - info_start)}); } else if (info_start + 1 < info_end - 1) { // Ignore the first and last blocks: ensures all the RAM is truly usable. - page_allocator.deallocate({.ptr = info_start + 1, .size = (info_end.get_phys_addr() - info_start.get_phys_addr()) / 0x1000 - 2}); + page_allocator.deallocate({.ptr = info_start + 1, .size = std::uint64_t(info_end - info_start - 2)}); } os::print("RAM:\n"); diff --git a/src/utils.hpp b/src/utils.hpp index 2f1ae15..5210817 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -59,6 +59,9 @@ namespace os { friend constexpr phys_ptr<T> operator-(phys_ptr<T> ptr, std::ptrdiff_t offset) { return phys_ptr<T>{ptr.phys_addr - offset * sizeof(T)}; } + friend constexpr std::ptrdiff_t operator-(phys_ptr<T> a, phys_ptr<T> b) { + return (a.phys_addr - b.phys_addr) / sizeof(T); + } constexpr std::uintptr_t get_phys_addr() const { return phys_addr;