Update shared_ptr function names

This commit is contained in:
Alex 2023-03-23 20:48:50 +02:00
parent 6b01a21a71
commit 7b2137bf4d
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
4 changed files with 74 additions and 62 deletions

View File

@ -200,13 +200,13 @@ namespace Execute
switch (ELFHeader->e_type) switch (ELFHeader->e_type)
{ {
case ET_REL: case ET_REL:
bl = ELFLoadRel(ElfFile, ExFile.Get(), Process); bl = ELFLoadRel(ElfFile, ExFile.get(), Process);
break; break;
case ET_EXEC: case ET_EXEC:
bl = ELFLoadExec(ElfFile, ExFile.Get(), Process); bl = ELFLoadExec(ElfFile, ExFile.get(), Process);
break; break;
case ET_DYN: case ET_DYN:
bl = ELFLoadDyn(ElfFile, ExFile.Get(), Process); bl = ELFLoadDyn(ElfFile, ExFile.get(), Process);
break; break;
case ET_CORE: case ET_CORE:
{ {

View File

@ -189,7 +189,7 @@ namespace Execute
strcpy(aux_platform, "x86_64"); 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_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_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_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}}}); ELFBase.auxv.push_back({.archaux = {.a_type = AT_BASE, .a_un = {.a_val = (uint64_t)MemoryImage.Virtual}}});

View File

@ -86,17 +86,17 @@ namespace VirtualFileSystem
continue; continue;
} }
size_t ElementSize = strlen(Path[i]); size_t ElementSize = strlen(Path[i]);
memcpy(FinalPath.Get() + Offset, Path[i], ElementSize); memcpy(FinalPath.get() + Offset, Path[i], ElementSize);
Offset += ElementSize; Offset += ElementSize;
} }
// Add a null terminator to the final path string // Add a null terminator to the final path string
FinalPath.Get()[Size - 1] = '\0'; FinalPath.get()[Size - 1] = '\0';
// Deallocate the Path array // Deallocate the Path array
delete[] Path, Path = nullptr; delete[] Path, Path = nullptr;
vfsdbg("GetPathFromNode()->\"%s\"", FinalPath.Get()); vfsdbg("GetPathFromNode()->\"%s\"", FinalPath.get());
return FinalPath; return FinalPath;
} }
@ -267,17 +267,17 @@ namespace VirtualFileSystem
if (cwk_path_is_relative(NormalizedPath)) if (cwk_path_is_relative(NormalizedPath))
{ {
std::shared_ptr<char> ParentPath = GetPathFromNode(Parent); std::shared_ptr<char> 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]); 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 else
{ {
RelativePath.reset(new char[strlen(NormalizedPath) + 1]); RelativePath.reset(new char[strlen(NormalizedPath) + 1]);
strcpy(RelativePath.Get(), NormalizedPath); strcpy(RelativePath.get(), NormalizedPath);
} }
delete[] NormalizedPath; delete[] NormalizedPath;
vfsdbg("NormalizePath()->\"%s\"", RelativePath.Get()); vfsdbg("NormalizePath()->\"%s\"", RelativePath.get());
return RelativePath; return RelativePath;
} }
@ -294,7 +294,7 @@ namespace VirtualFileSystem
vfsdbg("PathExists( Path: \"%s\" Parent: \"%s\" )", Path, Parent->Name); 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"); vfsdbg("PathExists()->OK");
return true; return true;
@ -330,16 +330,16 @@ namespace VirtualFileSystem
vfsdbg("Virtual::Create( Path: \"%s\" Parent: \"%s\" )", Path, Parent ? Parent->Name : CurrentParent->Name); vfsdbg("Virtual::Create( Path: \"%s\" Parent: \"%s\" )", Path, Parent ? Parent->Name : CurrentParent->Name);
std::shared_ptr<char> CleanPath = this->NormalizePath(Path, CurrentParent); std::shared_ptr<char> 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; goto CreatePathError;
} }
cwk_segment segment; 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."); error("Path doesn't have any segments.");
goto CreatePathError; goto CreatePathError;
@ -373,7 +373,7 @@ namespace VirtualFileSystem
} while (cwk_path_get_next_segment(&segment)); } while (cwk_path_get_next_segment(&segment));
vfsdbg("Virtual::Create()->\"%s\"", CurrentParent->Name); vfsdbg("Virtual::Create()->\"%s\"", CurrentParent->Name);
vfsdbg("Path created: \"%s\"", GetPathFromNode(CurrentParent).Get()); vfsdbg("Path created: \"%s\"", GetPathFromNode(CurrentParent).get());
return CurrentParent; return CurrentParent;
CreatePathError: CreatePathError:
@ -393,16 +393,16 @@ namespace VirtualFileSystem
Parent = FileSystemRoot; Parent = FileSystemRoot;
std::shared_ptr<char> CleanPath = this->NormalizePath(Path, Parent); std::shared_ptr<char> 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; return InvalidPath;
} }
Node *NodeToDelete = GetNodeFromPath(CleanPath.Get(), Parent); Node *NodeToDelete = GetNodeFromPath(CleanPath.get(), Parent);
Node *ParentNode = GetParent(CleanPath.Get(), Parent); Node *ParentNode = GetParent(CleanPath.get(), Parent);
if (NodeToDelete->Flags == NodeFlags::DIRECTORY) if (NodeToDelete->Flags == NodeFlags::DIRECTORY)
{ {
@ -411,7 +411,7 @@ namespace VirtualFileSystem
foreach (auto Child in NodeToDelete->Children) foreach (auto Child in NodeToDelete->Children)
{ {
VFSLock.Unlock(); VFSLock.Unlock();
FileStatus Status = Delete(GetPathFromNode(Child).Get(), true); FileStatus Status = Delete(GetPathFromNode(Child).get(), true);
VFSLock.Lock(__FUNCTION__); VFSLock.Lock(__FUNCTION__);
if (Status != FileStatus::OK) if (Status != FileStatus::OK)
{ {
@ -422,7 +422,7 @@ namespace VirtualFileSystem
} }
else if (NodeToDelete->Children.size() > 0) else if (NodeToDelete->Children.size() > 0)
{ {
vfsdbg("Directory %s is not empty.", CleanPath.Get()); vfsdbg("Directory %s is not empty.", CleanPath.get());
return DirectoryNotEmpty; return DirectoryNotEmpty;
} }
} }
@ -437,7 +437,7 @@ namespace VirtualFileSystem
return OK; 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 */ /* TODO: REWORK */
std::shared_ptr<File> Virtual::Mount(const char *Path, FileSystemOperations *Operator) std::shared_ptr<File> Virtual::Mount(const char *Path, FileSystemOperations *Operator)
@ -470,7 +470,7 @@ namespace VirtualFileSystem
FileStatus Virtual::Unmount(std::shared_ptr<File> File) FileStatus Virtual::Unmount(std::shared_ptr<File> File)
{ {
SmartLock(VFSLock); SmartLock(VFSLock);
if (unlikely(File.Get())) if (unlikely(File.get()))
return FileStatus::InvalidParameter; return FileStatus::InvalidParameter;
fixme("Unmounting %s", File->Name); fixme("Unmounting %s", File->Name);
return FileStatus::OK; return FileStatus::OK;
@ -479,7 +479,7 @@ namespace VirtualFileSystem
size_t Virtual::Read(std::shared_ptr<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); SmartLock(VFSLock);
if (unlikely(!File.Get())) if (unlikely(!File.get()))
return 0; return 0;
if (unlikely(!File->node)) if (unlikely(!File->node))
@ -503,7 +503,7 @@ namespace VirtualFileSystem
size_t Virtual::Write(std::shared_ptr<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); SmartLock(VFSLock);
if (unlikely(!File.Get())) if (unlikely(!File.get()))
return 0; return 0;
if (unlikely(!File->node)) if (unlikely(!File->node))
@ -537,7 +537,7 @@ namespace VirtualFileSystem
file->node = Parent; file->node = Parent;
if (unlikely(!file->node)) if (unlikely(!file->node))
file->Status = FileStatus::NotFound; 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); strcpy(file->Name, basename);
return file; return file;
} }
@ -551,7 +551,7 @@ namespace VirtualFileSystem
if (!file->node) if (!file->node)
file->Status = FileStatus::NotFound; 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); strcpy(file->Name, basename);
return file; return file;
} }
@ -562,11 +562,11 @@ namespace VirtualFileSystem
std::shared_ptr<File> file = std::make_shared<File>(); std::shared_ptr<File> file = std::make_shared<File>();
/* TODO: Check for other errors */ /* TODO: Check for other errors */
if (!PathExists(CleanPath.Get(), CurrentParent)) if (!PathExists(CleanPath.get(), CurrentParent))
{ {
foreach (auto Child in FileSystemRoot->Children) foreach (auto Child in FileSystemRoot->Children)
{ {
if (strcmp(Child->Name, CleanPath.Get()) == 0) if (strcmp(Child->Name, CleanPath.get()) == 0)
{ {
file->node = Child; file->node = Child;
if (file->node == nullptr) if (file->node == nullptr)
@ -575,24 +575,24 @@ namespace VirtualFileSystem
file->node = nullptr; file->node = nullptr;
return file; return file;
} }
cwk_path_get_basename(GetPathFromNode(Child).Get(), &basename, nullptr); cwk_path_get_basename(GetPathFromNode(Child).get(), &basename, nullptr);
strcpy(file->Name, basename); strcpy(file->Name, basename);
return file; return file;
} }
} }
file->node = GetNodeFromPath(CleanPath.Get(), FileSystemRoot->Children[0]); file->node = GetNodeFromPath(CleanPath.get(), FileSystemRoot->Children[0]);
if (file->node) 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); strcpy(file->Name, basename);
return file; return file;
} }
} }
else else
{ {
file->node = GetNodeFromPath(CleanPath.Get(), CurrentParent); file->node = GetNodeFromPath(CleanPath.get(), CurrentParent);
cwk_path_get_basename(CleanPath.Get(), &basename, nullptr); cwk_path_get_basename(CleanPath.get(), &basename, nullptr);
strcpy(file->Name, basename); strcpy(file->Name, basename);
return file; return file;
} }
@ -604,7 +604,7 @@ namespace VirtualFileSystem
FileStatus Virtual::Close(std::shared_ptr<File> File) FileStatus Virtual::Close(std::shared_ptr<File> File)
{ {
SmartLock(VFSLock); SmartLock(VFSLock);
if (unlikely(!File.Get())) if (unlikely(!File.get()))
return FileStatus::InvalidHandle; return FileStatus::InvalidHandle;
vfsdbg("Closing %s", File->Name); vfsdbg("Closing %s", File->Name);
return FileStatus::OK; return FileStatus::OK;

View File

@ -57,6 +57,12 @@ namespace std
spdbg("Smart pointer dereferenced (%#lx)", m_RealPointer); spdbg("Smart pointer dereferenced (%#lx)", m_RealPointer);
return m_RealPointer; return m_RealPointer;
} }
T *get()
{
spdbg("Smart pointer returned (%#lx)", m_RealPointer);
return m_RealPointer;
}
}; };
template <class T> template <class T>
@ -78,61 +84,61 @@ namespace std
class shared_ptr class shared_ptr
{ {
private: private:
class Counter class counter
{ {
private: private:
unsigned int m_RefCount{}; unsigned int m_RefCount{};
public: public:
Counter() : m_RefCount(0) { spdbg("Counter %#lx created", this); }; counter() : m_RefCount(0) { spdbg("Counter %#lx created", this); };
Counter(const Counter &) = delete; counter(const counter &) = delete;
Counter &operator=(const Counter &) = delete; counter &operator=(const counter &) = delete;
~Counter() { spdbg("Counter %#lx deleted", this); } ~counter() { spdbg("Counter %#lx deleted", this); }
void Reset() void reset()
{ {
m_RefCount = 0; m_RefCount = 0;
spdbg("Counter reset"); spdbg("reset");
} }
unsigned int Get() unsigned int get()
{ {
return m_RefCount; return m_RefCount;
spdbg("Counter returned"); spdbg("return");
} }
void operator++() void operator++()
{ {
m_RefCount++; m_RefCount++;
spdbg("Counter incremented"); spdbg("increment");
} }
void operator++(int) void operator++(int)
{ {
m_RefCount++; m_RefCount++;
spdbg("Counter incremented"); spdbg("increment");
} }
void operator--() void operator--()
{ {
m_RefCount--; m_RefCount--;
spdbg("Counter decremented"); spdbg("decrement");
} }
void operator--(int) void operator--(int)
{ {
m_RefCount--; m_RefCount--;
spdbg("Counter decremented"); spdbg("decrement");
} }
}; };
Counter *m_ReferenceCounter; counter *m_ReferenceCounter;
T *m_RealPointer; T *m_RealPointer;
public: public:
explicit shared_ptr(T *Pointer = nullptr) explicit shared_ptr(T *Pointer = nullptr)
{ {
m_RealPointer = Pointer; m_RealPointer = Pointer;
m_ReferenceCounter = new Counter(); m_ReferenceCounter = new counter();
spdbg("[%#lx] Shared pointer created (ptr=%#lx, ref=%#lx)", this, Pointer, m_ReferenceCounter); spdbg("[%#lx] Shared pointer created (ptr=%#lx, ref=%#lx)", this, Pointer, m_ReferenceCounter);
if (Pointer) if (Pointer)
(*m_ReferenceCounter)++; (*m_ReferenceCounter)++;
@ -150,7 +156,7 @@ namespace std
{ {
spdbg("[%#lx] Shared pointer destructor called", this); spdbg("[%#lx] Shared pointer destructor called", this);
(*m_ReferenceCounter)--; (*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); spdbg("[%#lx] Shared pointer deleted (ptr=%#lx, ref=%#lx)", this, m_RealPointer, m_ReferenceCounter);
delete m_ReferenceCounter, m_ReferenceCounter = nullptr; 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()); spdbg("[%#lx] Shared pointer count (%d)", this, m_ReferenceCounter->get());
return m_ReferenceCounter->Get(); return m_ReferenceCounter->get();
} }
T *Get() T *get()
{ {
spdbg("[%#lx] Shared pointer get (%#lx)", this, m_RealPointer); spdbg("[%#lx] Shared pointer get (%#lx)", this, m_RealPointer);
return m_RealPointer; return m_RealPointer;
@ -188,13 +194,13 @@ namespace std
return; return;
spdbg("[%#lx] Shared pointer reset (ptr=%#lx, ref=%#lx)", this, Pointer, m_ReferenceCounter); spdbg("[%#lx] Shared pointer reset (ptr=%#lx, ref=%#lx)", this, Pointer, m_ReferenceCounter);
(*m_ReferenceCounter)--; (*m_ReferenceCounter)--;
if (m_ReferenceCounter->Get() == 0) if (m_ReferenceCounter->get() == 0)
{ {
delete m_RealPointer; delete m_RealPointer;
delete m_ReferenceCounter; delete m_ReferenceCounter;
} }
m_RealPointer = Pointer; m_RealPointer = Pointer;
m_ReferenceCounter = new Counter(); m_ReferenceCounter = new counter();
if (Pointer) if (Pointer)
(*m_ReferenceCounter)++; (*m_ReferenceCounter)++;
} }
@ -202,7 +208,7 @@ namespace std
void reset() void reset()
{ {
spdbg("[%#lx] Shared pointer reset (ptr=%#lx, ref=%#lx)", this, m_RealPointer, m_ReferenceCounter); 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_RealPointer, m_RealPointer = nullptr;
delete m_ReferenceCounter, m_ReferenceCounter = 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)", spdbg("[%#lx] Shared pointer swap (ptr=%#lx, ref=%#lx <=> ptr=%#lx, ref=%#lx)",
this, m_RealPointer, m_ReferenceCounter, Other.m_RealPointer, Other.m_ReferenceCounter); this, m_RealPointer, m_ReferenceCounter, Other.m_RealPointer, Other.m_ReferenceCounter);
T *tempRealPointer = m_RealPointer; T *tempRealPointer = m_RealPointer;
Counter *tempReferenceCounter = m_ReferenceCounter; counter *tempReferenceCounter = m_ReferenceCounter;
m_RealPointer = Other.m_RealPointer; m_RealPointer = Other.m_RealPointer;
m_ReferenceCounter = Other.m_ReferenceCounter; m_ReferenceCounter = Other.m_ReferenceCounter;
Other.m_RealPointer = tempRealPointer; Other.m_RealPointer = tempRealPointer;
@ -258,6 +264,12 @@ namespace std
{ {
return shared_ptr<T>(new T(forward<Args>(args)...)); return shared_ptr<T>(new T(forward<Args>(args)...));
}; };
template <typename T, typename... Args>
smart_ptr<T> make_smart(Args &&...args)
{
return smart_ptr<T>(new T(forward<Args>(args)...));
};
} }
#endif // !__FENNIX_KERNEL_STD_SMART_POINTER_H__ #endif // !__FENNIX_KERNEL_STD_SMART_POINTER_H__