From fcdede2064856ffa554871b5a5c9f23d55c156a1 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 3 Jul 2021 18:22:06 -0500 Subject: [PATCH] [ox/fs] Cleanup --- .../src/ox/fs/filestore/filestoretemplate.hpp | 27 ++++++++++--------- deps/ox/src/ox/fs/filesystem/directory.hpp | 8 +++--- .../ox/src/ox/fs/filesystem/passthroughfs.hpp | 2 +- deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp | 22 +++++++-------- deps/ox/src/ox/fs/test/tests.cpp | 8 +++--- 5 files changed, 35 insertions(+), 32 deletions(-) diff --git a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp index bd1874c0..5c137d23 100644 --- a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp +++ b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp @@ -63,7 +63,6 @@ class FileStoreTemplate { using Buffer = ptrarith::NodeBuffer>; static constexpr InodeId_t ReservedInodeEnd = 100; - static constexpr auto MaxInode = MaxValue / 2; struct OX_PACKED FileStoreData { LittleEndian rootNode = 0; @@ -203,16 +202,18 @@ FileStoreTemplate::FileStoreTemplate(void *buff, size_t buffSize) { template Error FileStoreTemplate::format(void *buffer, size_t bufferSize) { auto nb = new (buffer) Buffer(bufferSize); - auto fsData = nb->malloc(sizeof(FileStoreData)); - if (fsData.valid()) { - auto data = nb->template dataOf(fsData); - if (data.valid()) { - new (data) FileStoreData; - return OxError(0); - } + auto fsData = nb->malloc(sizeof(FileStoreData)).value; + if (!fsData.valid()) { + oxTrace("ox::fs::FileStoreTemplate::format::fail", "Could not read data section of FileStoreData"); + return OxError(1, "Could not read data section of FileStoreData"); } - oxTrace("ox::fs::FileStoreTemplate::format::fail", "Could not read data section of FileStoreData"); - return OxError(1, "Could not read data section of FileStoreData"); + auto data = nb->template dataOf(fsData); + if (!data.valid()) { + oxTrace("ox::fs::FileStoreTemplate::format::fail", "Could not read data section of FileStoreData"); + return OxError(1, "Could not read data section of FileStoreData"); + } + new (data) FileStoreData; + return OxError(0); } template @@ -261,12 +262,12 @@ Error FileStoreTemplate::write(InodeId_t id, const void *data, FsSize_t existing = nullptr; } // write the given data - auto dest = m_buffer->malloc(dataSize); + auto dest = m_buffer->malloc(dataSize).value; // if first malloc failed, compact and try again if (!dest.valid()) { oxTrace("ox::fs::FileStoreTemplate::write", "Allocation failed, compacting"); oxReturnError(compact()); - dest = m_buffer->malloc(dataSize); + dest = m_buffer->malloc(dataSize).value; } if (dest.valid()) { oxTrace("ox::fs::FileStoreTemplate::write", "Memory allocated"); @@ -414,7 +415,7 @@ Error FileStoreTemplate::resize(std::size_t size, void *newBuff) { template Result FileStoreTemplate::stat(InodeId_t id) { - oxRequireM(inode, find(id).validate()); + oxRequire(inode, find(id).validate()); return StatInfo { id, inode->links, diff --git a/deps/ox/src/ox/fs/filesystem/directory.hpp b/deps/ox/src/ox/fs/filesystem/directory.hpp index 7dacfe61..56a421f3 100644 --- a/deps/ox/src/ox/fs/filesystem/directory.hpp +++ b/deps/ox/src/ox/fs/filesystem/directory.hpp @@ -62,10 +62,12 @@ struct OX_PACKED DirectoryEntry { /** * @return the size of the data + the size of the Item type */ + [[nodiscard]] InodeId_t fullSize() const { return m_bufferSize; } + [[nodiscard]] InodeId_t size() const { return fullSize() - sizeof(*this); } @@ -106,9 +108,9 @@ class Directory { /** * @param parents indicates the operation should create non-existent directories in the path, like mkdir -p */ - Error write(PathIterator it, InodeId_t inode, FileName *nameBuff = nullptr) noexcept; + Error write(PathIterator path, InodeId_t inode, FileName *nameBuff = nullptr) noexcept; - Error remove(PathIterator it, FileName *nameBuff = nullptr) noexcept; + Error remove(PathIterator path, FileName *nameBuff = nullptr) noexcept; template Error ls(F cb) noexcept; @@ -240,7 +242,7 @@ Error Directory::write(PathIterator path, InodeId_t inode, // TODO: look for old version of this entry and delete it oxReturnError(cpy->setSize(newSize)); - auto val = cpy->malloc(entryDataSize); + auto val = cpy->malloc(entryDataSize).value; if (!val.valid()) { oxTrace("ox::fs::Directory::write::fail", "Could not allocate memory for new directory entry"); return OxError(1, "Could not allocate memory for new directory entry"); diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp index 2e3c6a82..8f28f85b 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp @@ -30,7 +30,7 @@ class PassThroughFS: public FileSystem { std::filesystem::path m_path; public: - PassThroughFS(const char *dirPath); + explicit PassThroughFS(const char *dirPath); ~PassThroughFS() override; diff --git a/deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp b/deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp index 60110296..34255ef8 100644 --- a/deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp +++ b/deps/ox/src/ox/fs/ptrarith/nodebuffer.hpp @@ -129,12 +129,12 @@ class OX_PACKED NodeBuffer { ItemPtr ptr(size_t offset) noexcept; - ItemPtr malloc(size_t size) noexcept; + Result malloc(size_t size) noexcept; Error free(ItemPtr item) noexcept; [[nodiscard]] - bool valid(size_t maxSize) noexcept; + bool valid(size_t maxSize) const noexcept; /** * Set size, capacity. @@ -152,7 +152,7 @@ class OX_PACKED NodeBuffer { * @return the bytes still available in this NodeBuffer */ [[nodiscard]] - size_t available() noexcept; + size_t available() const noexcept; /** * @return the actual number a bytes need to store the given number of @@ -267,7 +267,7 @@ typename NodeBuffer::ItemPtr NodeBuffer::ptr(size_t } template -typename NodeBuffer::ItemPtr NodeBuffer::malloc(size_t size) noexcept { +Result::ItemPtr> NodeBuffer::malloc(size_t size) noexcept { oxTracef("ox::ptrarith::NodeBuffer::malloc", "Size: {}", size); size_t fullSize = size + sizeof(Item); if (m_header.size - m_header.bytesUsed >= fullSize) { @@ -283,14 +283,14 @@ typename NodeBuffer::ItemPtr NodeBuffer::malloc(size addr = m_header.firstItem; } else { oxTrace("ox::ptrarith::NodeBuffer::malloc::fail", "NodeBuffer is in invalid state."); - return nullptr; + return OxError(1, "NodeBuffer is in invalid state."); } } oxTracef("ox::ptrarith::NodeBuffer::malloc", "buffer size: {}; addr: {}; fullSize: {}", m_header.size.get(), addr, fullSize); auto out = ItemPtr(this, m_header.size, addr, fullSize); if (!out.valid()) { oxTrace("ox::ptrarith::NodeBuffer::malloc::fail", "Unknown"); - return nullptr; + return OxError(1, "NodeBuffer::malloc: unknown failure"); } ox_memset(out, 0, fullSize); new (out) Item; @@ -303,7 +303,7 @@ typename NodeBuffer::ItemPtr NodeBuffer::malloc(size first->prev = out.offset(); } else { oxTrace("ox::ptrarith::NodeBuffer::malloc::fail", "NodeBuffer malloc failed due to invalid first element pointer."); - return nullptr; + return OxError(1, "NodeBuffer malloc failed due to invalid first element pointer."); } if (oldLast.valid()) { @@ -313,7 +313,7 @@ typename NodeBuffer::ItemPtr NodeBuffer::malloc(size if (out.offset() != first.offset()) { // if this is not the first allocation, there should be an oldLast oxTrace("ox::ptrarith::NodeBuffer::malloc::fail", "NodeBuffer malloc failed due to invalid last element pointer."); - return nullptr; + return OxError(1, "NodeBuffer malloc failed due to invalid last element pointer."); } out->prev = out.offset(); } @@ -322,7 +322,7 @@ typename NodeBuffer::ItemPtr NodeBuffer::malloc(size return out; } oxTracef("ox::ptrarith::NodeBuffer::malloc::fail", "Insufficient space: {} needed, {} available", fullSize, available()); - return nullptr; + return OxError(1); } template @@ -379,12 +379,12 @@ constexpr size_t NodeBuffer::size() const noexcept { } template -bool NodeBuffer::valid(size_t maxSize) noexcept { +bool NodeBuffer::valid(size_t maxSize) const noexcept { return m_header.size <= maxSize; } template -size_t NodeBuffer::available() noexcept { +size_t NodeBuffer::available() const noexcept { return m_header.size - m_header.bytesUsed; } diff --git a/deps/ox/src/ox/fs/test/tests.cpp b/deps/ox/src/ox/fs/test/tests.cpp index 7f4e587e..5d594613 100644 --- a/deps/ox/src/ox/fs/test/tests.cpp +++ b/deps/ox/src/ox/fs/test/tests.cpp @@ -35,8 +35,8 @@ const std::map> tes ox::Vector buff(5 * ox::units::MB); auto buffer = new (buff.data()) ox::ptrarith::NodeBuffer>(buff.size()); using String = ox::BString<6>; - auto a1 = buffer->malloc(sizeof(String)); - auto a2 = buffer->malloc(sizeof(String)); + auto a1 = buffer->malloc(sizeof(String)).value; + auto a2 = buffer->malloc(sizeof(String)).value; oxAssert(a1.valid(), "Allocation 1 failed."); oxAssert(a2.valid(), "Allocation 2 failed."); auto s1Buff = buffer->dataOf(a1); @@ -158,8 +158,8 @@ const std::map> tes [](std::string_view) { constexpr auto buffLen = 5000; auto list = new (ox_alloca(buffLen)) ox::ptrarith::NodeBuffer>(buffLen); - oxAssert(list->malloc(50).valid(), "NodeBuffer::insert: malloc 1 failed"); - oxAssert(list->malloc(50).valid(), "NodeBuffer::insert: malloc 2 failed"); + oxAssert(list->malloc(50).value.valid(), "NodeBuffer::insert: malloc 1 failed"); + oxAssert(list->malloc(50).value.valid(), "NodeBuffer::insert: malloc 2 failed"); auto first = list->firstItem(); oxAssert(first.valid(), "NodeBuffer::insert: Could not access first item"); oxAssert(first->size() == 50, "NodeBuffer::insert: First item size invalid");