#include <stddef.h>
#include <stdint.h>
+#include <compare>
namespace os {
template <typename T>
class phys_ptr {
public:
- constexpr phys_ptr(uintptr_t phys_addr): phys_addr(phys_addr) {}
+ constexpr explicit phys_ptr(uintptr_t phys_addr): phys_addr(phys_addr) {}
constexpr phys_ptr(nullptr_t): phys_addr(0) {}
constexpr T& operator[](size_t i) const {
constexpr T* operator->() const {
return get_virt_addr();
}
- constexpr bool operator< (phys_ptr<T> other) const { return phys_addr < other.phys_addr; }
- constexpr bool operator<=(phys_ptr<T> other) const { return phys_addr <= other.phys_addr; }
- constexpr bool operator==(phys_ptr<T> other) const { return phys_addr == other.phys_addr; }
- constexpr bool operator!=(phys_ptr<T> other) const { return phys_addr != other.phys_addr; }
- constexpr bool operator> (phys_ptr<T> other) const { return phys_addr > other.phys_addr; }
- constexpr bool operator>=(phys_ptr<T> other) const { return phys_addr >= other.phys_addr; }
+
+ constexpr phys_ptr<T>& operator++() {
+ phys_addr += sizeof(T);
+ }
+ constexpr phys_ptr<T>& operator++(int) {
+ const auto old = *this;
+ operator++();
+ return old;
+ }
+ constexpr phys_ptr<T>& operator--() {
+ phys_addr -= sizeof(T);
+ }
+ constexpr phys_ptr<T>& operator--(int) {
+ const auto old = *this;
+ operator--();
+ return old;
+ }
+ constexpr phys_ptr<T>& operator+=(ptrdiff_t offset) {
+ phys_addr += offset * sizeof(T);
+ return *this;
+ }
+ constexpr phys_ptr<T>& operator-=(ptrdiff_t offset) {
+ phys_addr -= offset * sizeof(T);
+ return *this;
+ }
+
+ friend constexpr auto operator<=>(phys_ptr<T> a, phys_ptr<T> b) { return a.phys_addr <=> b.phys_addr; }
+ friend constexpr auto operator== (phys_ptr<T> a, phys_ptr<T> b) { return a.phys_addr == b.phys_addr; }
+
+ friend constexpr phys_ptr<T> operator+(phys_ptr<T> ptr, ptrdiff_t offset) {
+ return phys_ptr<T>{ptr.phys_addr + offset * sizeof(T)};
+ }
+ friend constexpr phys_ptr<T> operator+(ptrdiff_t offset, phys_ptr<T> ptr) {
+ return ptr + offset;
+ }
+ friend constexpr phys_ptr<T> operator-(phys_ptr<T> ptr, ptrdiff_t offset) {
+ return phys_ptr<T>{ptr.phys_addr - offset * sizeof(T)};
+ }
constexpr uintptr_t get_phys_addr() const {
return phys_addr;