[ox] General cleanup and updating to newer coding standards
This commit is contained in:
37
deps/ox/src/ox/std/heapmgr.cpp
vendored
37
deps/ox/src/ox/std/heapmgr.cpp
vendored
@@ -16,34 +16,34 @@ 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) {
|
||||
static constexpr std::size_t alignedSize(std::size_t sz) noexcept {
|
||||
return sz + (sz & 7);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static constexpr std::size_t alignedSize(T = {}) {
|
||||
static constexpr std::size_t alignedSize(T = {}) noexcept {
|
||||
return alignedSize(sizeof(T));
|
||||
}
|
||||
|
||||
void HeapSegment::init(std::size_t maxSize = ox::bit_cast<std::size_t>(g_heapEnd)) {
|
||||
void HeapSegment::init(std::size_t maxSize = ox::bit_cast<std::size_t>(g_heapEnd)) noexcept {
|
||||
this->size = maxSize - ox::bit_cast<std::size_t>(this);
|
||||
this->inUse = false;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T *HeapSegment::data() {
|
||||
T *HeapSegment::data() noexcept {
|
||||
return ox::bit_cast<T*>(ox::bit_cast<uint8_t*>(this) + alignedSize(this));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T *HeapSegment::end() {
|
||||
T *HeapSegment::end() noexcept {
|
||||
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) noexcept {
|
||||
g_heapBegin = ox::bit_cast<HeapSegment*>(heapBegin);
|
||||
g_heapEnd = ox::bit_cast<HeapSegment*>(heapEnd);
|
||||
heapIdx = g_heapBegin;
|
||||
@@ -56,7 +56,7 @@ struct SegmentPair {
|
||||
HeapSegment *segment = nullptr;
|
||||
};
|
||||
|
||||
static SegmentPair findSegmentOf(void *ptr) {
|
||||
static SegmentPair findSegmentOf(void *ptr) noexcept {
|
||||
HeapSegment *prev = nullptr;
|
||||
for (auto seg = g_heapBegin; seg < g_heapEnd;) {
|
||||
if (seg->data<void>() == ptr) {
|
||||
@@ -68,7 +68,7 @@ static SegmentPair findSegmentOf(void *ptr) {
|
||||
return {};
|
||||
}
|
||||
|
||||
static HeapSegment *findSegmentFor(std::size_t sz) {
|
||||
static HeapSegment *findSegmentFor(std::size_t sz) noexcept {
|
||||
for (auto s = g_heapBegin; s <= g_heapEnd; s = s->end<HeapSegment>()) {
|
||||
if (s->size >= sz && !s->inUse) {
|
||||
return s;
|
||||
@@ -78,7 +78,8 @@ static HeapSegment *findSegmentFor(std::size_t sz) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
[[nodiscard]] void *malloc(std::size_t allocSize) {
|
||||
[[nodiscard]]
|
||||
void *malloc(std::size_t allocSize) noexcept {
|
||||
const auto targetSize = alignedSize(sizeof(HeapSegment)) + alignedSize(allocSize);
|
||||
auto seg = findSegmentFor(targetSize);
|
||||
if (seg == nullptr) {
|
||||
@@ -91,7 +92,7 @@ static HeapSegment *findSegmentFor(std::size_t sz) {
|
||||
return seg->data<void>();
|
||||
}
|
||||
|
||||
void free(void *ptr) {
|
||||
void free(void *ptr) noexcept {
|
||||
auto p = findSegmentOf(ptr);
|
||||
if (p.anteSegment) {
|
||||
p.anteSegment->size += p.segment->size;
|
||||
@@ -108,35 +109,35 @@ void free(void *ptr) {
|
||||
|
||||
using namespace ox;
|
||||
|
||||
void *operator new(std::size_t allocSize) {
|
||||
void *operator new(std::size_t allocSize) noexcept {
|
||||
return heapmgr::malloc(allocSize);
|
||||
}
|
||||
|
||||
void *operator new[](std::size_t allocSize) {
|
||||
void *operator new[](std::size_t allocSize) noexcept {
|
||||
return heapmgr::malloc(allocSize);
|
||||
}
|
||||
|
||||
void operator delete(void *ptr) {
|
||||
void operator delete(void *ptr) noexcept {
|
||||
heapmgr::free(ptr);
|
||||
}
|
||||
|
||||
void operator delete[](void *ptr) {
|
||||
void operator delete[](void *ptr) noexcept {
|
||||
heapmgr::free(ptr);
|
||||
}
|
||||
|
||||
void operator delete(void *ptr, unsigned) {
|
||||
void operator delete(void *ptr, unsigned) noexcept {
|
||||
heapmgr::free(ptr);
|
||||
}
|
||||
|
||||
void operator delete[](void *ptr, unsigned) {
|
||||
void operator delete[](void *ptr, unsigned) noexcept {
|
||||
heapmgr::free(ptr);
|
||||
}
|
||||
|
||||
void operator delete(void *ptr, unsigned long int) {
|
||||
void operator delete(void *ptr, unsigned long int) noexcept {
|
||||
heapmgr::free(ptr);
|
||||
}
|
||||
|
||||
void operator delete[](void *ptr, unsigned long int) {
|
||||
void operator delete[](void *ptr, unsigned long int) noexcept {
|
||||
heapmgr::free(ptr);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user