From f86f3d929329b125457fd9aafbc67fb16a5e33fe Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 23 Mar 2023 04:56:49 +0200 Subject: [PATCH] Rename std functions to match the standard --- Architecture/amd64/acpi.hpp | 8 +- Core/Crash/Screens/Tasks.cpp | 2 +- Core/Driver/Driver.cpp | 10 +- Core/Driver/DriverLoading/BindInterrupt.cpp | 14 +- Core/Driver/DriverLoading/BindPCI.cpp | 16 +- Core/InterruptsManager.cpp | 2 +- Core/PeripheralComponentInterconnect.cpp | 8 +- ...iversalAsynchronousReceiverTransmitter.cpp | 2 +- Execute/Elf/BaseLoad.cpp | 14 +- Execute/Elf/Exec.cpp | 6 +- Execute/Elf/Parse.cpp | 2 +- Execute/Elf/SharedObjects.cpp | 2 +- Execute/Parse.cpp | 2 +- Execute/Spawn.cpp | 4 +- FileSystem/Filesystem.cpp | 42 +- Kernel.cpp | 6 +- Network/AddressResolutionProtocol.cpp | 2 +- Network/Ethernet.cpp | 2 +- Network/InternetProtocol.cpp | 2 +- Network/NetworkController.cpp | 2 +- Network/UserDatagramProtocol.cpp | 2 +- Recovery/RecoveryMain.cpp | 3 +- Tasking/Task.cpp | 6 +- Tests/MemoryAllocation.cpp | 2 +- Tests/String.cpp | 18 +- include/disk.hpp | 4 +- include/driver.hpp | 4 +- include/exec.hpp | 6 +- include/filesystem.hpp | 22 +- include/gui.hpp | 12 +- include/ipc.hpp | 2 +- include/memory.hpp | 5 +- include/net/arp.hpp | 2 +- include/net/nc.hpp | 2 +- include/pci.hpp | 10 +- include/smart_ptr.hpp | 2 + include/smartptr.hpp | 266 --------- include/std.hpp | 124 ++-- include/std/functional.hpp | 33 ++ include/std/list.hpp | 207 +++++++ include/std/smart_ptr.hpp | 263 +++++++++ include/std/stdexcept.hpp | 19 + include/std/string.hpp | 536 ++++++++++++++++++ include/std/unordered_map.hpp | 113 ++++ include/std/utility.hpp | 22 + include/std/vector.hpp | 267 +++++++++ include/string.hpp | 235 +------- include/task.hpp | 12 +- include/types.h | 2 + include/vector.hpp | 265 +-------- 50 files changed, 1635 insertions(+), 979 deletions(-) create mode 100644 include/smart_ptr.hpp delete mode 100644 include/smartptr.hpp create mode 100644 include/std/functional.hpp create mode 100644 include/std/list.hpp create mode 100644 include/std/smart_ptr.hpp create mode 100644 include/std/stdexcept.hpp create mode 100644 include/std/string.hpp create mode 100644 include/std/unordered_map.hpp create mode 100644 include/std/utility.hpp create mode 100644 include/std/vector.hpp diff --git a/Architecture/amd64/acpi.hpp b/Architecture/amd64/acpi.hpp index 6b34d35..54b3677 100644 --- a/Architecture/amd64/acpi.hpp +++ b/Architecture/amd64/acpi.hpp @@ -235,10 +235,10 @@ namespace ACPI void *VirtualAddress; }; - Vector ioapic; - Vector iso; - Vector nmi; - Vector lapic; + std::vector ioapic; + std::vector iso; + std::vector nmi; + std::vector lapic; struct LAPIC *LAPICAddress; uint16_t CPUCores; diff --git a/Core/Crash/Screens/Tasks.cpp b/Core/Crash/Screens/Tasks.cpp index 4ba7f4d..e6b7720 100644 --- a/Core/Crash/Screens/Tasks.cpp +++ b/Core/Crash/Screens/Tasks.cpp @@ -39,7 +39,7 @@ namespace CrashHandler "Terminated", // Terminated }; - Vector Plist = TaskManager->GetProcessList(); + std::vector Plist = TaskManager->GetProcessList(); if (TaskManager) { diff --git a/Core/Driver/Driver.cpp b/Core/Driver/Driver.cpp index 44908b8..8be1f7e 100644 --- a/Core/Driver/Driver.cpp +++ b/Core/Driver/Driver.cpp @@ -1,12 +1,13 @@ #include -#include #include +#include #include #include #include #include #include +// #include #include "../../kernel.h" #include "../../DAPI.hpp" @@ -174,7 +175,12 @@ namespace Driver Driver::Driver() { SmartCriticalSection(DriverInitLock); - SharedPointer DriverDirectory = vfs->Open(Config.DriverDirectory); + + std::string DriverConfigFile = Config.DriverDirectory; + DriverConfigFile << "/config.ini"; + fixme("Loading driver config file: %s", DriverConfigFile.c_str()); + + std::shared_ptr DriverDirectory = vfs->Open(Config.DriverDirectory); if (DriverDirectory->Status == VirtualFileSystem::FileStatus::OK) { foreach (auto driver in DriverDirectory->node->Children) diff --git a/Core/Driver/DriverLoading/BindInterrupt.cpp b/Core/Driver/DriverLoading/BindInterrupt.cpp index 6c317f3..54bd311 100644 --- a/Core/Driver/DriverLoading/BindInterrupt.cpp +++ b/Core/Driver/DriverLoading/BindInterrupt.cpp @@ -20,7 +20,7 @@ namespace Driver if (fexExtended->Driver.OverrideOnConflict) { - Vector DriversToRemove = Vector(); + std::vector DriversToRemove = std::vector(); foreach (auto Drv in Drivers) { FexExtended *fe = ((FexExtended *)((uintptr_t)Drv->Address + EXTENDED_SECTION_ADDRESS)); @@ -69,7 +69,7 @@ namespace Driver if (fexExtended->Driver.OverrideOnConflict) { - Vector DriversToRemove = Vector(); + std::vector DriversToRemove = std::vector(); foreach (auto Drv in Drivers) { FexExtended *fe = ((FexExtended *)((uintptr_t)Drv->Address + EXTENDED_SECTION_ADDRESS)); @@ -113,7 +113,7 @@ namespace Driver if (fexExtended->Driver.OverrideOnConflict) { - Vector DriversToRemove = Vector(); + std::vector DriversToRemove = std::vector(); foreach (auto Drv in Drivers) { FexExtended *fe = ((FexExtended *)((uintptr_t)Drv->Address + EXTENDED_SECTION_ADDRESS)); @@ -159,7 +159,7 @@ namespace Driver if (fexExtended->Driver.OverrideOnConflict) { - Vector DriversToRemove = Vector(); + std::vector DriversToRemove = std::vector(); foreach (auto Drv in Drivers) { FexExtended *fe = ((FexExtended *)((uintptr_t)Drv->Address + EXTENDED_SECTION_ADDRESS)); @@ -233,7 +233,7 @@ namespace Driver if (fexExtended->Driver.OverrideOnConflict) { - Vector DriversToRemove = Vector(); + std::vector DriversToRemove = std::vector(); foreach (auto Drv in Drivers) { FexExtended *fe = ((FexExtended *)((uintptr_t)Drv->Address + EXTENDED_SECTION_ADDRESS)); @@ -278,7 +278,7 @@ namespace Driver debug("Searching for conflicting drivers..."); if (fexExtended->Driver.OverrideOnConflict) { - Vector DriversToRemove = Vector(); + std::vector DriversToRemove = std::vector(); foreach (auto Drv in Drivers) { FexExtended *fe = ((FexExtended *)((uintptr_t)Drv->Address + EXTENDED_SECTION_ADDRESS)); @@ -378,7 +378,7 @@ namespace Driver if (fexExtended->Driver.OverrideOnConflict) { - Vector DriversToRemove = Vector(); + std::vector DriversToRemove = std::vector(); foreach (auto Drv in Drivers) { FexExtended *fe = ((FexExtended *)((uintptr_t)Drv->Address + EXTENDED_SECTION_ADDRESS)); diff --git a/Core/Driver/DriverLoading/BindPCI.cpp b/Core/Driver/DriverLoading/BindPCI.cpp index 50a61f0..3914f20 100644 --- a/Core/Driver/DriverLoading/BindPCI.cpp +++ b/Core/Driver/DriverLoading/BindPCI.cpp @@ -133,7 +133,7 @@ namespace Driver if (fexExtended->Driver.OverrideOnConflict) { - Vector DriversToRemove = Vector(); + std::vector DriversToRemove = std::vector(); foreach (auto Drv in Drivers) { FexExtended *fe = ((FexExtended *)((uintptr_t)Drv->Address + EXTENDED_SECTION_ADDRESS)); @@ -177,7 +177,7 @@ namespace Driver if (fexExtended->Driver.OverrideOnConflict) { - Vector DriversToRemove = Vector(); + std::vector DriversToRemove = std::vector(); foreach (auto Drv in Drivers) { FexExtended *fe = ((FexExtended *)((uintptr_t)Drv->Address + EXTENDED_SECTION_ADDRESS)); @@ -221,7 +221,7 @@ namespace Driver if (fexExtended->Driver.OverrideOnConflict) { - Vector DriversToRemove = Vector(); + std::vector DriversToRemove = std::vector(); foreach (auto Drv in Drivers) { FexExtended *fe = ((FexExtended *)((uintptr_t)Drv->Address + EXTENDED_SECTION_ADDRESS)); @@ -299,7 +299,7 @@ namespace Driver if (fexExtended->Driver.OverrideOnConflict) { - Vector DriversToRemove = Vector(); + std::vector DriversToRemove = std::vector(); foreach (auto Drv in Drivers) { FexExtended *fe = ((FexExtended *)((uintptr_t)Drv->Address + EXTENDED_SECTION_ADDRESS)); @@ -368,7 +368,7 @@ namespace Driver if (fexExtended->Driver.OverrideOnConflict) { - Vector DriversToRemove = Vector(); + std::vector DriversToRemove = std::vector(); foreach (auto Drv in Drivers) { FexExtended *fe = ((FexExtended *)((uintptr_t)Drv->Address + EXTENDED_SECTION_ADDRESS)); @@ -412,7 +412,7 @@ namespace Driver if (fexExtended->Driver.OverrideOnConflict) { - Vector DriversToRemove = Vector(); + std::vector DriversToRemove = std::vector(); foreach (auto Drv in Drivers) { FexExtended *fe = ((FexExtended *)((uintptr_t)Drv->Address + EXTENDED_SECTION_ADDRESS)); @@ -456,7 +456,7 @@ namespace Driver if (fexExtended->Driver.OverrideOnConflict) { - Vector DriversToRemove = Vector(); + std::vector DriversToRemove = std::vector(); foreach (auto Drv in Drivers) { FexExtended *fe = ((FexExtended *)((uintptr_t)Drv->Address + EXTENDED_SECTION_ADDRESS)); @@ -539,7 +539,7 @@ namespace Driver if (((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.VendorID[Vidx] == 0 || ((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.DeviceID[Didx] == 0) continue; - Vector devices = PCIManager->FindPCIDevice(((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.VendorID[Vidx], ((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.DeviceID[Didx]); + std::vector devices = PCIManager->FindPCIDevice(((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.VendorID[Vidx], ((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.DeviceID[Didx]); if (devices.size() == 0) continue; diff --git a/Core/InterruptsManager.cpp b/Core/InterruptsManager.cpp index 2667a3e..47aa347 100644 --- a/Core/InterruptsManager.cpp +++ b/Core/InterruptsManager.cpp @@ -28,7 +28,7 @@ namespace Interrupts int ID; void *Data; }; - Vector RegisteredEvents; + std::vector RegisteredEvents; #if defined(a64) /* APIC::APIC */ void *apic[MAX_CPU]; diff --git a/Core/PeripheralComponentInterconnect.cpp b/Core/PeripheralComponentInterconnect.cpp index 33f3289..2310dec 100644 --- a/Core/PeripheralComponentInterconnect.cpp +++ b/Core/PeripheralComponentInterconnect.cpp @@ -817,18 +817,18 @@ namespace PCI EnumerateDevice(BusAddress, Device); } - Vector PCI::FindPCIDevice(uint8_t Class, uint8_t Subclass, uint8_t ProgIF) + std::vector PCI::FindPCIDevice(uint8_t Class, uint8_t Subclass, uint8_t ProgIF) { - Vector DeviceFound; + std::vector DeviceFound; for (auto var : Devices) if (var->Class == Class && var->Subclass == Subclass && var->ProgIF == ProgIF) DeviceFound.push_back(var); return DeviceFound; } - Vector PCI::FindPCIDevice(int VendorID, int DeviceID) + std::vector PCI::FindPCIDevice(int VendorID, int DeviceID) { - Vector DeviceFound; + std::vector DeviceFound; for (auto var : Devices) if (var->VendorID == VendorID && var->DeviceID == DeviceID) DeviceFound.push_back(var); diff --git a/Core/UniversalAsynchronousReceiverTransmitter.cpp b/Core/UniversalAsynchronousReceiverTransmitter.cpp index 7cc585e..f99dd1e 100644 --- a/Core/UniversalAsynchronousReceiverTransmitter.cpp +++ b/Core/UniversalAsynchronousReceiverTransmitter.cpp @@ -4,7 +4,7 @@ #include volatile bool serialports[8] = {false, false, false, false, false, false, false, false}; -Vector RegisteredEvents; +std::vector RegisteredEvents; #if defined(a64) || defined(a32) NIF __always_inline inline uint8_t NoProfiler_inportb(uint16_t Port) diff --git a/Execute/Elf/BaseLoad.cpp b/Execute/Elf/BaseLoad.cpp index a3f3fba..b3fed9b 100644 --- a/Execute/Elf/BaseLoad.cpp +++ b/Execute/Elf/BaseLoad.cpp @@ -31,7 +31,7 @@ namespace Execute } InterpreterIPCData; /* Passing arguments as a sanity check and debugging. */ - void ELFInterpreterIPCThread(PCB *Process, char *Path, void *MemoryImage, void *ElfFile, Vector NeededLibraries) + void ELFInterpreterIPCThread(PCB *Process, char *Path, void *MemoryImage, void *ElfFile, std::vector NeededLibraries) { debug("Interpreter thread started for %s", Path); // Interpreter will create an IPC with token "LOAD". @@ -56,7 +56,7 @@ namespace Execute warn("Too many libraries! (max 256)"); for (size_t i = 0; i < NeededLibraries.size(); i++) { - strcpy(TmpBuffer->Libraries[i].Name, NeededLibraries[i].c_str()); + strcpy(TmpBuffer->Libraries[i].Name, NeededLibraries[i]); } #ifdef DEBUG @@ -66,7 +66,7 @@ namespace Execute debug("MemoryImage: %p", MemoryImage); for (size_t i = 0; i < NeededLibraries.size(); i++) { - debug("Library: %s", NeededLibraries[i].c_str()); + debug("Library: %s", NeededLibraries[i]); } debug("INSIDE DATA"); debug("Path: %s", TmpBuffer->Path); @@ -93,10 +93,10 @@ namespace Execute } PCB *InterpreterTargetProcess; - String *InterpreterTargetPath; /* We can't have String as a constructor :( */ + std::string *InterpreterTargetPath; /* We can't have String as a constructor :( */ void *InterpreterMemoryImage; void *InterpreterElfFile; - Vector InterpreterNeededLibraries; + std::vector InterpreterNeededLibraries; void ELFInterpreterThreadWrapper() { ELFInterpreterIPCThread(InterpreterTargetProcess, (char *)InterpreterTargetPath->c_str(), InterpreterMemoryImage, InterpreterElfFile, InterpreterNeededLibraries); @@ -111,7 +111,7 @@ namespace Execute cwk_path_get_basename(Path, &BaseName, nullptr); TaskArchitecture Arch = TaskArchitecture::UnknownArchitecture; - SharedPointer ExFile = vfs->Open(Path); + std::shared_ptr ExFile = vfs->Open(Path); if (ExFile->Status != FileStatus::OK) { @@ -230,7 +230,7 @@ namespace Execute debug("ElfFile: %p ELFHeader: %p", ElfFile, ELFHeader); InterpreterTargetProcess = Process; - InterpreterTargetPath = new String(Path); /* We store in a String because Path may get changed while outside ELFLoad(). */ + InterpreterTargetPath = new std::string(Path); /* We store in a String because Path may get changed while outside ELFLoad(). */ InterpreterMemoryImage = bl.VirtualMemoryImage; InterpreterElfFile = ElfFile; InterpreterNeededLibraries = bl.NeededLibraries; diff --git a/Execute/Elf/Exec.cpp b/Execute/Elf/Exec.cpp index c3ab83c..f4d5d92 100644 --- a/Execute/Elf/Exec.cpp +++ b/Execute/Elf/Exec.cpp @@ -136,8 +136,8 @@ namespace Execute break; } - String ReqLib = (char *)((uintptr_t)ElfFile + DynamicString->sh_offset + Dynamic[i].d_un.d_val); - debug("DT_NEEDED - Name[%ld]: %s", i, ReqLib.c_str()); + const char *ReqLib = (const char *)((uintptr_t)ElfFile + DynamicString->sh_offset + Dynamic[i].d_un.d_val); + debug("DT_NEEDED - Name[%ld]: %s", i, ReqLib); ELFBase.NeededLibraries.push_back(ReqLib); } else if (Dynamic[i].d_tag == DT_NULL) @@ -154,7 +154,7 @@ namespace Execute memcpy((void *)InterpreterPath, (uint8_t *)ElfFile + ItrPhdr.p_offset, 256); debug("Interpreter: %s", InterpreterPath); - SharedPointer InterpreterFile = vfs->Open(InterpreterPath); + std::shared_ptr InterpreterFile = vfs->Open(InterpreterPath); if (InterpreterFile->Status != VirtualFileSystem::FileStatus::OK) warn("Failed to open interpreter file: %s", InterpreterPath); diff --git a/Execute/Elf/Parse.cpp b/Execute/Elf/Parse.cpp index 18ff83d..056fc81 100644 --- a/Execute/Elf/Parse.cpp +++ b/Execute/Elf/Parse.cpp @@ -207,7 +207,7 @@ namespace Execute } /* No need to check if it's valid, the GetBinaryType() call above does that. */ - SharedPointer File = vfs->Open(Interpreter); + std::shared_ptr File = vfs->Open(Interpreter); Elf64_Ehdr *ELFHeader = (Elf64_Ehdr *)File->node->Address; diff --git a/Execute/Elf/SharedObjects.cpp b/Execute/Elf/SharedObjects.cpp index e167ba3..930ffe1 100644 --- a/Execute/Elf/SharedObjects.cpp +++ b/Execute/Elf/SharedObjects.cpp @@ -21,7 +21,7 @@ NewLock(ExecuteServiceLock); namespace Execute { Memory::MemMgr *mem = nullptr; - Vector Libs; + std::vector Libs; void StartExecuteService() { diff --git a/Execute/Parse.cpp b/Execute/Parse.cpp index d675109..586f33a 100644 --- a/Execute/Parse.cpp +++ b/Execute/Parse.cpp @@ -69,7 +69,7 @@ namespace Execute BinaryType GetBinaryType(char *Path) { BinaryType Type = BinaryType::BinTypeInvalid; - SharedPointer ExFile = vfs->Open(Path); + std::shared_ptr ExFile = vfs->Open(Path); if (ExFile->Status == VirtualFileSystem::FileStatus::OK) { diff --git a/Execute/Spawn.cpp b/Execute/Spawn.cpp index 82f8385..04fd1c4 100644 --- a/Execute/Spawn.cpp +++ b/Execute/Spawn.cpp @@ -20,7 +20,7 @@ namespace Execute .Process = nullptr, .Thread = nullptr}; - SharedPointer ExFile = vfs->Open(Path); + std::shared_ptr ExFile = vfs->Open(Path); if (ExFile->Status == VirtualFileSystem::FileStatus::OK) { @@ -48,7 +48,7 @@ namespace Execute for (size_t i = 0; i < TO_PAGES(ExFile->node->Length); i++) pva.Map((void *)((uintptr_t)BaseImage + (i * PAGE_SIZE)), (void *)((uintptr_t)BaseImage + (i * PAGE_SIZE)), Memory::PTFlag::RW | Memory::PTFlag::US); - Vector auxv; // TODO! + std::vector auxv; // TODO! TCB *Thread = TaskManager->CreateThread(Process, (IP)FexHdr->EntryPoint, diff --git a/FileSystem/Filesystem.cpp b/FileSystem/Filesystem.cpp index 3c2af94..c2cabb2 100644 --- a/FileSystem/Filesystem.cpp +++ b/FileSystem/Filesystem.cpp @@ -1,6 +1,6 @@ #include -#include +#include #include #include #include @@ -22,7 +22,7 @@ NewLock(VFSLock); namespace VirtualFileSystem { - SharedPointer Virtual::GetPathFromNode(Node *node) + std::shared_ptr Virtual::GetPathFromNode(Node *node) { vfsdbg("GetPathFromNode( Node: \"%s\" )", node->Name); Node *Parent = node; @@ -73,7 +73,7 @@ namespace VirtualFileSystem } // Allocate a new string for the final path - SharedPointer FinalPath; + std::shared_ptr FinalPath; FinalPath.reset(new char[Size]); size_t Offset = 0; @@ -152,9 +152,9 @@ namespace VirtualFileSystem return nullptr; } - SharedPointer Virtual::ConvertNodeToFILE(Node *node) + std::shared_ptr Virtual::ConvertNodeToFILE(Node *node) { - SharedPointer file = MakeShared(); + std::shared_ptr file = std::make_shared(); file->Status = FileStatus::OK; file->node = node; return file; @@ -256,17 +256,17 @@ namespace VirtualFileSystem return FileStatus::NotFound; } - SharedPointer Virtual::NormalizePath(const char *Path, Node *Parent) + std::shared_ptr Virtual::NormalizePath(const char *Path, Node *Parent) { vfsdbg("NormalizePath( Path: \"%s\" Parent: \"%s\" )", Path, Parent->Name); char *NormalizedPath = new char[strlen((char *)Path) + 1]; - SharedPointer RelativePath; + std::shared_ptr RelativePath; cwk_path_normalize(Path, NormalizedPath, strlen((char *)Path) + 1); if (cwk_path_is_relative(NormalizedPath)) { - SharedPointer ParentPath = GetPathFromNode(Parent); + std::shared_ptr ParentPath = GetPathFromNode(Parent); size_t PathSize = cwk_path_get_absolute(ParentPath.Get(), NormalizedPath, nullptr, 0); RelativePath.reset(new char[PathSize + 1]); cwk_path_get_absolute(ParentPath.Get(), NormalizedPath, RelativePath.Get(), PathSize + 1); @@ -329,7 +329,7 @@ namespace VirtualFileSystem Node *CurrentParent = this->GetParent(Path, Parent); vfsdbg("Virtual::Create( Path: \"%s\" Parent: \"%s\" )", Path, Parent ? Parent->Name : CurrentParent->Name); - SharedPointer CleanPath = this->NormalizePath(Path, CurrentParent); + std::shared_ptr CleanPath = this->NormalizePath(Path, CurrentParent); vfsdbg("CleanPath: \"%s\"", CleanPath.Get()); if (PathExists(CleanPath.Get(), CurrentParent)) @@ -392,7 +392,7 @@ namespace VirtualFileSystem if (Parent == nullptr) Parent = FileSystemRoot; - SharedPointer CleanPath = this->NormalizePath(Path, Parent); + std::shared_ptr CleanPath = this->NormalizePath(Path, Parent); vfsdbg("CleanPath: \"%s\"", CleanPath.Get()); if (!PathExists(CleanPath.Get(), Parent)) @@ -440,10 +440,10 @@ namespace VirtualFileSystem FileStatus Virtual::Delete(Node *Path, bool Recursive, Node *Parent) { return Delete(GetPathFromNode(Path).Get(), Recursive, Parent); } /* TODO: REWORK */ - SharedPointer Virtual::Mount(const char *Path, FileSystemOperations *Operator) + std::shared_ptr Virtual::Mount(const char *Path, FileSystemOperations *Operator) { SmartLock(VFSLock); - SharedPointer file = MakeShared(); + std::shared_ptr file = std::make_shared(); if (unlikely(!Operator)) { @@ -467,7 +467,7 @@ namespace VirtualFileSystem return file; } - FileStatus Virtual::Unmount(SharedPointer File) + FileStatus Virtual::Unmount(std::shared_ptr File) { SmartLock(VFSLock); if (unlikely(File.Get())) @@ -476,7 +476,7 @@ namespace VirtualFileSystem return FileStatus::OK; } - size_t Virtual::Read(SharedPointer File, size_t Offset, uint8_t *Buffer, size_t Size) + size_t Virtual::Read(std::shared_ptr File, size_t Offset, uint8_t *Buffer, size_t Size) { SmartLock(VFSLock); if (unlikely(!File.Get())) @@ -500,7 +500,7 @@ namespace VirtualFileSystem return File->node->Operator->Read(File->node, Offset, Size, Buffer); } - size_t Virtual::Write(SharedPointer File, size_t Offset, uint8_t *Buffer, size_t Size) + size_t Virtual::Write(std::shared_ptr File, size_t Offset, uint8_t *Buffer, size_t Size) { SmartLock(VFSLock); if (unlikely(!File.Get())) @@ -525,7 +525,7 @@ namespace VirtualFileSystem } /* TODO: CHECK Open */ - SharedPointer Virtual::Open(const char *Path, Node *Parent) + std::shared_ptr Virtual::Open(const char *Path, Node *Parent) { SmartLock(VFSLock); vfsdbg("Opening %s with parent %s", Path, Parent ? Parent->Name : "(null)"); @@ -533,7 +533,7 @@ namespace VirtualFileSystem if (strcmp(Path, ".") == 0) { - SharedPointer file = MakeShared(); + std::shared_ptr file = std::make_shared(); file->node = Parent; if (unlikely(!file->node)) file->Status = FileStatus::NotFound; @@ -544,7 +544,7 @@ namespace VirtualFileSystem if (strcmp(Path, "..") == 0) { - SharedPointer file = MakeShared(); + std::shared_ptr file = std::make_shared(); if (Parent->Parent != nullptr) file->node = Parent->Parent; @@ -557,9 +557,9 @@ namespace VirtualFileSystem } Node *CurrentParent = this->GetParent(Path, Parent); - SharedPointer CleanPath = NormalizePath(Path, CurrentParent); + std::shared_ptr CleanPath = NormalizePath(Path, CurrentParent); - SharedPointer file = MakeShared(); + std::shared_ptr file = std::make_shared(); /* TODO: Check for other errors */ if (!PathExists(CleanPath.Get(), CurrentParent)) @@ -601,7 +601,7 @@ namespace VirtualFileSystem return file; } - FileStatus Virtual::Close(SharedPointer File) + FileStatus Virtual::Close(std::shared_ptr File) { SmartLock(VFSLock); if (unlikely(!File.Get())) diff --git a/Kernel.cpp b/Kernel.cpp index 8b40945..afa4b8d 100644 --- a/Kernel.cpp +++ b/Kernel.cpp @@ -314,7 +314,7 @@ EXTERNC NIF void Main(BootInfo *Info) DevFS = vfs->Create("/system/dev", NodeFlags::DIRECTORY); else { - SharedPointer dev = vfs->Open("/system/dev"); + std::shared_ptr dev = vfs->Open("/system/dev"); if (dev->node->Flags != NodeFlags::DIRECTORY) { KPrint("\eE85230/system/dev is not a directory! Halting..."); @@ -328,7 +328,7 @@ EXTERNC NIF void Main(BootInfo *Info) MntFS = vfs->Create("/system/mnt", NodeFlags::DIRECTORY); else { - SharedPointer mnt = vfs->Open("/system/mnt"); + std::shared_ptr mnt = vfs->Open("/system/mnt"); if (mnt->node->Flags != NodeFlags::DIRECTORY) { KPrint("\eE85230/system/mnt is not a directory! Halting..."); @@ -342,7 +342,7 @@ EXTERNC NIF void Main(BootInfo *Info) ProcFS = vfs->Create("/system/proc", NodeFlags::DIRECTORY); else { - SharedPointer proc = vfs->Open("/system/proc", nullptr); + std::shared_ptr proc = vfs->Open("/system/proc", nullptr); if (proc->node->Flags != NodeFlags::DIRECTORY) { KPrint("\eE85230/system/proc is not a directory! Halting..."); diff --git a/Network/AddressResolutionProtocol.cpp b/Network/AddressResolutionProtocol.cpp index 5b94e03..2e3dc46 100644 --- a/Network/AddressResolutionProtocol.cpp +++ b/Network/AddressResolutionProtocol.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include "../kernel.h" diff --git a/Network/Ethernet.cpp b/Network/Ethernet.cpp index 21b3c1f..444e85a 100644 --- a/Network/Ethernet.cpp +++ b/Network/Ethernet.cpp @@ -11,7 +11,7 @@ namespace NetworkEthernet uint16_t Type; }; - Vector RegisteredEvents; + std::vector RegisteredEvents; Ethernet::Ethernet(NetworkInterfaceManager::DeviceInterface *Interface) : NetworkInterfaceManager::Events(Interface) { diff --git a/Network/InternetProtocol.cpp b/Network/InternetProtocol.cpp index ddba175..97f54af 100644 --- a/Network/InternetProtocol.cpp +++ b/Network/InternetProtocol.cpp @@ -49,7 +49,7 @@ namespace NetworkIPv4 kfree(Packet); } - Vector RegisteredEvents; + std::vector RegisteredEvents; bool IPv4::OnEthernetPacketReceived(uint8_t *Data, uint64_t Length) { diff --git a/Network/NetworkController.cpp b/Network/NetworkController.cpp index c5e185f..8403cc7 100644 --- a/Network/NetworkController.cpp +++ b/Network/NetworkController.cpp @@ -17,7 +17,7 @@ namespace NetworkInterfaceManager { - Vector RegisteredEvents; + std::vector RegisteredEvents; NetworkInterface::NetworkInterface() { diff --git a/Network/UserDatagramProtocol.cpp b/Network/UserDatagramProtocol.cpp index c7b7ffb..6be0ffb 100644 --- a/Network/UserDatagramProtocol.cpp +++ b/Network/UserDatagramProtocol.cpp @@ -10,7 +10,7 @@ namespace NetworkUDP Socket *UDPSocket; uint16_t Port; }; - Vector RegisteredEvents; + std::vector RegisteredEvents; UDPEvents::UDPEvents() {} diff --git a/Recovery/RecoveryMain.cpp b/Recovery/RecoveryMain.cpp index 36ac05c..e6ee9f7 100644 --- a/Recovery/RecoveryMain.cpp +++ b/Recovery/RecoveryMain.cpp @@ -52,7 +52,7 @@ namespace Recovery return; } - SharedPointer pcm = vfs->Open(AudioFile); + std::shared_ptr pcm = vfs->Open(AudioFile); if (pcm->Status != FileStatus::OK) { @@ -72,6 +72,7 @@ namespace Recovery int status = DriverManager->IOCB(AudioDrv->DriverUID, (void *)&callback); debug("Audio played! %d", status); KernelAllocator.FreePages((void *)PCMRaw, TO_PAGES(pcm->node->Length)); + vfs->Close(pcm); } void PlayAudioWrapper() { TaskManager->CreateThread(TaskManager->GetCurrentProcess(), (IP)PlayAudio)->SetPriority(Tasking::TaskPriority::Idle); } diff --git a/Tasking/Task.cpp b/Tasking/Task.cpp index 857ccb5..03a4981 100644 --- a/Tasking/Task.cpp +++ b/Tasking/Task.cpp @@ -336,7 +336,7 @@ namespace Tasking IP EntryPoint, const char **argv, const char **envp, - const Vector &auxv, + const std::vector &auxv, IPOffset Offset, TaskArchitecture Architecture, TaskCompatibility Compatibility) @@ -505,7 +505,7 @@ namespace Tasking *Stack64 = AT_NULL; // auxv_array is initialized with auxv elements. If the array is empty then we add a null terminator - Vector auxv_array = auxv; + std::vector auxv_array = auxv; if (auxv_array.size() == 0) auxv_array.push_back({.archaux = {.a_type = AT_NULL, .a_un = {.a_val = 0}}}); @@ -760,7 +760,7 @@ namespace Tasking TaskArchitecture Arch = TaskArchitecture::ARM64; #endif PCB *kproc = CreateProcess(nullptr, "Kernel", TaskTrustLevel::Kernel); - TCB *kthrd = CreateThread(kproc, EntryPoint, nullptr, nullptr, Vector(), 0, Arch); + TCB *kthrd = CreateThread(kproc, EntryPoint, nullptr, nullptr, std::vector(), 0, Arch); kthrd->Rename("Main Thread"); debug("Created Kernel Process: %s and Thread: %s", kproc->Name, kthrd->Name); TaskingLock.Lock(__FUNCTION__); diff --git a/Tests/MemoryAllocation.cpp b/Tests/MemoryAllocation.cpp index f0c28ea..55ea28b 100644 --- a/Tests/MemoryAllocation.cpp +++ b/Tests/MemoryAllocation.cpp @@ -1,7 +1,7 @@ #ifdef DEBUG -#include #include +#include #include /* Originally from: https://github.com/EnderIce2/FennixProject/blob/main/kernel/test.cpp */ diff --git a/Tests/String.cpp b/Tests/String.cpp index 69609bd..28248aa 100644 --- a/Tests/String.cpp +++ b/Tests/String.cpp @@ -1,11 +1,11 @@ #ifdef DEBUG -#include +#include #include void TestString() { - String hw("Hello, world!"); + std::string hw("Hello, world!"); debug("String length: %d", hw.length()); debug("String capacity: %d", hw.capacity()); debug("String data: %s", hw.c_str()); @@ -18,7 +18,7 @@ void TestString() ; } - String hi("Hi"); + std::string hi("Hi"); char chi[3]; chi[0] = hi[0]; chi[1] = hi[1]; @@ -52,9 +52,9 @@ void TestString() ; } - String eq0("Hello, world!"); - String eq1("Hello, world!"); - String eq2("World, hello!"); + std::string eq0("Hello, world!"); + std::string eq1("Hello, world!"); + std::string eq2("World, hello!"); if (eq0 == eq1) debug("String equality works!"); @@ -92,9 +92,9 @@ void TestString() ; } - String a("Hello"); - String b("World"); - String c; + std::string a("Hello"); + std::string b("World"); + std::string c; c = a + ", " + b + "!"; if (c == "Hello, World!") diff --git a/include/disk.hpp b/include/disk.hpp index c15d947..25c11b8 100644 --- a/include/disk.hpp +++ b/include/disk.hpp @@ -121,7 +121,7 @@ namespace Disk uint8_t *Buffer = nullptr; PartitionTable Table; PartitionStyle Style = PartitionStyle::Unknown; - Vector Partitions; + std::vector Partitions; bool MechanicalDisk = false; size_t UniqueIdentifier = 0xdeadbeef; @@ -153,7 +153,7 @@ namespace Disk unsigned char AvailablePorts = 0; int BytesPerSector = 0; - Vector drives; + std::vector drives; public: void FetchDisks(unsigned long DriverUID); diff --git a/include/driver.hpp b/include/driver.hpp index 6cfde2a..3f7348c 100644 --- a/include/driver.hpp +++ b/include/driver.hpp @@ -58,7 +58,7 @@ namespace Driver class Driver { private: - Vector Drivers; + std::vector Drivers; unsigned long DriverUIDs = 0; DriverCode CallDriverEntryPoint(void *fex, void *KAPIAddress); @@ -100,7 +100,7 @@ namespace Driver DriverCode DriverLoadBindProcess(void *DrvExtHdr, uintptr_t DriverAddress, size_t Size, bool IsElf = false); public: - Vector GetDrivers() { return Drivers; } + std::vector GetDrivers() { return Drivers; } void UnloadAllDrivers(); bool UnloadDriver(unsigned long DUID); int IOCB(unsigned long DUID, /* KernelCallback */ void *KCB); diff --git a/include/exec.hpp b/include/exec.hpp index 656ce93..6035b22 100644 --- a/include/exec.hpp +++ b/include/exec.hpp @@ -4,8 +4,8 @@ #include #include -#include #include +#include #include namespace Execute @@ -60,7 +60,7 @@ namespace Execute SpawnData sd; Tasking::IP InstructionPointer; - Vector NeededLibraries; + std::vector NeededLibraries; void *MemoryImage; void *VirtualMemoryImage; @@ -69,7 +69,7 @@ namespace Execute Memory::MemMgr *TmpMem; /* Same as above, for BaseLoad.cpp only */ - Vector auxv; + std::vector auxv; }; struct MmImage diff --git a/include/filesystem.hpp b/include/filesystem.hpp index 7ce21c2..befc455 100644 --- a/include/filesystem.hpp +++ b/include/filesystem.hpp @@ -3,7 +3,7 @@ #include -#include +#include #include namespace VirtualFileSystem @@ -104,7 +104,7 @@ namespace VirtualFileSystem 1 - etc ... */ - Vector Children; + std::vector Children; }; struct File @@ -121,9 +121,9 @@ namespace VirtualFileSystem Node *FileSystemRoot = nullptr; public: - SharedPointer GetPathFromNode(Node *node); + std::shared_ptr GetPathFromNode(Node *node); Node *GetNodeFromPath(const char *Path, Node *Parent = nullptr); - SharedPointer ConvertNodeToFILE(Node *node); + std::shared_ptr ConvertNodeToFILE(Node *node); Node *GetParent(const char *Path, Node *Parent); Node *GetRootNode() { return FileSystemRoot; } @@ -132,7 +132,7 @@ namespace VirtualFileSystem Node *GetChild(const char *Name, Node *Parent); FileStatus RemoveChild(const char *Name, Node *Parent); - SharedPointer NormalizePath(const char *Path, Node *Parent = nullptr); + std::shared_ptr NormalizePath(const char *Path, Node *Parent = nullptr); bool PathExists(const char *Path, Node *Parent = nullptr); Node *CreateRoot(const char *RootName, FileSystemOperations *Operator); Node *Create(const char *Path, NodeFlags Flag, Node *Parent = nullptr); @@ -140,14 +140,14 @@ namespace VirtualFileSystem FileStatus Delete(const char *Path, bool Recursive = false, Node *Parent = nullptr); FileStatus Delete(Node *Path, bool Recursive = false, Node *Parent = nullptr); - SharedPointer Mount(const char *Path, FileSystemOperations *Operator); - FileStatus Unmount(SharedPointer File); + std::shared_ptr Mount(const char *Path, FileSystemOperations *Operator); + FileStatus Unmount(std::shared_ptr File); - size_t Read(SharedPointer File, size_t Offset, uint8_t *Buffer, size_t Size); - size_t Write(SharedPointer File, size_t Offset, uint8_t *Buffer, size_t Size); + size_t Read(std::shared_ptr File, size_t Offset, uint8_t *Buffer, size_t Size); + size_t Write(std::shared_ptr File, size_t Offset, uint8_t *Buffer, size_t Size); - SharedPointer Open(const char *Path, Node *Parent = nullptr); - FileStatus Close(SharedPointer File); + std::shared_ptr Open(const char *Path, Node *Parent = nullptr); + FileStatus Close(std::shared_ptr File); Virtual(); ~Virtual(); diff --git a/include/gui.hpp b/include/gui.hpp index f0abb36..bc87d08 100644 --- a/include/gui.hpp +++ b/include/gui.hpp @@ -225,9 +225,9 @@ namespace GraphicalUserInterface uintptr_t OnClick; }; - Vector Labels; - Vector Panels; - Vector Buttons; + std::vector Labels; + std::vector Panels; + std::vector Buttons; public: void ReplaceFont(Video::Font *NewFont) @@ -286,7 +286,7 @@ namespace GraphicalUserInterface Rect Position; Rect LastPosition; char Title[256]; - Vector Widgets; + std::vector Widgets; void *ParentGUI; bool Maximized; @@ -360,8 +360,8 @@ namespace GraphicalUserInterface ScreenBitmap *DesktopBuffer; ScreenBitmap *OverlayBuffer; ScreenBitmap *CursorBuffer; - Vector Widgets; - Vector Windows; + std::vector Widgets; + std::vector Windows; CursorType Cursor = CursorType::Arrow; CursorType LastCursor = CursorType::Arrow; bool CursorVisible = true; diff --git a/include/ipc.hpp b/include/ipc.hpp index efc9927..5e88e92 100644 --- a/include/ipc.hpp +++ b/include/ipc.hpp @@ -50,7 +50,7 @@ namespace InterProcessCommunication private: NewLock(IPCLock); IPCID NextID = 0; - Vector Handles; + std::vector Handles; Memory::MemMgr *mem; VirtualFileSystem::Node *IPCNode; void *Process; diff --git a/include/memory.hpp b/include/memory.hpp index 640fa2d..496a355 100644 --- a/include/memory.hpp +++ b/include/memory.hpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #endif // __cplusplus @@ -639,7 +638,7 @@ namespace Memory size_t PageCount; }; - Vector GetAllocatedPagesList() { return AllocatedPagesList; } + std::vector GetAllocatedPagesList() { return AllocatedPagesList; } uint64_t GetAllocatedMemorySize(); bool Add(void *Address, size_t Count); @@ -657,7 +656,7 @@ namespace Memory PageTable4 *PageTable; VirtualFileSystem::Node *Directory; - Vector AllocatedPagesList; + std::vector AllocatedPagesList; }; } diff --git a/include/net/arp.hpp b/include/net/arp.hpp index 2206406..7b2fba4 100644 --- a/include/net/arp.hpp +++ b/include/net/arp.hpp @@ -59,7 +59,7 @@ namespace NetworkARP DA_UPDATE = 4 }; - Vector DiscoveredAddresses; + std::vector DiscoveredAddresses; DiscoveredAddress *ManageDiscoveredAddresses(DAType Type, InternetProtocol IP, MediaAccessControl MAC); DiscoveredAddress *Search(InternetProtocol TargetIP); DiscoveredAddress *Update(InternetProtocol TargetIP, MediaAccessControl TargetMAC); diff --git a/include/net/nc.hpp b/include/net/nc.hpp index 28cccd3..51a0931 100644 --- a/include/net/nc.hpp +++ b/include/net/nc.hpp @@ -50,7 +50,7 @@ namespace NetworkInterfaceManager private: Memory::MemMgr *mem; int CardIDs = 0; - Vector Interfaces; + std::vector Interfaces; Tasking::TCB *NetSvcThread; void StopNetworkStack(); diff --git a/include/pci.hpp b/include/pci.hpp index 7287002..93eb768 100644 --- a/include/pci.hpp +++ b/include/pci.hpp @@ -3,8 +3,8 @@ #include -#include #include +#include namespace PCI { @@ -205,15 +205,15 @@ namespace PCI class PCI { private: - Vector Devices; + std::vector Devices; public: - Vector &GetDevices() { return Devices; } + std::vector &GetDevices() { return Devices; } void EnumerateFunction(uintptr_t DeviceAddress, uint64_t Function); void EnumerateDevice(uintptr_t BusAddress, uint64_t Device); void EnumerateBus(uintptr_t BaseAddress, uint64_t Bus); - Vector FindPCIDevice(uint8_t Class, uint8_t Subclass, uint8_t ProgIF); - Vector FindPCIDevice(int VendorID, int DeviceID); + std::vector FindPCIDevice(uint8_t Class, uint8_t Subclass, uint8_t ProgIF); + std::vector FindPCIDevice(int VendorID, int DeviceID); PCI(); ~PCI(); diff --git a/include/smart_ptr.hpp b/include/smart_ptr.hpp new file mode 100644 index 0000000..da2eac0 --- /dev/null +++ b/include/smart_ptr.hpp @@ -0,0 +1,2 @@ +#pragma once +#include diff --git a/include/smartptr.hpp b/include/smartptr.hpp deleted file mode 100644 index 797d1bb..0000000 --- a/include/smartptr.hpp +++ /dev/null @@ -1,266 +0,0 @@ -#ifndef __FENNIX_KERNEL_SMART_POINTER_H__ -#define __FENNIX_KERNEL_SMART_POINTER_H__ - -#include - -#include - -// show debug messages -// #define DEBUG_SMARTPOINTERS 1 - -#ifdef DEBUG_SMARTPOINTERS -#define spdbg(m, ...) debug(m, ##__VA_ARGS__) -#else -#define spdbg(m, ...) -#endif - -/** - * @brief A smart pointer class - * - * This class is a smart pointer class. It is used to manage the lifetime of - * objects. It is a reference counted pointer, so when the last reference to - * the object is removed, the object is deleted. - * - * Basic Usage: - * SmartPointer pointer(new char()); - * *pointer = 'a'; - * printf("%c", *pointer); // Prints "a" - */ -template -class SmartPointer -{ - T *m_RealPointer; - -public: - explicit SmartPointer(T *Pointer = nullptr) - { - spdbg("Smart pointer created (%#lx)", m_RealPointer); - m_RealPointer = Pointer; - } - - ~SmartPointer() - { - spdbg("Smart pointer deleted (%#lx)", m_RealPointer); - delete m_RealPointer, m_RealPointer = nullptr; - } - - T &operator*() - { - spdbg("Smart pointer dereferenced (%#lx)", m_RealPointer); - return *m_RealPointer; - } - - T *operator->() - { - spdbg("Smart pointer dereferenced (%#lx)", m_RealPointer); - return m_RealPointer; - } -}; - -template -class AutoPointer -{ -}; - -template -class UniquePointer -{ -}; - -template -class WeakPointer -{ -}; - -template -class SharedPointer -{ -private: - class Counter - { - private: - unsigned int m_RefCount{}; - - public: - Counter() : m_RefCount(0) { spdbg("Counter %#lx created", this); }; - Counter(const Counter &) = delete; - Counter &operator=(const Counter &) = delete; - ~Counter() { spdbg("Counter %#lx deleted", this); } - void Reset() - { - m_RefCount = 0; - spdbg("Counter reset"); - } - - unsigned int Get() - { - return m_RefCount; - spdbg("Counter returned"); - } - - void operator++() - { - m_RefCount++; - spdbg("Counter incremented"); - } - - void operator++(int) - { - m_RefCount++; - spdbg("Counter incremented"); - } - - void operator--() - { - m_RefCount--; - spdbg("Counter decremented"); - } - - void operator--(int) - { - m_RefCount--; - spdbg("Counter decremented"); - } - }; - - Counter *m_ReferenceCounter; - T *m_RealPointer; - -public: - explicit SharedPointer(T *Pointer = nullptr) - { - m_RealPointer = Pointer; - m_ReferenceCounter = new Counter(); - spdbg("[%#lx] Shared pointer created (ptr=%#lx, ref=%#lx)", this, Pointer, m_ReferenceCounter); - if (Pointer) - (*m_ReferenceCounter)++; - } - - SharedPointer(SharedPointer &SPtr) - { - spdbg("[%#lx] Shared pointer copied (ptr=%#lx, ref=%#lx)", this, SPtr.m_RealPointer, SPtr.m_ReferenceCounter); - m_RealPointer = SPtr.m_RealPointer; - m_ReferenceCounter = SPtr.m_ReferenceCounter; - (*m_ReferenceCounter)++; - } - - ~SharedPointer() - { - spdbg("[%#lx] Shared pointer destructor called", this); - (*m_ReferenceCounter)--; - if (m_ReferenceCounter->Get() == 0) - { - spdbg("[%#lx] Shared pointer deleted (ptr=%#lx, ref=%#lx)", this, m_RealPointer, m_ReferenceCounter); - delete m_ReferenceCounter, m_ReferenceCounter = nullptr; - delete m_RealPointer, m_RealPointer = nullptr; - } - } - - unsigned int GetCount() - { - spdbg("[%#lx] Shared pointer count (%d)", this, m_ReferenceCounter->Get()); - return m_ReferenceCounter->Get(); - } - - T *Get() - { - spdbg("[%#lx] Shared pointer get (%#lx)", this, m_RealPointer); - return m_RealPointer; - } - - T &operator*() - { - spdbg("[%#lx] Shared pointer dereference (ptr*=%#lx)", this, *m_RealPointer); - return *m_RealPointer; - } - - T *operator->() - { - spdbg("[%#lx] Shared pointer dereference (ptr->%#lx)", this, m_RealPointer); - return m_RealPointer; - } - - void reset(T *Pointer = nullptr) - { - if (m_RealPointer == Pointer) - return; - spdbg("[%#lx] Shared pointer reset (ptr=%#lx, ref=%#lx)", this, Pointer, m_ReferenceCounter); - (*m_ReferenceCounter)--; - if (m_ReferenceCounter->Get() == 0) - { - delete m_RealPointer; - delete m_ReferenceCounter; - } - m_RealPointer = Pointer; - m_ReferenceCounter = new Counter(); - if (Pointer) - (*m_ReferenceCounter)++; - } - - void reset() - { - spdbg("[%#lx] Shared pointer reset (ptr=%#lx, ref=%#lx)", this, m_RealPointer, m_ReferenceCounter); - if (m_ReferenceCounter->Get() == 1) - { - delete m_RealPointer, m_RealPointer = nullptr; - delete m_ReferenceCounter, m_ReferenceCounter = nullptr; - } - else - { - (*m_ReferenceCounter)--; - } - } - - void swap(SharedPointer &Other) - { - spdbg("[%#lx] Shared pointer swap (ptr=%#lx, ref=%#lx <=> ptr=%#lx, ref=%#lx)", - this, m_RealPointer, m_ReferenceCounter, Other.m_RealPointer, Other.m_ReferenceCounter); - T *tempRealPointer = m_RealPointer; - Counter *tempReferenceCounter = m_ReferenceCounter; - m_RealPointer = Other.m_RealPointer; - m_ReferenceCounter = Other.m_ReferenceCounter; - Other.m_RealPointer = tempRealPointer; - Other.m_ReferenceCounter = tempReferenceCounter; - } -}; - -template -struct RemoveReference -{ - typedef T type; -}; - -template -struct RemoveReference -{ - typedef T type; -}; - -template -struct RemoveReference -{ - typedef T type; -}; - -template -using RemoveReference_t = typename RemoveReference::type; - -template -T &&forward(RemoveReference_t &t) -{ - return static_cast(t); -}; - -template -T &&forward(RemoveReference_t &&t) -{ - return static_cast(t); -}; - -template -SharedPointer MakeShared(Args &&...args) -{ - return SharedPointer(new T(forward(args)...)); -}; - -#endif // !__FENNIX_KERNEL_SMART_POINTER_H__ diff --git a/include/std.hpp b/include/std.hpp index 5f150cb..22d5b00 100644 --- a/include/std.hpp +++ b/include/std.hpp @@ -1,10 +1,18 @@ +/* This function includes all the standard headers and defines some useful macros. + * Note: This std implementation is not complete. + */ #ifndef __FENNIX_KERNEL_STD_H__ #define __FENNIX_KERNEL_STD_H__ #include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include /** * @brief // stub namespace for std::align_val_t and new operator @@ -13,103 +21,39 @@ namespace std { typedef __SIZE_TYPE__ size_t; + static const size_t npos = -1; + enum class align_val_t : std::size_t { }; - template - class smart_ptr + template + OutputIt transform(InputIt first, InputIt last, OutputIt result, UnaryOperation op) { - public: - using SmartPointer::SmartPointer; - using SmartPointer::operator*; - using SmartPointer::operator->; + while (first != last) + { + *result = op(*first); + ++first; + ++result; + } + return result; }; - template - class auto_ptr + inline __always_inline int tolower(int c) { - public: - using AutoPointer::AutoPointer; - }; + if (c >= 'A' && c <= 'Z') + return c + ('a' - 'A'); + else + return c; + } - template - class unique_ptr + inline __always_inline int toupper(int c) { - public: - using UniquePointer::UniquePointer; - }; - - template - class weak_ptr - { - public: - using WeakPointer::WeakPointer; - }; - - template - class shared_ptr - { - using SharedPointer::SharedPointer; - using SharedPointer::operator*; - using SharedPointer::operator->; - }; - - template - struct remove_reference - { - typedef T type; - }; - - template - struct remove_reference - { - typedef T type; - }; - - template - struct remove_reference - { - typedef T type; - }; - - template - using remove_reference_t = typename remove_reference::type; - - template - T &&forward(remove_reference_t &t) { return static_cast(t); }; - - template - T &&forward(remove_reference_t &&t) { return static_cast(t); }; - - template - shared_ptr make_shared(Args &&...args) - { - return SharedPointer(new T(forward(args)...)); - }; - - template - class vector : public Vector - { - public: - using Vector::Vector; - using Vector::operator=; - using Vector::operator[]; - using typename Vector::iterator; - }; - - class string : public String - { - public: - using String::String; - using String::operator=; - using String::operator+; - using String::operator<<; - using String::operator[]; - using String::operator==; - using String::operator!=; - using typename String::iterator; - }; + if (c >= 'a' && c <= 'z') + return c - ('a' - 'A'); + else + return c; + } } #endif // !__FENNIX_KERNEL_STD_H__ diff --git a/include/std/functional.hpp b/include/std/functional.hpp new file mode 100644 index 0000000..412fe30 --- /dev/null +++ b/include/std/functional.hpp @@ -0,0 +1,33 @@ +#ifndef __FENNIX_KERNEL_STD_FUNCTIONAL_H__ +#define __FENNIX_KERNEL_STD_FUNCTIONAL_H__ + +#include +#include + +namespace std +{ + template + struct hash + { + size_t operator()(const Key &key) const + { + static_assert(sizeof(size_t) == sizeof(uint64_t)); // size_t and uint64_t must have the same size + const uint64_t fnv_offset_basis = 14695981039346656037ull; + const uint64_t fnv_prime = 1099511628211ull; + + const uint8_t *data = reinterpret_cast(&key); + const size_t size = sizeof(Key); + uint64_t hash = fnv_offset_basis; + + for (size_t i = 0; i < size; ++i) + { + hash ^= static_cast(data[i]); + hash *= fnv_prime; + } + + return static_cast(hash); + } + }; +} + +#endif // !__FENNIX_KERNEL_STD_FUNCTIONAL_H__ diff --git a/include/std/list.hpp b/include/std/list.hpp new file mode 100644 index 0000000..ac08402 --- /dev/null +++ b/include/std/list.hpp @@ -0,0 +1,207 @@ +#ifndef __FENNIX_KERNEL_STD_LIST_H__ +#define __FENNIX_KERNEL_STD_LIST_H__ + +#include +#include + +namespace std +{ + template + class list + { + private: + struct Node + { + T value; + Node *prev; + Node *next; + + Node(const T &v, Node *p = nullptr, Node *n = nullptr) + : value(v), prev(p), next(n) {} + }; + + Node *head; + Node *tail; + size_t size; + + public: + list() : head(nullptr), tail(nullptr), size(0) {} + ~list() { clear(); } + list(const list &other) : head(nullptr), tail(nullptr), size(0) { *this = other; } + + void push_back(const T &value) + { + Node *new_node = new Node(value, tail, nullptr); + if (empty()) + { + head = tail = new_node; + } + else + { + tail->next = new_node; + tail = new_node; + } + ++size; + } + + void pop_back() + { + if (empty()) + { + throw std::runtime_error("list is empty"); + } + else if (head == tail) + { + delete tail; + head = tail = nullptr; + --size; + } + else + { + Node *old_tail = tail; + tail = tail->prev; + tail->next = nullptr; + delete old_tail; + --size; + } + } + + void push_front(const T &value) + { + Node *new_node = new Node(value, nullptr, head); + if (empty()) + { + head = tail = new_node; + } + else + { + head->prev = new_node; + head = new_node; + } + ++size; + } + + void pop_front() + { + if (empty()) + { + throw std::runtime_error("list is empty"); + } + else if (head == tail) + { + delete head; + head = tail = nullptr; + --size; + } + else + { + Node *old_head = head; + head = head->next; + head->prev = nullptr; + delete old_head; + --size; + } + } + + bool empty() const { return size == 0; } + + void clear() + { + while (!empty()) + pop_back(); + } + + list &operator=(const list &other) + { + if (this != &other) + { + clear(); + for (const T &value : other) + { + push_back(value); + } + } + return *this; + } + + class iterator + { + private: + Node *node; + friend class list; + + public: + iterator(Node *p = nullptr) : node(p) {} + T &operator*() const { return node->value; } + T *operator->() const { return &node->value; } + + iterator &operator++() + { + node = node->next; + return *this; + } + + iterator &operator--() + { + node = node->prev; + return *this; + } + + iterator operator++(int) + { + iterator tmp = *this; + ++*this; + return tmp; + } + + iterator operator--(int) + { + iterator tmp = *this; + --*this; + return tmp; + } + + bool operator==(const iterator &rhs) const { return node == rhs.node; } + bool operator!=(const iterator &rhs) const { return node != rhs.node; } + }; + + iterator begin() { return iterator(head); } + iterator end() { return iterator(nullptr); } + + iterator insert(iterator pos, const T &value) + { + if (pos == end()) + { + push_back(value); + return iterator(tail); + } + else if (pos == begin()) + { + push_front(value); + return iterator(head); + } + else + { + Node *p = pos.node; + Node *new_node = new Node(value, p->prev, p); + p->prev->next = new_node; + p->prev = new_node; + ++size; + return iterator(new_node); + } + } + + iterator erase(iterator pos) + { + Node *p = pos.node; + iterator next(p->next); + p->prev->next = p->next; + p->next->prev = p->prev; + delete p; + --size; + return next; + } + }; +} + +#endif // !__FENNIX_KERNEL_STD_LIST_H__ diff --git a/include/std/smart_ptr.hpp b/include/std/smart_ptr.hpp new file mode 100644 index 0000000..bdaf54f --- /dev/null +++ b/include/std/smart_ptr.hpp @@ -0,0 +1,263 @@ +#ifndef __FENNIX_KERNEL_STD_SMART_POINTER_H__ +#define __FENNIX_KERNEL_STD_SMART_POINTER_H__ + +#include + +#include + +// show debug messages +// #define DEBUG_SMARTPOINTERS 1 + +#ifdef DEBUG_SMARTPOINTERS +#define spdbg(m, ...) debug(m, ##__VA_ARGS__) +#else +#define spdbg(m, ...) +#endif + +namespace std +{ + /** + * @brief A smart pointer class + * + * This class is a smart pointer class. It is used to manage the lifetime of + * objects. It is a reference counted pointer, so when the last reference to + * the object is removed, the object is deleted. + * + * Basic Usage: + * smart_ptr pointer(new char()); + * *pointer = 'a'; + * printf("%c", *pointer); // Prints "a" + */ + template + class smart_ptr + { + T *m_RealPointer; + + public: + explicit smart_ptr(T *Pointer = nullptr) + { + spdbg("Smart pointer created (%#lx)", m_RealPointer); + m_RealPointer = Pointer; + } + + ~smart_ptr() + { + spdbg("Smart pointer deleted (%#lx)", m_RealPointer); + delete m_RealPointer, m_RealPointer = nullptr; + } + + T &operator*() + { + spdbg("Smart pointer dereferenced (%#lx)", m_RealPointer); + return *m_RealPointer; + } + + T *operator->() + { + spdbg("Smart pointer dereferenced (%#lx)", m_RealPointer); + return m_RealPointer; + } + }; + + template + class auto_ptr + { + }; + + template + class unique_ptr + { + }; + + template + class weak_ptr + { + }; + + template + class shared_ptr + { + private: + class Counter + { + private: + unsigned int m_RefCount{}; + + public: + Counter() : m_RefCount(0) { spdbg("Counter %#lx created", this); }; + Counter(const Counter &) = delete; + Counter &operator=(const Counter &) = delete; + ~Counter() { spdbg("Counter %#lx deleted", this); } + void Reset() + { + m_RefCount = 0; + spdbg("Counter reset"); + } + + unsigned int Get() + { + return m_RefCount; + spdbg("Counter returned"); + } + + void operator++() + { + m_RefCount++; + spdbg("Counter incremented"); + } + + void operator++(int) + { + m_RefCount++; + spdbg("Counter incremented"); + } + + void operator--() + { + m_RefCount--; + spdbg("Counter decremented"); + } + + void operator--(int) + { + m_RefCount--; + spdbg("Counter decremented"); + } + }; + + Counter *m_ReferenceCounter; + T *m_RealPointer; + + public: + explicit shared_ptr(T *Pointer = nullptr) + { + m_RealPointer = Pointer; + m_ReferenceCounter = new Counter(); + spdbg("[%#lx] Shared pointer created (ptr=%#lx, ref=%#lx)", this, Pointer, m_ReferenceCounter); + if (Pointer) + (*m_ReferenceCounter)++; + } + + shared_ptr(shared_ptr &SPtr) + { + spdbg("[%#lx] Shared pointer copied (ptr=%#lx, ref=%#lx)", this, SPtr.m_RealPointer, SPtr.m_ReferenceCounter); + m_RealPointer = SPtr.m_RealPointer; + m_ReferenceCounter = SPtr.m_ReferenceCounter; + (*m_ReferenceCounter)++; + } + + ~shared_ptr() + { + spdbg("[%#lx] Shared pointer destructor called", this); + (*m_ReferenceCounter)--; + if (m_ReferenceCounter->Get() == 0) + { + spdbg("[%#lx] Shared pointer deleted (ptr=%#lx, ref=%#lx)", this, m_RealPointer, m_ReferenceCounter); + delete m_ReferenceCounter, m_ReferenceCounter = nullptr; + delete m_RealPointer, m_RealPointer = nullptr; + } + } + + unsigned int GetCount() + { + spdbg("[%#lx] Shared pointer count (%d)", this, m_ReferenceCounter->Get()); + return m_ReferenceCounter->Get(); + } + + T *Get() + { + spdbg("[%#lx] Shared pointer get (%#lx)", this, m_RealPointer); + return m_RealPointer; + } + + T &operator*() + { + spdbg("[%#lx] Shared pointer dereference (ptr*=%#lx)", this, *m_RealPointer); + return *m_RealPointer; + } + + T *operator->() + { + spdbg("[%#lx] Shared pointer dereference (ptr->%#lx)", this, m_RealPointer); + return m_RealPointer; + } + + void reset(T *Pointer = nullptr) + { + if (m_RealPointer == Pointer) + return; + spdbg("[%#lx] Shared pointer reset (ptr=%#lx, ref=%#lx)", this, Pointer, m_ReferenceCounter); + (*m_ReferenceCounter)--; + if (m_ReferenceCounter->Get() == 0) + { + delete m_RealPointer; + delete m_ReferenceCounter; + } + m_RealPointer = Pointer; + m_ReferenceCounter = new Counter(); + if (Pointer) + (*m_ReferenceCounter)++; + } + + void reset() + { + spdbg("[%#lx] Shared pointer reset (ptr=%#lx, ref=%#lx)", this, m_RealPointer, m_ReferenceCounter); + if (m_ReferenceCounter->Get() == 1) + { + delete m_RealPointer, m_RealPointer = nullptr; + delete m_ReferenceCounter, m_ReferenceCounter = nullptr; + } + else + { + (*m_ReferenceCounter)--; + } + } + + void swap(shared_ptr &Other) + { + spdbg("[%#lx] Shared pointer swap (ptr=%#lx, ref=%#lx <=> ptr=%#lx, ref=%#lx)", + this, m_RealPointer, m_ReferenceCounter, Other.m_RealPointer, Other.m_ReferenceCounter); + T *tempRealPointer = m_RealPointer; + Counter *tempReferenceCounter = m_ReferenceCounter; + m_RealPointer = Other.m_RealPointer; + m_ReferenceCounter = Other.m_ReferenceCounter; + Other.m_RealPointer = tempRealPointer; + Other.m_ReferenceCounter = tempReferenceCounter; + } + }; + + template + struct remove_reference + { + typedef T type; + }; + + template + struct remove_reference + { + typedef T type; + }; + + template + struct remove_reference + { + typedef T type; + }; + + template + using remove_reference_t = typename remove_reference::type; + + template + T &&forward(remove_reference_t &t) { return static_cast(t); }; + + template + T &&forward(remove_reference_t &&t) { return static_cast(t); }; + + template + shared_ptr make_shared(Args &&...args) + { + return shared_ptr(new T(forward(args)...)); + }; +} + +#endif // !__FENNIX_KERNEL_STD_SMART_POINTER_H__ diff --git a/include/std/stdexcept.hpp b/include/std/stdexcept.hpp new file mode 100644 index 0000000..6260675 --- /dev/null +++ b/include/std/stdexcept.hpp @@ -0,0 +1,19 @@ +#ifndef __FENNIX_KERNEL_STD_STDEXCEPT_H__ +#define __FENNIX_KERNEL_STD_STDEXCEPT_H__ + +#include + +namespace std +{ + class runtime_error + { + private: + const char *m_what; + + public: + runtime_error(const char *what_arg) : m_what(what_arg) {} + const char *what() const { return m_what; } + }; +} + +#endif // !__FENNIX_KERNEL_STD_STDEXCEPT_H__ diff --git a/include/std/string.hpp b/include/std/string.hpp new file mode 100644 index 0000000..630efcb --- /dev/null +++ b/include/std/string.hpp @@ -0,0 +1,536 @@ +#ifndef __FENNIX_KERNEL_STD_STRING_H__ +#define __FENNIX_KERNEL_STD_STRING_H__ + +#include +#include +#include + +// show debug messages +// #define DEBUG_CPP_STRING 1 + +#ifdef DEBUG_CPP_STRING +#define strdbg(m, ...) debug(m, ##__VA_ARGS__) +#else +#define strdbg(m, ...) +#endif + +// TODO: Somewhere the delete is called twice, causing a double free error. + +namespace std +{ + /** + * @brief String class + * String class that can be used to store strings. + */ + class string + { + private: + char *m_Data; + size_t m_Length; + size_t m_Capacity; + + public: + static const size_t npos = -1; + + string(const char *Str = "") + { + this->m_Length = strlen(Str); + this->m_Capacity = this->m_Length + 1; + this->m_Data = new char[m_Capacity]; + strcpy(m_Data, Str); + strdbg("New string created: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); + } + + ~string() + { + strdbg("String deleted: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); + delete[] this->m_Data, this->m_Data = nullptr; + } + + int length() const + { + strdbg("String length: %d", this->m_Length); + return this->m_Length; + } + + int capacity() const + { + strdbg("String capacity: %d", this->m_Capacity); + return this->m_Capacity; + } + + const char *c_str() const + { + strdbg("String data: \"%s\"", this->m_Data); + return this->m_Data; + } + + void resize(size_t NewLength) + { + strdbg("String resize: %d", NewLength); + if (NewLength > this->m_Capacity) + { + size_t newCapacity = NewLength + 1; + char *newData = new char[newCapacity]; + + strcpy(newData, this->m_Data); + + strdbg("old: %#lx, new: %#lx", this->m_Data, newData); + delete[] this->m_Data; + this->m_Data = newData; + this->m_Capacity = newCapacity; + } + this->m_Length = NewLength; + this->m_Data[m_Length] = '\0'; + strdbg("String resized: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); + } + + void concat(const string &Other) + { + int NewLength = this->m_Length + Other.m_Length; + this->resize(NewLength); + + strcat(m_Data, Other.m_Data); + strdbg("String concatenated: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); + } + + bool empty() const + { + strdbg("String empty: %d", this->m_Length == 0); + return this->m_Length == 0; + } + + size_t size() const + { + strdbg("String size: %d", this->m_Length); + return this->m_Length; + } + + void clear() + { + strdbg("String clear"); + this->resize(0); + } + + size_t find(const char *Str, size_t Pos = 0) const + { + strdbg("String find: \"%s\", %d", Str, Pos); + if (Pos >= this->m_Length) + return npos; + + for (size_t i = Pos; i < this->m_Length; i++) + { + bool found = true; + for (size_t j = 0; Str[j] != '\0'; j++) + { + if (this->m_Data[i + j] != Str[j]) + { + found = false; + break; + } + } + if (found) + return i; + } + return npos; + } + + size_t find(const string &Str, size_t Pos = 0) const + { + strdbg("String find: \"%s\", %d", Str.c_str(), Pos); + return this->find(Str.c_str(), Pos); + } + + void erase(int Index, int Count = 1) + { + strdbg("String erase: %d, %d", Index, Count); + if (Index < 0 || (size_t)Index >= this->m_Length) + return; + + if (Count < 0) + return; + + if ((size_t)(Index + Count) > this->m_Length) + Count = this->m_Length - Index; + + for (size_t i = Index; i < this->m_Length - Count; i++) + this->m_Data[i] = this->m_Data[i + Count]; + + this->m_Length -= Count; + this->m_Data[m_Length] = '\0'; + strdbg("String erased: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); + } + + size_t find_last_not_of(const char *Str, size_t Pos = npos) const + { + strdbg("String find_last_not_of: \"%s\", %d", Str, Pos); + if (Pos == npos) + Pos = this->m_Length - 1; + + for (int i = (int)Pos; i >= 0; i--) + { + bool found = false; + for (size_t j = 0; Str[j] != '\0'; j++) + { + if (this->m_Data[i] == Str[j]) + { + found = true; + break; + } + } + if (!found) + return i; + } + return npos; + } + + size_t find_first_not_of(const char *Str, size_t Pos = 0) const + { + strdbg("String find_first_not_of: \"%s\", %d", Str, Pos); + if (Pos >= this->m_Length) + return npos; + + for (size_t i = Pos; i < this->m_Length; i++) + { + bool found = false; + for (size_t j = 0; Str[j] != '\0'; j++) + { + if (this->m_Data[i] == Str[j]) + { + found = true; + break; + } + } + if (!found) + return i; + } + return npos; + } + + size_t find_first_of(const char *Str, size_t Pos = 0) const + { + strdbg("String find_first_of: \"%s\", %d", Str, Pos); + if (Pos >= this->m_Length) + return npos; + + for (size_t i = Pos; i < this->m_Length; i++) + { + bool found = false; + for (size_t j = 0; Str[j] != '\0'; j++) + { + if (this->m_Data[i] == Str[j]) + { + found = true; + break; + } + } + if (found) + return i; + } + return npos; + } + + size_t find_last_of(const char *Str, size_t Pos = npos) const + { + strdbg("String find_last_of: \"%s\", %d", Str, Pos); + if (Pos == npos) + Pos = this->m_Length - 1; + + for (int i = (int)Pos; i >= 0; i--) + { + bool found = false; + for (int j = 0; Str[j] != '\0'; j++) + { + if (this->m_Data[i] == Str[j]) + { + found = true; + break; + } + } + if (found) + return i; + } + return npos; + } + + size_t find_first_of(char C, size_t Pos = 0) const + { + strdbg("String find_first_of: '%c', %d", C, Pos); + if (Pos >= this->m_Length) + return npos; + + for (size_t i = Pos; i < this->m_Length; i++) + { + if (this->m_Data[i] == C) + return i; + } + return npos; + } + + size_t find_last_of(char C, size_t Pos = npos) const + { + strdbg("String find_last_of: '%c', %d", C, Pos); + if (Pos == npos) + Pos = this->m_Length - 1; + + for (int i = (int)Pos; i >= 0; i--) + { + if (this->m_Data[i] == C) + return i; + } + return npos; + } + + size_t substr(const char *Str, size_t Pos = 0) const + { + strdbg("String substr: \"%s\", %d", Str, Pos); + if (Pos >= this->m_Length) + return npos; + + for (size_t i = Pos; i < this->m_Length; i++) + { + bool found = true; + for (size_t j = 0; Str[j] != '\0'; j++) + { + if (this->m_Data[i + j] != Str[j]) + { + found = false; + break; + } + } + if (found) + return i; + } + return npos; + } + + size_t substr(const string &Str, size_t Pos = 0) const + { + strdbg("String substr: \"%s\", %d", Str.c_str(), Pos); + return this->substr(Str.c_str(), Pos); + } + + string substr(size_t Pos = 0, size_t Count = npos) const + { + strdbg("String substr: %d, %d", Pos, Count); + if (Pos >= this->m_Length) + return string(); + + if (Count == npos) + Count = this->m_Length - Pos; + + if (Pos + Count > this->m_Length) + Count = this->m_Length - Pos; + + string ret; + ret.resize(Count); + for (size_t i = 0; i < Count; i++) + ret.m_Data[i] = this->m_Data[Pos + i]; + ret.m_Data[Count] = '\0'; + return ret; + } + + void replace(size_t Pos, size_t Count, const char *Str) + { + strdbg("String replace: %d, %d, \"%s\"", Pos, Count, Str); + if (Pos >= this->m_Length) + return; + + if ((int64_t)Count < 0) + return; + + if (Pos + Count > this->m_Length) + Count = this->m_Length - Pos; + + int NewLength = this->m_Length - Count + strlen(Str); + this->resize(NewLength); + + for (size_t i = this->m_Length - 1; i >= Pos + strlen(Str); i--) + this->m_Data[i] = this->m_Data[i - strlen(Str) + Count]; + + for (unsigned long i = 0; i < strlen(Str); i++) + this->m_Data[Pos + i] = Str[i]; + + strdbg("String replaced: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); + } + + void replace(size_t Pos, size_t Count, const string &Str) + { + strdbg("String replace: %d, %d, \"%s\"", Pos, Count, Str.m_Data); + if (Pos >= this->m_Length) + return; + + if ((int64_t)Count < 0) + return; + + if (Pos + Count > this->m_Length) + Count = this->m_Length - Pos; + + int NewLength = this->m_Length - Count + Str.m_Length; + this->resize(NewLength); + + for (size_t i = this->m_Length - 1; i >= Pos + Str.m_Length; i--) + this->m_Data[i] = this->m_Data[i - Str.m_Length + Count]; + + for (size_t i = 0; i < Str.m_Length; i++) + this->m_Data[Pos + i] = Str.m_Data[i]; + + strdbg("String replaced: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); + } + + string operator+(const string &Other) const + { + string result = *this; + result.concat(Other); + strdbg("String added: \"%s\" (data: %#lx, length: %d, capacity: %d)", result.m_Data, result.m_Data, result.m_Length, result.m_Capacity); + return result; + } + + string operator+(const char *Other) const + { + string result = *this; + result.concat(Other); + strdbg("String added: \"%s\" (data: %#lx, length: %d, capacity: %d)", result.m_Data, result.m_Data, result.m_Length, result.m_Capacity); + return result; + } + + string &operator+=(const string &Other) + { + this->concat(Other); + strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); + return *this; + } + + string &operator+=(const char *Other) + { + this->concat(Other); + strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); + return *this; + } + + /* warning: implicitly-declared ‘constexpr String::String(const String&)’ is deprecated [-Wdeprecated-copy] */ + string &operator=(const string &Other) = default; + + // string &operator=(const string &Other) + // { + // if (this != &Other) + // { + // delete[] this->m_Data; + // this->m_Data = Other.m_Data; + // this->m_Length = Other.m_Length; + // this->m_Capacity = Other.m_Capacity; + // strdbg("String assigned: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); + // } + // return *this; + // } + + string &operator=(const char *Other) + { + this->m_Length = strlen(Other); + this->m_Capacity = this->m_Length + 1; + delete[] this->m_Data; + this->m_Data = new char[m_Capacity]; + strcpy(m_Data, Other); + strdbg("String assigned: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); + return *this; + } + + string &operator<<(const string &Other) + { + this->concat(Other); + strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); + return *this; + } + + string &operator<<(const char *Other) + { + this->concat(Other); + strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); + return *this; + } + + char &operator[](int Index) + { + strdbg("String index: %d", Index); + return this->m_Data[Index]; + } + + const char &operator[](int Index) const + { + strdbg("String index: %d", Index); + return this->m_Data[Index]; + } + + bool operator==(const string &Other) const + { + strdbg("String compared: \"%s\" == \"%s\"", this->m_Data, Other.m_Data); + return strcmp(this->m_Data, Other.m_Data) == 0; + } + + bool operator!=(const char *Other) const + { + strdbg("String compared: \"%s\" != \"%s\"", this->m_Data, Other); + return strcmp(this->m_Data, Other) != 0; + } + + bool operator!=(const string &Other) const + { + strdbg("String compared: \"%s\" != \"%s\"", this->m_Data, Other.m_Data); + return strcmp(this->m_Data, Other.m_Data) != 0; + } + + bool operator==(const char *Other) const + { + strdbg("String compared: \"%s\" == \"%s\"", this->m_Data, Other); + return strcmp(this->m_Data, Other) == 0; + } + + class iterator + { + private: + char *m_Pointer; + + public: + iterator(char *Pointer) : m_Pointer(Pointer) {} + + iterator &operator++() + { + ++this->m_Pointer; + strdbg("String iterator incremented: %#lx", this->m_Pointer); + return *this; + } + + char &operator*() + { + strdbg("String iterator dereferenced: %#lx", this->m_Pointer); + return *this->m_Pointer; + } + + bool operator!=(const iterator &Other) const + { + strdbg("String iterator compared: %#lx != %#lx", this->m_Pointer, Other.m_Pointer); + return this->m_Pointer != Other.m_Pointer; + } + + bool operator==(const iterator &Other) const + { + strdbg("String iterator compared: %#lx == %#lx", this->m_Pointer, Other.m_Pointer); + return this->m_Pointer == Other.m_Pointer; + } + }; + + iterator begin() + { + strdbg("String iterator begin: %#lx", this->m_Data); + return iterator(this->m_Data); + } + + iterator end() + { + strdbg("String iterator end: %#lx", this->m_Data + this->m_Length); + return iterator(this->m_Data + this->m_Length); + } + }; +} + +#endif // !__FENNIX_KERNEL_STD_STRING_H__ diff --git a/include/std/unordered_map.hpp b/include/std/unordered_map.hpp new file mode 100644 index 0000000..8927ce8 --- /dev/null +++ b/include/std/unordered_map.hpp @@ -0,0 +1,113 @@ +#ifndef __FENNIX_KERNEL_STD_UNORDERED_MAP_H__ +#define __FENNIX_KERNEL_STD_UNORDERED_MAP_H__ + +#include +#include +#include +#include + +namespace std +{ + template + class unordered_map + { + public: + typedef std::pair key_value_pair; + typedef std::list bucket; + typedef typename std::vector::iterator iterator; + typedef typename std::vector::const_iterator const_iterator; + + private: + static const size_t DEFAULT_NUM_BUCKETS = 10; + std::vector m_buckets; + + size_t hash(const key_type &key) const + { + std::hash hash_function; + return hash_function(key) % m_buckets.size(); + } + + public: + unordered_map() : m_buckets(DEFAULT_NUM_BUCKETS) {} + unordered_map(size_t num_buckets) : m_buckets(num_buckets) {} + + void insert(const key_value_pair &pair) + { + size_t bucket_index = hash(pair.first); + bucket &bucket = m_buckets[bucket_index]; + for (auto it = bucket.begin(); it != bucket.end(); ++it) + { + if (it->first == pair.first) + { + it->second = pair.second; + return; + } + } + bucket.push_back(pair); + } + + bool contains(const key_type &key) const + { + size_t bucket_index = hash(key); + const bucket &bucket = m_buckets[bucket_index]; + for (auto it = bucket.begin(); it != bucket.end(); ++it) + { + if (it->first == key) + { + return true; + } + } + return false; + } + + iterator find(const key_type &k) + { + size_t bucket_index = hash(k); + bucket &bucket = m_buckets[bucket_index]; + for (auto it = bucket.begin(); it != bucket.end(); ++it) + { + if (it->first == k) + return it; + } + return bucket.end(); + } + + const_iterator find(const key_type &k) const + { + size_t bucket_index = hash(k); + const bucket &bucket = m_buckets[bucket_index]; + for (auto it = bucket.begin(); it != bucket.end(); ++it) + { + if (it->first == k) + return it; + } + return bucket.end(); + } + + iterator end() noexcept { return m_buckets.end(); } + + size_t size() const + { + size_t count = 0; + for (const auto &bucket : m_buckets) + count += bucket.size(); + + return count; + } + + value_type &operator[](const key_type &key) + { + size_t bucket_index = hash(key); + bucket &bucket = m_buckets[bucket_index]; + for (auto it = bucket.begin(); it != bucket.end(); ++it) + { + if (it->first == key) + return it->second; + } + bucket.emplace_back(key, value_type()); + return bucket.back().second; + } + }; +} + +#endif // !__FENNIX_KERNEL_STD_UNORDERED_MAP_H__ diff --git a/include/std/utility.hpp b/include/std/utility.hpp new file mode 100644 index 0000000..f371090 --- /dev/null +++ b/include/std/utility.hpp @@ -0,0 +1,22 @@ +#ifndef __FENNIX_KERNEL_STD_UTILITY_H__ +#define __FENNIX_KERNEL_STD_UTILITY_H__ + +#include + +namespace std +{ + template + struct pair + { + typedef T1 first_type; + typedef T2 second_type; + + T1 first; + T2 second; + + pair() : first(T1()), second(T2()) {} + pair(const T1 &x, const T2 &y) : first(x), second(y) {} + }; +} + +#endif // !__FENNIX_KERNEL_STD_UTILITY_H__ diff --git a/include/std/vector.hpp b/include/std/vector.hpp new file mode 100644 index 0000000..7332e9e --- /dev/null +++ b/include/std/vector.hpp @@ -0,0 +1,267 @@ +#ifndef __FENNIX_KERNEL_STD_VECTOR_H__ +#define __FENNIX_KERNEL_STD_VECTOR_H__ + +#include +#include +#include + +namespace std +{ + template + class vector + { + private: + size_t VectorSize = 0; + size_t VectorCapacity = 0; + T *VectorBuffer = nullptr; + + public: + typedef T *iterator; + typedef const T *const_iterator; + + NIF vector() + { +#ifdef DEBUG_MEM_ALLOCATION + debug("VECTOR INIT: vector( )"); +#endif + VectorCapacity = 0; + VectorSize = 0; + VectorBuffer = 0; + } + + NIF vector(size_t Size) + { + VectorCapacity = Size; + VectorSize = Size; +#ifdef DEBUG_MEM_ALLOCATION + debug("VECTOR INIT: vector( %lld )", Size); +#endif + VectorBuffer = new T[Size]; + } + + NIF vector(size_t Size, const T &Initial) + { + VectorSize = Size; + VectorCapacity = Size; +#ifdef DEBUG_MEM_ALLOCATION + debug("VECTOR INIT: vector( %lld %llx )", Size, Initial); +#endif + assert(Size > 0); + VectorBuffer = new T[Size]; + for (size_t i = 0; i < Size; i++) + VectorBuffer[i] = Initial; + } + + NIF vector(const vector &vector) + { + VectorSize = vector.VectorSize; + VectorCapacity = vector.VectorCapacity; +#ifdef DEBUG_MEM_ALLOCATION + debug("VECTOR INIT: vector( )->Size: %lld", VectorSize); +#endif + assert(VectorSize > 0); + VectorBuffer = new T[VectorSize]; + for (size_t i = 0; i < VectorSize; i++) + VectorBuffer[i] = vector.VectorBuffer[i]; + } + + NIF ~vector() + { +#ifdef DEBUG_MEM_ALLOCATION + debug("VECTOR INIT: ~vector( ~%lx )", VectorBuffer); +#endif + VectorSize = 0; + VectorCapacity = 0; + if (VectorBuffer != nullptr) + { + delete[] VectorBuffer, VectorBuffer = nullptr; + } + } + + NIF void remove(size_t Position) + { + if (Position >= VectorSize) + return; + memset(&*(VectorBuffer + Position), 0, sizeof(T)); + for (size_t i = 0; i < VectorSize - 1; i++) + { + *(VectorBuffer + Position + i) = *(VectorBuffer + Position + i + 1); + } + VectorSize--; + } + + NIF void remove(const T &Value) + { + for (size_t i = 0; i < VectorSize; i++) + { + if (VectorBuffer[i] == Value) + { + remove(i); + return; + } + } + } + + NIF T &null_elem() + { + static T null_elem; + return null_elem; + } + + NIF bool null_elem(size_t Index) + { + if (!reinterpret_cast(&VectorBuffer[Index])) + return false; + return true; + } + + NIF T &next(size_t Position) + { + if (Position + 1 < VectorSize && reinterpret_cast(&VectorBuffer[Position + 1])) + return VectorBuffer[Position + 1]; + warn("next( %lld ) is null (requested by %#lx)", Position, __builtin_return_address(0)); + return this->null_elem(); + } + + NIF T &prev(size_t Position) + { + if (Position > 0 && reinterpret_cast(&VectorBuffer[Position - 1])) + return VectorBuffer[Position - 1]; + warn("prev( %lld ) is null (requested by %#lx)", Position, __builtin_return_address(0)); + return this->null_elem(); + } + + NIF T &next(const T &Value) + { + for (size_t i = 0; i < VectorSize; i++) + { + if (VectorBuffer[i] == Value) + { + if (i + 1 < VectorSize && reinterpret_cast(&VectorBuffer[i + 1])) + return VectorBuffer[i + 1]; + else + break; + } + } + warn("next( %#lx ) is null (requested by %#lx)", Value, __builtin_return_address(0)); + return this->null_elem(); + } + + NIF T &prev(const T &Value) + { + for (size_t i = 0; i < VectorSize; i++) + { + if (VectorBuffer[i] == Value) + { + if (i > 0 && reinterpret_cast(&VectorBuffer[i - 1])) + return VectorBuffer[i - 1]; + else + break; + } + } + warn("prev( %#lx ) is null (requested by %#lx)", Value, __builtin_return_address(0)); + return this->null_elem(); + } + + NIF size_t capacity() const { return VectorCapacity; } + + NIF size_t size() const { return VectorSize; } + + NIF bool empty() const; + + NIF iterator begin() { return VectorBuffer; } + + NIF iterator end() { return VectorBuffer + size(); } + + NIF T &front() { return VectorBuffer[0]; } + + NIF T &back() { return VectorBuffer[VectorSize - 1]; } + + NIF void push_back(const T &Value) + { + if (VectorSize >= VectorCapacity) + reserve(VectorCapacity + 5); + VectorBuffer[VectorSize++] = Value; + } + + NIF void pop_back() { VectorSize--; } + + NIF void reverse() + { + if (VectorSize <= 1) + return; + for (size_t i = 0, j = VectorSize - 1; i < j; i++, j--) + { + T c = *(VectorBuffer + i); + *(VectorBuffer + i) = *(VectorBuffer + j); + *(VectorBuffer + j) = c; + } + } + + NIF void reserve(size_t Capacity) + { + if (VectorBuffer == 0) + { + VectorSize = 0; + VectorCapacity = 0; + } +#ifdef DEBUG_MEM_ALLOCATION + debug("VECTOR ALLOCATION: reverse( %lld )", Capacity); +#endif + T *NewBuffer = new T[Capacity]; + size_t _Size = Capacity < VectorSize ? Capacity : VectorSize; + for (size_t i = 0; i < _Size; i++) + NewBuffer[i] = VectorBuffer[i]; + VectorCapacity = Capacity; +#ifdef DEBUG_MEM_ALLOCATION + debug("VECTOR ALLOCATION: reverse( )->Buffer:~%lld", VectorBuffer); +#endif + delete[] VectorBuffer; + VectorBuffer = NewBuffer; + } + + NIF void resize(size_t Size) + { + reserve(Size); + VectorSize = Size; + } + + NIF void clear() + { + VectorCapacity = 0; + VectorSize = 0; + if (VectorBuffer != nullptr) + { + delete[] VectorBuffer, VectorBuffer = nullptr; + } + } + + NIF T *data() { return VectorBuffer; } + + NIF T &operator[](size_t Index) + { + if (!reinterpret_cast(&VectorBuffer[Index])) + { + warn("operator[]( %lld ) is null (requested by %#lx)", Index, __builtin_return_address(0)); + return this->null_elem(); + } + return VectorBuffer[Index]; + } + + NIF vector &operator=(const vector &vector) + { + delete[] VectorBuffer; + VectorSize = vector.VectorSize; + VectorCapacity = vector.VectorCapacity; +#ifdef DEBUG_MEM_ALLOCATION + debug("VECTOR ALLOCATION: operator=( )->Size:%lld", VectorSize); +#endif + VectorBuffer = new T[VectorSize]; + for (size_t i = 0; i < VectorSize; i++) + VectorBuffer[i] = vector.VectorBuffer[i]; + return *this; + } + }; +} + +#endif // !__FENNIX_KERNEL_STD_VECTOR_H__ diff --git a/include/string.hpp b/include/string.hpp index 610f695..d07e147 100644 --- a/include/string.hpp +++ b/include/string.hpp @@ -1,233 +1,2 @@ -#ifndef __FENNIX_KERNEL_STRING_H__ -#define __FENNIX_KERNEL_STRING_H__ - -#include -#include -#include - -// show debug messages -// #define DEBUG_CPP_STRING 1 - -#ifdef DEBUG_CPP_STRING -#define strdbg(m, ...) debug(m, ##__VA_ARGS__) -#else -#define strdbg(m, ...) -#endif - -// TODO: Somewhere the delete is called twice, causing a double free error. - -/** - * @brief String class - * String class that can be used to store strings. - */ -class String -{ -private: - char *m_Data; - int m_Length; - int m_Capacity; - -public: - String(const char *Str = "") - { - this->m_Length = strlen(Str); - this->m_Capacity = this->m_Length + 1; - this->m_Data = new char[m_Capacity]; - strcpy(m_Data, Str); - strdbg("New string created: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); - } - - ~String() - { - strdbg("String deleted: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); - delete[] this->m_Data, this->m_Data = nullptr; - } - - int length() const - { - strdbg("String length: %d", this->m_Length); - return this->m_Length; - } - - int capacity() const - { - strdbg("String capacity: %d", this->m_Capacity); - return this->m_Capacity; - } - - const char *c_str() const - { - strdbg("String data: \"%s\"", this->m_Data); - return this->m_Data; - } - - void resize(int NewLength) - { - strdbg("String resize: %d", NewLength); - if (NewLength > this->m_Capacity) - { - int newCapacity = NewLength + 1; - char *newData = new char[newCapacity]; - - strcpy(newData, this->m_Data); - - strdbg("old: %#lx, new: %#lx", this->m_Data, newData); - delete[] this->m_Data; - this->m_Data = newData; - this->m_Capacity = newCapacity; - } - this->m_Length = NewLength; - this->m_Data[m_Length] = '\0'; - strdbg("String resized: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); - } - - void concat(const String &Other) - { - int NewLength = this->m_Length + Other.m_Length; - this->resize(NewLength); - - strcat(m_Data, Other.m_Data); - strdbg("String concatenated: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); - } - - String operator+(const String &Other) const - { - String result = *this; - result.concat(Other); - strdbg("String added: \"%s\" (data: %#lx, length: %d, capacity: %d)", result.m_Data, result.m_Data, result.m_Length, result.m_Capacity); - return result; - } - - String operator+(const char *Other) const - { - String result = *this; - result.concat(Other); - strdbg("String added: \"%s\" (data: %#lx, length: %d, capacity: %d)", result.m_Data, result.m_Data, result.m_Length, result.m_Capacity); - return result; - } - - /* warning: implicitly-declared ‘constexpr String::String(const String&)’ is deprecated [-Wdeprecated-copy] */ - String &operator=(const String &Other) = default; - - // String &operator=(const String &Other) - // { - // if (this != &Other) - // { - // delete[] this->m_Data; - // this->m_Data = Other.m_Data; - // this->m_Length = Other.m_Length; - // this->m_Capacity = Other.m_Capacity; - // strdbg("String assigned: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); - // } - // return *this; - // } - - String &operator=(const char *Other) - { - this->m_Length = strlen(Other); - this->m_Capacity = this->m_Length + 1; - delete[] this->m_Data; - this->m_Data = new char[m_Capacity]; - strcpy(m_Data, Other); - strdbg("String assigned: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); - return *this; - } - - String &operator<<(const String &Other) - { - this->concat(Other); - strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); - return *this; - } - - String &operator<<(const char *Other) - { - this->concat(Other); - strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity); - return *this; - } - - char &operator[](int Index) - { - strdbg("String index: %d", Index); - return this->m_Data[Index]; - } - - const char &operator[](int Index) const - { - strdbg("String index: %d", Index); - return this->m_Data[Index]; - } - - bool operator==(const String &Other) const - { - strdbg("String compared: \"%s\" == \"%s\"", this->m_Data, Other.m_Data); - return strcmp(this->m_Data, Other.m_Data) == 0; - } - - bool operator!=(const String &Other) const - { - strdbg("String compared: \"%s\" != \"%s\"", this->m_Data, Other.m_Data); - return strcmp(this->m_Data, Other.m_Data) != 0; - } - - bool operator==(const char *Other) const - { - strdbg("String compared: \"%s\" == \"%s\"", this->m_Data, Other); - return strcmp(this->m_Data, Other) == 0; - } - - bool operator!=(const char *Other) const - { - strdbg("String compared: \"%s\" != \"%s\"", this->m_Data, Other); - return strcmp(this->m_Data, Other) != 0; - } - - class iterator - { - private: - char *m_Pointer; - - public: - iterator(char *Pointer) : m_Pointer(Pointer) {} - - iterator &operator++() - { - ++this->m_Pointer; - strdbg("String iterator incremented: %#lx", this->m_Pointer); - return *this; - } - - char &operator*() - { - strdbg("String iterator dereferenced: %#lx", this->m_Pointer); - return *this->m_Pointer; - } - - bool operator!=(const iterator &Other) const - { - strdbg("String iterator compared: %#lx != %#lx", this->m_Pointer, Other.m_Pointer); - return this->m_Pointer != Other.m_Pointer; - } - - bool operator==(const iterator &Other) const - { - strdbg("String iterator compared: %#lx == %#lx", this->m_Pointer, Other.m_Pointer); - return this->m_Pointer == Other.m_Pointer; - } - }; - - iterator begin() - { - strdbg("String iterator begin: %#lx", this->m_Data); - return iterator(this->m_Data); - } - - iterator end() - { - strdbg("String iterator end: %#lx", this->m_Data + this->m_Length); - return iterator(this->m_Data + this->m_Length); - } -}; - -#endif // !__FENNIX_KERNEL_STRING_H__ +#pragma once +#include diff --git a/include/task.hpp b/include/task.hpp index 0c7e9fc..6b9e273 100644 --- a/include/task.hpp +++ b/include/task.hpp @@ -173,8 +173,8 @@ namespace Tasking TaskStatus Status; TaskSecurity Security; TaskInfo Info; - Vector Threads; - Vector Children; + std::vector Threads; + std::vector Children; InterProcessCommunication::IPC *IPC; Memory::PageTable4 *PageTable; SymbolResolver::Symbols *ELFSymbolTable; @@ -203,7 +203,7 @@ namespace Tasking bool Process; }; - Vector Tokens; + std::vector Tokens; public: Token CreateToken(); @@ -226,7 +226,7 @@ namespace Tasking UPID NextPID = 0; UTID NextTID = 0; - Vector ListProcess; + std::vector ListProcess; PCB *IdleProcess = nullptr; TCB *IdleThread = nullptr; Atomic SchedulerTicks = 0; @@ -265,7 +265,7 @@ namespace Tasking public: uint64_t GetSchedulerTicks() { return SchedulerTicks.Load(); } uint64_t GetLastTaskTicks() { return LastTaskTicks.Load(); } - Vector GetProcessList() { return ListProcess; } + std::vector GetProcessList() { return ListProcess; } Security *GetSecurityManager() { return &SecurityManager; } void Panic() { StopScheduler = true; } void Schedule(); @@ -335,7 +335,7 @@ namespace Tasking IP EntryPoint, const char **argv = nullptr, const char **envp = nullptr, - const Vector &auxv = Vector(), + const std::vector &auxv = std::vector(), IPOffset Offset = 0, TaskArchitecture Architecture = TaskArchitecture::x64, TaskCompatibility Compatibility = TaskCompatibility::Native); diff --git a/include/types.h b/include/types.h index f024db5..818059c 100644 --- a/include/types.h +++ b/include/types.h @@ -35,8 +35,10 @@ #define UNUSED(x) (void)(x) #define CONCAT(x, y) x##y +#ifndef __cplusplus /* This conflicts with std */ #define toupper(c) ((c)-0x20 * (((c) >= 'a') && ((c) <= 'z'))) #define tolower(c) ((c) + 0x20 * (((c) >= 'A') && ((c) <= 'Z'))) +#endif #ifndef __va_list__ typedef __builtin_va_list va_list; diff --git a/include/vector.hpp b/include/vector.hpp index ed0198c..12d0bc7 100644 --- a/include/vector.hpp +++ b/include/vector.hpp @@ -1,263 +1,2 @@ -#ifndef __FENNIX_KERNEL_VECTOR_H__ -#define __FENNIX_KERNEL_VECTOR_H__ - -#include -#include -#include - -template -class Vector -{ -private: - size_t VectorSize = 0; - size_t VectorCapacity = 0; - T *VectorBuffer = nullptr; - -public: - typedef T *iterator; - - NIF Vector() - { -#ifdef DEBUG_MEM_ALLOCATION - debug("VECTOR INIT: Vector( )"); -#endif - VectorCapacity = 0; - VectorSize = 0; - VectorBuffer = 0; - } - - NIF Vector(size_t Size) - { - VectorCapacity = Size; - VectorSize = Size; -#ifdef DEBUG_MEM_ALLOCATION - debug("VECTOR INIT: Vector( %lld )", Size); -#endif - VectorBuffer = new T[Size]; - } - - NIF Vector(size_t Size, const T &Initial) - { - VectorSize = Size; - VectorCapacity = Size; -#ifdef DEBUG_MEM_ALLOCATION - debug("VECTOR INIT: Vector( %lld %llx )", Size, Initial); -#endif - assert(Size > 0); - VectorBuffer = new T[Size]; - for (size_t i = 0; i < Size; i++) - VectorBuffer[i] = Initial; - } - - NIF Vector(const Vector &Vector) - { - VectorSize = Vector.VectorSize; - VectorCapacity = Vector.VectorCapacity; -#ifdef DEBUG_MEM_ALLOCATION - debug("VECTOR INIT: Vector( )->Size: %lld", VectorSize); -#endif - assert(VectorSize > 0); - VectorBuffer = new T[VectorSize]; - for (size_t i = 0; i < VectorSize; i++) - VectorBuffer[i] = Vector.VectorBuffer[i]; - } - - NIF ~Vector() - { -#ifdef DEBUG_MEM_ALLOCATION - debug("VECTOR INIT: ~Vector( ~%lx )", VectorBuffer); -#endif - VectorSize = 0; - VectorCapacity = 0; - if (VectorBuffer != nullptr) - { - delete[] VectorBuffer, VectorBuffer = nullptr; - } - } - - NIF void remove(size_t Position) - { - if (Position >= VectorSize) - return; - memset(&*(VectorBuffer + Position), 0, sizeof(T)); - for (size_t i = 0; i < VectorSize - 1; i++) - { - *(VectorBuffer + Position + i) = *(VectorBuffer + Position + i + 1); - } - VectorSize--; - } - - NIF void remove(const T &Value) - { - for (size_t i = 0; i < VectorSize; i++) - { - if (VectorBuffer[i] == Value) - { - remove(i); - return; - } - } - } - - NIF T &null_elem() - { - static T null_elem; - return null_elem; - } - - NIF bool null_elem(size_t Index) - { - if (!reinterpret_cast(&VectorBuffer[Index])) - return false; - return true; - } - - NIF T &next(size_t Position) - { - if (Position + 1 < VectorSize && reinterpret_cast(&VectorBuffer[Position + 1])) - return VectorBuffer[Position + 1]; - warn("next( %lld ) is null (requested by %#lx)", Position, __builtin_return_address(0)); - return this->null_elem(); - } - - NIF T &prev(size_t Position) - { - if (Position > 0 && reinterpret_cast(&VectorBuffer[Position - 1])) - return VectorBuffer[Position - 1]; - warn("prev( %lld ) is null (requested by %#lx)", Position, __builtin_return_address(0)); - return this->null_elem(); - } - - NIF T &next(const T &Value) - { - for (size_t i = 0; i < VectorSize; i++) - { - if (VectorBuffer[i] == Value) - { - if (i + 1 < VectorSize && reinterpret_cast(&VectorBuffer[i + 1])) - return VectorBuffer[i + 1]; - else - break; - } - } - warn("next( %#lx ) is null (requested by %#lx)", Value, __builtin_return_address(0)); - return this->null_elem(); - } - - NIF T &prev(const T &Value) - { - for (size_t i = 0; i < VectorSize; i++) - { - if (VectorBuffer[i] == Value) - { - if (i > 0 && reinterpret_cast(&VectorBuffer[i - 1])) - return VectorBuffer[i - 1]; - else - break; - } - } - warn("prev( %#lx ) is null (requested by %#lx)", Value, __builtin_return_address(0)); - return this->null_elem(); - } - - NIF size_t capacity() const { return VectorCapacity; } - - NIF size_t size() const { return VectorSize; } - - NIF bool empty() const; - - NIF iterator begin() { return VectorBuffer; } - - NIF iterator end() { return VectorBuffer + size(); } - - NIF T &front() { return VectorBuffer[0]; } - - NIF T &back() { return VectorBuffer[VectorSize - 1]; } - - NIF void push_back(const T &Value) - { - if (VectorSize >= VectorCapacity) - reserve(VectorCapacity + 5); - VectorBuffer[VectorSize++] = Value; - } - - NIF void pop_back() { VectorSize--; } - - NIF void reverse() - { - if (VectorSize <= 1) - return; - for (size_t i = 0, j = VectorSize - 1; i < j; i++, j--) - { - T c = *(VectorBuffer + i); - *(VectorBuffer + i) = *(VectorBuffer + j); - *(VectorBuffer + j) = c; - } - } - - NIF void reserve(size_t Capacity) - { - if (VectorBuffer == 0) - { - VectorSize = 0; - VectorCapacity = 0; - } -#ifdef DEBUG_MEM_ALLOCATION - debug("VECTOR ALLOCATION: reverse( %lld )", Capacity); -#endif - T *NewBuffer = new T[Capacity]; - size_t _Size = Capacity < VectorSize ? Capacity : VectorSize; - for (size_t i = 0; i < _Size; i++) - NewBuffer[i] = VectorBuffer[i]; - VectorCapacity = Capacity; -#ifdef DEBUG_MEM_ALLOCATION - debug("VECTOR ALLOCATION: reverse( )->Buffer:~%lld", VectorBuffer); -#endif - delete[] VectorBuffer; - VectorBuffer = NewBuffer; - } - - NIF void resize(size_t Size) - { - reserve(Size); - VectorSize = Size; - } - - NIF void clear() - { - VectorCapacity = 0; - VectorSize = 0; - if (VectorBuffer != nullptr) - { - delete[] VectorBuffer, VectorBuffer = nullptr; - } - } - - NIF T *data() { return VectorBuffer; } - - NIF T &operator[](size_t Index) - { - if (!reinterpret_cast(&VectorBuffer[Index])) - { - warn("operator[]( %lld ) is null (requested by %#lx)", Index, __builtin_return_address(0)); - return this->null_elem(); - } - return VectorBuffer[Index]; - } - - NIF Vector &operator=(const Vector &Vector) - { - delete[] VectorBuffer; - VectorSize = Vector.VectorSize; - VectorCapacity = Vector.VectorCapacity; -#ifdef DEBUG_MEM_ALLOCATION - debug("VECTOR ALLOCATION: operator=( )->Size:%lld", VectorSize); -#endif - VectorBuffer = new T[VectorSize]; - for (size_t i = 0; i < VectorSize; i++) - VectorBuffer[i] = Vector.VectorBuffer[i]; - return *this; - } -}; - -#endif // !__FENNIX_KERNEL_VECTOR_H__ +#pragma once +#include