[nostalgia/core/gba][ox/std] Move heap manager from NostalgiaCore to OxStd

This commit is contained in:
Gary Talent 2020-05-29 19:36:00 -05:00
parent 0eb33f823c
commit 98a0c42040
11 changed files with 108 additions and 74 deletions

View File

@ -118,11 +118,11 @@ endif()
enable_testing() enable_testing()
add_subdirectory(deps/ox)
include_directories(SYSTEM deps/ox/src)
if(NOSTALGIA_BUILD_TYPE STREQUAL "GBA") if(NOSTALGIA_BUILD_TYPE STREQUAL "GBA")
add_subdirectory(deps/gbastartup) add_subdirectory(deps/gbastartup)
endif() endif()
add_subdirectory(deps/ox)
include_directories(SYSTEM deps/ox/src)
add_subdirectory(src) add_subdirectory(src)

View File

@ -1,4 +1,5 @@
enable_language(C ASM) enable_language(C ASM)
add_library( add_library(
GbaStartup GbaStartup
gba_crt0.s gba_crt0.s
@ -7,4 +8,5 @@ add_library(
target_link_libraries( target_link_libraries(
GbaStartup GbaStartup
OxStd
) )

View File

@ -6,17 +6,33 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/ */
#include <ox/std/bit.hpp>
#include <ox/std/heapmgr.hpp>
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic" #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<uint8_t*>(0x02000000)
#define MEM_WRAM_END reinterpret_cast<uint8_t*>(0x0203FFFF)
#define HEAP_BEGIN reinterpret_cast<ox::heapmgr::HeapSegment*>(MEM_WRAM_BEGIN)
// set size to half of WRAM
#define HEAP_SIZE ((MEM_WRAM_END - MEM_WRAM_BEGIN) / 2)
#define HEAP_END reinterpret_cast<ox::heapmgr::HeapSegment*>(MEM_WRAM_BEGIN + HEAP_SIZE)
extern void (*__preinit_array_start[]) (void); extern void (*__preinit_array_start[]) (void);
extern void (*__preinit_array_end[]) (void); extern void (*__preinit_array_end[]) (void);
extern void (*__init_array_start[]) (void); extern void (*__init_array_start[]) (void);
extern void (*__init_array_end[]) (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() { int c_start() {
const char *args[2] = {"", "rom.oxfs"}; const char *args[2] = {"", "rom.oxfs"};
nostalgia::core::initHeap(); ox::heapmgr::initHeap(ox::bit_cast<char*>(HEAP_BEGIN), ox::bit_cast<char*>(HEAP_END));
return main(2, args); return main(2, args);
} }

View File

@ -3,6 +3,7 @@ add_library(
assert.cpp assert.cpp
buildinfo.cpp buildinfo.cpp
byteswap.cpp byteswap.cpp
heapmgr.cpp
memops.cpp memops.cpp
new.cpp new.cpp
random.cpp random.cpp

View File

@ -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 * 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 * 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/. * 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 <ox/std/assert.hpp> namespace ox::heapmgr {
#include <ox/std/bit.hpp>
// 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<HeapSegment*>(MEM_WRAM_BEGIN)
// set size to half of WRAM
#define HEAP_SIZE ((MEM_WRAM_END - MEM_WRAM_BEGIN) / 2)
#define HEAP_END reinterpret_cast<HeapSegment*>(MEM_WRAM_BEGIN + HEAP_SIZE)
namespace nostalgia::core {
static struct HeapSegment *volatile g_heapBegin = nullptr; static struct HeapSegment *volatile g_heapBegin = nullptr;
static struct HeapSegment *volatile g_heapEnd = nullptr; static struct HeapSegment *volatile g_heapEnd = nullptr;
@ -36,28 +25,23 @@ static constexpr std::size_t alignedSize(T = {}) {
return alignedSize(sizeof(T)); return alignedSize(sizeof(T));
} }
struct HeapSegment { void HeapSegment::init(std::size_t maxSize = ox::bit_cast<std::size_t>(g_heapEnd)) {
std::size_t size; this->size = maxSize - ox::bit_cast<std::size_t>(this);
uint8_t inUse; this->inUse = false;
}
void init(std::size_t maxSize = ox::bit_cast<std::size_t>(g_heapEnd)) { template<typename T>
this->size = maxSize - ox::bit_cast<std::size_t>(this); T *HeapSegment::data() {
this->inUse = false; return ox::bit_cast<T*>(ox::bit_cast<uint8_t*>(this) + alignedSize(this));
} }
template<typename T> template<typename T = uint8_t>
T *data() { T *HeapSegment::end() {
return ox::bit_cast<T*>(ox::bit_cast<uint8_t*>(this) + alignedSize(this)); const auto size = alignedSize(this) + alignedSize(this->size);
} auto e = ox::bit_cast<uintptr_t>(ox::bit_cast<uint8_t*>(this) + size);
return ox::bit_cast<T*>(e);
}
template<typename T = uint8_t>
T *end() {
const auto size = alignedSize(this) + alignedSize(this->size);
auto e = ox::bit_cast<uintptr_t>(ox::bit_cast<uint8_t*>(this) + size);
return ox::bit_cast<T*>(e);
}
};
void initHeap(char *heapBegin, char *heapEnd) { void initHeap(char *heapBegin, char *heapEnd) {
g_heapBegin = ox::bit_cast<HeapSegment*>(heapBegin); g_heapBegin = ox::bit_cast<HeapSegment*>(heapBegin);
@ -67,10 +51,6 @@ void initHeap(char *heapBegin, char *heapEnd) {
heapIdx->inUse = false; heapIdx->inUse = false;
} }
void initHeap() {
initHeap(ox::bit_cast<char*>(HEAP_BEGIN), ox::bit_cast<char*>(HEAP_END));
}
struct SegmentPair { struct SegmentPair {
HeapSegment *anteSegment = nullptr; HeapSegment *anteSegment = nullptr;
HeapSegment *segment = nullptr; HeapSegment *segment = nullptr;
@ -78,7 +58,7 @@ struct SegmentPair {
static SegmentPair findSegmentOf(void *ptr) { static SegmentPair findSegmentOf(void *ptr) {
HeapSegment *prev = nullptr; HeapSegment *prev = nullptr;
for (auto seg = HEAP_BEGIN; seg < HEAP_END;) { for (auto seg = g_heapBegin; seg < g_heapEnd;) {
if (seg->data<void>() == ptr) { if (seg->data<void>() == ptr) {
return {prev, seg}; return {prev, seg};
} }
@ -126,38 +106,38 @@ void free(void *ptr) {
#ifndef OX_USE_STDLIB #ifndef OX_USE_STDLIB
using namespace nostalgia; using namespace ox;
void *operator new(std::size_t allocSize) { void *operator new(std::size_t allocSize) {
return core::malloc(allocSize); return heapmgr::malloc(allocSize);
} }
void *operator new[](std::size_t allocSize) { void *operator new[](std::size_t allocSize) {
return core::malloc(allocSize); return heapmgr::malloc(allocSize);
} }
void operator delete(void *ptr) { void operator delete(void *ptr) {
core::free(ptr); heapmgr::free(ptr);
} }
void operator delete[](void *ptr) { void operator delete[](void *ptr) {
core::free(ptr); heapmgr::free(ptr);
} }
void operator delete(void *ptr, unsigned) { void operator delete(void *ptr, unsigned) {
core::free(ptr); heapmgr::free(ptr);
} }
void operator delete[](void *ptr, unsigned) { void operator delete[](void *ptr, unsigned) {
core::free(ptr); heapmgr::free(ptr);
} }
void operator delete(void *ptr, unsigned long int) { void operator delete(void *ptr, unsigned long int) {
core::free(ptr); heapmgr::free(ptr);
} }
void operator delete[](void *ptr, unsigned long int) { void operator delete[](void *ptr, unsigned long int) {
core::free(ptr); heapmgr::free(ptr);
} }
#endif #endif

33
deps/ox/src/ox/std/heapmgr.hpp vendored Normal file
View File

@ -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<typename T>
T *data();
template<typename T = uint8_t>
T *end();
};
void initHeap(char *heapBegin, char *heapEnd);
[[nodiscard]] void *malloc(std::size_t allocSize);
void free(void *ptr);
}

View File

@ -15,6 +15,7 @@
#include "error.hpp" #include "error.hpp"
#include "hardware.hpp" #include "hardware.hpp"
#include "hashmap.hpp" #include "hashmap.hpp"
#include "heapmgr.hpp"
#include "math.hpp" #include "math.hpp"
#include "memops.hpp" #include "memops.hpp"
#include "new.hpp" #include "new.hpp"

View File

@ -15,3 +15,4 @@ add_test("Test\\ BString" StdTest "BString")
add_test("Test\\ String" StdTest "String") add_test("Test\\ String" StdTest "String")
add_test("Test\\ Vector" StdTest "Vector") add_test("Test\\ Vector" StdTest "Vector")
add_test("Test\\ HashMap" StdTest "HashMap") add_test("Test\\ HashMap" StdTest "HashMap")
add_test("Test\\ HeapMgr" StdTest malloc)

View File

@ -5,6 +5,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/ */
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <functional> #include <functional>
@ -13,6 +14,16 @@
using namespace std; using namespace std;
map<string, function<int()>> tests = { map<string, function<int()>> tests = {
{
"malloc",
[] {
std::vector<char> 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", "ABCDEFG != HIJKLMN",
[]() { []() {

View File

@ -4,7 +4,6 @@ if(NOSTALGIA_BUILD_TYPE STREQUAL "GBA")
core.cpp core.cpp
gfx.cpp gfx.cpp
media.cpp media.cpp
mem.cpp
panic.cpp panic.cpp
) )
@ -28,8 +27,6 @@ endif()
if(NOSTALGIA_BUILD_TYPE STREQUAL "Native") if(NOSTALGIA_BUILD_TYPE STREQUAL "Native")
add_executable(NostalgiaCore-GBA_Test add_executable(NostalgiaCore-GBA_Test
tests.cpp tests.cpp
mem.cpp
) )
target_link_libraries(NostalgiaCore-GBA_Test NostalgiaCore) target_link_libraries(NostalgiaCore-GBA_Test NostalgiaCore)
add_test("NostalgiaCore-GBA\\ Test\\ malloc" NostalgiaCore-GBA_Test malloc)
endif() endif()

View File

@ -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 <map> #include <map>
#include <string> #include <string>
@ -5,9 +12,7 @@
#include <ox/std/std.hpp> #include <ox/std/std.hpp>
#include <nostalgia/core/mem.hpp> namespace ox::heapmgr {
namespace nostalgia::core {
[[nodiscard]] void *malloc(std::size_t allocSize); [[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<char> 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<std::string, int(*)(std::string)> tests = { std::map<std::string, int(*)(std::string)> tests = {
{
{ "malloc", testMalloc },
}
}; };
int main(int argc, const char **args) { int main(int argc, const char **args) {