Fix new FileStore to correctly initialize the first item

This commit is contained in:
Gary Talent 2018-03-14 00:39:08 -05:00
parent ccf308d022
commit 080ca9df86
4 changed files with 36 additions and 25 deletions

View File

@ -24,6 +24,13 @@ struct __attribute__((packed)) FileStoreItem: public Item<size_t> {
explicit FileStoreItem(size_t size): Item<size_t>(size) {
}
/**
* @return the size of the data + the size of the Item type
*/
size_t fullSize() const {
return sizeof(*this) + this->size();
}
ox::fs::Ptr<uint8_t, size_t> data() {
return Ptr<uint8_t, size_t>(this, this->size(), sizeof(*this), this->size() - sizeof(*this));
}
@ -169,22 +176,24 @@ Error FileStoreTemplate<size_t>::write(InodeId_t id, void *data, FsSize_t dataSi
new (dest) FileStoreItem<size_t>(dataSize);
dest->id = id;
dest->fileType = fileType;
ox_memcpy(dest->data(), data, dest->size());
auto root = rootInode();
if (root.valid()) {
return placeItem(root, dest);
} else {
auto destData = dest->data();
if (destData.valid()) {
ox_memcpy(destData, data, dest->size());
auto fsData = fileStoreData();
if (fsData) {
fsData->rootNode = dest;
return 0;
auto root = m_buffer->ptr(fsData->rootNode);
if (root.valid()) {
return placeItem(root, dest);
} else {
fsData->rootNode = dest;
return 0;
}
} else {
m_buffer->free(dest);
oxTrace("ox::fs::FileStoreTemplate::write::fail") << "Could not place item due to absence of FileStore header.";
return 1;
}
}
}
m_buffer->free(dest);
}
return 1;
}

View File

@ -132,12 +132,21 @@ 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) {
size += sizeof(Item);
if (m_header.size - m_header.bytesUsed >= size) {
if (!m_header.firstItem) {
m_header.firstItem = sizeof(m_header);
auto fullSize = size + sizeof(Item);
if (m_header.size - m_header.bytesUsed >= fullSize) {
auto last = lastItem();
size_t addr = 0;
if (last.valid()) {
addr = last.offset() + last.size();
} else {
// there is no first item, so this may be the first item
if (!m_header.firstItem) {
oxTrace("ox::fs::NodeBuffer::malloc") << "No first item.";
m_header.firstItem = sizeof(m_header);
addr = m_header.firstItem;
}
}
auto out = ItemPtr(this, m_header.size, m_header.firstItem, size);
auto out = ItemPtr(this, m_header.size, addr, fullSize);
if (out.valid()) {
new (out) Item(size);
@ -261,13 +270,6 @@ struct __attribute__((packed)) Item {
size_t size() const {
return m_size;
}
/**
* @return the size of the data + the size of the Item type
*/
size_t fullSize() const {
return sizeof(*this) + m_size;
}
};
}

View File

@ -340,9 +340,9 @@ map<string, int(*)(string)> tests = {
uint8_t buff[buffLen];
auto list = new (buff) ox::fs::NodeBuffer<uint32_t, ox::fs::FileStoreItem<uint32_t>>(buffLen);
err |= !(list->malloc(50).valid());
//auto first = list->firstItem();
//err |= !(first.valid());
//err |= !(first->size() == 50);
auto first = list->firstItem();
ox_assert(first.valid(), "NodeBuffer::insert: Could not access first item");
ox_assert(first->size() == 50, "NodeBuffer::insert: First item size invalid");
return err;
}
},

View File

@ -93,4 +93,4 @@ class NullStream {
}
#define oxTrace(ch) ox::trace::StdOutStream(__FILE__, __LINE__, ch)
#define oxTrace(ch) ox::trace::NullStream(__FILE__, __LINE__, ch)