diff --git a/deps/ox/src/ox/fs/filestore/filestore.hpp b/deps/ox/src/ox/fs/filestore/filestore.hpp index 239dae97..f1e5c252 100644 --- a/deps/ox/src/ox/fs/filestore/filestore.hpp +++ b/deps/ox/src/ox/fs/filestore/filestore.hpp @@ -8,7 +8,7 @@ #pragma once -#include "linkedlist.hpp" +#include "nodebuffer.hpp" namespace ox::fs { diff --git a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp index 40f8b6b8..e3009adc 100644 --- a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp +++ b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp @@ -9,20 +9,36 @@ #pragma once #include "filestore.hpp" -#include "linkedlist.hpp" +#include "nodebuffer.hpp" namespace ox::fs { +template +struct __attribute__((packed)) FileStoreItem: public Item { + private: + ox::LittleEndian id = 0; + ox::LittleEndian left = 0; + ox::LittleEndian right = 0; + + public: + explicit FileStoreItem(size_t size): Item(size) { + } + + ox::fs::Ptr data() { + return Ptr(this, this->size(), sizeof(*this), this->size() - sizeof(*this)); + } +}; + template class FileStoreTemplate: public FileStore { private: struct __attribute__((packed)) FileStoreData { - ox::LittleEndian rootNode = sizeof(LinkedList); + ox::LittleEndian rootNode = sizeof(NodeBuffer>); }; size_t m_buffSize = 0; - ox::fs::LinkedList *m_linkedList = nullptr; + ox::fs::NodeBuffer> *m_buffer = nullptr; public: FileStoreTemplate(void *buff, size_t buffSize); @@ -57,17 +73,18 @@ class FileStoreTemplate: public FileStore { template FileStoreTemplate::FileStoreTemplate(void *buff, size_t buffSize) { m_buffSize = buffSize; - m_linkedList = static_cast*>(buff); - if (!m_linkedList->valid(buffSize)) { + m_buffer = static_cast>*>(buff); + if (!m_buffer->valid(buffSize)) { m_buffSize = 0; - m_linkedList = nullptr; + m_buffer = nullptr; } } template Error FileStoreTemplate::format() { Error err = 0; - auto data = m_linkedList->malloc(sizeof(FileStoreData)); + new (m_buffer) NodeBuffer>(m_buffSize); + auto data = m_buffer->malloc(sizeof(FileStoreData)); if (data.valid()) { new (data->data()) FileStoreData; } else { @@ -80,7 +97,7 @@ template Error FileStoreTemplate::setSize(InodeId_t size) { Error err = 0; if (m_buffSize >= size) { - err |= m_linkedList->setSize(size); + err |= m_buffer->setSize(size); } else { err = 1; } @@ -134,7 +151,7 @@ InodeId_t FileStoreTemplate::available() { template typename FileStoreTemplate::FileStoreData &FileStoreTemplate::fileStoreData() { - return *reinterpret_cast(m_linkedList->firstItem()); + return *reinterpret_cast(m_buffer->firstItem()); } using FileStore16 = FileStoreTemplate; diff --git a/deps/ox/src/ox/fs/filestore/linkedlist.hpp b/deps/ox/src/ox/fs/filestore/nodebuffer.hpp similarity index 80% rename from deps/ox/src/ox/fs/filestore/linkedlist.hpp rename to deps/ox/src/ox/fs/filestore/nodebuffer.hpp index 84bf8cff..aaacc4d1 100644 --- a/deps/ox/src/ox/fs/filestore/linkedlist.hpp +++ b/deps/ox/src/ox/fs/filestore/nodebuffer.hpp @@ -12,29 +12,8 @@ namespace ox::fs { -struct __attribute__((packed)) Item { - public: - ox::LittleEndian m_size = sizeof(Item); - - public: - ox::LittleEndian prev = 0; - ox::LittleEndian next = 0; - - explicit Item(size_t size) { - this->m_size = size; - } - - size_t size() const { - return m_size; - } - - ox::fs::Ptr data() { - return Ptr(this, m_size, sizeof(*this), m_size - sizeof(*this)); - } -}; - template -class __attribute__((packed)) LinkedList { +class __attribute__((packed)) NodeBuffer { private: struct __attribute__((packed)) Header { @@ -67,9 +46,9 @@ class __attribute__((packed)) LinkedList { Header m_header; public: - LinkedList() = default; + NodeBuffer() = default; - explicit LinkedList(size_t size); + explicit NodeBuffer(size_t size); ItemPtr firstItem(); @@ -94,7 +73,7 @@ class __attribute__((packed)) LinkedList { size_t size(); /** - * @return the bytes still available in this LinkedList + * @return the bytes still available in this NodeBuffer */ size_t available(); @@ -106,17 +85,17 @@ class __attribute__((packed)) LinkedList { }; template -LinkedList::LinkedList(size_t size) { +NodeBuffer::NodeBuffer(size_t size) { m_header.size = size; } template -typename LinkedList::ItemPtr LinkedList::firstItem() { +typename NodeBuffer::ItemPtr NodeBuffer::firstItem() { return ptr(m_header.firstItem); } template -typename LinkedList::ItemPtr LinkedList::lastItem() { +typename NodeBuffer::ItemPtr NodeBuffer::lastItem() { auto first = ptr(m_header.firstItem); if (first.valid()) { return prev(first); @@ -125,27 +104,27 @@ typename LinkedList::ItemPtr LinkedList::lastItem() } template -typename LinkedList::ItemPtr LinkedList::prev(Item *item) { +typename NodeBuffer::ItemPtr NodeBuffer::prev(Item *item) { return ptr(item->prev); } template -typename LinkedList::ItemPtr LinkedList::next(Item *item) { +typename NodeBuffer::ItemPtr NodeBuffer::next(Item *item) { return ptr(item->next); } template -typename LinkedList::ItemPtr LinkedList::ptr(size_t offset) { +typename NodeBuffer::ItemPtr NodeBuffer::ptr(size_t offset) { return ItemPtr(this, m_header.size, offset); } template -typename LinkedList::ItemPtr LinkedList::ptr(void *item) { +typename NodeBuffer::ItemPtr NodeBuffer::ptr(void *item) { return ItemPtr(this, m_header.size, reinterpret_cast(static_cast(item) - static_cast(this))); } template -typename LinkedList::ItemPtr LinkedList::malloc(size_t size) { +typename NodeBuffer::ItemPtr NodeBuffer::malloc(size_t size) { size += sizeof(Item); if (m_header.size - m_header.bytesUsed >= size) { if (!m_header.firstItem) { @@ -172,7 +151,7 @@ typename LinkedList::ItemPtr LinkedList::malloc(size } template -void LinkedList::free(ItemPtr item) { +void NodeBuffer::free(ItemPtr item) { auto prev = this->prev(item); auto next = this->next(item); if (prev.valid()) { @@ -185,7 +164,7 @@ void LinkedList::free(ItemPtr item) { } template -Error LinkedList::setSize(size_t size) { +Error NodeBuffer::setSize(size_t size) { auto last = lastItem(); if ((last.valid() and last.end() >= size) or size < sizeof(m_header)) { return 1; @@ -196,17 +175,17 @@ Error LinkedList::setSize(size_t size) { } template -bool LinkedList::valid(size_t maxSize) { +bool NodeBuffer::valid(size_t maxSize) { return m_header.size <= maxSize; } template -size_t LinkedList::available() { +size_t NodeBuffer::available() { return m_header.size - m_header.bytesUsed; } template -void LinkedList::compact(void (*cb)(ItemPtr)) { +void NodeBuffer::compact(void (*cb)(ItemPtr)) { auto src = firstItem(); auto dest = data(); while (src.valid()) { @@ -231,8 +210,28 @@ void LinkedList::compact(void (*cb)(ItemPtr)) { } template -uint8_t *LinkedList::data() { +uint8_t *NodeBuffer::data() { return reinterpret_cast(this + 1); } + +template +struct __attribute__((packed)) Item { + public: + ox::LittleEndian prev = 0; + ox::LittleEndian next = 0; + + private: + ox::LittleEndian m_size = sizeof(Item); + + public: + explicit Item(size_t size) { + this->m_size = size; + } + + size_t size() const { + return m_size; + } +}; + } diff --git a/deps/ox/src/ox/fs/test/CMakeLists.txt b/deps/ox/src/ox/fs/test/CMakeLists.txt index dfc796a0..69e85ad2 100644 --- a/deps/ox/src/ox/fs/test/CMakeLists.txt +++ b/deps/ox/src/ox/fs/test/CMakeLists.txt @@ -69,3 +69,4 @@ add_test("Test\\ FileSystem32::stripDirectories" FSTests "FileSystem32::stripDir add_test("Test\\ FileSystem32::ls" FSTests "FileSystem32::ls") add_test("Test\\ LinkedList::insert" FSTests "LinkedList::insert") +add_test("Test\\ NodeBuffer::readWrite" FSTests "NodeBuffer::readWrite") diff --git a/deps/ox/src/ox/fs/test/tests.cpp b/deps/ox/src/ox/fs/test/tests.cpp index 05093455..287e6a36 100644 --- a/deps/ox/src/ox/fs/test/tests.cpp +++ b/deps/ox/src/ox/fs/test/tests.cpp @@ -333,12 +333,12 @@ map tests = { } }, { - "LinkedList::insert", + "NodeBuffer::insert", [](string) { int err = 0; constexpr auto buffLen = 5000; uint8_t buff[buffLen]; - auto list = new (buff) ox::fs::LinkedList(buffLen); + auto list = new (buff) ox::fs::NodeBuffer>(buffLen); err |= !(list->malloc(50).valid()); err |= !(list->firstItem().valid()); err |= !(list->firstItem()->size() == 50); @@ -350,7 +350,7 @@ map tests = { [](string) { constexpr auto buffLen = 5000; uint8_t buff[buffLen]; - auto list = new (buff) ox::fs::LinkedList(buffLen); + auto list = new (buff) ox::fs::NodeBuffer>(buffLen); ox::fs::FileStore32 fileStore(list, buffLen); ox_assert(fileStore.format() == 0, "Filestore::format failed."); return 0;