From: Amelia Coutard Date: Thu, 12 May 2022 10:58:44 +0000 (+0200) Subject: Moved phys_ptr to a separate file X-Git-Url: https://git.ameliathe1st.gay/?a=commitdiff_plain;h=b73fb9fca3d9a4b797b655cfe7d6dd8af3ea0490;p=voyage-au-centre-des-fichiers.git Moved phys_ptr to a separate file --- diff --git a/src/fb.cpp b/src/fb.cpp index 3956290..d7f50c1 100644 --- a/src/fb.cpp +++ b/src/fb.cpp @@ -1,4 +1,5 @@ #include "serial.hpp" +#include "utils.hpp" #include "fb.hpp" os::color os::operator*(color c1, color c2) { diff --git a/src/fb.hpp b/src/fb.hpp index 7c512d9..7acb6af 100644 --- a/src/fb.hpp +++ b/src/fb.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include "utils.hpp" +#include "phys_ptr.hpp" namespace os { diff --git a/src/multiboot2.hpp b/src/multiboot2.hpp index 372fb13..9fd7c12 100644 --- a/src/multiboot2.hpp +++ b/src/multiboot2.hpp @@ -2,7 +2,7 @@ #ifdef __cplusplus # include -# include "utils.hpp" +# include "phys_ptr.hpp" namespace multiboot2 { #endif // __cplusplus diff --git a/src/paging.hpp b/src/paging.hpp index 9013fb5..4084905 100644 --- a/src/paging.hpp +++ b/src/paging.hpp @@ -3,6 +3,7 @@ #include #include "serial.hpp" #include "utils.hpp" +#include "phys_ptr.hpp" namespace os { namespace paging { diff --git a/src/phys_ptr.hpp b/src/phys_ptr.hpp new file mode 100644 index 0000000..f614de8 --- /dev/null +++ b/src/phys_ptr.hpp @@ -0,0 +1,78 @@ +#pragma once + +#include +#include +#include + +namespace os { + +template +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 T& operator[](std::size_t i) const { + return get_virt_addr()[i]; + } + constexpr T& operator*() const { + return *get_virt_addr(); + } + constexpr T* operator->() const { + return get_virt_addr(); + } + + constexpr phys_ptr& operator++() { + phys_addr += sizeof(T); + } + constexpr phys_ptr& operator++(int) { + const auto old = *this; + operator++(); + return old; + } + constexpr phys_ptr& operator--() { + phys_addr -= sizeof(T); + } + constexpr phys_ptr& operator--(int) { + const auto old = *this; + operator--(); + return old; + } + constexpr phys_ptr& operator+=(std::ptrdiff_t offset) { + phys_addr += offset * sizeof(T); + return *this; + } + constexpr phys_ptr& operator-=(std::ptrdiff_t offset) { + phys_addr -= offset * sizeof(T); + return *this; + } + + friend constexpr auto operator<=>(phys_ptr a, phys_ptr b) { return a.phys_addr <=> b.phys_addr; } + friend constexpr auto operator== (phys_ptr a, phys_ptr b) { return a.phys_addr == b.phys_addr; } + + friend constexpr phys_ptr operator+(phys_ptr ptr, std::ptrdiff_t offset) { + return phys_ptr{ptr.phys_addr + offset * sizeof(T)}; + } + friend constexpr phys_ptr operator+(std::ptrdiff_t offset, phys_ptr ptr) { + return ptr + offset; + } + friend constexpr phys_ptr operator-(phys_ptr ptr, std::ptrdiff_t offset) { + return phys_ptr{ptr.phys_addr - offset * sizeof(T)}; + } + friend constexpr std::ptrdiff_t operator-(phys_ptr a, phys_ptr b) { + return (a.phys_addr - b.phys_addr) / sizeof(T); + } + + constexpr std::uintptr_t get_phys_addr() const { + return phys_addr; + } + +private: + constexpr T* get_virt_addr() const { + return reinterpret_cast(phys_addr + 0xFFFF800000000000); + } + + std::uintptr_t phys_addr; +}; + +} // namespace os diff --git a/src/utils.hpp b/src/utils.hpp index 5210817..e2e242f 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -1,79 +1,7 @@ #pragma once -#include -#include -#include - namespace os { - template - 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 T& operator[](std::size_t i) const { - return get_virt_addr()[i]; - } - constexpr T& operator*() const { - return *get_virt_addr(); - } - constexpr T* operator->() const { - return get_virt_addr(); - } - - constexpr phys_ptr& operator++() { - phys_addr += sizeof(T); - } - constexpr phys_ptr& operator++(int) { - const auto old = *this; - operator++(); - return old; - } - constexpr phys_ptr& operator--() { - phys_addr -= sizeof(T); - } - constexpr phys_ptr& operator--(int) { - const auto old = *this; - operator--(); - return old; - } - constexpr phys_ptr& operator+=(std::ptrdiff_t offset) { - phys_addr += offset * sizeof(T); - return *this; - } - constexpr phys_ptr& operator-=(std::ptrdiff_t offset) { - phys_addr -= offset * sizeof(T); - return *this; - } - - friend constexpr auto operator<=>(phys_ptr a, phys_ptr b) { return a.phys_addr <=> b.phys_addr; } - friend constexpr auto operator== (phys_ptr a, phys_ptr b) { return a.phys_addr == b.phys_addr; } - - friend constexpr phys_ptr operator+(phys_ptr ptr, std::ptrdiff_t offset) { - return phys_ptr{ptr.phys_addr + offset * sizeof(T)}; - } - friend constexpr phys_ptr operator+(std::ptrdiff_t offset, phys_ptr ptr) { - return ptr + offset; - } - friend constexpr phys_ptr operator-(phys_ptr ptr, std::ptrdiff_t offset) { - return phys_ptr{ptr.phys_addr - offset * sizeof(T)}; - } - friend constexpr std::ptrdiff_t operator-(phys_ptr a, phys_ptr b) { - return (a.phys_addr - b.phys_addr) / sizeof(T); - } - - constexpr std::uintptr_t get_phys_addr() const { - return phys_addr; - } - - private: - constexpr T* get_virt_addr() const { - return reinterpret_cast(phys_addr + 0xFFFF800000000000); - } - - std::uintptr_t phys_addr; - }; +void halt(); - void halt(); } // namespace os