[nostalgia/core/gba][ox/std] Move heap manager from NostalgiaCore to OxStd
This commit is contained in:
parent
0eb33f823c
commit
98a0c42040
@ -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)
|
||||
|
2
deps/gbastartup/CMakeLists.txt
vendored
2
deps/gbastartup/CMakeLists.txt
vendored
@ -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
|
||||
)
|
||||
|
22
deps/gbastartup/cstartup.cpp
vendored
22
deps/gbastartup/cstartup.cpp
vendored
@ -6,17 +6,33 @@
|
||||
* 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 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_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<char*>(HEAP_BEGIN), ox::bit_cast<char*>(HEAP_END));
|
||||
return main(2, args);
|
||||
}
|
||||
|
||||
|
1
deps/ox/src/ox/std/CMakeLists.txt
vendored
1
deps/ox/src/ox/std/CMakeLists.txt
vendored
@ -3,6 +3,7 @@ add_library(
|
||||
assert.cpp
|
||||
buildinfo.cpp
|
||||
byteswap.cpp
|
||||
heapmgr.cpp
|
||||
memops.cpp
|
||||
new.cpp
|
||||
random.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 <ox/std/assert.hpp>
|
||||
#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 {
|
||||
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<std::size_t>(g_heapEnd)) {
|
||||
this->size = maxSize - ox::bit_cast<std::size_t>(this);
|
||||
this->inUse = false;
|
||||
}
|
||||
|
||||
void init(std::size_t maxSize = ox::bit_cast<std::size_t>(g_heapEnd)) {
|
||||
this->size = maxSize - ox::bit_cast<std::size_t>(this);
|
||||
this->inUse = false;
|
||||
}
|
||||
template<typename T>
|
||||
T *HeapSegment::data() {
|
||||
return ox::bit_cast<T*>(ox::bit_cast<uint8_t*>(this) + alignedSize(this));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T *data() {
|
||||
return ox::bit_cast<T*>(ox::bit_cast<uint8_t*>(this) + alignedSize(this));
|
||||
}
|
||||
template<typename T = uint8_t>
|
||||
T *HeapSegment::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);
|
||||
}
|
||||
|
||||
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) {
|
||||
g_heapBegin = ox::bit_cast<HeapSegment*>(heapBegin);
|
||||
@ -67,10 +51,6 @@ void initHeap(char *heapBegin, char *heapEnd) {
|
||||
heapIdx->inUse = false;
|
||||
}
|
||||
|
||||
void initHeap() {
|
||||
initHeap(ox::bit_cast<char*>(HEAP_BEGIN), ox::bit_cast<char*>(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<void>() == 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
|
33
deps/ox/src/ox/std/heapmgr.hpp
vendored
Normal file
33
deps/ox/src/ox/std/heapmgr.hpp
vendored
Normal 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);
|
||||
|
||||
}
|
1
deps/ox/src/ox/std/std.hpp
vendored
1
deps/ox/src/ox/std/std.hpp
vendored
@ -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"
|
||||
|
1
deps/ox/src/ox/std/test/CMakeLists.txt
vendored
1
deps/ox/src/ox/std/test/CMakeLists.txt
vendored
@ -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)
|
||||
|
11
deps/ox/src/ox/std/test/tests.cpp
vendored
11
deps/ox/src/ox/std/test/tests.cpp
vendored
@ -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 <iostream>
|
||||
#include <map>
|
||||
#include <functional>
|
||||
@ -13,6 +14,16 @@
|
||||
using namespace std;
|
||||
|
||||
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",
|
||||
[]() {
|
||||
|
@ -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()
|
||||
|
@ -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 <string>
|
||||
@ -5,9 +12,7 @@
|
||||
|
||||
#include <ox/std/std.hpp>
|
||||
|
||||
#include <nostalgia/core/mem.hpp>
|
||||
|
||||
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<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 = {
|
||||
{
|
||||
{ "malloc", testMalloc },
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, const char **args) {
|
||||
|
Loading…
Reference in New Issue
Block a user