From 7b2137bf4dbc7906970761b94e74a2506087336b Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 23 Mar 2023 20:48:50 +0200 Subject: [PATCH] Update shared_ptr function names --- Execute/Elf/BaseLoad.cpp | 6 ++-- Execute/Elf/Exec.cpp | 2 +- FileSystem/Filesystem.cpp | 68 +++++++++++++++++++-------------------- include/std/smart_ptr.hpp | 60 ++++++++++++++++++++-------------- 4 files changed, 74 insertions(+), 62 deletions(-) diff --git a/Execute/Elf/BaseLoad.cpp b/Execute/Elf/BaseLoad.cpp index b3fed9b..d299b57 100644 --- a/Execute/Elf/BaseLoad.cpp +++ b/Execute/Elf/BaseLoad.cpp @@ -200,13 +200,13 @@ namespace Execute switch (ELFHeader->e_type) { case ET_REL: - bl = ELFLoadRel(ElfFile, ExFile.Get(), Process); + bl = ELFLoadRel(ElfFile, ExFile.get(), Process); break; case ET_EXEC: - bl = ELFLoadExec(ElfFile, ExFile.Get(), Process); + bl = ELFLoadExec(ElfFile, ExFile.get(), Process); break; case ET_DYN: - bl = ELFLoadDyn(ElfFile, ExFile.Get(), Process); + bl = ELFLoadDyn(ElfFile, ExFile.get(), Process); break; case ET_CORE: { diff --git a/Execute/Elf/Exec.cpp b/Execute/Elf/Exec.cpp index f4d5d92..8127daf 100644 --- a/Execute/Elf/Exec.cpp +++ b/Execute/Elf/Exec.cpp @@ -189,7 +189,7 @@ namespace Execute strcpy(aux_platform, "x86_64"); ELFBase.auxv.push_back({.archaux = {.a_type = AT_NULL, .a_un = {.a_val = 0}}}); - ELFBase.auxv.push_back({.archaux = {.a_type = AT_EXECFN, .a_un = {.a_val = (uint64_t)vfs->GetPathFromNode(ExFile->node).Get()}}}); + ELFBase.auxv.push_back({.archaux = {.a_type = AT_EXECFN, .a_un = {.a_val = (uint64_t)vfs->GetPathFromNode(ExFile->node).get()}}}); ELFBase.auxv.push_back({.archaux = {.a_type = AT_PLATFORM, .a_un = {.a_val = (uint64_t)aux_platform}}}); ELFBase.auxv.push_back({.archaux = {.a_type = AT_ENTRY, .a_un = {.a_val = (uint64_t)EntryPoint}}}); ELFBase.auxv.push_back({.archaux = {.a_type = AT_BASE, .a_un = {.a_val = (uint64_t)MemoryImage.Virtual}}}); diff --git a/FileSystem/Filesystem.cpp b/FileSystem/Filesystem.cpp index c2cabb2..ef9f45f 100644 --- a/FileSystem/Filesystem.cpp +++ b/FileSystem/Filesystem.cpp @@ -86,17 +86,17 @@ namespace VirtualFileSystem continue; } size_t ElementSize = strlen(Path[i]); - memcpy(FinalPath.Get() + Offset, Path[i], ElementSize); + memcpy(FinalPath.get() + Offset, Path[i], ElementSize); Offset += ElementSize; } // Add a null terminator to the final path string - FinalPath.Get()[Size - 1] = '\0'; + FinalPath.get()[Size - 1] = '\0'; // Deallocate the Path array delete[] Path, Path = nullptr; - vfsdbg("GetPathFromNode()->\"%s\"", FinalPath.Get()); + vfsdbg("GetPathFromNode()->\"%s\"", FinalPath.get()); return FinalPath; } @@ -267,17 +267,17 @@ namespace VirtualFileSystem if (cwk_path_is_relative(NormalizedPath)) { std::shared_ptr ParentPath = GetPathFromNode(Parent); - size_t PathSize = cwk_path_get_absolute(ParentPath.Get(), NormalizedPath, nullptr, 0); + 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); + cwk_path_get_absolute(ParentPath.get(), NormalizedPath, RelativePath.get(), PathSize + 1); } else { RelativePath.reset(new char[strlen(NormalizedPath) + 1]); - strcpy(RelativePath.Get(), NormalizedPath); + strcpy(RelativePath.get(), NormalizedPath); } delete[] NormalizedPath; - vfsdbg("NormalizePath()->\"%s\"", RelativePath.Get()); + vfsdbg("NormalizePath()->\"%s\"", RelativePath.get()); return RelativePath; } @@ -294,7 +294,7 @@ namespace VirtualFileSystem vfsdbg("PathExists( Path: \"%s\" Parent: \"%s\" )", Path, Parent->Name); - if (GetNodeFromPath(NormalizePath(Path, Parent).Get(), Parent)) + if (GetNodeFromPath(NormalizePath(Path, Parent).get(), Parent)) { vfsdbg("PathExists()->OK"); return true; @@ -330,16 +330,16 @@ namespace VirtualFileSystem vfsdbg("Virtual::Create( Path: \"%s\" Parent: \"%s\" )", Path, Parent ? Parent->Name : CurrentParent->Name); std::shared_ptr CleanPath = this->NormalizePath(Path, CurrentParent); - vfsdbg("CleanPath: \"%s\"", CleanPath.Get()); + vfsdbg("CleanPath: \"%s\"", CleanPath.get()); - if (PathExists(CleanPath.Get(), CurrentParent)) + if (PathExists(CleanPath.get(), CurrentParent)) { - error("Path %s already exists.", CleanPath.Get()); + error("Path %s already exists.", CleanPath.get()); goto CreatePathError; } cwk_segment segment; - if (!cwk_path_get_first_segment(CleanPath.Get(), &segment)) + if (!cwk_path_get_first_segment(CleanPath.get(), &segment)) { error("Path doesn't have any segments."); goto CreatePathError; @@ -373,7 +373,7 @@ namespace VirtualFileSystem } while (cwk_path_get_next_segment(&segment)); vfsdbg("Virtual::Create()->\"%s\"", CurrentParent->Name); - vfsdbg("Path created: \"%s\"", GetPathFromNode(CurrentParent).Get()); + vfsdbg("Path created: \"%s\"", GetPathFromNode(CurrentParent).get()); return CurrentParent; CreatePathError: @@ -393,16 +393,16 @@ namespace VirtualFileSystem Parent = FileSystemRoot; std::shared_ptr CleanPath = this->NormalizePath(Path, Parent); - vfsdbg("CleanPath: \"%s\"", CleanPath.Get()); + vfsdbg("CleanPath: \"%s\"", CleanPath.get()); - if (!PathExists(CleanPath.Get(), Parent)) + if (!PathExists(CleanPath.get(), Parent)) { - vfsdbg("Path %s doesn't exist.", CleanPath.Get()); + vfsdbg("Path %s doesn't exist.", CleanPath.get()); return InvalidPath; } - Node *NodeToDelete = GetNodeFromPath(CleanPath.Get(), Parent); - Node *ParentNode = GetParent(CleanPath.Get(), Parent); + Node *NodeToDelete = GetNodeFromPath(CleanPath.get(), Parent); + Node *ParentNode = GetParent(CleanPath.get(), Parent); if (NodeToDelete->Flags == NodeFlags::DIRECTORY) { @@ -411,7 +411,7 @@ namespace VirtualFileSystem foreach (auto Child in NodeToDelete->Children) { VFSLock.Unlock(); - FileStatus Status = Delete(GetPathFromNode(Child).Get(), true); + FileStatus Status = Delete(GetPathFromNode(Child).get(), true); VFSLock.Lock(__FUNCTION__); if (Status != FileStatus::OK) { @@ -422,7 +422,7 @@ namespace VirtualFileSystem } else if (NodeToDelete->Children.size() > 0) { - vfsdbg("Directory %s is not empty.", CleanPath.Get()); + vfsdbg("Directory %s is not empty.", CleanPath.get()); return DirectoryNotEmpty; } } @@ -437,7 +437,7 @@ namespace VirtualFileSystem return OK; } - FileStatus Virtual::Delete(Node *Path, bool Recursive, Node *Parent) { return Delete(GetPathFromNode(Path).Get(), Recursive, Parent); } + FileStatus Virtual::Delete(Node *Path, bool Recursive, Node *Parent) { return Delete(GetPathFromNode(Path).get(), Recursive, Parent); } /* TODO: REWORK */ std::shared_ptr Virtual::Mount(const char *Path, FileSystemOperations *Operator) @@ -470,7 +470,7 @@ namespace VirtualFileSystem FileStatus Virtual::Unmount(std::shared_ptr File) { SmartLock(VFSLock); - if (unlikely(File.Get())) + if (unlikely(File.get())) return FileStatus::InvalidParameter; fixme("Unmounting %s", File->Name); return FileStatus::OK; @@ -479,7 +479,7 @@ namespace VirtualFileSystem size_t Virtual::Read(std::shared_ptr File, size_t Offset, uint8_t *Buffer, size_t Size) { SmartLock(VFSLock); - if (unlikely(!File.Get())) + if (unlikely(!File.get())) return 0; if (unlikely(!File->node)) @@ -503,7 +503,7 @@ namespace VirtualFileSystem size_t Virtual::Write(std::shared_ptr File, size_t Offset, uint8_t *Buffer, size_t Size) { SmartLock(VFSLock); - if (unlikely(!File.Get())) + if (unlikely(!File.get())) return 0; if (unlikely(!File->node)) @@ -537,7 +537,7 @@ namespace VirtualFileSystem file->node = Parent; if (unlikely(!file->node)) file->Status = FileStatus::NotFound; - cwk_path_get_basename(GetPathFromNode(Parent).Get(), &basename, nullptr); + cwk_path_get_basename(GetPathFromNode(Parent).get(), &basename, nullptr); strcpy(file->Name, basename); return file; } @@ -551,7 +551,7 @@ namespace VirtualFileSystem if (!file->node) file->Status = FileStatus::NotFound; - cwk_path_get_basename(GetPathFromNode(Parent).Get(), &basename, nullptr); + cwk_path_get_basename(GetPathFromNode(Parent).get(), &basename, nullptr); strcpy(file->Name, basename); return file; } @@ -562,11 +562,11 @@ namespace VirtualFileSystem std::shared_ptr file = std::make_shared(); /* TODO: Check for other errors */ - if (!PathExists(CleanPath.Get(), CurrentParent)) + if (!PathExists(CleanPath.get(), CurrentParent)) { foreach (auto Child in FileSystemRoot->Children) { - if (strcmp(Child->Name, CleanPath.Get()) == 0) + if (strcmp(Child->Name, CleanPath.get()) == 0) { file->node = Child; if (file->node == nullptr) @@ -575,24 +575,24 @@ namespace VirtualFileSystem file->node = nullptr; return file; } - cwk_path_get_basename(GetPathFromNode(Child).Get(), &basename, nullptr); + cwk_path_get_basename(GetPathFromNode(Child).get(), &basename, nullptr); strcpy(file->Name, basename); return file; } } - file->node = GetNodeFromPath(CleanPath.Get(), FileSystemRoot->Children[0]); + file->node = GetNodeFromPath(CleanPath.get(), FileSystemRoot->Children[0]); if (file->node) { - cwk_path_get_basename(GetPathFromNode(file->node).Get(), &basename, nullptr); + cwk_path_get_basename(GetPathFromNode(file->node).get(), &basename, nullptr); strcpy(file->Name, basename); return file; } } else { - file->node = GetNodeFromPath(CleanPath.Get(), CurrentParent); - cwk_path_get_basename(CleanPath.Get(), &basename, nullptr); + file->node = GetNodeFromPath(CleanPath.get(), CurrentParent); + cwk_path_get_basename(CleanPath.get(), &basename, nullptr); strcpy(file->Name, basename); return file; } @@ -604,7 +604,7 @@ namespace VirtualFileSystem FileStatus Virtual::Close(std::shared_ptr File) { SmartLock(VFSLock); - if (unlikely(!File.Get())) + if (unlikely(!File.get())) return FileStatus::InvalidHandle; vfsdbg("Closing %s", File->Name); return FileStatus::OK; diff --git a/include/std/smart_ptr.hpp b/include/std/smart_ptr.hpp index bdaf54f..130518b 100644 --- a/include/std/smart_ptr.hpp +++ b/include/std/smart_ptr.hpp @@ -57,6 +57,12 @@ namespace std spdbg("Smart pointer dereferenced (%#lx)", m_RealPointer); return m_RealPointer; } + + T *get() + { + spdbg("Smart pointer returned (%#lx)", m_RealPointer); + return m_RealPointer; + } }; template @@ -78,61 +84,61 @@ namespace std class shared_ptr { private: - class Counter + 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() + 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"); + spdbg("reset"); } - unsigned int Get() + unsigned int get() { return m_RefCount; - spdbg("Counter returned"); + spdbg("return"); } void operator++() { m_RefCount++; - spdbg("Counter incremented"); + spdbg("increment"); } void operator++(int) { m_RefCount++; - spdbg("Counter incremented"); + spdbg("increment"); } void operator--() { m_RefCount--; - spdbg("Counter decremented"); + spdbg("decrement"); } void operator--(int) { m_RefCount--; - spdbg("Counter decremented"); + spdbg("decrement"); } }; - Counter *m_ReferenceCounter; + counter *m_ReferenceCounter; T *m_RealPointer; public: explicit shared_ptr(T *Pointer = nullptr) { m_RealPointer = Pointer; - m_ReferenceCounter = new Counter(); + m_ReferenceCounter = new counter(); spdbg("[%#lx] Shared pointer created (ptr=%#lx, ref=%#lx)", this, Pointer, m_ReferenceCounter); if (Pointer) (*m_ReferenceCounter)++; @@ -150,7 +156,7 @@ namespace std { spdbg("[%#lx] Shared pointer destructor called", this); (*m_ReferenceCounter)--; - if (m_ReferenceCounter->Get() == 0) + 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; @@ -158,13 +164,13 @@ namespace std } } - unsigned int GetCount() + unsigned int get_count() { - spdbg("[%#lx] Shared pointer count (%d)", this, m_ReferenceCounter->Get()); - return m_ReferenceCounter->Get(); + spdbg("[%#lx] Shared pointer count (%d)", this, m_ReferenceCounter->get()); + return m_ReferenceCounter->get(); } - T *Get() + T *get() { spdbg("[%#lx] Shared pointer get (%#lx)", this, m_RealPointer); return m_RealPointer; @@ -188,13 +194,13 @@ namespace std return; spdbg("[%#lx] Shared pointer reset (ptr=%#lx, ref=%#lx)", this, Pointer, m_ReferenceCounter); (*m_ReferenceCounter)--; - if (m_ReferenceCounter->Get() == 0) + if (m_ReferenceCounter->get() == 0) { delete m_RealPointer; delete m_ReferenceCounter; } m_RealPointer = Pointer; - m_ReferenceCounter = new Counter(); + m_ReferenceCounter = new counter(); if (Pointer) (*m_ReferenceCounter)++; } @@ -202,7 +208,7 @@ namespace std void reset() { spdbg("[%#lx] Shared pointer reset (ptr=%#lx, ref=%#lx)", this, m_RealPointer, m_ReferenceCounter); - if (m_ReferenceCounter->Get() == 1) + if (m_ReferenceCounter->get() == 1) { delete m_RealPointer, m_RealPointer = nullptr; delete m_ReferenceCounter, m_ReferenceCounter = nullptr; @@ -218,7 +224,7 @@ namespace std 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; + counter *tempReferenceCounter = m_ReferenceCounter; m_RealPointer = Other.m_RealPointer; m_ReferenceCounter = Other.m_ReferenceCounter; Other.m_RealPointer = tempRealPointer; @@ -258,6 +264,12 @@ namespace std { return shared_ptr(new T(forward(args)...)); }; + + template + smart_ptr make_smart(Args &&...args) + { + return smart_ptr(new T(forward(args)...)); + }; } #endif // !__FENNIX_KERNEL_STD_SMART_POINTER_H__