Add ptr_t class to types.h

This commit is contained in:
EnderIce2 2024-03-27 18:21:17 +02:00
parent b8355ec455
commit d7cc1f5c47
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
2 changed files with 169 additions and 0 deletions

View File

@ -232,6 +232,67 @@ typedef long clock_t;
typedef int pid_t;
#endif
#ifdef __cplusplus
template <typename T>
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<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<T> &operator=(const ptr_t<T> &other)
{
ptr = other.ptr;
return *this;
}
ptr_t<T> &operator+=(uintptr_t offset)
{
ptr = (T)((uintptr_t)ptr + offset);
return *this;
}
ptr_t<T> &operator-=(uintptr_t offset)
{
ptr = (T)((uintptr_t)ptr - offset);
return *this;
}
bool operator==(const ptr_t<T> &other) const { return ptr == other.ptr; }
bool operator==(auto other) const { return (uintptr_t)ptr == (uintptr_t)other; }
bool operator!=(const ptr_t<T> &other) const { return ptr != other.ptr; }
bool operator!=(auto other) const { return (uintptr_t)ptr != (uintptr_t)other; }
bool operator>(const ptr_t<T> &other) const { return ptr > other.ptr; }
bool operator>(auto other) const { return (uintptr_t)ptr > (uintptr_t)other; }
bool operator<(const ptr_t<T> &other) const { return ptr < other.ptr; }
bool operator<(auto other) const { return (uintptr_t)ptr < (uintptr_t)other; }
bool operator>=(const ptr_t<T> &other) const { return ptr >= other.ptr; }
bool operator>=(auto other) const { return (uintptr_t)ptr >= (uintptr_t)other; }
bool operator<=(const ptr_t<T> &other) const { return ptr <= other.ptr; }
bool operator<=(auto other) const { return (uintptr_t)ptr <= (uintptr_t)other; }
ptr_t<T> operator+(auto offset) const { return ptr_t<T>((void *)((uintptr_t)ptr + offset)); }
ptr_t<T> operator-(auto offset) const { return ptr_t<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__

108
tests/ptr_t.cpp Normal file
View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#ifdef DEBUG
#include <types.h>
#include <debug.h>
#include <assert.h>
class __ptr_t_demo
{
public:
ptr_t<void *> Address;
__ptr_t_demo(ptr_t<void *> Address, ptr_t<void *> Address2 = nullptr)
{
this->Address = Address;
}
~__ptr_t_demo() {}
void *test1()
{
return Address + 0xdeadbeef;
}
uintptr_t test2()
{
return Address + 0xdeadbeef;
}
void *Map(ptr_t<void *> VirtualAddress, ptr_t<void *> &PhysicalAddress, size_t Length)
{
ptr_t<void *> 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<void *> VirtualAddress = 0x1000;
ptr_t<void *> 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<void *> v;
v += 1;
assert(v == 1);
v -= 1;
assert(v == 0);
v -= 1;
assert(v == (void *)-1);
}
#endif // DEBUG