diff options
| author | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-01-06 09:14:11 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-01-06 09:14:11 +0100 |
| commit | 5339d016c07bf717ee388f4feb73544087324af0 (patch) | |
| tree | 94be6f67ed626091f24aee24ec3b3be03d01e4e7 /NewKit | |
git: port from mercurial repo.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'NewKit')
| -rw-r--r-- | NewKit/Array.hpp | 61 | ||||
| -rw-r--r-- | NewKit/ArrayList.hpp | 60 | ||||
| -rw-r--r-- | NewKit/Atom.hpp | 48 | ||||
| -rw-r--r-- | NewKit/CRC32.hpp | 22 | ||||
| -rw-r--r-- | NewKit/CxxAbi.hpp | 31 | ||||
| -rw-r--r-- | NewKit/Defines.hpp | 93 | ||||
| -rw-r--r-- | NewKit/ErrorID.hpp | 21 | ||||
| -rw-r--r-- | NewKit/ErrorOr.hpp | 68 | ||||
| -rw-r--r-- | NewKit/Function.hpp | 49 | ||||
| -rw-r--r-- | NewKit/Heap.hpp | 47 | ||||
| -rw-r--r-- | NewKit/Json.hpp | 105 | ||||
| -rw-r--r-- | NewKit/KHeap.hpp | 23 | ||||
| -rw-r--r-- | NewKit/LockDelegate.hpp | 60 | ||||
| -rw-r--r-- | NewKit/Macros.hpp | 86 | ||||
| -rw-r--r-- | NewKit/MutableArray.hpp | 225 | ||||
| -rw-r--r-- | NewKit/New.hpp | 21 | ||||
| -rw-r--r-- | NewKit/NewKit.hpp | 27 | ||||
| -rw-r--r-- | NewKit/OwnPtr.hpp | 67 | ||||
| -rw-r--r-- | NewKit/PageAllocator.hpp | 64 | ||||
| -rw-r--r-- | NewKit/PageManager.hpp | 81 | ||||
| -rw-r--r-- | NewKit/Pair.hpp | 17 | ||||
| -rw-r--r-- | NewKit/Panic.hpp | 64 | ||||
| -rw-r--r-- | NewKit/Pmm.hpp | 45 | ||||
| -rw-r--r-- | NewKit/Ref.hpp | 88 | ||||
| -rw-r--r-- | NewKit/Stream.hpp | 61 | ||||
| -rw-r--r-- | NewKit/String.hpp | 80 | ||||
| -rw-r--r-- | NewKit/Utils.hpp | 32 | ||||
| -rw-r--r-- | NewKit/Variant.hpp | 49 | ||||
| -rw-r--r-- | NewKit/compile_flags.txt | 5 |
29 files changed, 1700 insertions, 0 deletions
diff --git a/NewKit/Array.hpp b/NewKit/Array.hpp new file mode 100644 index 00000000..6987ca72 --- /dev/null +++ b/NewKit/Array.hpp @@ -0,0 +1,61 @@ +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ +#pragma once + +#include <KernelKit/DebugOutput.hpp> +#include <NewKit/ErrorOr.hpp> +#include <NewKit/Defines.hpp> + +namespace hCore +{ +template <typename T, Size N> +class Array final +{ +public: + Array() {} + ~Array() {} + + Array &operator=(const Array &) = default; + Array(const Array &) = default; + + ErrorOr<T> operator[](Size At) + { + if (At > N) + return {}; + + kcout << "Returning element\r\n"; + return ErrorOr<T>(m_Array[At]); + } + + Boolean Empty() const + { + for (auto Val : m_Array) + { + if (Val) + return false; + } + + return true; + } + + const T *CData() + { + return m_Array; + } + + operator bool() + { + return !Empty(); + } + +private: + T m_Array[N]; + +}; +} // namespace hCore diff --git a/NewKit/ArrayList.hpp b/NewKit/ArrayList.hpp new file mode 100644 index 00000000..20517d22 --- /dev/null +++ b/NewKit/ArrayList.hpp @@ -0,0 +1,60 @@ +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/Defines.hpp> + +namespace hCore +{ + template <typename T> + class ArrayList final + { + public: + explicit ArrayList(T *list) + : m_List(reinterpret_cast<T>(list)) + {} + + ~ArrayList() = default; + + ArrayList &operator=(const ArrayList &) = default; + ArrayList(const ArrayList &) = default; + + T *Data() + { + return m_List; + } + + const T *CData() + { + return m_List; + } + + T &operator[](int index) const + { + return m_List[index]; + } + + operator bool() + { + return m_List; + } + + private: + T *m_List; + + friend class InitHelpers; + + }; + + template <typename ValueType> ArrayList<ValueType> make_list(ValueType val) + { + return ArrayList<ValueType>{val}; + } +} // namespace hCore diff --git a/NewKit/Atom.hpp b/NewKit/Atom.hpp new file mode 100644 index 00000000..99e7e6a7 --- /dev/null +++ b/NewKit/Atom.hpp @@ -0,0 +1,48 @@ +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ +#pragma once + +#include <NewKit/Defines.hpp> + +namespace hCore +{ +template <typename T> class Atom final +{ + public: + explicit Atom() = default; + ~Atom() = default; + + public: + Atom &operator=(const Atom &) = delete; + Atom(const Atom &) = delete; + + public: + T operator[](Size sz) + { + return (m_ArrayOfAtoms & sz); + } + void operator|(Size sz) + { + m_ArrayOfAtoms |= sz; + } + + friend Boolean operator==(Atom<T> &atomic, const T &idx) + { + return atomic[idx] == idx; + } + + friend Boolean operator!=(Atom<T> &atomic, const T &idx) + { + return atomic[idx] == idx; + } + + private: + T m_ArrayOfAtoms; +}; +} // namespace hCore diff --git a/NewKit/CRC32.hpp b/NewKit/CRC32.hpp new file mode 100644 index 00000000..2ddc876b --- /dev/null +++ b/NewKit/CRC32.hpp @@ -0,0 +1,22 @@ +/* + * ======================================================== + * + * hCore Date Added: 13/02/2023 + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#ifndef __CRC32_H__ +#define __CRC32_H__ + +#include <NewKit/Defines.hpp> + +#define kCrcCnt (256) + +namespace hCore +{ + Int crc32(const Char* crc, Int len); +} // namespace hCore + +#endif // !__CRC32_H__ diff --git a/NewKit/CxxAbi.hpp b/NewKit/CxxAbi.hpp new file mode 100644 index 00000000..bd2e76c8 --- /dev/null +++ b/NewKit/CxxAbi.hpp @@ -0,0 +1,31 @@ +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ +#pragma once + +#include <NewKit/Defines.hpp> + +#ifdef __GNUC__ + +#define DSO_MAX_OBJECTS (128) + +struct atexit_func_entry_t +{ + void (*destructor_func)(void *); + void *obj_ptr; + void *dso_handle; +}; + +typedef unsigned uarch_t; + +namespace cxxabiv1 +{ + typedef void *__guard; +} + +#endif // __GNUC__
\ No newline at end of file diff --git a/NewKit/Defines.hpp b/NewKit/Defines.hpp new file mode 100644 index 00000000..497032d7 --- /dev/null +++ b/NewKit/Defines.hpp @@ -0,0 +1,93 @@ +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/Macros.hpp> + +#define NEWKIT_VERSION "v1.00" + +#if !defined(_INC_NO_STDC_HEADERS) && defined(__GNUC__) +# include <CRT/__cxxkit_defines.h> +#endif + +#ifdef __has_feature +#if !__has_feature(cxx_nullptr) +# error You must at least have nullptr featured on your C++ compiler. +#endif +#endif + +namespace hCore +{ + using voidPtr = void*; + using VoidPtr = void*; + using nullPtr = decltype(nullptr); + using NullPtr = decltype(nullptr); + + using Int = int; + using Int32 = int; + using UShort = unsigned short; + using UInt16 = unsigned short; + using Short = short; + using Int16 = short; + using UInt = unsigned int; + using UInt32 = unsigned int; + using Long = long int; + using Int64 = long int; + using ULong = unsigned long int; + using UInt64 = unsigned long int; + using Boolean = bool; + using Bool = bool; + using Char = char; + using UChar = unsigned char; + using UInt8 = unsigned char; + + using SSize = Int64; + using SSizeT = Int64; + using Size = __SIZE_TYPE__; + using SizeT = __SIZE_TYPE__; + using IntPtr = __INTPTR_TYPE__; + using UIntPtr = __UINTPTR_TYPE__; + using IntFast = __INT_FAST32_TYPE__; + using IntFast64 = __INT_FAST64_TYPE__; + using PtrDiff = __PTRDIFF_TYPE__; + + using Utf8Char = char8_t; + using Utf16Char = char16_t; + using Utf32Char = char32_t; + + using Void = void; + + using Lba = SSizeT; + + enum class Endian : UChar + { + kLittle, + kBig + }; + + template <typename Args> Args &&forward(Args &arg) + { + return static_cast<Args &&>(arg); + } + + template <typename Args> Args &&move(Args &&arg) + { + return static_cast<Args &&>(arg); + } +} // namespace hCore + +#define DEDUCE_ENDIAN(address, value) \ + (((reinterpret_cast<hCore::Char*>(address)[0]) == (value)) ? (hCore::Endian::kBig) \ + : (hCore::Endian::kLittle)) + +#define Yes (true) +#define No (false) + +#define VoidStar hCore::voidPtr
\ No newline at end of file diff --git a/NewKit/ErrorID.hpp b/NewKit/ErrorID.hpp new file mode 100644 index 00000000..e26cc41b --- /dev/null +++ b/NewKit/ErrorID.hpp @@ -0,0 +1,21 @@ +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/ErrorOr.hpp> +#include <NewKit/Defines.hpp> + +#define ME_EXEC_ERROR -30 +#define ME_FILE_NOT_FOUND -31 +#define ME_DIR_NOT_FOUND -32 +#define ME_FILE_EXISTS -33 +#define ME_TOO_LONG -34 +#define ME_INVALID_DATA -35 +#define ME_UNIMPLEMENTED -36 diff --git a/NewKit/ErrorOr.hpp b/NewKit/ErrorOr.hpp new file mode 100644 index 00000000..736340c7 --- /dev/null +++ b/NewKit/ErrorOr.hpp @@ -0,0 +1,68 @@ +/* +* ======================================================== +* +* hCore +* Copyright Mahrouss Logic, all rights reserved. +* +* ======================================================== +*/ + +#pragma once + +#include <NewKit/Defines.hpp> +#include <NewKit/Ref.hpp> + +namespace hCore +{ +using ErrorT = UInt; + +template <typename T> class ErrorOr final +{ + public: + ErrorOr() = default; + ~ErrorOr() = default; + + public: + explicit ErrorOr(Int32 err) + : mId(err) + {} + + explicit ErrorOr(nullPtr Null) + {} + + explicit ErrorOr(T Class) + : mRef(Class) + {} + + ErrorOr &operator=(const ErrorOr &) = default; + ErrorOr(const ErrorOr &) = default; + + ErrorOr &operator=(const Ref<T> &refErr) + { + mRef = refErr; + return *this; + } + + Ref<T> Leak() + { + return mRef; + } + + Int32 Error() + { + return mId; + } + + operator bool() + { + return mRef; + } + + private: + Ref<T> mRef; + Int32 mId{0}; +}; + +using ErrorOrAny = ErrorOr<voidPtr>; + +} // namespace hCore diff --git a/NewKit/Function.hpp b/NewKit/Function.hpp new file mode 100644 index 00000000..772755e1 --- /dev/null +++ b/NewKit/Function.hpp @@ -0,0 +1,49 @@ +#ifndef _INC_FUNCTION_HPP__ +#define _INC_FUNCTION_HPP__ + +#include <NewKit/Defines.hpp> + +namespace hCore +{ + template <typename T, typename... Args> + class Function final + { + public: + Function() = default; + + public: + explicit Function(T (*Fn)(Args... args)) + : m_Fn(Fn) + {} + + ~Function() = default; + + Function &operator=(const Function &) = default; + Function(const Function &) = default; + + template <typename... XArgs> T operator()(Args... args) + { + return m_Fn(args...); + } + + template <typename... XArgs> T Call(Args... args) + { + return m_Fn(args...); + } + + operator bool() + { + return m_Fn; + } + bool operator!() + { + return !m_Fn; + } + + private: + T (*m_Fn)(Args... args); + + }; +} // namespace hCore + +#endif // !_INC_FUNCTION_HPP__ diff --git a/NewKit/Heap.hpp b/NewKit/Heap.hpp new file mode 100644 index 00000000..1e8dd056 --- /dev/null +++ b/NewKit/Heap.hpp @@ -0,0 +1,47 @@ +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/Array.hpp> +#include <NewKit/ArrayList.hpp> +#include <NewKit/ErrorOr.hpp> +#include <NewKit/PageManager.hpp> +#include <NewKit/Pmm.hpp> +#include <NewKit/Ref.hpp> + +// last-rev 5/11/23 +// file: pool.hpp +// description: memory pool for user programs. + +#define kPoolMaxSz 4096 +#define kPoolMag 0x5500A1 + +namespace hCore +{ + enum + { + kPoolHypervisor = 0x2, + kPoolShared = 0x4, + kPoolUser = 0x6, + kPoolRw = 0x8, + }; + + struct HeapHeader final + { + UInt32 Magic; + Int32 Flags; + Boolean Free; + UIntPtr Pad; + }; + + VoidPtr pool_new_ptr(Int32 flags); + Int32 pool_free_ptr(voidPtr pointer); + Boolean pool_ptr_exists(UIntPtr thePool, UIntPtr thePtr, SizeT theLimit); +} // namespace hCore diff --git a/NewKit/Json.hpp b/NewKit/Json.hpp new file mode 100644 index 00000000..9b937550 --- /dev/null +++ b/NewKit/Json.hpp @@ -0,0 +1,105 @@ + +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +// last-rev: 5/11/23 + +#include <NewKit/Defines.hpp> +#include <NewKit/String.hpp> +#include <NewKit/Stream.hpp> +#include <NewKit/Utils.hpp> + +#include <CompilerKit/Compiler.hpp> + +namespace hCore +{ + class JsonType final + { + public: + explicit JsonType(SizeT lhsLen, SizeT rhsLen) + : fKey(lhsLen), fValue(rhsLen) + {} + + ~JsonType() = default; + + HCORE_COPY_DEFAULT(JsonType); + + StringView fKey; + StringView fValue; + + StringView& AsKey() { return fKey; } + StringView& AsValue() { return fValue; } + + static JsonType kUndefined; + + }; + + struct JsonStreamTraits final + { + JsonType In(const char* full_array) + { + SizeT len = string_length(full_array); + + if (full_array[0] == '\"' && + full_array[len - 1] == ',' || + full_array[len - 1] == '\"') + { + Boolean probe_key = true; + + SizeT key_len = 0; + SizeT value_len = 0; + + for (SizeT i = 1; i < len; i++) + { + if (full_array[i] == ' ') + continue; + + JsonType type(255, 255); + + if (probe_key) + { + type.AsKey().Data()[key_len] = full_array[i]; + ++key_len; + + if (full_array[i] == '\"') + { + probe_key = false; + type.AsKey().Data()[key_len] = 0; + + ++i; + } + } + else + { + type.AsValue().Data()[value_len] = full_array[i]; + ++value_len; + + if (full_array[i] == '\"') + { + type.AsValue().Data()[value_len] = 0; + } + } + } + + } + + return JsonType::kUndefined; + } + + JsonType Out(JsonType& out) + { + return out; + } + + }; + + using JsonStream = Stream<JsonStreamTraits, JsonType>; +}
\ No newline at end of file diff --git a/NewKit/KHeap.hpp b/NewKit/KHeap.hpp new file mode 100644 index 00000000..92e4d87b --- /dev/null +++ b/NewKit/KHeap.hpp @@ -0,0 +1,23 @@ +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +// last-rev 5/03/23 +// file: KHeap.hpp +// description: page allocation for kernel. + +#include <NewKit/Defines.hpp> +#include <NewKit/Pmm.hpp> + +namespace hCore +{ + Int32 kernel_delete_ptr(voidPtr ptr); + voidPtr kernel_new_ptr(const SizeT& sz, const bool rw, const bool user); +} // namespace hCore diff --git a/NewKit/LockDelegate.hpp b/NewKit/LockDelegate.hpp new file mode 100644 index 00000000..87ad1914 --- /dev/null +++ b/NewKit/LockDelegate.hpp @@ -0,0 +1,60 @@ +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/Atom.hpp> +#include <NewKit/Defines.hpp> + +#define kLockDone (200U) /* job is done */ +#define kLockTimedOut (100U) /* has timed out */ + +namespace hCore +{ +template <Size N> +class LockDelegate final +{ + public: + LockDelegate() = delete; + + public: + explicit LockDelegate(Boolean *expr) + { + auto spin = 0U; + while (spin != N) + { + if (*expr) + { + m_LockStatus | kLockDone; + break; + } + } + + if (spin == N) + m_LockStatus | kLockTimedOut; + } + + ~LockDelegate() = default; + + LockDelegate &operator=(const LockDelegate &) = delete; + LockDelegate(const LockDelegate &) = delete; + + bool Done() + { + return m_LockStatus[kLockDone] == kLockDone; + } + bool HasTimedOut() + { + return m_LockStatus[kLockTimedOut] != kLockTimedOut; + } + + private: + Atom<UInt> m_LockStatus; +}; +} // namespace hCore diff --git a/NewKit/Macros.hpp b/NewKit/Macros.hpp new file mode 100644 index 00000000..87f7c6c7 --- /dev/null +++ b/NewKit/Macros.hpp @@ -0,0 +1,86 @@ +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#ifndef KIB +# define KIB(X) ((X)*1024) +#endif + +#ifndef MIB +# define MIB(X) (KIB(X) * 1024) +#endif + +#ifndef GIB +# define GIB(X) (MIB(X) * 1024) +#endif + +#ifndef TIB +# define TIB(X) (GIB(X) * 1024) +#endif + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(a) (((sizeof(a) / sizeof(*(a))) / (static_cast<hCore::Size>(!(sizeof(a) % sizeof(*(a))))))) +#endif + +#ifndef ALIGN +#define ALIGN(X) __attribute__((aligned(X))) +#endif // #ifndef ALIGN + +#ifndef ATTRIBUTE +#define ATTRIBUTE(X) __attribute__((X)) +#endif // #ifndef ATTRIBUTE + +#ifndef __hCore__ +#define __hCore__ (20230208) +#endif // !__hCore__ + +#ifndef EXTERN_C +#define EXTERN_C() \ + extern "C" \ + { +#define END_EXTERN_C() \ + } \ + ; +#endif + +#ifndef MAKE_ENUM +#define MAKE_ENUM(NAME) \ + enum NAME \ + { +#endif + +#ifndef END_ENUM +#define END_ENUM() \ + } \ + ; +#endif + +#ifndef MAKE_STRING_ENUM +#define MAKE_STRING_ENUM(NAME) \ + namespace NAME \ + { +#endif + +#ifndef ENUM_STRING +#define ENUM_STRING(NAME, VAL) constexpr const char *NAME = VAL +#endif + +#ifndef END_STRING_ENUM +#define END_STRING_ENUM() } +#endif + +#ifndef Alloca +#define Alloca(Sz) __builtin_alloca(Sz) +#endif // #ifndef Alloca + +#ifndef CantReach +#define CantReach() __builtin_unreachable() +#endif + diff --git a/NewKit/MutableArray.hpp b/NewKit/MutableArray.hpp new file mode 100644 index 00000000..5739305a --- /dev/null +++ b/NewKit/MutableArray.hpp @@ -0,0 +1,225 @@ +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ +#pragma once + +#include <CompilerKit/Compiler.hpp> +#include <NewKit/Array.hpp> +#include <NewKit/Defines.hpp> + +#define TRY_FIND_NODE(NAME, NODE) \ + auto *NAME = NODE; \ + while (NAME) \ + { \ + if (NAME->m_Index == Index) \ + return NAME->m_Val; \ + NAME = NAME->m_Next; \ + } + + + +#define TRY_FIND_NODE2(NAME, NODE) \ + auto *NAME = NODE; \ + while (NAME) \ + { \ + if (NAME->m_Index == Index) \ + return Ref<T>{NAME->m_Val}; \ + NAME = NAME->m_Next; \ + } + + + +#define TRY_REMOVE_NODE(NODE) \ + if (NODE && NODE->m_Index == Index) \ + { \ + NODE->m_Used = false; \ + NODE->m_Index = 0; \ + \ + return true; \ + } + + + +// FIXME: this is a shitty algorithm, which is consumer hungry. +// Remove and occurences of that, and remove that class. +namespace hCore +{ +template <typename T> class MutableArray; + +template <typename T, T _PlaceHolderValue> class NullableMutableArray; + +template <typename T> class MutableLinkedList +{ + public: + T m_Val; + SizeT m_Index{0}; + Boolean m_Used{false}; + + MutableLinkedList *m_Prev{nullptr}; + MutableLinkedList *m_Next{nullptr}; +}; + +template <typename T, T _PlaceHolderValue> class NullableMutableArray +{ + public: + // explicit this. + explicit NullableMutableArray() : m_FirstNode(new MutableLinkedList<T>()) {} + + /* + * We free all the nodes allocated by the array + * and store the next one inside "NextIt" + */ + + virtual ~NullableMutableArray() + { + auto *It = m_FirstNode; + MutableLinkedList<T> *NextIt = nullptr; + + while (It) + { + NextIt = It->m_Next; + delete It; + + It = NextIt; + } + } + + NullableMutableArray &operator=(const NullableMutableArray &) = default; + NullableMutableArray(const NullableMutableArray &) = default; + + operator bool() { return Count() > 1; } + + public: + T operator[](const SizeT &Index) const + { + TRY_FIND_NODE(first, m_FirstNode); + TRY_FIND_NODE(last, m_LastNode); + + return _PlaceHolderValue; + } + + SizeT Count() const { return m_NodeCount; } + + public: + Boolean Remove(const SizeT &Index) + { + TRY_REMOVE_NODE(m_FirstNode); + TRY_REMOVE_NODE(m_LastNode); + + return false; + } + + Boolean Add(const T val) + { + auto *iterationNode = m_FirstNode; + MUST_PASS(iterationNode); + + while (iterationNode) + { + if (!iterationNode->m_Used) + { + iterationNode->m_Val = val; + iterationNode->m_Index = 0; + + iterationNode->m_Used = true; + + ++m_NodeCount; + + return true; + } + + iterationNode = iterationNode->m_Next; + } + + return false; + } + + private: + /* Avoid useless lookups */ + MutableLinkedList<T> *m_LastNode{nullptr}; + MutableLinkedList<T> *m_FirstNode{nullptr}; + + /* Number of nodes inside of this dynamic array. */ + hCore::SizeT m_NodeCount{0}; + + private: + // don't remove that + friend MutableArray<T>; +}; + +template <typename T> +class MutableArray : public NullableMutableArray<voidPtr, nullptr> +{ + public: + // explicit this. + explicit MutableArray() = default; + virtual ~MutableArray() = default; + + HCORE_COPY_DEFAULT(MutableArray) + + public: + Boolean Add(const T val) + { + auto *iterationNode = m_FirstNode; + MUST_PASS(iterationNode); + + while (iterationNode) + { + if (!iterationNode->m_Used) + { + iterationNode->m_Val = val; + iterationNode->m_Index = 0; + + iterationNode->m_Used = true; + + ++m_NodeCount; + + return true; + } + + iterationNode = iterationNode->m_Next; + } + + return false; + } + + public: + Ref<T> operator[](const SizeT &Index) const + { + TRY_FIND_NODE2(first, m_FirstNode); + TRY_FIND_NODE2(last, m_LastNode); + + return {}; + } + + SizeT Count() const { return m_NodeCount; } + + bool Contains(T &value) noexcept + { + MutableLinkedList<T> *first = m_FirstNode; + + while (first) + { + if (first->m_Val == value && first->m_Used) + return true; + + first = first->m_Next; + } + + return false; + } + + private: + /* Avoid useless lookups */ + MutableLinkedList<T> *m_LastNode{nullptr}; + MutableLinkedList<T> *m_FirstNode{nullptr}; + + /* Number of nodes inside of this dynamic array. */ + hCore::SizeT m_NodeCount{0}; +}; +} // namespace hCore diff --git a/NewKit/New.hpp b/NewKit/New.hpp new file mode 100644 index 00000000..cf97f82a --- /dev/null +++ b/NewKit/New.hpp @@ -0,0 +1,21 @@ + +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ +#pragma once + +#include <NewKit/KHeap.hpp> + +typedef __SIZE_TYPE__ size_t; // gcc will complain about that + +void* operator new(size_t ptr); +void* operator new[](size_t ptr); + +void operator delete(void* ptr); +void operator delete(void* ptr, unsigned long); +void operator delete[](void* ptr); diff --git a/NewKit/NewKit.hpp b/NewKit/NewKit.hpp new file mode 100644 index 00000000..110aed7d --- /dev/null +++ b/NewKit/NewKit.hpp @@ -0,0 +1,27 @@ + +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/OwnPtr.hpp> +#include <NewKit/New.hpp> +#include <NewKit/Json.hpp> +#include <NewKit/Heap.hpp> +#include <NewKit/ErrorOr.hpp> +#include <NewKit/ArrayList.hpp> +#include <NewKit/Array.hpp> +#include <NewKit/Ref.hpp> +#include <NewKit/Panic.hpp> +#include <NewKit/Stream.hpp> +#include <NewKit/Utils.hpp> +#include <NewKit/MutableArray.hpp> +#include <NewKit/LockDelegate.hpp> + + diff --git a/NewKit/OwnPtr.hpp b/NewKit/OwnPtr.hpp new file mode 100644 index 00000000..591e7170 --- /dev/null +++ b/NewKit/OwnPtr.hpp @@ -0,0 +1,67 @@ + +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/Defines.hpp> +#include <NewKit/Panic.hpp> +#include <NewKit/Ref.hpp> + +namespace hCore +{ +template <typename T> class OwnPtr; + +template <typename T> class NonNullRefPtr; + +template <typename T> class OwnPtr final +{ + public: + OwnPtr() {} + ~OwnPtr() { this->Delete(); } + + OwnPtr &operator=(const OwnPtr &) = default; + OwnPtr(const OwnPtr &) = default; + + public: + template <typename... Args> bool New(Args &&...arg) + { + m_Cls = new T(arg...); + return m_Cls; + } + + void Delete() + { + if (m_Cls) + delete m_Cls; + + m_Cls = nullptr; + } + + T *operator->() const { return m_Cls; }; + T *Raw() { return m_Cls; } + + Ref<T> AsRef() { return Ref<T>(m_Cls); } + + operator bool() { return m_Cls; } + bool operator!() { return !m_Cls; } + + private: + T *m_Cls; +}; + +template <typename T, typename... Args> OwnPtr<T> make_ptr(Args... args) +{ + OwnPtr<T> ret; + ret.template New<Args...>(forward(args)...); + MUST_PASS(ret); + + return ret; +} +} // namespace hCore diff --git a/NewKit/PageAllocator.hpp b/NewKit/PageAllocator.hpp new file mode 100644 index 00000000..ab6dfdba --- /dev/null +++ b/NewKit/PageAllocator.hpp @@ -0,0 +1,64 @@ +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <HALKit/AMD64/HalPageAlloc.hpp> +#include <NewKit/Defines.hpp> +#include <NewKit/PageManager.hpp> + +namespace hCore +{ +namespace Detail +{ +enum PAGE_BIT +{ + ProtectedModeEnable = 0, + MonitorCoProcessor = 1, + Emulation = 2, + TaskSwitched = 3, + ExtensionType = 4, + NumericError = 5, + WriteProtect = 16, + AlignementMask = 18, + NotWriteThrough = 29, + CacheDisable = 30, + Paging = 31, +}; +} // namespace Detail + +class ATTRIBUTE(packed) PTE final +{ + public: + Boolean Present : 1; + Boolean Rw : 1; + Boolean User : 1; + Boolean Wt : 1; + Boolean Cache : 1; + Boolean Accessed : 1; + Boolean Shared : 1; + Boolean ExecDisable : 1; + UIntPtr PhysAddr : 32; +}; + +class ATTRIBUTE(packed) PDE final +{ + public: + ATTRIBUTE(aligned(PTE_ALIGN)) PTE Entries[PTE_MAX]; +}; + +namespace Detail +{ +UIntPtr create_page_wrapper(Boolean rw, Boolean user); +void exec_disable(UIntPtr addr); +bool page_disable(UIntPtr addr); +} // namespace Detail + +// TODO: SwapVirtualMemoryDevice class! +} // namespace hCore diff --git a/NewKit/PageManager.hpp b/NewKit/PageManager.hpp new file mode 100644 index 00000000..b54de4ba --- /dev/null +++ b/NewKit/PageManager.hpp @@ -0,0 +1,81 @@ +// a way to create and find our pages. +// I'm thinking about a separate way of getting a paged area. + +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/Defines.hpp> +#include <NewKit/PageAllocator.hpp> +#include <NewKit/Ref.hpp> + +#ifndef kBadAddress +#define kBadAddress (0) +#endif // #ifndef kBadAddress + +namespace hCore +{ +class PageManager; + +class PTEWrapper final +{ + public: + explicit PTEWrapper(Boolean Rw = false, Boolean User = false, Boolean ExecDisable = false, UIntPtr Address = 0); + ~PTEWrapper(); + + PTEWrapper &operator=(const PTEWrapper &) = default; + PTEWrapper(const PTEWrapper &) = default; + + public: + void FlushTLB(Ref<PageManager> &pm); + const UIntPtr &VirtualAddress(); + + bool Reclaim(); + bool Shareable(); + bool Present(); + bool Access(); + + private: + Boolean m_Rw; + Boolean m_User; + Boolean m_ExecDisable; + UIntPtr m_VirtAddr; + Boolean m_Cache; + Boolean m_Shareable; + Boolean m_Wt; + Boolean m_Present; + Boolean m_Accessed; + + private: + friend class PageManager; + friend class Pmm; +}; + +struct PageManager final +{ + public: + PageManager() = default; + ~PageManager() = default; + + PageManager &operator=(const PageManager &) = default; + PageManager(const PageManager &) = default; + + public: + PTEWrapper *Request(Boolean Rw, Boolean User, Boolean ExecDisable); + bool Free(Ref<PTEWrapper*> &wrapper); + + private: + void FlushTLB(UIntPtr VirtAddr); + + private: + friend PTEWrapper; + friend class Pmm; +}; +} // namespace hCore diff --git a/NewKit/Pair.hpp b/NewKit/Pair.hpp new file mode 100644 index 00000000..51a73c25 --- /dev/null +++ b/NewKit/Pair.hpp @@ -0,0 +1,17 @@ +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/Defines.hpp> + +namespace hCore +{ + +} // namespace hCore diff --git a/NewKit/Panic.hpp b/NewKit/Panic.hpp new file mode 100644 index 00000000..c3f8eb2a --- /dev/null +++ b/NewKit/Panic.hpp @@ -0,0 +1,64 @@ + +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/Defines.hpp> + +namespace hCore +{ + void runtime_check(bool bExpression, const char *file, const char *line); +} + +#define MUST_PASS_COMPILER(EXPR, MSG) static_assert(EXPR, MSG) +#define MUST_PASS(EXPR) hCore::runtime_check(EXPR, __FILE__, " %d -> ") +#define assert(EXPR) MUST_PASS(EXPR, RUNTIME_CHECK_EXPRESSION) + +enum RUNTIME_CHECK +{ + RUNTIME_CHECK_FAILED = -1, + RUNTIME_CHECK_POINTER = 0, + RUNTIME_CHECK_EXPRESSION, + RUNTIME_CHECK_FILE, + RUNTIME_CHECK_IPC, + RUNTIME_CHECK_TLS, + RUNTIME_CHECK_LD, + RUNTIME_CHECK_HANDSHAKE, + RUNTIME_CHECK_ACPI, + RUNTIME_CHECK_INVALID_PRIVILEGE, + RUNTIME_CHECK_PROCESS, + RUNTIME_CHECK_BAD_BEHAVIOR, + RUNTIME_CHECK_COUNT, + RUNTIME_CHECK_BOOTSTRAP, +}; + +namespace hCore +{ + class DumpManager final + { + public: + static void Dump(void) + { + // TODO: + } + }; + + void panic(const Int &id); +} // namespace hCore + +#ifdef TRY +#undef TRY +#endif + +#define TRY(FN) \ + if (!FN()) \ + { \ + MUST_PASS(false); \ + } diff --git a/NewKit/Pmm.hpp b/NewKit/Pmm.hpp new file mode 100644 index 00000000..aafbb850 --- /dev/null +++ b/NewKit/Pmm.hpp @@ -0,0 +1,45 @@ + +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/PageManager.hpp> +#include <NewKit/Ref.hpp> + +namespace hCore +{ + class Pmm; + class PTEWrapper; + + class Pmm final + { + public: + Pmm(); + ~Pmm(); + + public: + explicit Pmm(Ref<PageManager*> &pm); + + Pmm &operator=(const Pmm &) = delete; + Pmm(const Pmm &) = delete; + + Ref<PTEWrapper*> RequestPage(Boolean user = false, Boolean readWrite = false); + Boolean FreePage(Ref<PTEWrapper*> refPage); + + Boolean ToggleRw(Ref<PTEWrapper*> refPage, Boolean enable = true); + Boolean TogglePresent(Ref<PTEWrapper*> refPage, Boolean enable = true); + Boolean ToggleUser(Ref<PTEWrapper*> refPage, Boolean enable = true); + Boolean ToggleShare(Ref<PTEWrapper*> refPage, Boolean enable = true); + + private: + Ref<PageManager*> m_PageManager; + + }; +} // namespace hCore diff --git a/NewKit/Ref.hpp b/NewKit/Ref.hpp new file mode 100644 index 00000000..610776a3 --- /dev/null +++ b/NewKit/Ref.hpp @@ -0,0 +1,88 @@ + +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/Defines.hpp> +#include <NewKit/Panic.hpp> + +namespace hCore +{ + template <typename T> + class Ref final + { + public: + Ref() = default; + ~Ref() = default; + + public: + Ref(T cls, const bool &strong = false) : m_Class(cls), m_Strong(strong) {} + + Ref &operator=(T ref) + { + m_Class = ref; + return *this; + } + + public: + T operator->() const + { + return m_Class; + } + + T &Leak() + { + return m_Class; + } + + T operator*() + { + return m_Class; + } + + bool IsStrong() const + { + return m_Strong; + } + + operator bool() + { + return m_Class; + } + + private: + T m_Class; + bool m_Strong{ false }; + + }; + + template <typename T> + class NonNullRef final + { + public: + NonNullRef() = delete; + NonNullRef(nullPtr) = delete; + + NonNullRef(T *ref) : m_Ref(ref, true) {} + + Ref<T> &operator->() + { + MUST_PASS(m_Ref); + return m_Ref; + } + + NonNullRef &operator=(const NonNullRef<T> &ref) = delete; + NonNullRef(const NonNullRef<T> &ref) = default; + + private: + Ref<T> m_Ref{nullptr}; + + }; +} // namespace hCore diff --git a/NewKit/Stream.hpp b/NewKit/Stream.hpp new file mode 100644 index 00000000..6107d961 --- /dev/null +++ b/NewKit/Stream.hpp @@ -0,0 +1,61 @@ + +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/Defines.hpp> +#include <NewKit/Ref.hpp> + +namespace hCore +{ + template <typename StreamTraits, typename Kind> + class Stream final + { + public: + explicit Stream(Ref<Stream> ref) + : m_Stream(ref) + {} + + ~Stream() = default; + + Stream &operator=(const Stream &) = default; + Stream(const Stream &) = default; + + template <typename Data> + friend Stream<StreamTraits, Kind> &operator>>(Stream<StreamTraits, Kind> &Ks, Ref<Data>& Buf) + { + Ks.m_Kind = Ks.m_Stream->In(Buf); + return *Ks; + } + + template <typename Data> + friend Stream<StreamTraits, Kind> &operator<<(Stream<StreamTraits, Kind> &Ks, Ref<Data>& Buf) + { + Ks.m_Kind = Buf; + Ks.m_Stream->Out(Buf.Leak()); + return *Ks; + } + + Ref<StreamTraits> &AsStreamTraits() + { + return m_Stream; + } + + Ref<Kind>& AsType() + { + return m_Kind; + } + + private: + Ref<StreamTraits> m_Stream; + Ref<Kind> m_Kind; + + }; +} // namespace hCore diff --git a/NewKit/String.hpp b/NewKit/String.hpp new file mode 100644 index 00000000..a1a1ad44 --- /dev/null +++ b/NewKit/String.hpp @@ -0,0 +1,80 @@ +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/ErrorOr.hpp> +#include <NewKit/Defines.hpp> +#include <NewKit/Panic.hpp> + +namespace hCore +{ + class StringView final + { + public: + explicit StringView() = default; + + explicit StringView(Size Sz) : m_Sz(Sz) + { + MUST_PASS(Sz > 1); + m_Data = new Char[Sz]; + MUST_PASS(m_Data); + } + + ~StringView() + { + if (m_Data) + delete[] m_Data; + } + + StringView &operator=(const StringView &) = default; + StringView(const StringView &) = default; + + Char *Data(); + const Char *CData(); + Size Length() const; + + bool operator==(const Char *rhs) const; + bool operator!=(const Char *rhs) const; + + bool operator==(const StringView &rhs) const; + bool operator!=(const StringView &rhs) const; + + StringView &operator+=(const Char *rhs); + StringView &operator+=(const StringView &rhs); + + operator bool() + { + return m_Data; + } + + bool operator!() + { + return m_Data; + } + + private: + Char *m_Data{nullptr}; + Size m_Sz{0}; + Size m_Cur{0}; + + friend class StringBuilder; + + }; + + struct StringBuilder final + { + static ErrorOr<StringView> Construct(const Char *data); + static const char* FromInt(const char *fmt, int n); + static const char* FromBool(const char *fmt, bool n); + static const char* Format(const char *fmt, const char* from); + static bool Equals(const char *lhs, const char *rhs); + + }; +} // namespace hCore diff --git a/NewKit/Utils.hpp b/NewKit/Utils.hpp new file mode 100644 index 00000000..5acdb8de --- /dev/null +++ b/NewKit/Utils.hpp @@ -0,0 +1,32 @@ + +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/Defines.hpp> + +namespace hCore +{ + Int rt_copy_memory(const voidPtr src, voidPtr dst, Size len); + Int rt_move_memory(const voidPtr src, voidPtr dst, Size len); + voidPtr rt_set_memory(voidPtr dst, Char val, Size len); + void rt_zero_memory(voidPtr pointer, Size len); + Int string_compare(const Char *src, const Char *cmp, Size len); + const Char *alloc_string(const Char *text); + Size string_length(const Char *str); + Size string_length(const Char *str, SizeT _len); + Boolean to_str(Char *buf, Int limit, Int base); + Boolean is_newln(Char chr); + Boolean is_space(Char chr); + Int to_uppercase(Int c); + Int to_lower(Int c); + voidPtr string_in_string(const char* in, const char* needle); + char* string_from_char(char* str, const char chr); +} // namespace hCore diff --git a/NewKit/Variant.hpp b/NewKit/Variant.hpp new file mode 100644 index 00000000..87862723 --- /dev/null +++ b/NewKit/Variant.hpp @@ -0,0 +1,49 @@ +/* + * ======================================================== + * + * hCore + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/Defines.hpp> +#include <NewKit/String.hpp> + +namespace hCore +{ + class Variant final + { + public: + enum class VariantKind + { + kString, + kPointer, + kUndefined + }; + + public: + Variant() = delete; + + public: + Variant &operator=(const Variant &) = default; + Variant(const Variant &) = default; + + ~Variant() = default; + + public: + explicit Variant(StringView* stringView) : m_Ptr((voidPtr)stringView), m_Kind(VariantKind::kString) {} + explicit Variant(nullPtr) : m_Ptr(nullptr), m_Kind(VariantKind::kUndefined) {} + explicit Variant(voidPtr ptr) : m_Ptr(ptr), m_Kind(VariantKind::kPointer) {} + + public: + const Char* ToString(); + + private: + voidPtr m_Ptr{nullptr}; + VariantKind m_Kind{VariantKind::kUndefined}; + + }; +} // namespace hCore diff --git a/NewKit/compile_flags.txt b/NewKit/compile_flags.txt new file mode 100644 index 00000000..a37ae6bf --- /dev/null +++ b/NewKit/compile_flags.txt @@ -0,0 +1,5 @@ +-nostdlib +-ffreestanding +-std=c++20 +-I./ +-I../ |
