[ox/fs] Cleanup

This commit is contained in:
Gary Talent 2021-07-03 18:22:06 -05:00
parent 66a11eae31
commit fcdede2064
5 changed files with 35 additions and 32 deletions

View File

@ -63,7 +63,6 @@ class FileStoreTemplate {
using Buffer = ptrarith::NodeBuffer<size_t, FileStoreItem<size_t>>; using Buffer = ptrarith::NodeBuffer<size_t, FileStoreItem<size_t>>;
static constexpr InodeId_t ReservedInodeEnd = 100; static constexpr InodeId_t ReservedInodeEnd = 100;
static constexpr auto MaxInode = MaxValue<size_t> / 2;
struct OX_PACKED FileStoreData { struct OX_PACKED FileStoreData {
LittleEndian<size_t> rootNode = 0; LittleEndian<size_t> rootNode = 0;
@ -203,16 +202,18 @@ FileStoreTemplate<size_t>::FileStoreTemplate(void *buff, size_t buffSize) {
template<typename size_t> template<typename size_t>
Error FileStoreTemplate<size_t>::format(void *buffer, size_t bufferSize) { Error FileStoreTemplate<size_t>::format(void *buffer, size_t bufferSize) {
auto nb = new (buffer) Buffer(bufferSize); auto nb = new (buffer) Buffer(bufferSize);
auto fsData = nb->malloc(sizeof(FileStoreData)); auto fsData = nb->malloc(sizeof(FileStoreData)).value;
if (fsData.valid()) { if (!fsData.valid()) {
auto data = nb->template dataOf<FileStoreData>(fsData); oxTrace("ox::fs::FileStoreTemplate::format::fail", "Could not read data section of FileStoreData");
if (data.valid()) { return OxError(1, "Could not read data section of FileStoreData");
new (data) FileStoreData;
return OxError(0);
}
} }
oxTrace("ox::fs::FileStoreTemplate::format::fail", "Could not read data section of FileStoreData"); auto data = nb->template dataOf<FileStoreData>(fsData);
return OxError(1, "Could not read data section of FileStoreData"); 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<typename size_t> template<typename size_t>
@ -261,12 +262,12 @@ Error FileStoreTemplate<size_t>::write(InodeId_t id, const void *data, FsSize_t
existing = nullptr; existing = nullptr;
} }
// write the given data // 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 first malloc failed, compact and try again
if (!dest.valid()) { if (!dest.valid()) {
oxTrace("ox::fs::FileStoreTemplate::write", "Allocation failed, compacting"); oxTrace("ox::fs::FileStoreTemplate::write", "Allocation failed, compacting");
oxReturnError(compact()); oxReturnError(compact());
dest = m_buffer->malloc(dataSize); dest = m_buffer->malloc(dataSize).value;
} }
if (dest.valid()) { if (dest.valid()) {
oxTrace("ox::fs::FileStoreTemplate::write", "Memory allocated"); oxTrace("ox::fs::FileStoreTemplate::write", "Memory allocated");
@ -414,7 +415,7 @@ Error FileStoreTemplate<size_t>::resize(std::size_t size, void *newBuff) {
template<typename size_t> template<typename size_t>
Result<StatInfo> FileStoreTemplate<size_t>::stat(InodeId_t id) { Result<StatInfo> FileStoreTemplate<size_t>::stat(InodeId_t id) {
oxRequireM(inode, find(id).validate()); oxRequire(inode, find(id).validate());
return StatInfo { return StatInfo {
id, id,
inode->links, inode->links,

View File

@ -62,10 +62,12 @@ struct OX_PACKED DirectoryEntry {
/** /**
* @return the size of the data + the size of the Item type * @return the size of the data + the size of the Item type
*/ */
[[nodiscard]]
InodeId_t fullSize() const { InodeId_t fullSize() const {
return m_bufferSize; return m_bufferSize;
} }
[[nodiscard]]
InodeId_t size() const { InodeId_t size() const {
return fullSize() - sizeof(*this); 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 * @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<typename F> template<typename F>
Error ls(F cb) noexcept; Error ls(F cb) noexcept;
@ -240,7 +242,7 @@ Error Directory<FileStore, InodeId_t>::write(PathIterator path, InodeId_t inode,
// TODO: look for old version of this entry and delete it // TODO: look for old version of this entry and delete it
oxReturnError(cpy->setSize(newSize)); oxReturnError(cpy->setSize(newSize));
auto val = cpy->malloc(entryDataSize); auto val = cpy->malloc(entryDataSize).value;
if (!val.valid()) { if (!val.valid()) {
oxTrace("ox::fs::Directory::write::fail", "Could not allocate memory for new directory entry"); 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"); return OxError(1, "Could not allocate memory for new directory entry");

View File

@ -30,7 +30,7 @@ class PassThroughFS: public FileSystem {
std::filesystem::path m_path; std::filesystem::path m_path;
public: public:
PassThroughFS(const char *dirPath); explicit PassThroughFS(const char *dirPath);
~PassThroughFS() override; ~PassThroughFS() override;

View File

@ -129,12 +129,12 @@ class OX_PACKED NodeBuffer {
ItemPtr ptr(size_t offset) noexcept; ItemPtr ptr(size_t offset) noexcept;
ItemPtr malloc(size_t size) noexcept; Result<ItemPtr> malloc(size_t size) noexcept;
Error free(ItemPtr item) noexcept; Error free(ItemPtr item) noexcept;
[[nodiscard]] [[nodiscard]]
bool valid(size_t maxSize) noexcept; bool valid(size_t maxSize) const noexcept;
/** /**
* Set size, capacity. * Set size, capacity.
@ -152,7 +152,7 @@ class OX_PACKED NodeBuffer {
* @return the bytes still available in this NodeBuffer * @return the bytes still available in this NodeBuffer
*/ */
[[nodiscard]] [[nodiscard]]
size_t available() noexcept; size_t available() const noexcept;
/** /**
* @return the actual number a bytes need to store the given number of * @return the actual number a bytes need to store the given number of
@ -267,7 +267,7 @@ typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::ptr(size_t
} }
template<typename size_t, typename Item> template<typename size_t, typename Item>
typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::malloc(size_t size) noexcept { Result<typename NodeBuffer<size_t, Item>::ItemPtr> NodeBuffer<size_t, Item>::malloc(size_t size) noexcept {
oxTracef("ox::ptrarith::NodeBuffer::malloc", "Size: {}", size); oxTracef("ox::ptrarith::NodeBuffer::malloc", "Size: {}", size);
size_t fullSize = size + sizeof(Item); size_t fullSize = size + sizeof(Item);
if (m_header.size - m_header.bytesUsed >= fullSize) { if (m_header.size - m_header.bytesUsed >= fullSize) {
@ -283,14 +283,14 @@ typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::malloc(size
addr = m_header.firstItem; addr = m_header.firstItem;
} else { } else {
oxTrace("ox::ptrarith::NodeBuffer::malloc::fail", "NodeBuffer is in invalid state."); 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); oxTracef("ox::ptrarith::NodeBuffer::malloc", "buffer size: {}; addr: {}; fullSize: {}", m_header.size.get(), addr, fullSize);
auto out = ItemPtr(this, m_header.size, addr, fullSize); auto out = ItemPtr(this, m_header.size, addr, fullSize);
if (!out.valid()) { if (!out.valid()) {
oxTrace("ox::ptrarith::NodeBuffer::malloc::fail", "Unknown"); oxTrace("ox::ptrarith::NodeBuffer::malloc::fail", "Unknown");
return nullptr; return OxError(1, "NodeBuffer::malloc: unknown failure");
} }
ox_memset(out, 0, fullSize); ox_memset(out, 0, fullSize);
new (out) Item; new (out) Item;
@ -303,7 +303,7 @@ typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::malloc(size
first->prev = out.offset(); first->prev = out.offset();
} else { } else {
oxTrace("ox::ptrarith::NodeBuffer::malloc::fail", "NodeBuffer malloc failed due to invalid first element pointer."); 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()) { if (oldLast.valid()) {
@ -313,7 +313,7 @@ typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::malloc(size
if (out.offset() != first.offset()) { if (out.offset() != first.offset()) {
// if this is not the first allocation, there should be an oldLast // 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."); 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(); out->prev = out.offset();
} }
@ -322,7 +322,7 @@ typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::malloc(size
return out; return out;
} }
oxTracef("ox::ptrarith::NodeBuffer::malloc::fail", "Insufficient space: {} needed, {} available", fullSize, available()); oxTracef("ox::ptrarith::NodeBuffer::malloc::fail", "Insufficient space: {} needed, {} available", fullSize, available());
return nullptr; return OxError(1);
} }
template<typename size_t, typename Item> template<typename size_t, typename Item>
@ -379,12 +379,12 @@ constexpr size_t NodeBuffer<size_t, Item>::size() const noexcept {
} }
template<typename size_t, typename Item> template<typename size_t, typename Item>
bool NodeBuffer<size_t, Item>::valid(size_t maxSize) noexcept { bool NodeBuffer<size_t, Item>::valid(size_t maxSize) const noexcept {
return m_header.size <= maxSize; return m_header.size <= maxSize;
} }
template<typename size_t, typename Item> template<typename size_t, typename Item>
size_t NodeBuffer<size_t, Item>::available() noexcept { size_t NodeBuffer<size_t, Item>::available() const noexcept {
return m_header.size - m_header.bytesUsed; return m_header.size - m_header.bytesUsed;
} }

View File

@ -35,8 +35,8 @@ const std::map<std::string_view, std::function<ox::Error(std::string_view)>> tes
ox::Vector<char> buff(5 * ox::units::MB); ox::Vector<char> buff(5 * ox::units::MB);
auto buffer = new (buff.data()) ox::ptrarith::NodeBuffer<BuffPtr_t, NodeType<BuffPtr_t>>(buff.size()); auto buffer = new (buff.data()) ox::ptrarith::NodeBuffer<BuffPtr_t, NodeType<BuffPtr_t>>(buff.size());
using String = ox::BString<6>; using String = ox::BString<6>;
auto a1 = buffer->malloc(sizeof(String)); auto a1 = buffer->malloc(sizeof(String)).value;
auto a2 = buffer->malloc(sizeof(String)); auto a2 = buffer->malloc(sizeof(String)).value;
oxAssert(a1.valid(), "Allocation 1 failed."); oxAssert(a1.valid(), "Allocation 1 failed.");
oxAssert(a2.valid(), "Allocation 2 failed."); oxAssert(a2.valid(), "Allocation 2 failed.");
auto s1Buff = buffer->dataOf<String>(a1); auto s1Buff = buffer->dataOf<String>(a1);
@ -158,8 +158,8 @@ const std::map<std::string_view, std::function<ox::Error(std::string_view)>> tes
[](std::string_view) { [](std::string_view) {
constexpr auto buffLen = 5000; constexpr auto buffLen = 5000;
auto list = new (ox_alloca(buffLen)) ox::ptrarith::NodeBuffer<uint32_t, ox::FileStoreItem<uint32_t>>(buffLen); auto list = new (ox_alloca(buffLen)) ox::ptrarith::NodeBuffer<uint32_t, ox::FileStoreItem<uint32_t>>(buffLen);
oxAssert(list->malloc(50).valid(), "NodeBuffer::insert: malloc 1 failed"); oxAssert(list->malloc(50).value.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 2 failed");
auto first = list->firstItem(); auto first = list->firstItem();
oxAssert(first.valid(), "NodeBuffer::insert: Could not access first item"); oxAssert(first.valid(), "NodeBuffer::insert: Could not access first item");
oxAssert(first->size() == 50, "NodeBuffer::insert: First item size invalid"); oxAssert(first->size() == 50, "NodeBuffer::insert: First item size invalid");