From d7cc1f5c470fbfa1101d7e56c2b7685cce8b0449 Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Wed, 27 Mar 2024 18:21:17 +0200 Subject: [PATCH] Add ptr_t class to types.h --- include/types.h | 61 +++++++++++++++++++++++++++ tests/ptr_t.cpp | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 tests/ptr_t.cpp diff --git a/include/types.h b/include/types.h index 0ee02da..3263985 100644 --- a/include/types.h +++ b/include/types.h @@ -232,6 +232,67 @@ typedef long clock_t; typedef int pid_t; #endif +#ifdef __cplusplus +template +class ptr_t +{ + T ptr; + +public: + ptr_t() : ptr(nullptr) {} + ptr_t(T p) : ptr(p) {} + ptr_t(int p) : ptr((T)(uintptr_t)p) {} + ptr_t(const ptr_t &other) : ptr(other.ptr) {} + + operator T() { return ptr; } + operator uintptr_t() { return (uintptr_t)ptr; } + + operator bool() { return (void *)(uintptr_t)ptr != nullptr; } + + ptr_t &operator=(const ptr_t &other) + { + ptr = other.ptr; + return *this; + } + + ptr_t &operator+=(uintptr_t offset) + { + ptr = (T)((uintptr_t)ptr + offset); + return *this; + } + + ptr_t &operator-=(uintptr_t offset) + { + ptr = (T)((uintptr_t)ptr - offset); + return *this; + } + + bool operator==(const ptr_t &other) const { return ptr == other.ptr; } + bool operator==(auto other) const { return (uintptr_t)ptr == (uintptr_t)other; } + + bool operator!=(const ptr_t &other) const { return ptr != other.ptr; } + bool operator!=(auto other) const { return (uintptr_t)ptr != (uintptr_t)other; } + + bool operator>(const ptr_t &other) const { return ptr > other.ptr; } + bool operator>(auto other) const { return (uintptr_t)ptr > (uintptr_t)other; } + + bool operator<(const ptr_t &other) const { return ptr < other.ptr; } + bool operator<(auto other) const { return (uintptr_t)ptr < (uintptr_t)other; } + + bool operator>=(const ptr_t &other) const { return ptr >= other.ptr; } + bool operator>=(auto other) const { return (uintptr_t)ptr >= (uintptr_t)other; } + + bool operator<=(const ptr_t &other) const { return ptr <= other.ptr; } + bool operator<=(auto other) const { return (uintptr_t)ptr <= (uintptr_t)other; } + + ptr_t operator+(auto offset) const { return ptr_t((void *)((uintptr_t)ptr + offset)); } + ptr_t operator-(auto offset) const { return ptr_t((void *)((uintptr_t)ptr - offset)); } + + T operator->() { return ptr; } + T operator*() { return *ptr; } +}; +#endif // __cplusplus + #define INT8_MAX __INT8_MAX__ #define INT8_MIN (-INT8_MAX - 1) #define UINT8_MAX __UINT8_MAX__ diff --git a/tests/ptr_t.cpp b/tests/ptr_t.cpp new file mode 100644 index 0000000..3e7da84 --- /dev/null +++ b/tests/ptr_t.cpp @@ -0,0 +1,108 @@ +/* + This file is part of Fennix Kernel. + + Fennix Kernel is free software: you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of + the License, or (at your option) any later version. + + Fennix Kernel is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Fennix Kernel. If not, see . +*/ + +#ifdef DEBUG + +#include +#include +#include + +class __ptr_t_demo +{ +public: + ptr_t Address; + + __ptr_t_demo(ptr_t Address, ptr_t Address2 = nullptr) + { + this->Address = Address; + } + + ~__ptr_t_demo() {} + + void *test1() + { + return Address + 0xdeadbeef; + } + + uintptr_t test2() + { + return Address + 0xdeadbeef; + } + + void *Map(ptr_t VirtualAddress, ptr_t &PhysicalAddress, size_t Length) + { + ptr_t Final; + for (size_t i = 0; i < Length; i++) + Final = VirtualAddress + (i * 0x1000); + + PhysicalAddress = 0; + return Final; + } +}; + +__constructor void TestPtr_T() +{ + __ptr_t_demo demo(nullptr); + assert(demo.test1() == (void *)0xdeadbeef); + assert(demo.test2() == 0xdeadbeef); + + ptr_t VirtualAddress = 0x1000; + ptr_t PhysicalAddress = 0x2000; + size_t Length = 4; + + void *ret = demo.Map(VirtualAddress, PhysicalAddress, Length); + assert(ret == (void *)0x4000); + + VirtualAddress -= 0x1000; + assert(PhysicalAddress == 0); + + VirtualAddress = 0x1; + assert(VirtualAddress > PhysicalAddress); + assert(VirtualAddress > 0); + + PhysicalAddress = 0x2; + assert(VirtualAddress < PhysicalAddress); + PhysicalAddress = 0; + assert(!(PhysicalAddress < 0)); + + PhysicalAddress = 0x1; + assert(VirtualAddress >= PhysicalAddress); + assert(VirtualAddress >= 0); + + VirtualAddress = 0; + PhysicalAddress = 0; + assert(VirtualAddress <= PhysicalAddress); + assert(PhysicalAddress <= 0); + + VirtualAddress = 0x1000; + assert(VirtualAddress == 0x1000); + assert(VirtualAddress != 0x2000); + + PhysicalAddress = 0x1000; + assert(PhysicalAddress == 0x1000); + assert(PhysicalAddress != 0x2000); + + ptr_t v; + v += 1; + assert(v == 1); + v -= 1; + assert(v == 0); + v -= 1; + assert(v == (void *)-1); +} + +#endif // DEBUG