Initial commit.
This commit is contained in:
@@ -0,0 +1 @@
|
||||
build
|
||||
@@ -0,0 +1,18 @@
|
||||
cmake_minimum_required(VERSION 2.8.8)
|
||||
|
||||
project(Memphis)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/Modules)
|
||||
include(address_sanitizer)
|
||||
|
||||
add_definitions(
|
||||
-std=c++11
|
||||
-Wall
|
||||
-Wsign-compare
|
||||
-nostdlib
|
||||
#-Werror
|
||||
#--analyze
|
||||
#-Os # GCC size optimization flag
|
||||
)
|
||||
|
||||
add_subdirectory(src)
|
||||
@@ -0,0 +1,49 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
|
||||
# - Try to find Jansson
|
||||
# Once done this will define
|
||||
# JANSSON_FOUND - System has Jansson
|
||||
# JANSSON_INCLUDE_DIRS - The Jansson include directories
|
||||
# JANSSON_LIBRARIES - The libraries needed to use Jansson
|
||||
# JANSSON_DEFINITIONS - Compiler switches required for using Jansson
|
||||
|
||||
find_path(JANSSON_INCLUDE_DIR jansson.h
|
||||
PATHS
|
||||
/usr/include
|
||||
/usr/local/include
|
||||
)
|
||||
|
||||
find_library(JANSSON_LIBRARY
|
||||
NAMES
|
||||
jansson
|
||||
PATHS
|
||||
/usr/lib
|
||||
/usr/local/lib
|
||||
)
|
||||
|
||||
set(JANSSON_LIBRARIES ${JANSSON_LIBRARY})
|
||||
set(JANSSON_INCLUDE_DIRS ${JANSSON_INCLUDE_DIR})
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
# handle the QUIETLY and REQUIRED arguments and set JANSSON_FOUND to TRUE
|
||||
# if all listed variables are TRUE
|
||||
find_package_handle_standard_args(Jansson DEFAULT_MSG
|
||||
JANSSON_LIBRARY JANSSON_INCLUDE_DIR)
|
||||
|
||||
mark_as_advanced(JANSSON_INCLUDE_DIR JANSSON_LIBRARY)
|
||||
@@ -0,0 +1,54 @@
|
||||
set(CMAKE_SYSTEM_NAME "Generic")
|
||||
|
||||
set(DEVKITARM $ENV{DEVKITARM})
|
||||
set(DEVKITPRO $ENV{DEVKITPRO})
|
||||
|
||||
if(NOT DEVKITPRO)
|
||||
message(FATAL_ERROR "DEVKITPRO environment variable not set")
|
||||
endif()
|
||||
|
||||
if(NOT DEVKITARM)
|
||||
message(FATAL_ERROR "DEVKITARM environment variable not set")
|
||||
endif()
|
||||
|
||||
set(CMAKE_C_COMPILER ${DEVKITARM}/bin/arm-none-eabi-gcc)
|
||||
set(CMAKE_CXX_COMPILER ${DEVKITARM}/bin/arm-none-eabi-g++)
|
||||
set(CMAKE_FIND_ROOT_PATH ${DEVKITARM} ${DEVKITPRO}/libgba)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
|
||||
|
||||
set(CMAKE_FIND_LIBRARY_PREFIXES lib)
|
||||
set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
|
||||
|
||||
set(LINKER_FLAGS "-specs=gba.specs")
|
||||
add_definitions (
|
||||
-DARM7
|
||||
)
|
||||
|
||||
find_library(GBA_LIBRARY
|
||||
NAMES
|
||||
gba
|
||||
PATHS
|
||||
/lib
|
||||
)
|
||||
find_library(FILESYSTEM_LIBRARY
|
||||
NAMES
|
||||
filesystem
|
||||
PATHS
|
||||
/lib
|
||||
)
|
||||
find_library(FAT_LIBRARY
|
||||
NAMES
|
||||
fat
|
||||
PATHS
|
||||
/lib
|
||||
)
|
||||
find_path(GBA_INCLUDE_DIR gba.h
|
||||
PATHS
|
||||
/include
|
||||
)
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(GBA DEFAULT_MSG GBA_LIBRARY
|
||||
FAT_LIBRARY FILESYSTEM_LIBRARY)
|
||||
@@ -0,0 +1,52 @@
|
||||
# This file belongs Nick Overdijk, and is from https://github.com/NickNick/wubwubcmake
|
||||
# The MIT License (MIT)
|
||||
#
|
||||
# Copyright (c) 2013 Nick Overdijk
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
# this software and associated documentation files (the "Software"), to deal in
|
||||
# the Software without restriction, including without limitation the rights to
|
||||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
# the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
# subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in all
|
||||
# copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.option(USE_ASAN "Enable Address Sanitizer, if your compiler supports it" ON)
|
||||
|
||||
option(USE_ASAN "Enable Address Sanitizer, if your compiler supports it" OFF)
|
||||
if(USE_ASAN)
|
||||
include(CheckCXXSourceCompiles)
|
||||
# If the compiler understands -fsanitize=address, add it to the flags (gcc since 4.8 & clang since version 3.2)
|
||||
set(CMAKE_REQUIRED_FLAGS_BAK "${CMAKE_REQUIRED_FLAGS}")
|
||||
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fsanitize=address")
|
||||
CHECK_CXX_SOURCE_COMPILES("int main() { return 0; }" FLAG_FSANA_SUPPORTED)
|
||||
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS_BAK}")
|
||||
|
||||
if(FLAG_FSANA_SUPPORTED)
|
||||
set(asan_flag "-fsanitize=address")
|
||||
else(FLAG_FSANA_SUPPORTED)
|
||||
# Alternatively, try if it understands -faddress-sanitizer (clang until version 3.2)
|
||||
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -faddress-sanitizer")
|
||||
CHECK_CXX_SOURCE_COMPILES("int main() { return 0; }" FLAG_FASAN_SUPPORTED)
|
||||
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS_BAK}")
|
||||
|
||||
if(FLAG_FASAN_SUPPORTED)
|
||||
set(asan_flag "-faddress-sanitizer")
|
||||
endif(FLAG_FASAN_SUPPORTED)
|
||||
endif(FLAG_FSANA_SUPPORTED)
|
||||
|
||||
if(FLAG_FSANA_SUPPORTED OR FLAG_FASAN_SUPPORTED)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${asan_flag}")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${asan_flag}")
|
||||
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${asan_flag}")
|
||||
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${asan_flag}")
|
||||
endif()
|
||||
|
||||
endif(USE_ASAN)
|
||||
@@ -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