From 9a393df4fcc946d5b4d5e1406b8bdc85a174830c Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 2 Jun 2016 20:58:50 -0500 Subject: [PATCH] Renamed project to wfs and memfs to FileStore. --- src/CMakeLists.txt | 14 ++++++- src/{memphis => }/_memops.cpp | 4 +- src/{memphis => }/_memops.hpp | 8 ++-- src/{memphis => }/_types.hpp | 11 ++++-- src/{memphis/memfs.cpp => filestore.cpp} | 44 ++++++++++----------- src/{memphis/memfs.hpp => filestore.hpp} | 49 +++++++++++++++--------- src/memphis/CMakeLists.txt | 15 -------- test/test.cpp | 15 ++++++-- 8 files changed, 91 insertions(+), 69 deletions(-) rename src/{memphis => }/_memops.cpp (94%) rename src/{memphis => }/_memops.hpp (79%) rename src/{memphis => }/_types.hpp (83%) rename src/{memphis/memfs.cpp => filestore.cpp} (73%) rename src/{memphis/memfs.hpp => filestore.hpp} (71%) delete mode 100644 src/memphis/CMakeLists.txt diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c8e5ca22f..3e108b850 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,15 @@ cmake_minimum_required(VERSION 2.8.8) -add_subdirectory(memphis) +add_library( + Memphis + filestore.cpp + _memops.cpp +) + +install( + FILES + filestore.hpp + _memops.hpp + DESTINATION + include/Memphis +) diff --git a/src/memphis/_memops.cpp b/src/_memops.cpp similarity index 94% rename from src/memphis/_memops.cpp rename to src/_memops.cpp index 26f69a229..db87884c8 100644 --- a/src/memphis/_memops.cpp +++ b/src/_memops.cpp @@ -7,7 +7,8 @@ */ #include "_memops.hpp" -namespace memphis { +namespace wombat { +namespace fs { void memcpy(void *src, void *dest, int size) { char *srcBuf = (char*) src; @@ -25,3 +26,4 @@ void memset(void *ptr, char val, int size) { } } +} diff --git a/src/memphis/_memops.hpp b/src/_memops.hpp similarity index 79% rename from src/memphis/_memops.hpp rename to src/_memops.hpp index d11121a8d..d8960217e 100644 --- a/src/memphis/_memops.hpp +++ b/src/_memops.hpp @@ -5,15 +5,17 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef MEMPHIS_MEMOPS_HPP -#define MEMPHIS_MEMOPS_HPP +#ifndef WOMBAT_FS_MEMOPS_HPP +#define WOMBAT_FS_MEMOPS_HPP -namespace memphis { +namespace wombat { +namespace fs { void memcpy(void *src, void *dest, int size); void memset(void *ptr, char val, int size); +} } #endif diff --git a/src/memphis/_types.hpp b/src/_types.hpp similarity index 83% rename from src/memphis/_types.hpp rename to src/_types.hpp index 47ea2c0ea..b4be82589 100644 --- a/src/memphis/_types.hpp +++ b/src/_types.hpp @@ -5,10 +5,13 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef MEMPHIS_TYPES_HPP -#define MEMPHIS_TYPES_HPP +#ifndef WOMBAT_FS_TYPES_HPP +#define WOMBAT_FS_TYPES_HPP -namespace memphis { +#define offsetof(st, m) ((size_t)(&((st *)0)->m)) + +namespace wombat { +namespace fs { typedef char int8_t; typedef unsigned char uint8_t; @@ -28,7 +31,7 @@ typedef uint64_t size_t; typedef uint32_t size_t; #endif - +} } #endif diff --git a/src/memphis/memfs.cpp b/src/filestore.cpp similarity index 73% rename from src/memphis/memfs.cpp rename to src/filestore.cpp index 61bbbc018..cf54f554c 100644 --- a/src/memphis/memfs.cpp +++ b/src/filestore.cpp @@ -7,37 +7,36 @@ */ #include #include "_memops.hpp" -#include "memfs.hpp" +#include "filestore.hpp" -#define offsetof(st, m) ((size_t)(&((st *)0)->m)) +namespace wombat { +namespace fs { -namespace memphis { - -uint32_t MemFs::version = 0; +uint32_t FileStore::version = 0; uint8_t *initFs(uint8_t *buffer, size_t size, bool hasDirectories) { auto fs = (MemFsHeader*) (buffer ? buffer : malloc(size)); - fs->version = MemFs::version; + fs->version = FileStore::version; return (uint8_t*) fs; } -MemFsPtr MemFs::Record::size() { - return offsetof(MemFs::Record, m_id) + dataLen; +MemFsPtr FileStore::Record::size() { + return offsetof(FileStore::Record, m_id) + dataLen; } -void MemFs::Record::setId(RecordId id) { +void FileStore::Record::setId(RecordId id) { this->m_id = id; } -void MemFs::Record::setData(uint8_t *data, int size) { +void FileStore::Record::setData(uint8_t *data, int size) { memcpy(this + m_data, data, size); m_data = size; } -// MemFs +// FileStore -MemFs::MemFs(uint8_t *begin, uint8_t *end, Error *error): m_version(*((uint32_t*) begin)), m_lastRec(*(MemFsPtr*) (begin + sizeof(m_version))) { +FileStore::FileStore(uint8_t *begin, uint8_t *end, Error *error): m_version(*((uint32_t*) begin)), m_lastRec(*(MemFsPtr*) (begin + sizeof(m_version))) { if (version != m_version) { // version mismatch if (error) { @@ -54,19 +53,19 @@ MemFs::MemFs(uint8_t *begin, uint8_t *end, Error *error): m_version(*((uint32_t* } } -void MemFs::init() { +void FileStore::init() { memset(m_begin, 0, m_end - m_begin); m_version = version; } -void MemFs::write(RecordId id, uint8_t *data, MemFsPtr dataLen) { - const MemFsPtr size = offsetof(MemFs::Record, m_id) + dataLen; +void FileStore::write(RecordId id, uint8_t *data, MemFsPtr dataLen) { + const MemFsPtr size = offsetof(FileStore::Record, m_id) + dataLen; auto rec = (Record*) alloc(size); rec->dataLen = dataLen; insert(m_root, rec); } -int MemFs::read(RecordId id, uint8_t **data, MemFsPtr *size) { +int FileStore::read(RecordId id, uint8_t **data, MemFsPtr *size) { auto rec = getRecord(m_root, id); int retval = 1; if (rec) { @@ -78,7 +77,7 @@ int MemFs::read(RecordId id, uint8_t **data, MemFsPtr *size) { return retval; } -MemFs::Record *MemFs::getRecord(MemFs::Record *root, RecordId id) { +FileStore::Record *FileStore::getRecord(FileStore::Record *root, RecordId id) { auto cmp = root->m_id > id; MemFsPtr recPt; if (cmp) { @@ -95,7 +94,7 @@ MemFs::Record *MemFs::getRecord(MemFs::Record *root, RecordId id) { } } -void *MemFs::alloc(MemFsPtr size) { +void *FileStore::alloc(MemFsPtr size) { const auto iterator = this->iterator(); if ((iterator + size) > (uint64_t) m_end) { compress(); @@ -112,7 +111,7 @@ void *MemFs::alloc(MemFsPtr size) { return rec; } -void MemFs::compress() { +void FileStore::compress() { auto current = m_root; while (current->next) { auto prevEnd = current + current->size(); @@ -124,7 +123,7 @@ void MemFs::compress() { } } -bool MemFs::insert(Record *root, Record *insertValue, MemFsPtr *rootParentPtr) { +bool FileStore::insert(Record *root, Record *insertValue, MemFsPtr *rootParentPtr) { auto cmp = root->m_id > insertValue->m_id; if (cmp) { if (root->left) { @@ -156,12 +155,13 @@ bool MemFs::insert(Record *root, Record *insertValue, MemFsPtr *rootParentPtr) { return false; } -MemFsPtr MemFs::iterator() { +MemFsPtr FileStore::iterator() { return m_lastRec + ((Record*) m_begin + m_lastRec)->size(); } -MemFsPtr MemFs::ptr(void *ptr) { +MemFsPtr FileStore::ptr(void *ptr) { return ((uint8_t*) ptr) - m_begin; } } +} diff --git a/src/memphis/memfs.hpp b/src/filestore.hpp similarity index 71% rename from src/memphis/memfs.hpp rename to src/filestore.hpp index 0c87b75d0..eb9cffd92 100644 --- a/src/memphis/memfs.hpp +++ b/src/filestore.hpp @@ -5,22 +5,32 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef MEMPHIS_MEMFS_HPP -#define MEMPHIS_MEMFS_HPP +#ifndef WOMBAT_FS_FILESTORE_HPP +#define WOMBAT_FS_FILESTORE_HPP #include "_types.hpp" -namespace memphis { +namespace wombat { +namespace fs { typedef uint32_t MemFsPtr; typedef uint32_t RecordId; struct MemFsHeader { uint32_t version; - bool hasDirectories; + MemFsPtr rootDir = -1; }; -class MemFs { +class DirectoryHeader { + /** + * Count of the number of files listed in this directory. + */ + MemFsPtr files; + + const char* operator[](int i); +}; + +class FileStore { public: static uint32_t version; private: @@ -40,48 +50,48 @@ class MemFs { uint8_t *m_begin, *m_end; uint32_t &m_version; - // the last Record in the MemFs's memory chunk + // the last Record in the FileStore's memory chunk MemFsPtr &m_lastRec; Record *m_root; public: /** * Constructor - * @param begin pointer to the beginning of this MemFs's memory chunk - * @param end pointer to the end of this MemFs's memory chunk + * @param begin pointer to the beginning of this FileStore's memory chunk + * @param end pointer to the end of this FileStore's memory chunk * @param error pointer to a integer return errors into */ - MemFs(uint8_t *begin, uint8_t *end, Error *error = nullptr); + FileStore(uint8_t *begin, uint8_t *end, Error *error = nullptr); /** - * Initializes the memory chunk of this MemFs was given. + * Initializes the memory chunk of this FileStore was given. * This clears the previous contents. */ void init(); /** - * Writes the given data to a "file" with the given path. - * @param path the path of the file + * Writes the given data to a "file" with the given id. + * @param id the id of the file * @param data the contents of the file * @param dataLen the number of bytes data points to */ - void write(RecordId path, uint8_t *data, MemFsPtr dataLen); + void write(RecordId id, uint8_t *data, MemFsPtr dataLen); /** - * Reads the "file" at the given path. You are responsible for freeing + * Reads the "file" at the given id. You are responsible for freeing * the data when done with it. - * @param path path of the "file" + * @param id id of the "file" * @param data pointer to the pointer where the data is stored * @param size pointer to a value that will be assigned the size of data * @return 0 if read is a success */ - int read(RecordId path, uint8_t **data, MemFsPtr *size); + int read(RecordId id, uint8_t **data, MemFsPtr *size); private: /** - * Gets the record at the given path. + * Gets the record at the given id. * @param root the root node to start comparing on - * @param path path of the "file" + * @param id id of the "file" * @param pathLen number of characters in pathLen * @return the requested Record, if available */ @@ -124,12 +134,13 @@ class MemFs { }; template -T MemFs::ptr(MemFsPtr ptr) { +T FileStore::ptr(MemFsPtr ptr) { return (T) m_begin + ptr; } uint8_t *initFs(uint8_t *buffer, size_t size, bool hasDirectories); +} } #endif diff --git a/src/memphis/CMakeLists.txt b/src/memphis/CMakeLists.txt deleted file mode 100644 index 28f98abf8..000000000 --- a/src/memphis/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -cmake_minimum_required(VERSION 2.8.8) - -add_library( - Memphis - memfs.cpp - _memops.cpp -) - -install( - FILES - memfs.hpp - _memops.hpp - DESTINATION - include/Memphis -) diff --git a/test/test.cpp b/test/test.cpp index d515e7016..83c0d45ee 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -1,12 +1,19 @@ -#include +/* + * Copyright 2015 gtalent2@gmail.com + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +#include -using namespace memphis; +using namespace wombat::fs; int main() { - const auto size = 1024; + const auto size = 1 << 16; uint8_t volume[size]; uint32_t err; initFs(volume, size, false); - MemFs memfs(volume, volume + size, &err); + FileStore(volume, volume + size, &err); return err; }