diff options
| author | Amlal <amlal.elmahrouss@icloud.com> | 2024-10-28 07:01:58 +0100 |
|---|---|---|
| committer | Amlal <amlal.elmahrouss@icloud.com> | 2024-10-28 07:01:58 +0100 |
| commit | e0024d9ea688ee91a77abc0e28c5ea24b13ca67d (patch) | |
| tree | a4e29bd919cbeccf2689e81a5d52bfc02f2a8b77 /dev/ZKAKit/NewKit | |
| parent | 36a3600ff7fc65a63b7386b7a680dbe8e647bd8f (diff) | |
IMP: Refactor whole source code to make it even.
- That is because previously the source was both in lowercase and lettercase.
Signed-off-by: Amlal <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'dev/ZKAKit/NewKit')
| -rw-r--r-- | dev/ZKAKit/NewKit/Array.h | 74 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/ArrayList.h | 58 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/Atom.h | 46 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/Crc32.h | 23 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/CxxAbi.h | 28 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/Defines.h | 190 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/ErrorOr.h | 72 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/Function.h | 53 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/Json.h | 151 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/Macros.h | 137 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/MutableArray.h | 239 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/New.h | 18 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/NewKit.h | 20 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/OwnPtr.h | 94 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/PageMgr.h | 81 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/Pair.h | 14 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/Pmm.h | 44 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/Ref.h | 103 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/Stop.h | 71 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/Stream.h | 58 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/String.h | 91 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/Utils.h | 29 | ||||
| -rw-r--r-- | dev/ZKAKit/NewKit/Variant.h | 70 |
23 files changed, 1764 insertions, 0 deletions
diff --git a/dev/ZKAKit/NewKit/Array.h b/dev/ZKAKit/NewKit/Array.h new file mode 100644 index 00000000..31a6a6d0 --- /dev/null +++ b/dev/ZKAKit/NewKit/Array.h @@ -0,0 +1,74 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include <KernelKit/DebugOutput.h> +#include <NewKit/ErrorOr.h> +#include <NewKit/Defines.h> + +namespace Kernel +{ + template <typename T, SizeT N> + class Array final + { + public: + explicit Array() + { + for (SizeT i = 0; i < N; i++) + { + if (!fArray[i]) + fArray[i] = T(); + } + } + + ~Array() = default; + + Array& operator=(const Array&) = default; + Array(const Array&) = default; + + T& operator[](const SizeT& At) + { + return fArray[At]; + } + + Boolean Empty() const + { + return No; + } + + const SizeT Capacity() + { + return N; + } + + const SizeT Count() + { + SizeT count = 0; + + for (SizeT i = 0; i < N; i++) + { + if (fArray[i]) + ++count; + } + + return count; + } + + const T* CData() + { + return fArray; + } + + operator bool() + { + return !Empty(); + } + + private: + T fArray[N]; + }; +} // namespace Kernel diff --git a/dev/ZKAKit/NewKit/ArrayList.h b/dev/ZKAKit/NewKit/ArrayList.h new file mode 100644 index 00000000..12949b64 --- /dev/null +++ b/dev/ZKAKit/NewKit/ArrayList.h @@ -0,0 +1,58 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.h> + +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/ZKAKit/NewKit/Atom.h b/dev/ZKAKit/NewKit/Atom.h new file mode 100644 index 00000000..955d40fb --- /dev/null +++ b/dev/ZKAKit/NewKit/Atom.h @@ -0,0 +1,46 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ +#pragma once + +#include <NewKit/Defines.h> + +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/ZKAKit/NewKit/Crc32.h b/dev/ZKAKit/NewKit/Crc32.h new file mode 100644 index 00000000..cacb4105 --- /dev/null +++ b/dev/ZKAKit/NewKit/Crc32.h @@ -0,0 +1,23 @@ +/* + * ======================================================== + * + * ZKA + * Date Added: 13/02/2023 + * Copyright ZKA Web Services Co., all rights reserved. + * + * ======================================================== + */ + +#ifndef __CRC32_H__ +#define __CRC32_H__ + +#include <NewKit/Defines.h> + +#define kCrcCnt (256) + +namespace Kernel +{ + UInt ke_calculate_crc32(const Char* crc, UInt len) noexcept; +} // namespace Kernel + +#endif // !__CRC32_H__ diff --git a/dev/ZKAKit/NewKit/CxxAbi.h b/dev/ZKAKit/NewKit/CxxAbi.h new file mode 100644 index 00000000..c6408af1 --- /dev/null +++ b/dev/ZKAKit/NewKit/CxxAbi.h @@ -0,0 +1,28 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ +#pragma once + +#include <NewKit/Defines.h> + +#ifndef __TOOLCHAINKIT__ + +#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/ZKAKit/NewKit/Defines.h b/dev/ZKAKit/NewKit/Defines.h new file mode 100644 index 00000000..83cc7338 --- /dev/null +++ b/dev/ZKAKit/NewKit/Defines.h @@ -0,0 +1,190 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Macros.h> + +#define NEWKIT_VERSION "1.1.0" +#define NEWKIT_VERSION_CB 0x01100 + +#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 **Kernel** 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__; + + using SInt16 = Int16; + using SInt32 = Int32; + using SInt64 = Int64; + + typedef UIntPtr* Ptr64; + typedef UInt32* Ptr32; + + using Utf8Char = char8_t; + using Utf16Char = char16_t; + using WideChar = wchar_t; + using Utf32Char = char32_t; + + typedef UInt32 PhysicalAddressKind; + typedef UIntPtr VirtualAddressKind; + + using Void = void; + + using Lba = UInt64; + + enum class Endian : UInt8 + { + kEndianInvalid, + kEndianBig, + kEndianLittle, + 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 Encoding interface, used as a proxy to convert T to Char* + /// Used to cast A to B or B to A. + class IEncoderObject + { + public: + explicit IEncoderObject() = default; + virtual ~IEncoderObject() = default; + + IEncoderObject& operator=(const IEncoderObject&) = default; + IEncoderObject(const IEncoderObject&) = 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 nullptr; + } + + /// @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 + { + if (type.IsSerializable()) + { + return reinterpret_cast<Char*>(type); + } + + return type.template As<Y>(); + } + }; + + /// \brief Scheduler interface, represents a scheduler object. + /// @note This is used to schedule tasks, such as threads, drivers, user threads, etc. + class ISchedulerObject + { + public: + explicit ISchedulerObject() = default; + virtual ~ISchedulerObject() = default; + + ISchedulerObject& operator=(const ISchedulerObject&) = default; + ISchedulerObject(const ISchedulerObject&) = default; + + /// @brief Is this object only accepting user tasks? + virtual const Bool IsUser() + { + return false; + } + + /// @brief Is this object only accepting kernel tasks? + virtual const Bool IsKernel() + { + return false; + } + + /// @brief Is this object offloading to another CPU? + virtual const Bool HasMP() + { + return false; + } + }; +} // namespace Kernel + +#define cDeduceEndian(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/ZKAKit/NewKit/ErrorOr.h b/dev/ZKAKit/NewKit/ErrorOr.h new file mode 100644 index 00000000..eae5bcd1 --- /dev/null +++ b/dev/ZKAKit/NewKit/ErrorOr.h @@ -0,0 +1,72 @@ +/* + * ======================================================== + * + * ZKA + * Copyright ZKA Web Services Co., all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include <NewKit/Defines.h> +#include <NewKit/Ref.h> + +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) + { + } + + 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/ZKAKit/NewKit/Function.h b/dev/ZKAKit/NewKit/Function.h new file mode 100644 index 00000000..9fa218af --- /dev/null +++ b/dev/ZKAKit/NewKit/Function.h @@ -0,0 +1,53 @@ +#ifndef _INC_FUNCTION_H__ +#define _INC_FUNCTION_H__ + +#include <NewKit/Defines.h> + +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_H__ diff --git a/dev/ZKAKit/NewKit/Json.h b/dev/ZKAKit/NewKit/Json.h new file mode 100644 index 00000000..68210e00 --- /dev/null +++ b/dev/ZKAKit/NewKit/Json.h @@ -0,0 +1,151 @@ + +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +// last-rev: 30/01/24 + +#include <CompilerKit/CompilerKit.h> +#include <NewKit/Defines.h> +#include <NewKit/Stream.h> +#include <NewKit/String.h> +#include <NewKit/Utils.h> + +#define cMaxJsonPath 4096 +#define cJSONLen 32 +#define cJSONNull "null" + +namespace Kernel +{ + /// @brief Json class + class JSON final + { + public: + explicit JSON() + { + auto len = cJSONLen; + StringView key = StringView(len); + key += cJSONNull; + + this->AsKey() = key; + this->AsValue() = key; + } + + explicit JSON(SizeT lhsLen, SizeT rhsLen) + : fKey(lhsLen), fValue(rhsLen) + { + } + + ~JSON() = default; + + ZKA_COPY_DEFAULT(JSON); + + 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 JSON kNull; + }; + + /// @brief Json stream reader helper. + struct JsonStreamReader final + { + STATIC JSON In(const Char* full_array) + { + auto start_val = '{'; + auto end_val = '}'; + Boolean probe_value = false; + + if (full_array[0] != start_val) + { + if (full_array[0] != '[') + return JSON::kNull; + + start_val = '['; + end_val = ']'; + + probe_value = true; + } + + SizeT len = rt_string_len(full_array); + + SizeT key_len = 0; + SizeT value_len = 0; + + JSON 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] == end_val || + full_array[i] == ',') + { + probe_value = false; + + ++value_len; + } + else + { + type.AsValue().Data()[value_len] = full_array[i]; + + ++value_len; + } + } + else + { + if (start_val == '[') + continue; + + 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, JSON>; +} // namespace Kernel diff --git a/dev/ZKAKit/NewKit/Macros.h b/dev/ZKAKit/NewKit/Macros.h new file mode 100644 index 00000000..848ec010 --- /dev/null +++ b/dev/ZKAKit/NewKit/Macros.h @@ -0,0 +1,137 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#ifndef KIB +#define KIB(X) (Kernel::UInt64)((X) / 1024) +#endif + +#ifndef kib_cast +#define kib_cast(X) (Kernel::UInt64)((X) * 1024) +#endif + +#ifndef MIB +#define MIB(X) (Kernel::UInt64)((Kernel::UInt64)KIB(X) / 1024) +#endif + +#ifndef mib_cast +#define mib_cast(X) (Kernel::UInt64)((Kernel::UInt64)kib_cast(X) * 1024) +#endif + +#ifndef GIB +#define GIB(X) (Kernel::UInt64)((Kernel::UInt64)MIB(X) / 1024) +#endif + +#ifndef gib_cast +#define gib_cast(X) (Kernel::UInt64)((Kernel::UInt64)mib_cast(X) * 1024) +#endif + +#ifndef TIB +#define TIB(X) (Kernel::UInt64)((Kernel::UInt64)GIB(X) / 1024) +#endif + +#ifndef tib_cast +#define tib_cast(X) ((Kernel::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 __ZKA_VER__ +#define __ZKA_VER__ (2024) +#endif // !__ZKA_VER__ + +#ifndef EXTERN +#define EXTERN extern +#endif + +#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 cAlloca +#define cAlloca(Sz) __builtin_alloca(Sz) +#endif // #ifndef cAlloca + +#ifndef CANT_REACH +#define CANT_REACH() __builtin_unreachable() +#endif + +#define kInvalidAddress 0xFBFBFBFBFBFBFBFB +#define kBadAddress 0x0000000000000000 +#define kMaxAddr 0xFFFFFFFFFFFFFFFF +#define kPathLen 0x100 + +#define PACKED ATTRIBUTE(packed) +#define NO_EXEC ATTRIBUTE(noexec) + +#define EXTERN extern +#define STATIC static + +#define CONST const + +#define STRINGIFY(X) #X +#define ZKA_UNUSED(X) ((Kernel::Void)X) + +#ifndef RGB +#define RGB(R, G, B) (Kernel::UInt32)(R | G << 0x8 | B << 0x10) +#endif // !RGB + +/// @brief The system page file. +#define kPageSys "\\System\\syspage.sys" + +/// @brief The main system driver. +#define kStartupSys "\\Boot\\startup.sys" + +/// @brief The main font file. +#define kUrbanistTTF "\\Fonts\\urbanist.ttf" + +/// @brief License file +#define kEulaTxt "\\Misc\\EULA.txt" + +/// @brief The main kernel file. +#define kStartupWav "\\Rsrc\\startup.wav" + +/// @brief The main system loader. +#define kUserName "ZKA AUTHORITY\\KERNEL" diff --git a/dev/ZKAKit/NewKit/MutableArray.h b/dev/ZKAKit/NewKit/MutableArray.h new file mode 100644 index 00000000..61b547e2 --- /dev/null +++ b/dev/ZKAKit/NewKit/MutableArray.h @@ -0,0 +1,239 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ +#pragma once + +#include <CompilerKit/CompilerKit.h> +#include <NewKit/Array.h> +#include <NewKit/Defines.h> + +#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; + + ZKA_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/ZKAKit/NewKit/New.h b/dev/ZKAKit/NewKit/New.h new file mode 100644 index 00000000..33bf3b24 --- /dev/null +++ b/dev/ZKAKit/NewKit/New.h @@ -0,0 +1,18 @@ + +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ +#pragma once + +#include <KernelKit/Heap.h> + +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/ZKAKit/NewKit/NewKit.h b/dev/ZKAKit/NewKit/NewKit.h new file mode 100644 index 00000000..f5bab516 --- /dev/null +++ b/dev/ZKAKit/NewKit/NewKit.h @@ -0,0 +1,20 @@ + +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Array.h> +#include <NewKit/ArrayList.h> +#include <NewKit/ErrorOr.h> +#include <NewKit/Json.h> +#include <NewKit/Stop.h> +#include <NewKit/MutableArray.h> +#include <NewKit/New.h> +#include <NewKit/OwnPtr.h> +#include <NewKit/Ref.h> +#include <NewKit/Stream.h> +#include <NewKit/Utils.h> diff --git a/dev/ZKAKit/NewKit/OwnPtr.h b/dev/ZKAKit/NewKit/OwnPtr.h new file mode 100644 index 00000000..b8f78ac1 --- /dev/null +++ b/dev/ZKAKit/NewKit/OwnPtr.h @@ -0,0 +1,94 @@ + +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.h> +#include <NewKit/Stop.h> +#include <NewKit/Ref.h> + +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/ZKAKit/NewKit/PageMgr.h b/dev/ZKAKit/NewKit/PageMgr.h new file mode 100644 index 00000000..702139f6 --- /dev/null +++ b/dev/ZKAKit/NewKit/PageMgr.h @@ -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 Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.h> +#include <NewKit/Ref.h> + +namespace Kernel +{ + class PageMgr; + + 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(); + + operator bool() + { + return fVirtAddr; + } + + 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 PageMgr; + friend class Pmm; + }; + + struct PageMgr final + { + public: + PageMgr() = default; + ~PageMgr() = default; + + PageMgr& operator=(const PageMgr&) = default; + PageMgr(const PageMgr&) = default; + + public: + PTEWrapper Request(Boolean Rw, Boolean User, Boolean ExecDisable, SizeT Sz); + bool Free(Ref<PTEWrapper>& wrapper); + + private: + void FlushTLB(); + + private: + friend PTEWrapper; + friend class Pmm; + }; +} // namespace Kernel diff --git a/dev/ZKAKit/NewKit/Pair.h b/dev/ZKAKit/NewKit/Pair.h new file mode 100644 index 00000000..aabc66ff --- /dev/null +++ b/dev/ZKAKit/NewKit/Pair.h @@ -0,0 +1,14 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.h> + +namespace Kernel +{ + +} // namespace Kernel diff --git a/dev/ZKAKit/NewKit/Pmm.h b/dev/ZKAKit/NewKit/Pmm.h new file mode 100644 index 00000000..da347b53 --- /dev/null +++ b/dev/ZKAKit/NewKit/Pmm.h @@ -0,0 +1,44 @@ + +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/PageMgr.h> +#include <NewKit/Ref.h> + +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<PageMgr>& Leak() + { + return fPageMgr; + } + + private: + Ref<PageMgr> fPageMgr; + }; +} // namespace Kernel diff --git a/dev/ZKAKit/NewKit/Ref.h b/dev/ZKAKit/NewKit/Ref.h new file mode 100644 index 00000000..cc6821c0 --- /dev/null +++ b/dev/ZKAKit/NewKit/Ref.h @@ -0,0 +1,103 @@ + +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#ifndef _NEWKIT_REF_HPP_ +#define _NEWKIT_REF_HPP_ + +#include <NewKit/Defines.h> +#include <NewKit/Stop.h> +#include <KernelKit/Heap.h> + +namespace Kernel +{ + template <typename T> + class Ref final + { + public: + Ref() = default; + + ~Ref() + { + if (mm_is_valid_heap(fClass)) + delete fClass; + } + + public: + Ref(T cls) + : fClass(&cls) + { + } + + Ref& operator=(T ref) + { + if (!fClass) + return *this; + + fClass = &ref; + return *this; + } + + public: + T operator->() const + { + MUST_PASS(*fClass); + return *fClass; + } + + T& Leak() noexcept + { + return *fClass; + } + + T& TryLeak() const noexcept + { + MUST_PASS(*fClass); + return *fClass; + } + + T operator*() + { + return *fClass; + } + + operator bool() noexcept + { + return fClass; + } + + private: + T* fClass{nullptr}; + }; + + template <typename T> + class NonNullRef final + { + public: + NonNullRef() = delete; + NonNullRef(nullPtr) = delete; + + NonNullRef(T* ref) + : fRef(ref) + { + MUST_PASS(ref); + } + + 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/ZKAKit/NewKit/Stop.h b/dev/ZKAKit/NewKit/Stop.h new file mode 100644 index 00000000..4f9e5795 --- /dev/null +++ b/dev/ZKAKit/NewKit/Stop.h @@ -0,0 +1,71 @@ + +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.h> + +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)) + +#ifdef __DEBUG__ +#define MUST_PASS(EXPR) __MUST_PASS((EXPR), __FILE__, __LINE__) +#define assert(EXPR) MUST_PASS(EXPR, RUNTIME_CHECK_EXPRESSION) +#else +#define MUST_PASS(EXPR) (Kernel::Void)(EXPR) +#define assert(EXPR) (Kernel::Void)(EXPR) +#endif + +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_FILESYSTEM, + RUNTIME_CHECK_VIRTUAL_OUT_OF_MEM, + RUNTIME_CHECK_PAGE, + 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/ZKAKit/NewKit/Stream.h b/dev/ZKAKit/NewKit/Stream.h new file mode 100644 index 00000000..ad0edf28 --- /dev/null +++ b/dev/ZKAKit/NewKit/Stream.h @@ -0,0 +1,58 @@ + +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.h> +#include <NewKit/Ref.h> + +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/ZKAKit/NewKit/String.h b/dev/ZKAKit/NewKit/String.h new file mode 100644 index 00000000..8e77b61b --- /dev/null +++ b/dev/ZKAKit/NewKit/String.h @@ -0,0 +1,91 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include <CompilerKit/CompilerKit.h> +#include <NewKit/Defines.h> +#include <NewKit/ErrorOr.h> +#include <NewKit/Utils.h> +#include <NewKit/Stop.h> + +#define cMinimumStringSize 8196 + +namespace Kernel +{ + /// @brief StringView class, using dynamic or static memory. + class StringView final + { + public: + explicit StringView() + { + fDataSz = cMinimumStringSize; + + fData = new Char[fDataSz]; + MUST_PASS(fData); + + rt_set_memory(fData, 0, fDataSz); + } + + explicit StringView(const SizeT& Sz) + : fDataSz(Sz) + { + MUST_PASS(Sz > 1); + + fData = new Char[Sz]; + MUST_PASS(fData); + + rt_set_memory(fData, 0, Sz); + } + + ~StringView() + { + if (fData) + delete[] fData; + } + + ZKA_COPY_DEFAULT(StringView); + + 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 fDataSz{0}; + Size fCur{0}; + + friend class StringBuilder; + }; + + struct StringBuilder final + { + static ErrorOr<StringView> Construct(const Char* data); + 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); + static bool Equals(const WideChar* lhs, const WideChar* rhs); + }; +} // namespace Kernel diff --git a/dev/ZKAKit/NewKit/Utils.h b/dev/ZKAKit/NewKit/Utils.h new file mode 100644 index 00000000..a689cf51 --- /dev/null +++ b/dev/ZKAKit/NewKit/Utils.h @@ -0,0 +1,29 @@ + +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.h> + +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, UInt32 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/ZKAKit/NewKit/Variant.h b/dev/ZKAKit/NewKit/Variant.h new file mode 100644 index 00000000..92eee500 --- /dev/null +++ b/dev/ZKAKit/NewKit/Variant.h @@ -0,0 +1,70 @@ +/* ------------------------------------------- + + Copyright ZKA Web Services Co. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.h> +#include <NewKit/String.h> +#include <NewKit/Json.h> + +namespace Kernel +{ + class Variant final + { + public: + enum class VariantKind + { + kString, + kBlob, + kNull, + kJson, + kXML, + }; + + public: + explicit Variant() = delete; + + public: + ZKA_COPY_DEFAULT(Variant); + + ~Variant() = default; + + public: + explicit Variant(StringView* stringView) + : fPtr((VoidPtr)stringView), fKind(VariantKind::kString) + { + } + + explicit Variant(JSON* 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(); + + template <typename T> + T* As() + { + return reinterpret_cast<T*>(fPtr); + } + + private: + voidPtr fPtr{nullptr}; + VariantKind fKind{VariantKind::kNull}; + }; +} // namespace Kernel |
