[nostalgia/core/gba][ox/std] Move heap manager from NostalgiaCore to OxStd
(synced from 98a0c42040)
This commit is contained in:
@@ -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
|
||||||
|
|||||||
@@ -0,0 +1,143 @@
|
|||||||
|
/*
|
||||||
|
* 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 "assert.hpp"
|
||||||
|
#include "bit.hpp"
|
||||||
|
#include "heapmgr.hpp"
|
||||||
|
|
||||||
|
namespace ox::heapmgr {
|
||||||
|
|
||||||
|
static struct HeapSegment *volatile g_heapBegin = nullptr;
|
||||||
|
static struct HeapSegment *volatile g_heapEnd = nullptr;
|
||||||
|
static struct HeapSegment *volatile heapIdx = nullptr;
|
||||||
|
|
||||||
|
static constexpr std::size_t alignedSize(std::size_t sz) {
|
||||||
|
return sz + (sz & 7);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static constexpr std::size_t alignedSize(T = {}) {
|
||||||
|
return alignedSize(sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T *HeapSegment::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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void initHeap(char *heapBegin, char *heapEnd) {
|
||||||
|
g_heapBegin = ox::bit_cast<HeapSegment*>(heapBegin);
|
||||||
|
g_heapEnd = ox::bit_cast<HeapSegment*>(heapEnd);
|
||||||
|
heapIdx = g_heapBegin;
|
||||||
|
heapIdx->size = ox::bit_cast<std::size_t>(heapEnd) - ox::bit_cast<std::size_t>(heapIdx);
|
||||||
|
heapIdx->inUse = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SegmentPair {
|
||||||
|
HeapSegment *anteSegment = nullptr;
|
||||||
|
HeapSegment *segment = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
static SegmentPair findSegmentOf(void *ptr) {
|
||||||
|
HeapSegment *prev = nullptr;
|
||||||
|
for (auto seg = g_heapBegin; seg < g_heapEnd;) {
|
||||||
|
if (seg->data<void>() == ptr) {
|
||||||
|
return {prev, seg};
|
||||||
|
}
|
||||||
|
prev = seg;
|
||||||
|
seg = seg->end<HeapSegment>();
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
static HeapSegment *findSegmentFor(std::size_t sz) {
|
||||||
|
for (auto s = g_heapBegin; s <= g_heapEnd; s = s->end<HeapSegment>()) {
|
||||||
|
if (s->size >= sz && !s->inUse) {
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
oxPanic(OxError(1), "malloc: could not find segment");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] void *malloc(std::size_t allocSize) {
|
||||||
|
const auto targetSize = alignedSize(sizeof(HeapSegment)) + alignedSize(allocSize);
|
||||||
|
auto seg = findSegmentFor(targetSize);
|
||||||
|
if (seg == nullptr) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
const auto bytesRemaining = seg->size - targetSize;
|
||||||
|
seg->size = targetSize;
|
||||||
|
seg->inUse = true;
|
||||||
|
seg->end<HeapSegment>()->init(bytesRemaining);
|
||||||
|
return seg->data<void>();
|
||||||
|
}
|
||||||
|
|
||||||
|
void free(void *ptr) {
|
||||||
|
auto p = findSegmentOf(ptr);
|
||||||
|
if (p.anteSegment) {
|
||||||
|
p.anteSegment->size += p.segment->size;
|
||||||
|
} else if (p.segment) {
|
||||||
|
p.segment->inUse = false;
|
||||||
|
} else {
|
||||||
|
oxPanic(OxError(1), "Bad heap free");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef OX_USE_STDLIB
|
||||||
|
|
||||||
|
using namespace ox;
|
||||||
|
|
||||||
|
void *operator new(std::size_t allocSize) {
|
||||||
|
return heapmgr::malloc(allocSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *operator new[](std::size_t allocSize) {
|
||||||
|
return heapmgr::malloc(allocSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator delete(void *ptr) {
|
||||||
|
heapmgr::free(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator delete[](void *ptr) {
|
||||||
|
heapmgr::free(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator delete(void *ptr, unsigned) {
|
||||||
|
heapmgr::free(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator delete[](void *ptr, unsigned) {
|
||||||
|
heapmgr::free(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator delete(void *ptr, unsigned long int) {
|
||||||
|
heapmgr::free(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator delete[](void *ptr, unsigned long int) {
|
||||||
|
heapmgr::free(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -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);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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"
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -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",
|
||||||
[]() {
|
[]() {
|
||||||
|
|||||||
Reference in New Issue
Block a user