From 98a0c420408f9e6a1ceebbb39376835643dd97db Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 29 May 2020 19:36:00 -0500 Subject: [PATCH] [nostalgia/core/gba][ox/std] Move heap manager from NostalgiaCore to OxStd --- CMakeLists.txt | 6 +- deps/gbastartup/CMakeLists.txt | 2 + deps/gbastartup/cstartup.cpp | 22 +++++- deps/ox/src/ox/std/CMakeLists.txt | 1 + .../mem.cpp => deps/ox/src/ox/std/heapmgr.cpp | 78 +++++++------------ deps/ox/src/ox/std/heapmgr.hpp | 33 ++++++++ deps/ox/src/ox/std/std.hpp | 1 + deps/ox/src/ox/std/test/CMakeLists.txt | 1 + deps/ox/src/ox/std/test/tests.cpp | 11 +++ src/nostalgia/core/gba/CMakeLists.txt | 3 - src/nostalgia/core/gba/tests.cpp | 24 ++---- 11 files changed, 108 insertions(+), 74 deletions(-) rename src/nostalgia/core/gba/mem.cpp => deps/ox/src/ox/std/heapmgr.cpp (62%) create mode 100644 deps/ox/src/ox/std/heapmgr.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 408402ae..c9b2c620 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -118,11 +118,11 @@ endif() enable_testing() +add_subdirectory(deps/ox) +include_directories(SYSTEM deps/ox/src) + if(NOSTALGIA_BUILD_TYPE STREQUAL "GBA") add_subdirectory(deps/gbastartup) endif() -add_subdirectory(deps/ox) -include_directories(SYSTEM deps/ox/src) - add_subdirectory(src) diff --git a/deps/gbastartup/CMakeLists.txt b/deps/gbastartup/CMakeLists.txt index 203bbb72..fef3fe5d 100644 --- a/deps/gbastartup/CMakeLists.txt +++ b/deps/gbastartup/CMakeLists.txt @@ -1,4 +1,5 @@ enable_language(C ASM) + add_library( GbaStartup gba_crt0.s @@ -7,4 +8,5 @@ add_library( target_link_libraries( GbaStartup + OxStd ) diff --git a/deps/gbastartup/cstartup.cpp b/deps/gbastartup/cstartup.cpp index 80952fc2..c28408a0 100644 --- a/deps/gbastartup/cstartup.cpp +++ b/deps/gbastartup/cstartup.cpp @@ -6,17 +6,33 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include +#include + #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpedantic" +// this warning is too dumb to realize that it can actually confirm the hard +// coded address aligns with the requirement of HeapSegment, so it must be +// suppressed +#pragma GCC diagnostic ignored "-Wcast-align" + +#define MEM_WRAM_BEGIN reinterpret_cast(0x02000000) +#define MEM_WRAM_END reinterpret_cast(0x0203FFFF) + +#define HEAP_BEGIN reinterpret_cast(MEM_WRAM_BEGIN) +// set size to half of WRAM +#define HEAP_SIZE ((MEM_WRAM_END - MEM_WRAM_BEGIN) / 2) +#define HEAP_END reinterpret_cast(MEM_WRAM_BEGIN + HEAP_SIZE) + extern void (*__preinit_array_start[]) (void); extern void (*__preinit_array_end[]) (void); extern void (*__init_array_start[]) (void); extern void (*__init_array_end[]) (void); -namespace nostalgia::core { +namespace ox::heapmgr { -void initHeap(); +void initHeap(char *heapBegin, char *heapEnd); } @@ -38,7 +54,7 @@ int main(int argc, const char **argv); int c_start() { const char *args[2] = {"", "rom.oxfs"}; - nostalgia::core::initHeap(); + ox::heapmgr::initHeap(ox::bit_cast(HEAP_BEGIN), ox::bit_cast(HEAP_END)); return main(2, args); } diff --git a/deps/ox/src/ox/std/CMakeLists.txt b/deps/ox/src/ox/std/CMakeLists.txt index e327ec7a..3ca9116d 100644 --- a/deps/ox/src/ox/std/CMakeLists.txt +++ b/deps/ox/src/ox/std/CMakeLists.txt @@ -3,6 +3,7 @@ add_library( assert.cpp buildinfo.cpp byteswap.cpp + heapmgr.cpp memops.cpp new.cpp random.cpp diff --git a/src/nostalgia/core/gba/mem.cpp b/deps/ox/src/ox/std/heapmgr.cpp similarity index 62% rename from src/nostalgia/core/gba/mem.cpp rename to deps/ox/src/ox/std/heapmgr.cpp index 46c0e531..a7067f38 100644 --- a/src/nostalgia/core/gba/mem.cpp +++ b/deps/ox/src/ox/std/heapmgr.cpp @@ -1,27 +1,16 @@ /* - * Copyright 2016 - 2019 gtalent2@gmail.com + * Copyright 2016 - 2020 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 "addresses.hpp" +#include "assert.hpp" +#include "bit.hpp" +#include "heapmgr.hpp" -#include -#include - -// this warning is too dumb to realize that it can actually confirm the hard -// coded address aligns with the requirement of HeapSegment, so it must be -// suppressed -#pragma GCC diagnostic ignored "-Wcast-align" - -#define HEAP_BEGIN reinterpret_cast(MEM_WRAM_BEGIN) -// set size to half of WRAM -#define HEAP_SIZE ((MEM_WRAM_END - MEM_WRAM_BEGIN) / 2) -#define HEAP_END reinterpret_cast(MEM_WRAM_BEGIN + HEAP_SIZE) - -namespace nostalgia::core { +namespace ox::heapmgr { static struct HeapSegment *volatile g_heapBegin = nullptr; static struct HeapSegment *volatile g_heapEnd = nullptr; @@ -36,28 +25,23 @@ static constexpr std::size_t alignedSize(T = {}) { return alignedSize(sizeof(T)); } -struct HeapSegment { - std::size_t size; - uint8_t inUse; +void HeapSegment::init(std::size_t maxSize = ox::bit_cast(g_heapEnd)) { + this->size = maxSize - ox::bit_cast(this); + this->inUse = false; +} - void init(std::size_t maxSize = ox::bit_cast(g_heapEnd)) { - this->size = maxSize - ox::bit_cast(this); - this->inUse = false; - } +template +T *HeapSegment::data() { + return ox::bit_cast(ox::bit_cast(this) + alignedSize(this)); +} - template - T *data() { - return ox::bit_cast(ox::bit_cast(this) + alignedSize(this)); - } +template +T *HeapSegment::end() { + const auto size = alignedSize(this) + alignedSize(this->size); + auto e = ox::bit_cast(ox::bit_cast(this) + size); + return ox::bit_cast(e); +} - template - T *end() { - const auto size = alignedSize(this) + alignedSize(this->size); - auto e = ox::bit_cast(ox::bit_cast(this) + size); - return ox::bit_cast(e); - } - -}; void initHeap(char *heapBegin, char *heapEnd) { g_heapBegin = ox::bit_cast(heapBegin); @@ -67,10 +51,6 @@ void initHeap(char *heapBegin, char *heapEnd) { heapIdx->inUse = false; } -void initHeap() { - initHeap(ox::bit_cast(HEAP_BEGIN), ox::bit_cast(HEAP_END)); -} - struct SegmentPair { HeapSegment *anteSegment = nullptr; HeapSegment *segment = nullptr; @@ -78,7 +58,7 @@ struct SegmentPair { static SegmentPair findSegmentOf(void *ptr) { HeapSegment *prev = nullptr; - for (auto seg = HEAP_BEGIN; seg < HEAP_END;) { + for (auto seg = g_heapBegin; seg < g_heapEnd;) { if (seg->data() == ptr) { return {prev, seg}; } @@ -126,38 +106,38 @@ void free(void *ptr) { #ifndef OX_USE_STDLIB -using namespace nostalgia; +using namespace ox; void *operator new(std::size_t allocSize) { - return core::malloc(allocSize); + return heapmgr::malloc(allocSize); } void *operator new[](std::size_t allocSize) { - return core::malloc(allocSize); + return heapmgr::malloc(allocSize); } void operator delete(void *ptr) { - core::free(ptr); + heapmgr::free(ptr); } void operator delete[](void *ptr) { - core::free(ptr); + heapmgr::free(ptr); } void operator delete(void *ptr, unsigned) { - core::free(ptr); + heapmgr::free(ptr); } void operator delete[](void *ptr, unsigned) { - core::free(ptr); + heapmgr::free(ptr); } void operator delete(void *ptr, unsigned long int) { - core::free(ptr); + heapmgr::free(ptr); } void operator delete[](void *ptr, unsigned long int) { - core::free(ptr); + heapmgr::free(ptr); } #endif diff --git a/deps/ox/src/ox/std/heapmgr.hpp b/deps/ox/src/ox/std/heapmgr.hpp new file mode 100644 index 00000000..22876152 --- /dev/null +++ b/deps/ox/src/ox/std/heapmgr.hpp @@ -0,0 +1,33 @@ +/* + * Copyright 2016 - 2020 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 "types.hpp" + +namespace ox::heapmgr { + +struct HeapSegment { + std::size_t size; + uint8_t inUse; + + void init(std::size_t maxSize); + + template + T *data(); + + template + T *end(); + +}; + +void initHeap(char *heapBegin, char *heapEnd); + +[[nodiscard]] void *malloc(std::size_t allocSize); + +void free(void *ptr); + +} diff --git a/deps/ox/src/ox/std/std.hpp b/deps/ox/src/ox/std/std.hpp index dfc49797..11b55e04 100644 --- a/deps/ox/src/ox/std/std.hpp +++ b/deps/ox/src/ox/std/std.hpp @@ -15,6 +15,7 @@ #include "error.hpp" #include "hardware.hpp" #include "hashmap.hpp" +#include "heapmgr.hpp" #include "math.hpp" #include "memops.hpp" #include "new.hpp" diff --git a/deps/ox/src/ox/std/test/CMakeLists.txt b/deps/ox/src/ox/std/test/CMakeLists.txt index 81079e32..b38ded6e 100644 --- a/deps/ox/src/ox/std/test/CMakeLists.txt +++ b/deps/ox/src/ox/std/test/CMakeLists.txt @@ -15,3 +15,4 @@ add_test("Test\\ BString" StdTest "BString") add_test("Test\\ String" StdTest "String") add_test("Test\\ Vector" StdTest "Vector") add_test("Test\\ HashMap" StdTest "HashMap") +add_test("Test\\ HeapMgr" StdTest malloc) diff --git a/deps/ox/src/ox/std/test/tests.cpp b/deps/ox/src/ox/std/test/tests.cpp index dad60511..f693484a 100644 --- a/deps/ox/src/ox/std/test/tests.cpp +++ b/deps/ox/src/ox/std/test/tests.cpp @@ -5,6 +5,7 @@ * 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 #include #include @@ -13,6 +14,16 @@ using namespace std; map> tests = { + { + "malloc", + [] { + std::vector buff(ox::units::MB); + ox::heapmgr::initHeap(&buff.front(), &buff.back()); + oxAssert(ox::heapmgr::malloc(5) != nullptr, "malloc is broken"); + oxAssert(ox::heapmgr::malloc(5) != nullptr, "malloc is broken"); + return 0; + } + }, { "ABCDEFG != HIJKLMN", []() { diff --git a/src/nostalgia/core/gba/CMakeLists.txt b/src/nostalgia/core/gba/CMakeLists.txt index 678f9434..89b25ad2 100644 --- a/src/nostalgia/core/gba/CMakeLists.txt +++ b/src/nostalgia/core/gba/CMakeLists.txt @@ -4,7 +4,6 @@ if(NOSTALGIA_BUILD_TYPE STREQUAL "GBA") core.cpp gfx.cpp media.cpp - mem.cpp panic.cpp ) @@ -28,8 +27,6 @@ endif() if(NOSTALGIA_BUILD_TYPE STREQUAL "Native") add_executable(NostalgiaCore-GBA_Test tests.cpp - mem.cpp ) target_link_libraries(NostalgiaCore-GBA_Test NostalgiaCore) - add_test("NostalgiaCore-GBA\\ Test\\ malloc" NostalgiaCore-GBA_Test malloc) endif() diff --git a/src/nostalgia/core/gba/tests.cpp b/src/nostalgia/core/gba/tests.cpp index 3d45eff9..1b16699d 100644 --- a/src/nostalgia/core/gba/tests.cpp +++ b/src/nostalgia/core/gba/tests.cpp @@ -1,3 +1,10 @@ +/* + * Copyright 2016 - 2020 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 #include @@ -5,9 +12,7 @@ #include -#include - -namespace nostalgia::core { +namespace ox::heapmgr { [[nodiscard]] void *malloc(std::size_t allocSize); @@ -17,20 +22,7 @@ void initHeap(char *heapBegin, char *heapEnd); } -using namespace nostalgia; - -int testMalloc(std::string) { - std::vector buff(ox::units::MB); - core::initHeap(&buff.front(), &buff.back()); - oxAssert(core::malloc(5) != nullptr, "malloc is broken"); - oxAssert(core::malloc(5) != nullptr, "malloc is broken"); - return 0; -} - std::map tests = { - { - { "malloc", testMalloc }, - } }; int main(int argc, const char **args) {