Add NodeBuffer init to FileStore format

This commit is contained in:
Gary Talent 2018-03-08 00:24:02 -06:00
parent 2e29f48810
commit b77a41abd2
5 changed files with 69 additions and 52 deletions

View File

@ -8,7 +8,7 @@
#pragma once
#include "linkedlist.hpp"
#include "nodebuffer.hpp"
namespace ox::fs {

View File

@ -9,20 +9,36 @@
#pragma once
#include "filestore.hpp"
#include "linkedlist.hpp"
#include "nodebuffer.hpp"
namespace ox::fs {
template<typename size_t>
struct __attribute__((packed)) FileStoreItem: public Item<size_t> {
private:
ox::LittleEndian<size_t> id = 0;
ox::LittleEndian<size_t> left = 0;
ox::LittleEndian<size_t> right = 0;
public:
explicit FileStoreItem(size_t size): Item<size_t>(size) {
}
ox::fs::Ptr<uint8_t, size_t> data() {
return Ptr<uint8_t, size_t>(this, this->size(), sizeof(*this), this->size() - sizeof(*this));
}
};
template<typename size_t>
class FileStoreTemplate: public FileStore {
private:
struct __attribute__((packed)) FileStoreData {
ox::LittleEndian<size_t> rootNode = sizeof(LinkedList<size_t, Item>);
ox::LittleEndian<size_t> rootNode = sizeof(NodeBuffer<size_t, FileStoreItem<uint32_t>>);
};
size_t m_buffSize = 0;
ox::fs::LinkedList<size_t, Item> *m_linkedList = nullptr;
ox::fs::NodeBuffer<size_t, FileStoreItem<uint32_t>> *m_buffer = nullptr;
public:
FileStoreTemplate(void *buff, size_t buffSize);
@ -57,17 +73,18 @@ class FileStoreTemplate: public FileStore {
template<typename size_t>
FileStoreTemplate<size_t>::FileStoreTemplate(void *buff, size_t buffSize) {
m_buffSize = buffSize;
m_linkedList = static_cast<ox::fs::LinkedList<size_t, Item>*>(buff);
if (!m_linkedList->valid(buffSize)) {
m_buffer = static_cast<ox::fs::NodeBuffer<size_t, FileStoreItem<uint32_t>>*>(buff);
if (!m_buffer->valid(buffSize)) {
m_buffSize = 0;
m_linkedList = nullptr;
m_buffer = nullptr;
}
}
template<typename size_t>
Error FileStoreTemplate<size_t>::format() {
Error err = 0;
auto data = m_linkedList->malloc(sizeof(FileStoreData));
new (m_buffer) NodeBuffer<size_t, FileStoreItem<uint32_t>>(m_buffSize);
auto data = m_buffer->malloc(sizeof(FileStoreData));
if (data.valid()) {
new (data->data()) FileStoreData;
} else {
@ -80,7 +97,7 @@ template<typename size_t>
Error FileStoreTemplate<size_t>::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<size_t>::available() {
template<typename size_t>
typename FileStoreTemplate<size_t>::FileStoreData &FileStoreTemplate<size_t>::fileStoreData() {
return *reinterpret_cast<FileStoreData*>(m_linkedList->firstItem());
return *reinterpret_cast<FileStoreData*>(m_buffer->firstItem());
}
using FileStore16 = FileStoreTemplate<uint16_t>;

View File

@ -12,29 +12,8 @@
namespace ox::fs {
struct __attribute__((packed)) Item {
public:
ox::LittleEndian<size_t> m_size = sizeof(Item);
public:
ox::LittleEndian<size_t> prev = 0;
ox::LittleEndian<size_t> next = 0;
explicit Item(size_t size) {
this->m_size = size;
}
size_t size() const {
return m_size;
}
ox::fs::Ptr<uint8_t, size_t> data() {
return Ptr<uint8_t, size_t>(this, m_size, sizeof(*this), m_size - sizeof(*this));
}
};
template<typename size_t, typename Item>
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<typename size_t, typename Item>
LinkedList<size_t, Item>::LinkedList(size_t size) {
NodeBuffer<size_t, Item>::NodeBuffer(size_t size) {
m_header.size = size;
}
template<typename size_t, typename Item>
typename LinkedList<size_t, Item>::ItemPtr LinkedList<size_t, Item>::firstItem() {
typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::firstItem() {
return ptr(m_header.firstItem);
}
template<typename size_t, typename Item>
typename LinkedList<size_t, Item>::ItemPtr LinkedList<size_t, Item>::lastItem() {
typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::lastItem() {
auto first = ptr(m_header.firstItem);
if (first.valid()) {
return prev(first);
@ -125,27 +104,27 @@ typename LinkedList<size_t, Item>::ItemPtr LinkedList<size_t, Item>::lastItem()
}
template<typename size_t, typename Item>
typename LinkedList<size_t, Item>::ItemPtr LinkedList<size_t, Item>::prev(Item *item) {
typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::prev(Item *item) {
return ptr(item->prev);
}
template<typename size_t, typename Item>
typename LinkedList<size_t, Item>::ItemPtr LinkedList<size_t, Item>::next(Item *item) {
typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::next(Item *item) {
return ptr(item->next);
}
template<typename size_t, typename Item>
typename LinkedList<size_t, Item>::ItemPtr LinkedList<size_t, Item>::ptr(size_t offset) {
typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::ptr(size_t offset) {
return ItemPtr(this, m_header.size, offset);
}
template<typename size_t, typename Item>
typename LinkedList<size_t, Item>::ItemPtr LinkedList<size_t, Item>::ptr(void *item) {
typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::ptr(void *item) {
return ItemPtr(this, m_header.size, reinterpret_cast<size_t>(static_cast<uint8_t*>(item) - static_cast<uint8_t*>(this)));
}
template<typename size_t, typename Item>
typename LinkedList<size_t, Item>::ItemPtr LinkedList<size_t, Item>::malloc(size_t size) {
typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::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<size_t, Item>::ItemPtr LinkedList<size_t, Item>::malloc(size
}
template<typename size_t, typename Item>
void LinkedList<size_t, Item>::free(ItemPtr item) {
void NodeBuffer<size_t, Item>::free(ItemPtr item) {
auto prev = this->prev(item);
auto next = this->next(item);
if (prev.valid()) {
@ -185,7 +164,7 @@ void LinkedList<size_t, Item>::free(ItemPtr item) {
}
template<typename size_t, typename Item>
Error LinkedList<size_t, Item>::setSize(size_t size) {
Error NodeBuffer<size_t, Item>::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<size_t, Item>::setSize(size_t size) {
}
template<typename size_t, typename Item>
bool LinkedList<size_t, Item>::valid(size_t maxSize) {
bool NodeBuffer<size_t, Item>::valid(size_t maxSize) {
return m_header.size <= maxSize;
}
template<typename size_t, typename Item>
size_t LinkedList<size_t, Item>::available() {
size_t NodeBuffer<size_t, Item>::available() {
return m_header.size - m_header.bytesUsed;
}
template<typename size_t, typename Item>
void LinkedList<size_t, Item>::compact(void (*cb)(ItemPtr)) {
void NodeBuffer<size_t, Item>::compact(void (*cb)(ItemPtr)) {
auto src = firstItem();
auto dest = data();
while (src.valid()) {
@ -231,8 +210,28 @@ void LinkedList<size_t, Item>::compact(void (*cb)(ItemPtr)) {
}
template<typename size_t, typename Item>
uint8_t *LinkedList<size_t, Item>::data() {
uint8_t *NodeBuffer<size_t, Item>::data() {
return reinterpret_cast<uint8_t*>(this + 1);
}
template<typename size_t>
struct __attribute__((packed)) Item {
public:
ox::LittleEndian<size_t> prev = 0;
ox::LittleEndian<size_t> next = 0;
private:
ox::LittleEndian<size_t> m_size = sizeof(Item);
public:
explicit Item(size_t size) {
this->m_size = size;
}
size_t size() const {
return m_size;
}
};
}

View File

@ -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")

View File

@ -333,12 +333,12 @@ map<string, int(*)(string)> tests = {
}
},
{
"LinkedList::insert",
"NodeBuffer::insert",
[](string) {
int err = 0;
constexpr auto buffLen = 5000;
uint8_t buff[buffLen];
auto list = new (buff) ox::fs::LinkedList<uint32_t, ox::fs::Item>(buffLen);
auto list = new (buff) ox::fs::NodeBuffer<uint32_t, ox::fs::FileStoreItem<uint32_t>>(buffLen);
err |= !(list->malloc(50).valid());
err |= !(list->firstItem().valid());
err |= !(list->firstItem()->size() == 50);
@ -350,7 +350,7 @@ map<string, int(*)(string)> tests = {
[](string) {
constexpr auto buffLen = 5000;
uint8_t buff[buffLen];
auto list = new (buff) ox::fs::LinkedList<uint32_t, ox::fs::Item>(buffLen);
auto list = new (buff) ox::fs::NodeBuffer<uint32_t, ox::fs::FileStoreItem<uint32_t>>(buffLen);
ox::fs::FileStore32 fileStore(list, buffLen);
ox_assert(fileStore.format() == 0, "Filestore::format failed.");
return 0;