Initial commit.
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
cmake_minimum_required(VERSION 2.8.8)
|
||||
|
||||
add_library(
|
||||
Memphis
|
||||
memfs.cpp
|
||||
_memops.cpp
|
||||
)
|
||||
|
||||
install(
|
||||
FILES
|
||||
memfs.hpp
|
||||
_memops.hpp
|
||||
DESTINATION
|
||||
include/Memphis
|
||||
)
|
||||
@@ -0,0 +1,20 @@
|
||||
#include "_memops.hpp"
|
||||
|
||||
namespace memphis {
|
||||
|
||||
void memcpy(void *src, void *dest, int size) {
|
||||
char *srcBuf = (char*) src;
|
||||
char *dstBuf = (char*) dest;
|
||||
for (int i = 0; i < size; i++) {
|
||||
dstBuf[i] = (char) srcBuf[i];
|
||||
}
|
||||
}
|
||||
|
||||
void memset(void *ptr, char val, int size) {
|
||||
char *buf = (char*) ptr;
|
||||
for (int i = 0; i < size; i++) {
|
||||
buf[i] = val;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef MEMPHIS_MEMOPS_HPP
|
||||
#define MEMPHIS_MEMOPS_HPP
|
||||
|
||||
namespace memphis {
|
||||
|
||||
void memcpy(void *src, void *dest, int size);
|
||||
|
||||
void memset(void *ptr, char val, int size);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
+169
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* Copyright 2013-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 <string.h>
|
||||
#include "_memops.hpp"
|
||||
#include "memfs.hpp"
|
||||
|
||||
#define offsetof(st, m) ((size_t)(&((st *)0)->m))
|
||||
|
||||
namespace memphis {
|
||||
|
||||
uint32_t MemFs::version = 0;
|
||||
|
||||
MemFsPtr MemFs::Record::pathLen() {
|
||||
return offsetof(MemFs::Record, m_path) + m_data;
|
||||
}
|
||||
|
||||
MemFsPtr MemFs::Record::size() {
|
||||
return offsetof(MemFs::Record, m_path) + pathLen() + dataLen;
|
||||
}
|
||||
|
||||
void MemFs::Record::setPath(std::string path) {
|
||||
char *ptr = (char*) (this + m_path);
|
||||
for (unsigned i = 0; i < path.size(); i++) {
|
||||
*(ptr + i) = path[i];
|
||||
}
|
||||
}
|
||||
|
||||
void MemFs::Record::setData(uint8_t *data, int size) {
|
||||
memcpy(this + m_data, data, size);
|
||||
m_data = size;
|
||||
}
|
||||
|
||||
MemFs::MemFs(uint8_t *begin, uint8_t *end): m_version(*((uint32_t*) begin)), m_lastRec(*(MemFsPtr*) (begin + sizeof(m_version))) {
|
||||
if (version != m_version) {
|
||||
throw "MemFs version mismatch";
|
||||
}
|
||||
m_begin = begin;
|
||||
m_end = end;
|
||||
m_root = (Record*) (begin + sizeof(MemFsPtr));
|
||||
}
|
||||
|
||||
void MemFs::init() {
|
||||
memset(m_begin, 0, m_end - m_begin);
|
||||
m_version = version;
|
||||
}
|
||||
|
||||
void MemFs::write(std::string path, uint8_t *data, MemFsPtr dataLen) {
|
||||
const MemFsPtr size = offsetof(MemFs::Record, m_path) + path.size() + dataLen;
|
||||
auto rec = (Record*) alloc(size);
|
||||
rec->dataLen = dataLen;
|
||||
insert(m_root, rec);
|
||||
}
|
||||
|
||||
int MemFs::read(std::string path, uint8_t **data, MemFsPtr *size) {
|
||||
auto rec = getRecord(m_root, path.c_str(), path.size());
|
||||
int retval = 1;
|
||||
if (rec) {
|
||||
*size = rec->dataLen;
|
||||
*data = (uint8_t*) malloc(*size);
|
||||
memcpy(*data, ptr<uint8_t*>(rec->m_data), *size);
|
||||
retval = 0;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
MemFs::Record *MemFs::getRecord(MemFs::Record *root, const char *path, MemFsPtr pathLen) {
|
||||
MemFsPtr len;
|
||||
if (root->pathLen() < pathLen) {
|
||||
len = root->pathLen();
|
||||
} else {
|
||||
len = pathLen;
|
||||
}
|
||||
auto cmp = strncmp(ptr<char*>(root->m_path), path, len);
|
||||
MemFsPtr recPt;
|
||||
if (cmp < 0) {
|
||||
recPt = root->left;
|
||||
} else if (cmp > 0) {
|
||||
recPt = root->right;
|
||||
} else {
|
||||
recPt = ptr(root);
|
||||
}
|
||||
if (recPt) {
|
||||
return getRecord(ptr<Record*>(recPt), path, pathLen);
|
||||
} else {
|
||||
return ptr<Record*>(recPt);
|
||||
}
|
||||
}
|
||||
|
||||
void *MemFs::alloc(MemFsPtr size) {
|
||||
const auto iterator = this->iterator();
|
||||
if ((iterator + size) > (uint64_t) m_end) {
|
||||
compress();
|
||||
if ((iterator + size) > (uint64_t) m_end) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
ptr<Record*>(m_lastRec)->next = iterator;
|
||||
|
||||
auto rec = ptr<uint8_t*>(iterator);
|
||||
memset(rec, 0, size);
|
||||
ptr<Record*>(iterator)->prev = m_lastRec;
|
||||
m_lastRec = iterator;
|
||||
return rec;
|
||||
}
|
||||
|
||||
void MemFs::compress() {
|
||||
auto current = m_root;
|
||||
while (current->next) {
|
||||
auto prevEnd = current + current->size();
|
||||
current = ptr<Record*>(current->next);
|
||||
if (prevEnd != current) {
|
||||
memcpy(prevEnd, current, current->size());
|
||||
current = prevEnd;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool MemFs::insert(Record *root, Record *insertValue, MemFsPtr *rootParentPtr) {
|
||||
MemFsPtr len;
|
||||
if (root->pathLen() < insertValue->pathLen()) {
|
||||
len = root->pathLen();
|
||||
} else {
|
||||
len = insertValue->pathLen();
|
||||
}
|
||||
auto cmp = strncmp(ptr<char*>(root->m_path), ptr<char*>(insertValue->m_path), len);
|
||||
if (cmp < 0) {
|
||||
if (root->left) {
|
||||
return insert(ptr<Record*>(root->left), insertValue, &root->left);
|
||||
} else {
|
||||
root->left = ((uint8_t*) insertValue) - m_begin;
|
||||
return true;
|
||||
}
|
||||
} else if (cmp > 0) {
|
||||
if (root->right) {
|
||||
return insert(ptr<Record*>(root->right), insertValue, &root->right);
|
||||
} else {
|
||||
root->right = ((uint8_t*) insertValue) - m_begin;
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
auto ivAddr = ((uint8_t*) insertValue) - m_begin;
|
||||
if (root->prev) {
|
||||
ptr<Record*>(root->prev)->next = ivAddr;
|
||||
}
|
||||
if (root->next) {
|
||||
ptr<Record*>(root->next)->prev = ivAddr;
|
||||
}
|
||||
if (rootParentPtr) {
|
||||
*rootParentPtr = ivAddr;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
MemFsPtr MemFs::iterator() {
|
||||
return m_lastRec + ((Record*) m_begin + m_lastRec)->size();
|
||||
}
|
||||
|
||||
MemFsPtr MemFs::ptr(void *ptr) {
|
||||
return ((uint8_t*) ptr) - m_begin;
|
||||
}
|
||||
|
||||
}
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright 2013-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/.
|
||||
*/
|
||||
#ifndef MEMPHIS_MEMFS_HPP
|
||||
#define MEMPHIS_MEMFS_HPP
|
||||
|
||||
#include <string>
|
||||
#include "types.hpp"
|
||||
|
||||
namespace memphis {
|
||||
|
||||
typedef uint32_t MemFsPtr;
|
||||
|
||||
class MemFs {
|
||||
public:
|
||||
static uint32_t version;
|
||||
private:
|
||||
struct Record {
|
||||
// the next Record in memory
|
||||
MemFsPtr prev, next;
|
||||
MemFsPtr left, right;
|
||||
MemFsPtr dataLen;
|
||||
// offsets from Record this
|
||||
MemFsPtr m_path;
|
||||
MemFsPtr m_data;
|
||||
|
||||
MemFsPtr pathLen();
|
||||
MemFsPtr size();
|
||||
void setPath(std::string);
|
||||
void setData(uint8_t *data, int size);
|
||||
};
|
||||
|
||||
uint8_t *m_begin, *m_end;
|
||||
uint32_t &m_version;
|
||||
// the last Record in the MemFs'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
|
||||
*/
|
||||
MemFs(uint8_t *begin, uint8_t *end);
|
||||
|
||||
/**
|
||||
* Initializes the memory chunk of this MemFs 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
|
||||
* @param data the contents of the file
|
||||
* @param dataLen the number of bytes data points to
|
||||
*/
|
||||
void write(std::string path, uint8_t *data, MemFsPtr dataLen);
|
||||
|
||||
/**
|
||||
* Reads the "file" at the given path. You are responsible for freeing
|
||||
* the data when done with it.
|
||||
* @param path path 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(std::string path, uint8_t **data, MemFsPtr *size);
|
||||
|
||||
private:
|
||||
/**
|
||||
* Gets the record at the given path.
|
||||
* @param root the root node to start comparing on
|
||||
* @param path path of the "file"
|
||||
* @param pathLen number of characters in pathLen
|
||||
* @return the requested Record, if available
|
||||
*/
|
||||
Record *getRecord(Record *root, const char *path, MemFsPtr pathLen);
|
||||
|
||||
/**
|
||||
* Gets an address for a new Record.
|
||||
* @param size the size of the Record
|
||||
*/
|
||||
void *alloc(MemFsPtr size);
|
||||
|
||||
/**
|
||||
* Compresses all of the records into a contiguous space, starting at m_root.
|
||||
*/
|
||||
void compress();
|
||||
|
||||
/**
|
||||
* Inserts the given insertValue into the tree of the given root.
|
||||
* If the record already exists, it replaces the old on deletes it.
|
||||
* @return true if the record was inserted
|
||||
*/
|
||||
bool insert(Record *root, Record *insertValue, MemFsPtr *rootParentPtr = 0);
|
||||
|
||||
/**
|
||||
* Gets the MemFsPtr associated with the next Record to be allocated.
|
||||
* @retrun the MemFsPtr associated with the next Record to be allocated
|
||||
*/
|
||||
MemFsPtr iterator();
|
||||
|
||||
/**
|
||||
* Converts an actual pointer to a MemFsPtr.
|
||||
*/
|
||||
MemFsPtr ptr(void *ptr);
|
||||
|
||||
/**
|
||||
* Converts a MemFsPtr to an actual pointer.
|
||||
*/
|
||||
template<typename T>
|
||||
T ptr(MemFsPtr ptr);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
T MemFs::ptr(MemFsPtr ptr) {
|
||||
return (T) m_begin + ptr;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2013-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/.
|
||||
*/
|
||||
#ifndef MEMPHIS_TYPES_HPP
|
||||
#define MEMPHIS_TYPES_HPP
|
||||
|
||||
namespace memphis {
|
||||
|
||||
typedef char int8_t;
|
||||
typedef unsigned char uint8_t;
|
||||
typedef short int16_t;
|
||||
typedef unsigned short uint16_t;
|
||||
typedef int int32_t;
|
||||
typedef unsigned uint_t;
|
||||
typedef long long int64_t;
|
||||
typedef unsigned long long uint64_t;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user