diff options
Diffstat (limited to 'dev/Kernel/NewKit')
25 files changed, 1684 insertions, 0 deletions
diff --git a/dev/Kernel/NewKit/Array.hxx b/dev/Kernel/NewKit/Array.hxx new file mode 100644 index 00000000..1a2f6e86 --- /dev/null +++ b/dev/Kernel/NewKit/Array.hxx @@ -0,0 +1,61 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ +#pragma once + +#include <KernelKit/DebugOutput.hxx> +#include <NewKit/ErrorOr.hxx> +#include <NewKit/Defines.hxx> + +namespace Kernel +{ + template <typename T, Size N> + class Array final + { + public: + explicit Array() = default; + ~Array() = default; + + Array& operator=(const Array&) = default; + Array(const Array&) = default; + + ErrorOr<T*> operator[](Size At) + { + if (At > N) + return {}; + + return ErrorOr<T*>(&fArray[At]); + } + + Boolean Empty() const + { + for (auto Val : fArray) + { + if (Val) + return false; + } + + return true; + } + + SizeT Count() const + { + return N; + } + + const T* CData() + { + return fArray; + } + + operator bool() + { + return !Empty(); + } + + private: + T fArray[N]; + }; +} // namespace Kernel diff --git a/dev/Kernel/NewKit/ArrayList.hxx b/dev/Kernel/NewKit/ArrayList.hxx new file mode 100644 index 00000000..03b0a360 --- /dev/null +++ b/dev/Kernel/NewKit/ArrayList.hxx @@ -0,0 +1,58 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.hxx> + +namespace Kernel +{ + template <typename T> + class ArrayList final + { + public: + explicit ArrayList(T* list) + : fList(reinterpret_cast<T>(list)) + { + } + + ~ArrayList() = default; + + ArrayList& operator=(const ArrayList&) = default; + ArrayList(const ArrayList&) = default; + + T* Data() + { + return fList; + } + + const T* CData() + { + return fList; + } + + T& operator[](int index) const + { + return fList[index]; + } + + operator bool() + { + return fList; + } + + private: + T* fList; + + friend class InitHelpers; + }; + + template <typename ValueType> + ArrayList<ValueType> make_list(ValueType val) + { + return ArrayList<ValueType>{val}; + } +} // namespace Kernel diff --git a/dev/Kernel/NewKit/Atom.hxx b/dev/Kernel/NewKit/Atom.hxx new file mode 100644 index 00000000..616179e9 --- /dev/null +++ b/dev/Kernel/NewKit/Atom.hxx @@ -0,0 +1,46 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ +#pragma once + +#include <NewKit/Defines.hxx> + +namespace Kernel +{ + 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 (fArrayOfAtoms & sz); + } + void operator|(Size sz) + { + fArrayOfAtoms |= 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 fArrayOfAtoms; + }; +} // namespace Kernel diff --git a/dev/Kernel/NewKit/Crc32.hxx b/dev/Kernel/NewKit/Crc32.hxx new file mode 100644 index 00000000..b050df24 --- /dev/null +++ b/dev/Kernel/NewKit/Crc32.hxx @@ -0,0 +1,22 @@ +/* + * ======================================================== + * + * Kernel Date Added: 13/02/2023 + * Copyright ZKA Technologies., all rights reserved. + * + * ======================================================== + */ + +#ifndef __CRC32_H__ +#define __CRC32_H__ + +#include <NewKit/Defines.hxx> + +#define kCrcCnt (256) + +namespace Kernel +{ + UInt ke_calculate_crc32(const Char* crc, UInt len) noexcept; +} // namespace Kernel + +#endif // !__CRC32_H__ diff --git a/dev/Kernel/NewKit/CxxAbi.hxx b/dev/Kernel/NewKit/CxxAbi.hxx new file mode 100644 index 00000000..f0b3cf51 --- /dev/null +++ b/dev/Kernel/NewKit/CxxAbi.hxx @@ -0,0 +1,28 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ +#pragma once + +#include <NewKit/Defines.hxx> + +#ifndef __NDK__ + +#define kDSOMaxObjects (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__ diff --git a/dev/Kernel/NewKit/Defines.hxx b/dev/Kernel/NewKit/Defines.hxx new file mode 100644 index 00000000..e0ad3bf6 --- /dev/null +++ b/dev/Kernel/NewKit/Defines.hxx @@ -0,0 +1,153 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Macros.hxx> + +#define NEWKIT_VERSION "1.01" + +#if !defined(_INC_NO_STDC_HEADERS) && defined(__GNUC__) +#include <CRTKit/__ndk_defines.hxx> +#endif + +#ifdef __has_feature +#if !__has_feature(cxx_nullptr) +#if !__has_nullptr +#error !!! You must at least have nullptr featured on your C++ compiler. !!! +#endif +#endif +#endif + +/// @brief The **newoskrnl** namespace where it's API resides. +namespace Kernel +{ + 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 = __INT64_TYPE__; + using Int64 = __INT64_TYPE__; + using ULong = __UINT64_TYPE__; + using UInt64 = __UINT64_TYPE__; + 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__; + + typedef UIntPtr* Ptr64; + typedef UInt32* Ptr32; + + using Utf8Char = char8_t; + using Utf16Char = char16_t; + using WideChar = wchar_t; + using Utf32Char = char32_t; + + typedef UInt64 PhysicalAddress; + typedef UInt64 VirtualAddress; + + using Void = void; + + using Lba = UInt64; + + enum class Endian : UChar + { + kEndianLittle, + kEndianBig, + kEndianMixed, + kCount + }; + + /// @brief Forward object. + /// @tparam Args the object type. + /// @param arg the object. + /// @return object's rvalue + template <typename Args> + inline Args&& forward(Args& arg) + { + return static_cast<Args&&>(arg); + } + + /// @brief Move object. + /// @tparam Args the object type. + /// @param arg the object. + /// @return object's rvalue + template <typename Args> + inline Args&& move(Args&& arg) + { + return static_cast<Args&&>(arg); + } + + /// @brief Encoder class + /// Used to cast A to B or B to A. + class Encoder final + { + public: + explicit Encoder() = default; + ~Encoder() = default; + + Encoder& operator=(const Encoder&) = default; + Encoder(const Encoder&) = default; + + public: + /// @brief Convert type to bytes. + /// @tparam T the type. + /// @param type (a1) the data. + /// @return a1 as Char* + template <typename T> + Char* AsBytes(T type) noexcept + { + return reinterpret_cast<Char*>(type); + } + + /// @brief Convert T class to Y class. + /// @tparam T the class type of type. + /// @tparam Y the result class. + /// @param type the class to cast. + /// @return the class as Y. + template <typename T, typename Y> + Y As(T type) noexcept + { + return type.template As<Y>(); + } + }; +} // namespace Kernel + +#define DEDUCE_ENDIAN(address, value) \ + (((reinterpret_cast<Kernel::Char*>(address)[0]) == (value)) \ + ? (Kernel::Endian::kEndianBig) \ + : (Kernel::Endian::kEndianLittle)) + +#define Yes true +#define No false + +#define VoidStar Kernel::VoidPtr + +#ifdef cInitObject +#undef cInitObject +#endif // ifdef cInitObject + +#define cInitObject(OBJ, TYPE, ...) TYPE OBJ = TYPE(__VA_ARGS__) diff --git a/dev/Kernel/NewKit/ErrorOr.hxx b/dev/Kernel/NewKit/ErrorOr.hxx new file mode 100644 index 00000000..ae2763b2 --- /dev/null +++ b/dev/Kernel/NewKit/ErrorOr.hxx @@ -0,0 +1,72 @@ +/* + * ======================================================== + * + * Kernel + * Copyright ZKA Technologies., all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/Defines.hxx> +#include <NewKit/Ref.hxx> + +namespace Kernel +{ + 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, true) + { + } + + 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 Kernel diff --git a/dev/Kernel/NewKit/Function.hxx b/dev/Kernel/NewKit/Function.hxx new file mode 100644 index 00000000..e54ff456 --- /dev/null +++ b/dev/Kernel/NewKit/Function.hxx @@ -0,0 +1,53 @@ +#ifndef _INC_FUNCTION_HPP__ +#define _INC_FUNCTION_HPP__ + +#include <NewKit/Defines.hxx> + +namespace Kernel +{ + template <typename T, typename... Args> + class Function final + { + public: + Function() = default; + + public: + explicit Function(T (*Fn)(Args... args)) + : fFn(Fn) + { + } + + ~Function() = default; + + Function& operator=(const Function&) = default; + Function(const Function&) = default; + + template <typename... XArgs> + T operator()(Args... args) + { + return fFn(args...); + } + + template <typename... XArgs> + T Call(Args... args) + { + return fFn(args...); + } + + operator bool() + { + return fFn; + } + + bool operator!() + { + return !fFn; + } + + private: + T(*fFn) + (Args... args); + }; +} // namespace Kernel + +#endif // !_INC_FUNCTION_HPP__ diff --git a/dev/Kernel/NewKit/Json.hxx b/dev/Kernel/NewKit/Json.hxx new file mode 100644 index 00000000..74566942 --- /dev/null +++ b/dev/Kernel/NewKit/Json.hxx @@ -0,0 +1,134 @@ + +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +// last-rev: 30/01/24 + +#include <CompilerKit/CompilerKit.hxx> +#include <NewKit/Defines.hxx> +#include <NewKit/Stream.hxx> +#include <NewKit/String.hxx> +#include <NewKit/Utils.hxx> + +#define cMaxJsonPath 4096 +#define cJSONLen 32 +#define cJSONNull "null" + +namespace Kernel +{ + /// @brief Json value class + class JsonType final + { + public: + explicit JsonType() + { + auto len = cJSONLen; + StringView key = StringView(len); + key += cJSONNull; + + this->AsKey() = key; + this->AsValue() = key; + } + + explicit JsonType(SizeT lhsLen, SizeT rhsLen) + : fKey(lhsLen), fValue(rhsLen) + { + } + + ~JsonType() = default; + + NEWOS_COPY_DEFAULT(JsonType); + + const Bool& IsUndefined() { return fUndefined; } + + private: + Bool fUndefined; // is this instance undefined? + StringView fKey; + StringView fValue; + + public: + /// @brief returns the key of the json + /// @return the key as string view. + StringView& AsKey() + { + return fKey; + } + + /// @brief returns the value of the json. + /// @return the key as string view. + StringView& AsValue() + { + return fValue; + } + + static JsonType kNull; + }; + + /// @brief Json stream reader helper. + struct JsonStreamReader final + { + STATIC JsonType In(const Char* full_array) + { + if (full_array[0] != '{') + return JsonType::kNull; + + SizeT len = rt_string_len(full_array); + Boolean probe_value = false; + + SizeT key_len = 0; + SizeT value_len = 0; + + JsonType type(cMaxJsonPath, cMaxJsonPath); + + for (SizeT i = 1; i < len; ++i) + { + if (full_array[i] == '\r' || + full_array[i] == '\n') + continue; + + if (probe_value) + { + if (full_array[i] == '}' || + full_array[i] == ',') + { + probe_value = false; + + ++value_len; + } + else + { + type.AsValue().Data()[value_len] = full_array[i]; + + ++value_len; + } + } + else + { + if (full_array[i] == ':') + { + probe_value = true; + type.AsKey().Data()[key_len] = 0; + ++key_len; + } + else + { + type.AsKey().Data()[key_len] = full_array[i]; + + ++key_len; + } + } + } + + type.AsValue().Data()[value_len] = 0; + + return type; + } + }; + + using JsonStream = Stream<JsonStreamReader, JsonType>; +} // namespace Kernel diff --git a/dev/Kernel/NewKit/KernelCheck.hxx b/dev/Kernel/NewKit/KernelCheck.hxx new file mode 100644 index 00000000..78d7506f --- /dev/null +++ b/dev/Kernel/NewKit/KernelCheck.hxx @@ -0,0 +1,61 @@ + +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.hxx> + +namespace Kernel +{ + void ke_runtime_check(bool bExpression, const char* file, const char* line); +} + +#define MUST_PASS_COMPILER(EXPR, MSG) static_assert(EXPR, MSG) +#define __MUST_PASS(EXPR, FILE, LINE) \ + Kernel::ke_runtime_check(EXPR, FILE, STRINGIFY(LINE)) +#define MUST_PASS(EXPR) __MUST_PASS(EXPR, __FILE__, __LINE__) +#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_HANDSHAKE, + RUNTIME_CHECK_ACPI, + RUNTIME_CHECK_INVALID_PRIVILEGE, + RUNTIME_CHECK_PROCESS, + RUNTIME_CHECK_BAD_BEHAVIOR, + RUNTIME_CHECK_BOOTSTRAP, + RUNTIME_CHECK_UNEXCPECTED, + RUNTIME_CHECK_COUNT, +}; + +namespace Kernel +{ + /// @brief Dumping factory class. + class RecoveryFactory final + { + public: + STATIC Void Recover() noexcept; + }; + + void ke_stop(const Int& id); +} // namespace Kernel + +#ifdef TRY +#undef TRY +#endif + +#define TRY(FN) \ + if (!FN()) \ + { \ + MUST_PASS(false); \ + } diff --git a/dev/Kernel/NewKit/Macros.hxx b/dev/Kernel/NewKit/Macros.hxx new file mode 100644 index 00000000..a17d91fb --- /dev/null +++ b/dev/Kernel/NewKit/Macros.hxx @@ -0,0 +1,114 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +#ifndef KIB +#define KIB(X) ((X) / 1024) +#endif + +#ifndef kib_cast +#define kib_cast(X) ((X) * 1024) +#endif + +#ifndef MIB +#define MIB(X) ((UInt64)KIB(X) / 1024) +#endif + +#ifndef mib_cast +#define mib_cast(X) ((UInt64)kib_cast(X) * 1024) +#endif + +#ifndef GIB +#define GIB(X) ((UInt64)MIB(X) / 1024) +#endif + +#ifndef gib_cast +#define gib_cast(X) ((UInt64)mib_cast(X) * 1024) +#endif + +#ifndef TIB +#define TIB(X) ((UInt64)GIB(X) / 1024) +#endif + +#ifndef tib_cast +#define tib_cast(X) ((UInt64)gib_cast(X) * 1024) +#endif + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(a) \ + (((sizeof(a) / sizeof(*(a))) / \ + (static_cast<Kernel::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 __MAHROUSS_VER__ +#define __MAHROUSS_VER__ (2024) +#endif // !__MAHROUSS_VER__ + +#ifndef EXTERN_C +#define EXTERN_C 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) inline constexpr const char* e##NAME = VAL +#endif + +#ifndef END_STRING_ENUM +#define END_STRING_ENUM() } +#endif + +#ifndef ALLOCA +#define ALLOCA(Sz) __builtin_alloca(Sz) +#endif // #ifndef ALLOCA + +#ifndef CANT_REACH +#define CANT_REACH() __builtin_unreachable() +#endif + +#define kBadPtr 0xFBFBFBFBFBFBFBFB +#define kMaxAddr 0xFFFFFFFFFFFFFFFF +#define kPathLen 255 + +#define PACKED ATTRIBUTE(packed) +#define NO_EXEC ATTRIBUTE(noexec) + +#define EXTERN extern +#define STATIC static + +#define CONST const + +#define STRINGIFY(X) #X +#define NEWOS_UNUSED(X) ((Kernel::Void)X) + +#ifndef RGB +#define RGB(R, G, B) (Kernel::UInt32)(R | G << 0x8 | B << 0x10) +#endif // !RGB diff --git a/dev/Kernel/NewKit/MutableArray.hxx b/dev/Kernel/NewKit/MutableArray.hxx new file mode 100644 index 00000000..0015f9a9 --- /dev/null +++ b/dev/Kernel/NewKit/MutableArray.hxx @@ -0,0 +1,239 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ +#pragma once + +#include <CompilerKit/CompilerKit.hxx> +#include <NewKit/Array.hxx> +#include <NewKit/Defines.hxx> + +#define TRY_FIND_NODE(NAME, NODE) \ + auto* NAME = NODE; \ + while (NAME) \ + { \ + if (NAME->fIndex == Index) \ + return NAME->fVal; \ + NAME = NAME->fNext; \ + } + +#define TRY_FIND_NODE2(NAME, NODE) \ + auto* NAME = NODE; \ + while (NAME) \ + { \ + if (NAME->fIndex == Index) \ + return Ref<T>{NAME->fVal}; \ + NAME = NAME->fNext; \ + } + +#define TRY_REMOVE_NODE(NODE) \ + if (NODE && NODE->fIndex == Index) \ + { \ + NODE->fUsed = false; \ + NODE->fIndex = 0; \ + \ + return true; \ + } + +// FIXME: this is a shitty algorithm, which is consumer hungry. +// Remove and occurences of that, and remove that class. +namespace Kernel +{ + template <typename T> + class MutableArray; + + template <typename T, T _PlaceHolderValue> + class NullableMutableArray; + + template <typename T> + class MutableLinkedList + { + public: + T fVal; + SizeT fIndex{0}; + Boolean fUsed{false}; + + MutableLinkedList* fPrev{nullptr}; + MutableLinkedList* fNext{nullptr}; + }; + + template <typename T, T _PlaceHolderValue> + class NullableMutableArray + { + public: + // explicit this. + explicit NullableMutableArray() + : fFirstNode(new MutableLinkedList<T>()) + { + } + + /* + * We free all the nodes allocated by the array + * and store the next one inside "NextIt" + */ + + virtual ~NullableMutableArray() + { + auto* It = fFirstNode; + MutableLinkedList<T>* NextIt = nullptr; + + while (It) + { + NextIt = It->fNext; + 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, fFirstNode); + TRY_FIND_NODE(last, fLastNode); + + return _PlaceHolderValue; + } + + SizeT Count() const + { + return fNodeCount; + } + + public: + Boolean Remove(const SizeT& Index) + { + TRY_REMOVE_NODE(fFirstNode); + TRY_REMOVE_NODE(fLastNode); + + return false; + } + + Boolean Add(const T val) + { + auto* iterationNode = fFirstNode; + MUST_PASS(iterationNode); + + while (iterationNode) + { + if (!iterationNode->fUsed) + { + iterationNode->fVal = val; + iterationNode->fIndex = 0; + + iterationNode->fUsed = true; + + ++fNodeCount; + + return true; + } + + iterationNode = iterationNode->fNext; + } + + return false; + } + + private: + /* Avoid useless lookups */ + MutableLinkedList<T>* fLastNode{nullptr}; + MutableLinkedList<T>* fFirstNode{nullptr}; + + /* Number of nodes inside of this dynamic array. */ + Kernel::SizeT fNodeCount{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; + + NEWOS_COPY_DEFAULT(MutableArray) + + public: + Boolean Add(const T val) + { + auto* iterationNode = fFirstNode; + + if (!iterationNode) + { + fFirstNode = new MutableLinkedList<T>(); + iterationNode = fFirstNode; + } + + MUST_PASS(iterationNode); + + while (iterationNode) + { + if (!iterationNode->fUsed) + { + iterationNode->fVal = val; + iterationNode->fIndex = 0; + + iterationNode->fUsed = true; + + ++fNodeCount; + + return true; + } + + iterationNode = iterationNode->fNext; + } + + return false; + } + + public: + Ref<T> operator[](const SizeT& Index) const + { + TRY_FIND_NODE2(first, fFirstNode); + TRY_FIND_NODE2(last, fLastNode); + + return {}; + } + + SizeT Count() const + { + return fNodeCount; + } + + bool Contains(T& value) noexcept + { + MutableLinkedList<T>* first = fFirstNode; + + while (first) + { + if (first->fVal == value && first->fUsed) + return true; + + first = first->fNext; + } + + return false; + } + + private: + /* Avoid useless lookups */ + MutableLinkedList<T>* fLastNode{nullptr}; + MutableLinkedList<T>* fFirstNode{nullptr}; + + /* Number of nodes inside of this dynamic array. */ + Kernel::SizeT fNodeCount{0}; + }; +} // namespace Kernel diff --git a/dev/Kernel/NewKit/New.hxx b/dev/Kernel/NewKit/New.hxx new file mode 100644 index 00000000..8605b9c3 --- /dev/null +++ b/dev/Kernel/NewKit/New.hxx @@ -0,0 +1,18 @@ + +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ +#pragma once + +#include <KernelKit/Heap.hxx> + +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/dev/Kernel/NewKit/NewKit.hxx b/dev/Kernel/NewKit/NewKit.hxx new file mode 100644 index 00000000..e7766bde --- /dev/null +++ b/dev/Kernel/NewKit/NewKit.hxx @@ -0,0 +1,22 @@ + +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Array.hxx> +#include <NewKit/ArrayList.hxx> +#include <NewKit/ErrorOr.hxx> +#include <NewKit/Json.hxx> +#include <NewKit/KernelCheck.hxx> +#include <KernelKit/LockDelegate.hxx> +#include <NewKit/MutableArray.hxx> +#include <NewKit/New.hxx> +#include <NewKit/OwnPtr.hxx> +#include <NewKit/Ref.hxx> +#include <NewKit/Stream.hxx> +#include <KernelKit/ProcessHeap.hxx> +#include <NewKit/Utils.hxx> diff --git a/dev/Kernel/NewKit/OwnPtr.hxx b/dev/Kernel/NewKit/OwnPtr.hxx new file mode 100644 index 00000000..6e42b33f --- /dev/null +++ b/dev/Kernel/NewKit/OwnPtr.hxx @@ -0,0 +1,94 @@ + +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.hxx> +#include <NewKit/KernelCheck.hxx> +#include <NewKit/Ref.hxx> + +namespace Kernel +{ + 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) + { + if (fCls) + { + return false; + } + + fCls = new T(arg...); + return fCls; + } + + void Delete() + { + if (fCls) + delete fCls; + + fCls = nullptr; + } + + T* operator->() const + { + return fCls; + }; + T* Raw() + { + return fCls; + } + + Ref<T> AsRef() + { + return Ref<T>(fCls); + } + + operator bool() + { + return fCls; + } + bool operator!() + { + return !fCls; + } + + private: + T* fCls; + }; + + 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 Kernel diff --git a/dev/Kernel/NewKit/PageAllocator.hxx b/dev/Kernel/NewKit/PageAllocator.hxx new file mode 100644 index 00000000..b485e722 --- /dev/null +++ b/dev/Kernel/NewKit/PageAllocator.hxx @@ -0,0 +1,20 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.hxx> +#include <NewKit/PageManager.hxx> + +namespace Kernel +{ + namespace Detail + { + VoidPtr create_page_wrapper(Boolean rw, Boolean user, SizeT pageSz); + void exec_disable(UIntPtr addr); + bool page_disable(UIntPtr addr); + } // namespace Detail +} // namespace Kernel diff --git a/dev/Kernel/NewKit/PageManager.hxx b/dev/Kernel/NewKit/PageManager.hxx new file mode 100644 index 00000000..745395ec --- /dev/null +++ b/dev/Kernel/NewKit/PageManager.hxx @@ -0,0 +1,81 @@ +// a way to create and find our pages. +// I'm thinking about a separate way of getting a paged area. + +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.hxx> +#include <NewKit/PageAllocator.hxx> +#include <NewKit/Ref.hxx> + +#ifndef kBadAddress +#define kBadAddress (0) +#endif // #ifndef kBadAddress + +namespace Kernel +{ + 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: + const UIntPtr VirtualAddress(); + + void NoExecute(const bool enable = false); + const bool& NoExecute(); + + bool Reclaim(); + bool Shareable(); + bool Present(); + bool Access(); + + private: + Boolean fRw; + Boolean fUser; + Boolean fExecDisable; + UIntPtr fVirtAddr; + Boolean fCache; + Boolean fShareable; + Boolean fWt; + Boolean fPresent; + Boolean fAccessed; + + 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, SizeT Sz); + bool Free(Ref<PTEWrapper*>& wrapper); + + private: + void FlushTLB(UIntPtr VirtAddr); + + private: + friend PTEWrapper; + friend class Pmm; + }; +} // namespace Kernel diff --git a/dev/Kernel/NewKit/Pair.hxx b/dev/Kernel/NewKit/Pair.hxx new file mode 100644 index 00000000..e7ca064a --- /dev/null +++ b/dev/Kernel/NewKit/Pair.hxx @@ -0,0 +1,14 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.hxx> + +namespace Kernel +{ + +} // namespace Kernel diff --git a/dev/Kernel/NewKit/Pmm.hxx b/dev/Kernel/NewKit/Pmm.hxx new file mode 100644 index 00000000..111b3044 --- /dev/null +++ b/dev/Kernel/NewKit/Pmm.hxx @@ -0,0 +1,44 @@ + +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/PageManager.hxx> +#include <NewKit/Ref.hxx> + +namespace Kernel +{ + class Pmm; + class PTEWrapper; + + class Pmm final + { + public: + explicit Pmm(); + ~Pmm(); + + Pmm& operator=(const Pmm&) = delete; + Pmm(const Pmm&) = default; + + 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); + + /// @brief Get the page manager of this. + Ref<PageManager>& Leak() + { + return fPageManager; + } + + private: + Ref<PageManager> fPageManager; + }; +} // namespace Kernel diff --git a/dev/Kernel/NewKit/Ref.hxx b/dev/Kernel/NewKit/Ref.hxx new file mode 100644 index 00000000..14d66f0a --- /dev/null +++ b/dev/Kernel/NewKit/Ref.hxx @@ -0,0 +1,106 @@ + +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#ifndef _NEWKIT_REF_HPP_ +#define _NEWKIT_REF_HPP_ + +#include <NewKit/Defines.hxx> +#include <NewKit/KernelCheck.hxx> + +namespace Kernel +{ + template <typename T> + class Ref final + { + public: + Ref() = default; + + ~Ref() + { + if (fStrong) + { + fClass = nullptr; + } + } + + public: + Ref(T cls, const bool& strong = false) + : fClass(&cls), fStrong(strong) + { + } + + Ref& operator=(T ref) + { + *fClass = ref; + return *this; + } + + public: + T operator->() const + { + return *fClass; + } + + T& Leak() noexcept + { + return *fClass; + } + + T& TryLeak() const noexcept + { + MUST_PASS(*fClass); + return *fClass; + } + + T operator*() + { + return *fClass; + } + + bool IsStrong() const + { + return fStrong; + } + + operator bool() noexcept + { + return fStrong; + } + + private: + T* fClass; + Bool fStrong{false}; + }; + + template <typename T> + class NonNullRef final + { + public: + NonNullRef() = delete; + NonNullRef(nullPtr) = delete; + + NonNullRef(T* ref) + : fRef(ref, true) + { + MUST_PASS(ref != nullptr); + } + + Ref<T>& operator->() + { + MUST_PASS(fRef); + return fRef; + } + + NonNullRef& operator=(const NonNullRef<T>& ref) = delete; + NonNullRef(const NonNullRef<T>& ref) = default; + + private: + Ref<T> fRef{nullptr}; + }; +} // namespace Kernel + +#endif // ifndef _NEWKIT_REF_HPP_ diff --git a/dev/Kernel/NewKit/Stream.hxx b/dev/Kernel/NewKit/Stream.hxx new file mode 100644 index 00000000..e2f63b17 --- /dev/null +++ b/dev/Kernel/NewKit/Stream.hxx @@ -0,0 +1,58 @@ + +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.hxx> +#include <NewKit/Ref.hxx> + +namespace Kernel +{ + template <typename StreamTrait, typename Kind> + class Stream final + { + public: + explicit Stream(Ref<Stream> ref) + : fStream(ref) + { + } + + ~Stream() = default; + + Stream& operator=(const Stream&) = default; + Stream(const Stream&) = default; + + template <typename Data> + friend Stream<StreamTrait, Kind>& operator>>(Stream<StreamTrait, Kind>& Ks, Ref<Data>& Buf) + { + Ks.fKind = Ks.fStream->In(Buf); + return *Ks; + } + + template <typename Data> + friend Stream<StreamTrait, Kind>& operator<<(Stream<StreamTrait, Kind>& Ks, Ref<Data>& Buf) + { + Ks.fKind = Buf; + Ks.fStream->Out(Buf.Leak()); + return *Ks; + } + + Ref<StreamTrait>& AsStreamTrait() + { + return fStream; + } + + Ref<Kind>& AsType() + { + return fKind; + } + + private: + Ref<StreamTrait> fStream; + Ref<Kind> fKind; + }; +} // namespace Kernel diff --git a/dev/Kernel/NewKit/String.hxx b/dev/Kernel/NewKit/String.hxx new file mode 100644 index 00000000..2f3d2096 --- /dev/null +++ b/dev/Kernel/NewKit/String.hxx @@ -0,0 +1,87 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.hxx> +#include <NewKit/ErrorOr.hxx> +#include <NewKit/Utils.hxx> +#include <NewKit/KernelCheck.hxx> + +namespace Kernel +{ + class StringView final + { + public: + explicit StringView() + { + fSz = 4096; + + fData = new Char[fSz]; + MUST_PASS(fData); + + rt_set_memory(fData, 0, fSz); + } + + explicit StringView(Size Sz) + : fSz(Sz) + { + MUST_PASS(Sz > 1); + fData = new Char[Sz]; + MUST_PASS(fData); + + rt_set_memory(fData, 0, Sz); + } + + ~StringView() + { + if (fData) + delete[] fData; + } + + StringView& operator=(const StringView&) = default; + StringView(const StringView&) = default; + + Char* Data(); + const Char* CData() const; + 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 fData; + } + + bool operator!() + { + return fData; + } + + private: + Char* fData{nullptr}; + Size fSz{0}; + Size fCur{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 Kernel diff --git a/dev/Kernel/NewKit/Utils.hxx b/dev/Kernel/NewKit/Utils.hxx new file mode 100644 index 00000000..a7213b5a --- /dev/null +++ b/dev/Kernel/NewKit/Utils.hxx @@ -0,0 +1,29 @@ + +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.hxx> + +namespace Kernel +{ + 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 rt_string_cmp(const Char* src, const Char* cmp, Size len); + const Char* alloc_string(const Char* text); + Size rt_string_len(const Char* str); + Size rt_string_len(const Char* str, SizeT _len); + Boolean rt_to_string(Char* buf, Int limit, Int base); + Boolean is_newln(Char chr); + Boolean is_space(Char chr); + Int rt_to_uppercase(Int c); + Int rt_to_lower(Int c); + voidPtr rt_string_in_string(const char* in, const char* needle); + char* rt_string_has_char(char* str, const char chr); +} // namespace Kernel diff --git a/dev/Kernel/NewKit/Variant.hxx b/dev/Kernel/NewKit/Variant.hxx new file mode 100644 index 00000000..b60d2824 --- /dev/null +++ b/dev/Kernel/NewKit/Variant.hxx @@ -0,0 +1,64 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.hxx> +#include <NewKit/String.hxx> +#include <NewKit/Json.hxx> + +namespace Kernel +{ + class Variant final + { + public: + enum class VariantKind + { + kString, + kBlob, + kNull, + kJson, + }; + + public: + explicit Variant() = delete; + + public: + Variant& operator=(const Variant&) = default; + Variant(const Variant&) = default; + + ~Variant() = default; + + public: + explicit Variant(StringView* stringView) + : fPtr((voidPtr)stringView), fKind(VariantKind::kString) + { + } + + explicit Variant(JsonType* json) + : fPtr((voidPtr)json), fKind(VariantKind::kJson) + { + } + + explicit Variant(nullPtr) + : fPtr(nullptr), fKind(VariantKind::kNull) + { + } + + explicit Variant(voidPtr ptr) + : fPtr(ptr), fKind(VariantKind::kBlob) + { + } + + public: + const Char* ToString(); + VoidPtr Leak(); + + private: + voidPtr fPtr{nullptr}; + VariantKind fKind{VariantKind::kNull}; + }; +} // namespace Kernel diff --git a/dev/Kernel/NewKit/compile_flags.txt b/dev/Kernel/NewKit/compile_flags.txt new file mode 100644 index 00000000..14c5bc51 --- /dev/null +++ b/dev/Kernel/NewKit/compile_flags.txt @@ -0,0 +1,6 @@ +-nostdlib +-ffreestanding +-std=c++20 +-I./ +-I../ +-I../../../ |
