[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>>;
static constexpr InodeId_t ReservedInodeEnd = 100;
static constexpr auto MaxInode = MaxValue<size_t> / 2;
struct OX_PACKED FileStoreData {
LittleEndian<size_t> rootNode = 0;
@ -203,16 +202,18 @@ FileStoreTemplate<size_t>::FileStoreTemplate(void *buff, size_t buffSize) {
template<typename size_t>
Error FileStoreTemplate<size_t>::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<FileStoreData>(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<FileStoreData>(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<typename size_t>
@ -261,12 +262,12 @@ Error FileStoreTemplate<size_t>::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<size_t>::resize(std::size_t size, void *newBuff) {
template<typename size_t>
Result<StatInfo> FileStoreTemplate<size_t>::stat(InodeId_t id) {
oxRequireM(inode, find(id).validate());
oxRequire(inode, find(id).validate());
return StatInfo {
id,
inode->links,

View File

@ -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<typename F>
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
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");

View File

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

View File

@ -129,12 +129,12 @@ class OX_PACKED NodeBuffer {
ItemPtr ptr(size_t offset) noexcept;
ItemPtr malloc(size_t size) noexcept;
Result<ItemPtr> 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<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::ptr(size_t
}
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);
size_t fullSize = size + sizeof(Item);
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;
} 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<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::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<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::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<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::malloc(size
return out;
}
oxTracef("ox::ptrarith::NodeBuffer::malloc::fail", "Insufficient space: {} needed, {} available", fullSize, available());
return nullptr;
return OxError(1);
}
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>
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;
}
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;
}

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);
auto buffer = new (buff.data()) ox::ptrarith::NodeBuffer<BuffPtr_t, NodeType<BuffPtr_t>>(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<String>(a1);
@ -158,8 +158,8 @@ const std::map<std::string_view, std::function<ox::Error(std::string_view)>> tes
[](std::string_view) {
constexpr auto buffLen = 5000;
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).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");