From c20db6a8cad987676c63c74177941686927842bb Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Tue, 16 Mar 2021 13:19:41 +0100 Subject: [PATCH] minor data storage improvements --- include/fxos/memory.h | 25 ++++++++++++------- include/fxos/util.h | 2 +- lib/memory.cpp | 56 +++++++++++++++++++------------------------ lib/util.cpp | 2 +- 4 files changed, 43 insertions(+), 42 deletions(-) diff --git a/include/fxos/memory.h b/include/fxos/memory.h index a6e136c..2e8c981 100644 --- a/include/fxos/memory.h +++ b/include/fxos/memory.h @@ -6,6 +6,7 @@ #define LIBFXOS_MEMORY_H #include +#include #include namespace FxOS { @@ -14,7 +15,7 @@ namespace FxOS { class MemoryArea { public: - enum MemoryAreaName { + enum MemoryAreaName: int8_t { /* Userspace seen from user and privileged mode */ U0, P0, /* Second half of memory, only for privileged mode */ @@ -50,14 +51,17 @@ struct MemoryRegion { /* Address space regions that correspond to standard (ie. contiguous multi-addressable) memory */ - static MemoryRegion const &ROM; - static MemoryRegion const &RAM; - static MemoryRegion const &ROM_P2; - static MemoryRegion const &RAM_P2; - static MemoryRegion const &RS; - static MemoryRegion const &ILRAM; - static MemoryRegion const &XRAM; - static MemoryRegion const &YRAM; + static MemoryRegion const ROM; + static MemoryRegion const RAM; + static MemoryRegion const ROM_P2; + static MemoryRegion const RAM_P2; + static MemoryRegion const RS; + static MemoryRegion const ILRAM; + static MemoryRegion const XRAM; + static MemoryRegion const YRAM; + + /* All standard regions. */ + static std::array const all(); /* Determine if an address falls into one of the standard regions. Throws std::out_of_range if none. */ @@ -93,6 +97,9 @@ struct MemoryRegion contained in one, which should always be the case) */ MemoryArea area() const noexcept; +private: + static std::array const m_all; + /* Automatically guess the cacheable and mappable flags */ void guess_flags() noexcept; }; diff --git a/include/fxos/util.h b/include/fxos/util.h index 532fa5f..c9f19ba 100644 --- a/include/fxos/util.h +++ b/include/fxos/util.h @@ -91,7 +91,7 @@ public: /* Buffer initialized from file by looking in one of the specified directories only. */ - Buffer(std::string filepath, std::vector &folders, + Buffer(std::string filepath, std::vector const &folders, ssize_t size=-1, int fill=0x00); /* Create a buffer by copying and resizing another buffer */ diff --git a/lib/memory.cpp b/lib/memory.cpp index a10a24c..cd0717a 100644 --- a/lib/memory.cpp +++ b/lib/memory.cpp @@ -67,7 +67,7 @@ uint32_t MemoryRegion::size() const noexcept MemoryArea MemoryRegion::area() const noexcept { using Area = MemoryArea; - Area areas[5] = { Area::P4, Area::P3, Area::P2, Area::P1, Area::P0 }; + static Area areas[5]={ Area::P4, Area::P3, Area::P2, Area::P1, Area::P0 }; for(int i = 0; i < 5; i++) { @@ -101,50 +101,44 @@ void MemoryRegion::guess_flags() noexcept } } -using R = MemoryRegion; +using R = MemoryRegion; /* Basic memory regions */ -R const &R::ROM = MemoryRegion("ROM", 0x80000000, 0x81ffffff, false); -R const &R::RAM = MemoryRegion("RAM", 0x88000000, 0x88040000, true); -R const &R::ROM_P2 = MemoryRegion("ROM_P2", 0xa0000000, 0xa07fffff, false); -R const &R::RAM_P2 = MemoryRegion("RAM_P2", 0xa8000000, 0xa8040000, true); -R const &R::RS = MemoryRegion("RS", 0xfd800000, 0xfd8007ff, true); -R const &R::ILRAM = MemoryRegion("ILRAM", 0xe5200000, 0xe5203fff, true); -R const &R::XRAM = MemoryRegion("XRAM", 0xe5007000, 0xe5008fff, true); -R const &R::YRAM = MemoryRegion("YRAM", 0xe5017000, 0xe5018fff, true); +R const R::ROM = MemoryRegion("ROM", 0x80000000, 0x81ffffff, false); +R const R::RAM = MemoryRegion("RAM", 0x88000000, 0x88040000, true); +R const R::ROM_P2 = MemoryRegion("ROM_P2", 0xa0000000, 0xa07fffff, false); +R const R::RAM_P2 = MemoryRegion("RAM_P2", 0xa8000000, 0xa8040000, true); +R const R::RS = MemoryRegion("RS", 0xfd800000, 0xfd8007ff, true); +R const R::ILRAM = MemoryRegion("ILRAM", 0xe5200000, 0xe5203fff, true); +R const R::XRAM = MemoryRegion("XRAM", 0xe5007000, 0xe5008fff, true); +R const R::YRAM = MemoryRegion("YRAM", 0xe5017000, 0xe5018fff, true); + +std::array const MemoryRegion::m_all = { + &R::ROM, &R::RAM, &R::ROM_P2, &R::RAM_P2, + &R::RS, &R::ILRAM, &R::XRAM, &R::YRAM, +}; + +std::array const MemoryRegion::all() +{ + return m_all; +} MemoryRegion const *MemoryRegion::region_for(uint32_t address) { - MemoryRegion const *regs[8] = { - &R::ROM, &R::RAM, &R::ROM_P2, &R::RAM_P2, - &R::RS, &R::ILRAM, &R::XRAM, &R::YRAM - }; - - for(int i = 0; i < 8; i++) - { - if(regs[i]->start <= address && address < regs[i]->end) - return regs[i]; + for(auto r: MemoryRegion::all()) { + if(r->start <= address && address < r->end) return r; } - return nullptr; } MemoryRegion::MemoryRegion(std::string name) { - MemoryRegion const *regs[8] = { - &R::ROM, &R::RAM, &R::ROM_P2, &R::RAM_P2, - &R::RS, &R::ILRAM, &R::XRAM, &R::YRAM - }; - - for(int i = 0; i < 8; i++) - { - if(regs[i]->name == name) - { - *this = *regs[i]; + for(auto r: MemoryRegion::all()) { + if(r->name == name) { + *this = *r; return; } } - throw std::runtime_error("No standard region named '" + name + "'"); } diff --git a/lib/util.cpp b/lib/util.cpp index 870d6fd..07c9b88 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -86,7 +86,7 @@ Buffer::Buffer(std::string filepath, ssize_t bufsize, int fill) this->path = filepath; } -Buffer::Buffer(std::string filepath, std::vector &folders, +Buffer::Buffer(std::string filepath, std::vector const &folders, ssize_t size, int fill): Buffer() {