From 2e29f488108112840e79558c511ce117ea295225 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 7 Mar 2018 22:40:38 -0600 Subject: [PATCH] Make FS linked list Item a template parameter --- .../src/ox/fs/filestore/filestoretemplate.hpp | 6 +- deps/ox/src/ox/fs/filestore/linkedlist.hpp | 108 +++++++++--------- deps/ox/src/ox/fs/test/tests.cpp | 4 +- 3 files changed, 59 insertions(+), 59 deletions(-) diff --git a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp index aaacff51..40f8b6b8 100644 --- a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp +++ b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp @@ -18,11 +18,11 @@ class FileStoreTemplate: public FileStore { private: struct __attribute__((packed)) FileStoreData { - ox::LittleEndian rootNode = sizeof(LinkedList); + ox::LittleEndian rootNode = sizeof(LinkedList); }; size_t m_buffSize = 0; - ox::fs::LinkedList *m_linkedList = nullptr; + ox::fs::LinkedList *m_linkedList = nullptr; public: FileStoreTemplate(void *buff, size_t buffSize); @@ -57,7 +57,7 @@ class FileStoreTemplate: public FileStore { template FileStoreTemplate::FileStoreTemplate(void *buff, size_t buffSize) { m_buffSize = buffSize; - m_linkedList = static_cast*>(buff); + m_linkedList = static_cast*>(buff); if (!m_linkedList->valid(buffSize)) { m_buffSize = 0; m_linkedList = nullptr; diff --git a/deps/ox/src/ox/fs/filestore/linkedlist.hpp b/deps/ox/src/ox/fs/filestore/linkedlist.hpp index 61c469b7..84bf8cff 100644 --- a/deps/ox/src/ox/fs/filestore/linkedlist.hpp +++ b/deps/ox/src/ox/fs/filestore/linkedlist.hpp @@ -12,33 +12,29 @@ namespace ox::fs { -template -class __attribute__((packed)) LinkedList { +struct __attribute__((packed)) Item { + public: + ox::LittleEndian m_size = sizeof(Item); public: - struct __attribute__((packed)) Item { - friend LinkedList; + ox::LittleEndian prev = 0; + ox::LittleEndian next = 0; - public: - ox::LittleEndian m_size = sizeof(Item); + explicit Item(size_t size) { + this->m_size = size; + } - protected: - ox::LittleEndian prev = 0; - ox::LittleEndian next = 0; + size_t size() const { + return m_size; + } - public: - explicit Item(size_t size) { - this->m_size = size; - } + ox::fs::Ptr data() { + return Ptr(this, m_size, sizeof(*this), m_size - sizeof(*this)); + } +}; - 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 { private: struct __attribute__((packed)) Header { @@ -70,7 +66,7 @@ class __attribute__((packed)) LinkedList { Header m_header; - public: + public: LinkedList() = default; explicit LinkedList(size_t size); @@ -109,18 +105,18 @@ class __attribute__((packed)) LinkedList { }; -template -LinkedList::LinkedList(size_t size) { +template +LinkedList::LinkedList(size_t size) { m_header.size = size; } -template -typename LinkedList::ItemPtr LinkedList::firstItem() { +template +typename LinkedList::ItemPtr LinkedList::firstItem() { return ptr(m_header.firstItem); } -template -typename LinkedList::ItemPtr LinkedList::lastItem() { +template +typename LinkedList::ItemPtr LinkedList::lastItem() { auto first = ptr(m_header.firstItem); if (first.valid()) { return prev(first); @@ -128,28 +124,28 @@ typename LinkedList::ItemPtr LinkedList::lastItem() { return ItemPtr(); } -template -typename LinkedList::ItemPtr LinkedList::prev(Item *item) { +template +typename LinkedList::ItemPtr LinkedList::prev(Item *item) { return ptr(item->prev); } -template -typename LinkedList::ItemPtr LinkedList::next(Item *item) { +template +typename LinkedList::ItemPtr LinkedList::next(Item *item) { return ptr(item->next); } -template -typename LinkedList::ItemPtr LinkedList::ptr(size_t offset) { +template +typename LinkedList::ItemPtr LinkedList::ptr(size_t offset) { return ItemPtr(this, m_header.size, offset); } -template -typename LinkedList::ItemPtr LinkedList::ptr(void *item) { +template +typename LinkedList::ItemPtr LinkedList::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) { +template +typename LinkedList::ItemPtr LinkedList::malloc(size_t size) { size += sizeof(Item); if (m_header.size - m_header.bytesUsed >= size) { if (!m_header.firstItem) { @@ -175,17 +171,21 @@ typename LinkedList::ItemPtr LinkedList::malloc(size_t size) { return ItemPtr(); } -template -void LinkedList::free(ItemPtr item) { +template +void LinkedList::free(ItemPtr item) { auto prev = this->prev(item); auto next = this->next(item); - prev->next = next; - next->prev = prev; + if (prev.valid()) { + prev->next = next; + } + if (next.valid()) { + next->prev = prev; + } m_header.bytesUsed -= item.size(); } -template -Error LinkedList::setSize(size_t size) { +template +Error LinkedList::setSize(size_t size) { auto last = lastItem(); if ((last.valid() and last.end() >= size) or size < sizeof(m_header)) { return 1; @@ -195,18 +195,18 @@ Error LinkedList::setSize(size_t size) { } } -template -bool LinkedList::valid(size_t maxSize) { +template +bool LinkedList::valid(size_t maxSize) { return m_header.size <= maxSize; } -template -size_t LinkedList::available() { +template +size_t LinkedList::available() { return m_header.size - m_header.bytesUsed; } -template -void LinkedList::compact(void (*cb)(ItemPtr)) { +template +void LinkedList::compact(void (*cb)(ItemPtr)) { auto src = firstItem(); auto dest = data(); while (src.valid()) { @@ -217,11 +217,11 @@ void LinkedList::compact(void (*cb)(ItemPtr)) { } // update surrounding nodes auto prev = ptr(dest->next); - if (prev) { + if (prev.valid()) { prev->next = dest; } auto next = ptr(dest->next); - if (next) { + if (next.valid()) { next->prev = dest; } // update iterators @@ -230,8 +230,8 @@ void LinkedList::compact(void (*cb)(ItemPtr)) { } } -template -uint8_t *LinkedList::data() { +template +uint8_t *LinkedList::data() { return reinterpret_cast(this + 1); } diff --git a/deps/ox/src/ox/fs/test/tests.cpp b/deps/ox/src/ox/fs/test/tests.cpp index 7991447f..05093455 100644 --- a/deps/ox/src/ox/fs/test/tests.cpp +++ b/deps/ox/src/ox/fs/test/tests.cpp @@ -338,7 +338,7 @@ map tests = { 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::LinkedList(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::LinkedList(buffLen); ox::fs::FileStore32 fileStore(list, buffLen); ox_assert(fileStore.format() == 0, "Filestore::format failed."); return 0;