Rename std functions to match the standard

This commit is contained in:
Alex 2023-03-23 04:56:49 +02:00
parent 4cd4e4cdc6
commit f86f3d9293
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
50 changed files with 1635 additions and 979 deletions

View File

@ -235,10 +235,10 @@ namespace ACPI
void *VirtualAddress;
};
Vector<MADTIOApic *> ioapic;
Vector<MADTIso *> iso;
Vector<MADTNmi *> nmi;
Vector<LocalAPIC *> lapic;
std::vector<MADTIOApic *> ioapic;
std::vector<MADTIso *> iso;
std::vector<MADTNmi *> nmi;
std::vector<LocalAPIC *> lapic;
struct LAPIC *LAPICAddress;
uint16_t CPUCores;

View File

@ -39,7 +39,7 @@ namespace CrashHandler
"Terminated", // Terminated
};
Vector<Tasking::PCB *> Plist = TaskManager->GetProcessList();
std::vector<Tasking::PCB *> Plist = TaskManager->GetProcessList();
if (TaskManager)
{

View File

@ -1,12 +1,13 @@
#include <driver.hpp>
#include <ints.hpp>
#include <memory.hpp>
#include <ints.hpp>
#include <task.hpp>
#include <lock.hpp>
#include <printf.h>
#include <cwalk.h>
#include <md5.h>
// #include <ini.h>
#include "../../kernel.h"
#include "../../DAPI.hpp"
@ -174,7 +175,12 @@ namespace Driver
Driver::Driver()
{
SmartCriticalSection(DriverInitLock);
SharedPointer<VirtualFileSystem::File> 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<VirtualFileSystem::File> DriverDirectory = vfs->Open(Config.DriverDirectory);
if (DriverDirectory->Status == VirtualFileSystem::FileStatus::OK)
{
foreach (auto driver in DriverDirectory->node->Children)

View File

@ -20,7 +20,7 @@ namespace Driver
if (fexExtended->Driver.OverrideOnConflict)
{
Vector<int> DriversToRemove = Vector<int>();
std::vector<int> DriversToRemove = std::vector<int>();
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<int> DriversToRemove = Vector<int>();
std::vector<int> DriversToRemove = std::vector<int>();
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<int> DriversToRemove = Vector<int>();
std::vector<int> DriversToRemove = std::vector<int>();
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<int> DriversToRemove = Vector<int>();
std::vector<int> DriversToRemove = std::vector<int>();
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<int> DriversToRemove = Vector<int>();
std::vector<int> DriversToRemove = std::vector<int>();
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<int> DriversToRemove = Vector<int>();
std::vector<int> DriversToRemove = std::vector<int>();
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<int> DriversToRemove = Vector<int>();
std::vector<int> DriversToRemove = std::vector<int>();
foreach (auto Drv in Drivers)
{
FexExtended *fe = ((FexExtended *)((uintptr_t)Drv->Address + EXTENDED_SECTION_ADDRESS));

View File

@ -133,7 +133,7 @@ namespace Driver
if (fexExtended->Driver.OverrideOnConflict)
{
Vector<int> DriversToRemove = Vector<int>();
std::vector<int> DriversToRemove = std::vector<int>();
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<int> DriversToRemove = Vector<int>();
std::vector<int> DriversToRemove = std::vector<int>();
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<int> DriversToRemove = Vector<int>();
std::vector<int> DriversToRemove = std::vector<int>();
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<int> DriversToRemove = Vector<int>();
std::vector<int> DriversToRemove = std::vector<int>();
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<int> DriversToRemove = Vector<int>();
std::vector<int> DriversToRemove = std::vector<int>();
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<int> DriversToRemove = Vector<int>();
std::vector<int> DriversToRemove = std::vector<int>();
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<int> DriversToRemove = Vector<int>();
std::vector<int> DriversToRemove = std::vector<int>();
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<PCI::PCIDeviceHeader *> devices = PCIManager->FindPCIDevice(((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.VendorID[Vidx], ((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.DeviceID[Didx]);
std::vector<PCI::PCIDeviceHeader *> devices = PCIManager->FindPCIDevice(((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.VendorID[Vidx], ((FexExtended *)DrvExtHdr)->Driver.Bind.PCI.DeviceID[Didx]);
if (devices.size() == 0)
continue;

View File

@ -28,7 +28,7 @@ namespace Interrupts
int ID;
void *Data;
};
Vector<Event> RegisteredEvents;
std::vector<Event> RegisteredEvents;
#if defined(a64)
/* APIC::APIC */ void *apic[MAX_CPU];

View File

@ -817,18 +817,18 @@ namespace PCI
EnumerateDevice(BusAddress, Device);
}
Vector<PCIDeviceHeader *> PCI::FindPCIDevice(uint8_t Class, uint8_t Subclass, uint8_t ProgIF)
std::vector<PCIDeviceHeader *> PCI::FindPCIDevice(uint8_t Class, uint8_t Subclass, uint8_t ProgIF)
{
Vector<PCIDeviceHeader *> DeviceFound;
std::vector<PCIDeviceHeader *> DeviceFound;
for (auto var : Devices)
if (var->Class == Class && var->Subclass == Subclass && var->ProgIF == ProgIF)
DeviceFound.push_back(var);
return DeviceFound;
}
Vector<PCIDeviceHeader *> PCI::FindPCIDevice(int VendorID, int DeviceID)
std::vector<PCIDeviceHeader *> PCI::FindPCIDevice(int VendorID, int DeviceID)
{
Vector<PCIDeviceHeader *> DeviceFound;
std::vector<PCIDeviceHeader *> DeviceFound;
for (auto var : Devices)
if (var->VendorID == VendorID && var->DeviceID == DeviceID)
DeviceFound.push_back(var);

View File

@ -4,7 +4,7 @@
#include <debug.h>
volatile bool serialports[8] = {false, false, false, false, false, false, false, false};
Vector<UniversalAsynchronousReceiverTransmitter::Events *> RegisteredEvents;
std::vector<UniversalAsynchronousReceiverTransmitter::Events *> RegisteredEvents;
#if defined(a64) || defined(a32)
NIF __always_inline inline uint8_t NoProfiler_inportb(uint16_t Port)

View File

@ -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<String> NeededLibraries)
void ELFInterpreterIPCThread(PCB *Process, char *Path, void *MemoryImage, void *ElfFile, std::vector<const char *> 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<String> InterpreterNeededLibraries;
std::vector<const char *> 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<File> ExFile = vfs->Open(Path);
std::shared_ptr<File> 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;

View File

@ -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<VirtualFileSystem::File> InterpreterFile = vfs->Open(InterpreterPath);
std::shared_ptr<VirtualFileSystem::File> InterpreterFile = vfs->Open(InterpreterPath);
if (InterpreterFile->Status != VirtualFileSystem::FileStatus::OK)
warn("Failed to open interpreter file: %s", InterpreterPath);

View File

@ -207,7 +207,7 @@ namespace Execute
}
/* No need to check if it's valid, the GetBinaryType() call above does that. */
SharedPointer<VirtualFileSystem::File> File = vfs->Open(Interpreter);
std::shared_ptr<VirtualFileSystem::File> File = vfs->Open(Interpreter);
Elf64_Ehdr *ELFHeader = (Elf64_Ehdr *)File->node->Address;

View File

@ -21,7 +21,7 @@ NewLock(ExecuteServiceLock);
namespace Execute
{
Memory::MemMgr *mem = nullptr;
Vector<SharedLibraries> Libs;
std::vector<SharedLibraries> Libs;
void StartExecuteService()
{

View File

@ -69,7 +69,7 @@ namespace Execute
BinaryType GetBinaryType(char *Path)
{
BinaryType Type = BinaryType::BinTypeInvalid;
SharedPointer<VirtualFileSystem::File> ExFile = vfs->Open(Path);
std::shared_ptr<VirtualFileSystem::File> ExFile = vfs->Open(Path);
if (ExFile->Status == VirtualFileSystem::FileStatus::OK)
{

View File

@ -20,7 +20,7 @@ namespace Execute
.Process = nullptr,
.Thread = nullptr};
SharedPointer<VirtualFileSystem::File> ExFile = vfs->Open(Path);
std::shared_ptr<VirtualFileSystem::File> 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<AuxiliaryVector> auxv; // TODO!
std::vector<AuxiliaryVector> auxv; // TODO!
TCB *Thread = TaskManager->CreateThread(Process,
(IP)FexHdr->EntryPoint,

View File

@ -1,6 +1,6 @@
#include <filesystem.hpp>
#include <smartptr.hpp>
#include <smart_ptr.hpp>
#include <convert.h>
#include <printf.h>
#include <lock.hpp>
@ -22,7 +22,7 @@ NewLock(VFSLock);
namespace VirtualFileSystem
{
SharedPointer<char> Virtual::GetPathFromNode(Node *node)
std::shared_ptr<char> 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<char> FinalPath;
std::shared_ptr<char> FinalPath;
FinalPath.reset(new char[Size]);
size_t Offset = 0;
@ -152,9 +152,9 @@ namespace VirtualFileSystem
return nullptr;
}
SharedPointer<File> Virtual::ConvertNodeToFILE(Node *node)
std::shared_ptr<File> Virtual::ConvertNodeToFILE(Node *node)
{
SharedPointer<File> file = MakeShared<File>();
std::shared_ptr<File> file = std::make_shared<File>();
file->Status = FileStatus::OK;
file->node = node;
return file;
@ -256,17 +256,17 @@ namespace VirtualFileSystem
return FileStatus::NotFound;
}
SharedPointer<char> Virtual::NormalizePath(const char *Path, Node *Parent)
std::shared_ptr<char> 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<char> RelativePath;
std::shared_ptr<char> RelativePath;
cwk_path_normalize(Path, NormalizedPath, strlen((char *)Path) + 1);
if (cwk_path_is_relative(NormalizedPath))
{
SharedPointer<char> ParentPath = GetPathFromNode(Parent);
std::shared_ptr<char> 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<char> CleanPath = this->NormalizePath(Path, CurrentParent);
std::shared_ptr<char> 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<char> CleanPath = this->NormalizePath(Path, Parent);
std::shared_ptr<char> 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<File> Virtual::Mount(const char *Path, FileSystemOperations *Operator)
std::shared_ptr<File> Virtual::Mount(const char *Path, FileSystemOperations *Operator)
{
SmartLock(VFSLock);
SharedPointer<File> file = MakeShared<File>();
std::shared_ptr<File> file = std::make_shared<File>();
if (unlikely(!Operator))
{
@ -467,7 +467,7 @@ namespace VirtualFileSystem
return file;
}
FileStatus Virtual::Unmount(SharedPointer<File> File)
FileStatus Virtual::Unmount(std::shared_ptr<File> File)
{
SmartLock(VFSLock);
if (unlikely(File.Get()))
@ -476,7 +476,7 @@ namespace VirtualFileSystem
return FileStatus::OK;
}
size_t Virtual::Read(SharedPointer<File> File, size_t Offset, uint8_t *Buffer, size_t Size)
size_t Virtual::Read(std::shared_ptr<File> 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> File, size_t Offset, uint8_t *Buffer, size_t Size)
size_t Virtual::Write(std::shared_ptr<File> 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<File> Virtual::Open(const char *Path, Node *Parent)
std::shared_ptr<File> 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> file = MakeShared<File>();
std::shared_ptr<File> file = std::make_shared<File>();
file->node = Parent;
if (unlikely(!file->node))
file->Status = FileStatus::NotFound;
@ -544,7 +544,7 @@ namespace VirtualFileSystem
if (strcmp(Path, "..") == 0)
{
SharedPointer<File> file = MakeShared<File>();
std::shared_ptr<File> file = std::make_shared<File>();
if (Parent->Parent != nullptr)
file->node = Parent->Parent;
@ -557,9 +557,9 @@ namespace VirtualFileSystem
}
Node *CurrentParent = this->GetParent(Path, Parent);
SharedPointer<char> CleanPath = NormalizePath(Path, CurrentParent);
std::shared_ptr<char> CleanPath = NormalizePath(Path, CurrentParent);
SharedPointer<File> file = MakeShared<File>();
std::shared_ptr<File> file = std::make_shared<File>();
/* TODO: Check for other errors */
if (!PathExists(CleanPath.Get(), CurrentParent))
@ -601,7 +601,7 @@ namespace VirtualFileSystem
return file;
}
FileStatus Virtual::Close(SharedPointer<File> File)
FileStatus Virtual::Close(std::shared_ptr<File> File)
{
SmartLock(VFSLock);
if (unlikely(!File.Get()))

View File

@ -314,7 +314,7 @@ EXTERNC NIF void Main(BootInfo *Info)
DevFS = vfs->Create("/system/dev", NodeFlags::DIRECTORY);
else
{
SharedPointer<File> dev = vfs->Open("/system/dev");
std::shared_ptr<File> 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<File> mnt = vfs->Open("/system/mnt");
std::shared_ptr<File> 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<File> proc = vfs->Open("/system/proc", nullptr);
std::shared_ptr<File> proc = vfs->Open("/system/proc", nullptr);
if (proc->node->Flags != NodeFlags::DIRECTORY)
{
KPrint("\eE85230/system/proc is not a directory! Halting...");

View File

@ -1,6 +1,6 @@
#include <net/arp.hpp>
#include <debug.h>
#include <smartptr.hpp>
#include <smart_ptr.hpp>
#include "../kernel.h"

View File

@ -11,7 +11,7 @@ namespace NetworkEthernet
uint16_t Type;
};
Vector<EthernetEventHelperStruct> RegisteredEvents;
std::vector<EthernetEventHelperStruct> RegisteredEvents;
Ethernet::Ethernet(NetworkInterfaceManager::DeviceInterface *Interface) : NetworkInterfaceManager::Events(Interface)
{

View File

@ -49,7 +49,7 @@ namespace NetworkIPv4
kfree(Packet);
}
Vector<IPv4Events *> RegisteredEvents;
std::vector<IPv4Events *> RegisteredEvents;
bool IPv4::OnEthernetPacketReceived(uint8_t *Data, uint64_t Length)
{

View File

@ -17,7 +17,7 @@
namespace NetworkInterfaceManager
{
Vector<Events *> RegisteredEvents;
std::vector<Events *> RegisteredEvents;
NetworkInterface::NetworkInterface()
{

View File

@ -10,7 +10,7 @@ namespace NetworkUDP
Socket *UDPSocket;
uint16_t Port;
};
Vector<EventInfo> RegisteredEvents;
std::vector<EventInfo> RegisteredEvents;
UDPEvents::UDPEvents() {}

View File

@ -52,7 +52,7 @@ namespace Recovery
return;
}
SharedPointer<VirtualFileSystem::File> pcm = vfs->Open(AudioFile);
std::shared_ptr<VirtualFileSystem::File> 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); }

View File

@ -336,7 +336,7 @@ namespace Tasking
IP EntryPoint,
const char **argv,
const char **envp,
const Vector<AuxiliaryVector> &auxv,
const std::vector<AuxiliaryVector> &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<AuxiliaryVector> auxv_array = auxv;
std::vector<AuxiliaryVector> 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<AuxiliaryVector>(), 0, Arch);
TCB *kthrd = CreateThread(kproc, EntryPoint, nullptr, nullptr, std::vector<AuxiliaryVector>(), 0, Arch);
kthrd->Rename("Main Thread");
debug("Created Kernel Process: %s and Thread: %s", kproc->Name, kthrd->Name);
TaskingLock.Lock(__FUNCTION__);

View File

@ -1,7 +1,7 @@
#ifdef DEBUG
#include <string.hpp>
#include <memory.hpp>
#include <string.hpp>
#include <debug.h>
/* Originally from: https://github.com/EnderIce2/FennixProject/blob/main/kernel/test.cpp */

View File

@ -1,11 +1,11 @@
#ifdef DEBUG
#include <string.hpp>
#include <std/string.hpp>
#include <debug.h>
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!")

View File

@ -121,7 +121,7 @@ namespace Disk
uint8_t *Buffer = nullptr;
PartitionTable Table;
PartitionStyle Style = PartitionStyle::Unknown;
Vector<Partition *> Partitions;
std::vector<Partition *> Partitions;
bool MechanicalDisk = false;
size_t UniqueIdentifier = 0xdeadbeef;
@ -153,7 +153,7 @@ namespace Disk
unsigned char AvailablePorts = 0;
int BytesPerSector = 0;
Vector<Drive *> drives;
std::vector<Drive *> drives;
public:
void FetchDisks(unsigned long DriverUID);

View File

@ -58,7 +58,7 @@ namespace Driver
class Driver
{
private:
Vector<DriverFile *> Drivers;
std::vector<DriverFile *> 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<DriverFile *> GetDrivers() { return Drivers; }
std::vector<DriverFile *> GetDrivers() { return Drivers; }
void UnloadAllDrivers();
bool UnloadDriver(unsigned long DUID);
int IOCB(unsigned long DUID, /* KernelCallback */ void *KCB);

View File

@ -4,8 +4,8 @@
#include <types.h>
#include <filesystem.hpp>
#include <string.hpp>
#include <task.hpp>
#include <std.hpp>
#include <elf.h>
namespace Execute
@ -60,7 +60,7 @@ namespace Execute
SpawnData sd;
Tasking::IP InstructionPointer;
Vector<String> NeededLibraries;
std::vector<const char *> NeededLibraries;
void *MemoryImage;
void *VirtualMemoryImage;
@ -69,7 +69,7 @@ namespace Execute
Memory::MemMgr *TmpMem;
/* Same as above, for BaseLoad.cpp only */
Vector<AuxiliaryVector> auxv;
std::vector<AuxiliaryVector> auxv;
};
struct MmImage

View File

@ -3,7 +3,7 @@
#include <types.h>
#include <smartptr.hpp>
#include <smart_ptr.hpp>
#include <vector.hpp>
namespace VirtualFileSystem
@ -104,7 +104,7 @@ namespace VirtualFileSystem
1 - etc
...
*/
Vector<Node *> Children;
std::vector<Node *> Children;
};
struct File
@ -121,9 +121,9 @@ namespace VirtualFileSystem
Node *FileSystemRoot = nullptr;
public:
SharedPointer<char> GetPathFromNode(Node *node);
std::shared_ptr<char> GetPathFromNode(Node *node);
Node *GetNodeFromPath(const char *Path, Node *Parent = nullptr);
SharedPointer<File> ConvertNodeToFILE(Node *node);
std::shared_ptr<File> 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<char> NormalizePath(const char *Path, Node *Parent = nullptr);
std::shared_ptr<char> 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<File> Mount(const char *Path, FileSystemOperations *Operator);
FileStatus Unmount(SharedPointer<File> File);
std::shared_ptr<File> Mount(const char *Path, FileSystemOperations *Operator);
FileStatus Unmount(std::shared_ptr<File> File);
size_t Read(SharedPointer<File> File, size_t Offset, uint8_t *Buffer, size_t Size);
size_t Write(SharedPointer<File> File, size_t Offset, uint8_t *Buffer, size_t Size);
size_t Read(std::shared_ptr<File> File, size_t Offset, uint8_t *Buffer, size_t Size);
size_t Write(std::shared_ptr<File> File, size_t Offset, uint8_t *Buffer, size_t Size);
SharedPointer<File> Open(const char *Path, Node *Parent = nullptr);
FileStatus Close(SharedPointer<File> File);
std::shared_ptr<File> Open(const char *Path, Node *Parent = nullptr);
FileStatus Close(std::shared_ptr<File> File);
Virtual();
~Virtual();

View File

@ -225,9 +225,9 @@ namespace GraphicalUserInterface
uintptr_t OnClick;
};
Vector<LabelObject *> Labels;
Vector<PanelObject *> Panels;
Vector<ButtonObject *> Buttons;
std::vector<LabelObject *> Labels;
std::vector<PanelObject *> Panels;
std::vector<ButtonObject *> Buttons;
public:
void ReplaceFont(Video::Font *NewFont)
@ -286,7 +286,7 @@ namespace GraphicalUserInterface
Rect Position;
Rect LastPosition;
char Title[256];
Vector<WidgetCollection *> Widgets;
std::vector<WidgetCollection *> Widgets;
void *ParentGUI;
bool Maximized;
@ -360,8 +360,8 @@ namespace GraphicalUserInterface
ScreenBitmap *DesktopBuffer;
ScreenBitmap *OverlayBuffer;
ScreenBitmap *CursorBuffer;
Vector<WidgetCollection *> Widgets;
Vector<Window *> Windows;
std::vector<WidgetCollection *> Widgets;
std::vector<Window *> Windows;
CursorType Cursor = CursorType::Arrow;
CursorType LastCursor = CursorType::Arrow;
bool CursorVisible = true;

View File

@ -50,7 +50,7 @@ namespace InterProcessCommunication
private:
NewLock(IPCLock);
IPCID NextID = 0;
Vector<IPCHandle *> Handles;
std::vector<IPCHandle *> Handles;
Memory::MemMgr *mem;
VirtualFileSystem::Node *IPCNode;
void *Process;

View File

@ -5,7 +5,6 @@
#include <filesystem.hpp>
#include <boot/binfo.h>
#include <bitmap.hpp>
#include <vector.hpp>
#include <lock.hpp>
#include <std.hpp>
#endif // __cplusplus
@ -639,7 +638,7 @@ namespace Memory
size_t PageCount;
};
Vector<AllocatedPages> GetAllocatedPagesList() { return AllocatedPagesList; }
std::vector<AllocatedPages> 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<AllocatedPages> AllocatedPagesList;
std::vector<AllocatedPages> AllocatedPagesList;
};
}

View File

@ -59,7 +59,7 @@ namespace NetworkARP
DA_UPDATE = 4
};
Vector<NetworkARP::DiscoveredAddress *> DiscoveredAddresses;
std::vector<NetworkARP::DiscoveredAddress *> DiscoveredAddresses;
DiscoveredAddress *ManageDiscoveredAddresses(DAType Type, InternetProtocol IP, MediaAccessControl MAC);
DiscoveredAddress *Search(InternetProtocol TargetIP);
DiscoveredAddress *Update(InternetProtocol TargetIP, MediaAccessControl TargetMAC);

View File

@ -50,7 +50,7 @@ namespace NetworkInterfaceManager
private:
Memory::MemMgr *mem;
int CardIDs = 0;
Vector<DeviceInterface *> Interfaces;
std::vector<DeviceInterface *> Interfaces;
Tasking::TCB *NetSvcThread;
void StopNetworkStack();

View File

@ -3,8 +3,8 @@
#include <types.h>
#include <vector.hpp>
#include <debug.h>
#include <vector.hpp>
namespace PCI
{
@ -205,15 +205,15 @@ namespace PCI
class PCI
{
private:
Vector<PCIDeviceHeader *> Devices;
std::vector<PCIDeviceHeader *> Devices;
public:
Vector<PCIDeviceHeader *> &GetDevices() { return Devices; }
std::vector<PCIDeviceHeader *> &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<PCIDeviceHeader *> FindPCIDevice(uint8_t Class, uint8_t Subclass, uint8_t ProgIF);
Vector<PCIDeviceHeader *> FindPCIDevice(int VendorID, int DeviceID);
std::vector<PCIDeviceHeader *> FindPCIDevice(uint8_t Class, uint8_t Subclass, uint8_t ProgIF);
std::vector<PCIDeviceHeader *> FindPCIDevice(int VendorID, int DeviceID);
PCI();
~PCI();

2
include/smart_ptr.hpp Normal file
View File

@ -0,0 +1,2 @@
#pragma once
#include <std/smart_ptr.hpp>

View File

@ -1,266 +0,0 @@
#ifndef __FENNIX_KERNEL_SMART_POINTER_H__
#define __FENNIX_KERNEL_SMART_POINTER_H__
#include <types.h>
#include <debug.h>
// 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<char> pointer(new char());
* *pointer = 'a';
* printf("%c", *pointer); // Prints "a"
*/
template <class T>
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 T>
class AutoPointer
{
};
template <class T>
class UniquePointer
{
};
template <class T>
class WeakPointer
{
};
template <typename T>
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<T> &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<T> &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 <typename T>
struct RemoveReference
{
typedef T type;
};
template <typename T>
struct RemoveReference<T &>
{
typedef T type;
};
template <typename T>
struct RemoveReference<T &&>
{
typedef T type;
};
template <typename T>
using RemoveReference_t = typename RemoveReference<T>::type;
template <typename T>
T &&forward(RemoveReference_t<T> &t)
{
return static_cast<T &&>(t);
};
template <typename T>
T &&forward(RemoveReference_t<T> &&t)
{
return static_cast<T &&>(t);
};
template <typename T, typename... Args>
SharedPointer<T> MakeShared(Args &&...args)
{
return SharedPointer<T>(new T(forward<Args>(args)...));
};
#endif // !__FENNIX_KERNEL_SMART_POINTER_H__

View File

@ -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 <types.h>
#include <smartptr.hpp>
#include <vector.hpp>
#include <string.hpp>
#include <std/unordered_map.hpp>
#include <std/functional.hpp>
#include <std/stdexcept.hpp>
#include <std/smart_ptr.hpp>
#include <std/utility.hpp>
#include <std/vector.hpp>
#include <std/string.hpp>
#include <std/list.hpp>
/**
* @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 T>
class smart_ptr
template <typename InputIt, typename OutputIt, typename UnaryOperation>
OutputIt transform(InputIt first, InputIt last, OutputIt result, UnaryOperation op)
{
public:
using SmartPointer<T>::SmartPointer;
using SmartPointer<T>::operator*;
using SmartPointer<T>::operator->;
while (first != last)
{
*result = op(*first);
++first;
++result;
}
return result;
};
template <class T>
class auto_ptr
inline __always_inline int tolower(int c)
{
public:
using AutoPointer<T>::AutoPointer;
};
if (c >= 'A' && c <= 'Z')
return c + ('a' - 'A');
else
return c;
}
template <class T>
class unique_ptr
inline __always_inline int toupper(int c)
{
public:
using UniquePointer<T>::UniquePointer;
};
template <class T>
class weak_ptr
{
public:
using WeakPointer<T>::WeakPointer;
};
template <typename T>
class shared_ptr
{
using SharedPointer<T>::SharedPointer;
using SharedPointer<T>::operator*;
using SharedPointer<T>::operator->;
};
template <typename T>
struct remove_reference
{
typedef T type;
};
template <typename T>
struct remove_reference<T &>
{
typedef T type;
};
template <typename T>
struct remove_reference<T &&>
{
typedef T type;
};
template <typename T>
using remove_reference_t = typename remove_reference<T>::type;
template <typename T>
T &&forward(remove_reference_t<T> &t) { return static_cast<T &&>(t); };
template <typename T>
T &&forward(remove_reference_t<T> &&t) { return static_cast<T &&>(t); };
template <typename T, typename... Args>
shared_ptr<T> make_shared(Args &&...args)
{
return SharedPointer<T>(new T(forward<Args>(args)...));
};
template <class T>
class vector : public Vector<T>
{
public:
using Vector<T>::Vector;
using Vector<T>::operator=;
using Vector<T>::operator[];
using typename Vector<T>::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__

View File

@ -0,0 +1,33 @@
#ifndef __FENNIX_KERNEL_STD_FUNCTIONAL_H__
#define __FENNIX_KERNEL_STD_FUNCTIONAL_H__
#include <types.h>
#include <assert.h>
namespace std
{
template <typename Key>
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<const uint8_t *>(&key);
const size_t size = sizeof(Key);
uint64_t hash = fnv_offset_basis;
for (size_t i = 0; i < size; ++i)
{
hash ^= static_cast<uint64_t>(data[i]);
hash *= fnv_prime;
}
return static_cast<size_t>(hash);
}
};
}
#endif // !__FENNIX_KERNEL_STD_FUNCTIONAL_H__

207
include/std/list.hpp Normal file
View File

@ -0,0 +1,207 @@
#ifndef __FENNIX_KERNEL_STD_LIST_H__
#define __FENNIX_KERNEL_STD_LIST_H__
#include <types.h>
#include <std/stdexcept.hpp>
namespace std
{
template <typename T>
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__

263
include/std/smart_ptr.hpp Normal file
View File

@ -0,0 +1,263 @@
#ifndef __FENNIX_KERNEL_STD_SMART_POINTER_H__
#define __FENNIX_KERNEL_STD_SMART_POINTER_H__
#include <types.h>
#include <debug.h>
// 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<char> pointer(new char());
* *pointer = 'a';
* printf("%c", *pointer); // Prints "a"
*/
template <class T>
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 T>
class auto_ptr
{
};
template <class T>
class unique_ptr
{
};
template <class T>
class weak_ptr
{
};
template <typename T>
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<T> &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<T> &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 <typename T>
struct remove_reference
{
typedef T type;
};
template <typename T>
struct remove_reference<T &>
{
typedef T type;
};
template <typename T>
struct remove_reference<T &&>
{
typedef T type;
};
template <typename T>
using remove_reference_t = typename remove_reference<T>::type;
template <typename T>
T &&forward(remove_reference_t<T> &t) { return static_cast<T &&>(t); };
template <typename T>
T &&forward(remove_reference_t<T> &&t) { return static_cast<T &&>(t); };
template <typename T, typename... Args>
shared_ptr<T> make_shared(Args &&...args)
{
return shared_ptr<T>(new T(forward<Args>(args)...));
};
}
#endif // !__FENNIX_KERNEL_STD_SMART_POINTER_H__

19
include/std/stdexcept.hpp Normal file
View File

@ -0,0 +1,19 @@
#ifndef __FENNIX_KERNEL_STD_STDEXCEPT_H__
#define __FENNIX_KERNEL_STD_STDEXCEPT_H__
#include <types.h>
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__

536
include/std/string.hpp Normal file
View File

@ -0,0 +1,536 @@
#ifndef __FENNIX_KERNEL_STD_STRING_H__
#define __FENNIX_KERNEL_STD_STRING_H__
#include <types.h>
#include <convert.h>
#include <debug.h>
// 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__

View File

@ -0,0 +1,113 @@
#ifndef __FENNIX_KERNEL_STD_UNORDERED_MAP_H__
#define __FENNIX_KERNEL_STD_UNORDERED_MAP_H__
#include <types.h>
#include <std/functional.hpp>
#include <std/utility.hpp>
#include <std/list.hpp>
namespace std
{
template <typename key_type, typename value_type>
class unordered_map
{
public:
typedef std::pair<key_type, value_type> key_value_pair;
typedef std::list<key_value_pair> bucket;
typedef typename std::vector<bucket>::iterator iterator;
typedef typename std::vector<bucket>::const_iterator const_iterator;
private:
static const size_t DEFAULT_NUM_BUCKETS = 10;
std::vector<bucket> m_buckets;
size_t hash(const key_type &key) const
{
std::hash<key_type> 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__

22
include/std/utility.hpp Normal file
View File

@ -0,0 +1,22 @@
#ifndef __FENNIX_KERNEL_STD_UTILITY_H__
#define __FENNIX_KERNEL_STD_UTILITY_H__
#include <types.h>
namespace std
{
template <typename T1, typename T2>
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__

267
include/std/vector.hpp Normal file
View File

@ -0,0 +1,267 @@
#ifndef __FENNIX_KERNEL_STD_VECTOR_H__
#define __FENNIX_KERNEL_STD_VECTOR_H__
#include <types.h>
#include <assert.h>
#include <cstring>
namespace std
{
template <class T>
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<T> &vector)
{
VectorSize = vector.VectorSize;
VectorCapacity = vector.VectorCapacity;
#ifdef DEBUG_MEM_ALLOCATION
debug("VECTOR INIT: vector( <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<uintptr_t>(&VectorBuffer[Index]))
return false;
return true;
}
NIF T &next(size_t Position)
{
if (Position + 1 < VectorSize && reinterpret_cast<uintptr_t>(&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<uintptr_t>(&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<uintptr_t>(&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<uintptr_t>(&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( <Capacity> )->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<uintptr_t>(&VectorBuffer[Index]))
{
warn("operator[]( %lld ) is null (requested by %#lx)", Index, __builtin_return_address(0));
return this->null_elem();
}
return VectorBuffer[Index];
}
NIF vector<T> &operator=(const vector<T> &vector)
{
delete[] VectorBuffer;
VectorSize = vector.VectorSize;
VectorCapacity = vector.VectorCapacity;
#ifdef DEBUG_MEM_ALLOCATION
debug("VECTOR ALLOCATION: operator=( <vector> )->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__

View File

@ -1,233 +1,2 @@
#ifndef __FENNIX_KERNEL_STRING_H__
#define __FENNIX_KERNEL_STRING_H__
#include <types.h>
#include <convert.h>
#include <debug.h>
// 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 <std/string.hpp>

View File

@ -173,8 +173,8 @@ namespace Tasking
TaskStatus Status;
TaskSecurity Security;
TaskInfo Info;
Vector<TCB *> Threads;
Vector<PCB *> Children;
std::vector<TCB *> Threads;
std::vector<PCB *> Children;
InterProcessCommunication::IPC *IPC;
Memory::PageTable4 *PageTable;
SymbolResolver::Symbols *ELFSymbolTable;
@ -203,7 +203,7 @@ namespace Tasking
bool Process;
};
Vector<TokenData> Tokens;
std::vector<TokenData> Tokens;
public:
Token CreateToken();
@ -226,7 +226,7 @@ namespace Tasking
UPID NextPID = 0;
UTID NextTID = 0;
Vector<PCB *> ListProcess;
std::vector<PCB *> ListProcess;
PCB *IdleProcess = nullptr;
TCB *IdleThread = nullptr;
Atomic<uint64_t> SchedulerTicks = 0;
@ -265,7 +265,7 @@ namespace Tasking
public:
uint64_t GetSchedulerTicks() { return SchedulerTicks.Load(); }
uint64_t GetLastTaskTicks() { return LastTaskTicks.Load(); }
Vector<PCB *> GetProcessList() { return ListProcess; }
std::vector<PCB *> 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<AuxiliaryVector> &auxv = Vector<AuxiliaryVector>(),
const std::vector<AuxiliaryVector> &auxv = std::vector<AuxiliaryVector>(),
IPOffset Offset = 0,
TaskArchitecture Architecture = TaskArchitecture::x64,
TaskCompatibility Compatibility = TaskCompatibility::Native);

View File

@ -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;

View File

@ -1,263 +1,2 @@
#ifndef __FENNIX_KERNEL_VECTOR_H__
#define __FENNIX_KERNEL_VECTOR_H__
#include <types.h>
#include <assert.h>
#include <cstring>
template <class T>
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<T> &Vector)
{
VectorSize = Vector.VectorSize;
VectorCapacity = Vector.VectorCapacity;
#ifdef DEBUG_MEM_ALLOCATION
debug("VECTOR INIT: Vector( <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<uintptr_t>(&VectorBuffer[Index]))
return false;
return true;
}
NIF T &next(size_t Position)
{
if (Position + 1 < VectorSize && reinterpret_cast<uintptr_t>(&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<uintptr_t>(&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<uintptr_t>(&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<uintptr_t>(&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( <Capacity> )->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<uintptr_t>(&VectorBuffer[Index]))
{
warn("operator[]( %lld ) is null (requested by %#lx)", Index, __builtin_return_address(0));
return this->null_elem();
}
return VectorBuffer[Index];
}
NIF Vector<T> &operator=(const Vector<T> &Vector)
{
delete[] VectorBuffer;
VectorSize = Vector.VectorSize;
VectorCapacity = Vector.VectorCapacity;
#ifdef DEBUG_MEM_ALLOCATION
debug("VECTOR ALLOCATION: operator=( <vector> )->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 <std/vector.hpp>