WUMSLoader/wumsloader/src/utils/memory.cpp
2026-04-19 12:05:01 +02:00

214 lines
6.8 KiB
C++

/****************************************************************************
* Copyright (C) 2015 Dimok
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "logger.h"
#include <cerrno>
#include <coreinit/cache.h>
#include <coreinit/memdefaultheap.h>
#include <coreinit/memexpheap.h>
#include <coreinit/memorymap.h>
#include <cstring>
#include <malloc.h>
extern MEMHeapHandle gHeapHandle;
void *MEMAllocSafe(uint32_t size, uint32_t align) {
void *res = nullptr;
MEMHeapHandle heapHandle = gHeapHandle;
res = MEMAllocFromExpHeapEx(heapHandle, size, align);
return res;
}
void *MemoryAlloc(uint32_t size) {
void *res = MEMAllocSafe(size, 4);
if (res == nullptr) {
OSFatal("Failed to MemoryAlloc");
}
return res;
}
void *MemoryAllocEx(uint32_t size, uint32_t align) {
void *res = MEMAllocSafe(size, align);
if (res == nullptr) {
OSFatal("Failed to MemoryAllocEX");
}
return res;
}
void MemoryFree(void *ptr) {
if (ptr) {
MEMFreeToExpHeap(gHeapHandle, ptr);
}
}
uint32_t MEMAlloc __attribute__((__section__(".data"))) = (uint32_t) MemoryAlloc;
uint32_t MEMAllocEx __attribute__((__section__(".data"))) = (uint32_t) MemoryAllocEx;
uint32_t MEMFree __attribute__((__section__(".data"))) = (uint32_t) MemoryFree;
//!-------------------------------------------------------------------------------------------
//! reent versions
//!-------------------------------------------------------------------------------------------
void *_malloc_r(struct _reent *r, size_t size) {
void *ptr = MemoryAllocEx(size, 4);
if (!ptr) {
r->_errno = ENOMEM;
}
return ptr;
}
void *_calloc_r(struct _reent *r, size_t num, size_t size) {
void *ptr = MemoryAllocEx(num * size, 4);
if (ptr) {
memset(ptr, 0, num * size);
} else {
r->_errno = ENOMEM;
}
return ptr;
}
void *_memalign_r(struct _reent *r, size_t align, size_t size) {
return MemoryAllocEx(size, align);
}
void _free_r(struct _reent *r, void *ptr) {
if (ptr) {
MemoryFree(ptr);
}
}
void *_realloc_r(struct _reent *r, void *p, size_t size) {
void *new_ptr = MemoryAllocEx(size, 4);
if (!new_ptr) {
r->_errno = ENOMEM;
return new_ptr;
}
if (p) {
size_t old_size = MEMGetSizeForMBlockExpHeap(p);
memcpy(new_ptr, p, old_size <= size ? old_size : size);
MemoryFree(p);
}
return new_ptr;
}
struct mallinfo _mallinfo_r(struct _reent *r) {
struct mallinfo info = {0};
return info;
}
void _malloc_stats_r(struct _reent *r) {
}
int _mallopt_r(struct _reent *r, int param, int value) {
return 0;
}
size_t
_malloc_usable_size_r(struct _reent *r, void *ptr) {
return MEMGetSizeForMBlockExpHeap(ptr);
}
void *
_valloc_r(struct _reent *r, size_t size) {
return MemoryAllocEx(size, OS_PAGE_SIZE);
}
void *
_pvalloc_r(struct _reent *r, size_t size) {
return MemoryAllocEx((size + (OS_PAGE_SIZE - 1)) & ~(OS_PAGE_SIZE - 1), OS_PAGE_SIZE);
}
int _malloc_trim_r(struct _reent *r, size_t pad) {
return 0;
}
bool CheckMemExpHeapBlock(MEMExpHeap *heap, MEMExpHeapBlockList *block, uint32_t tag, const char *listName, uint32_t &totalSizeOut) {
MEMExpHeapBlock *prevBlock = nullptr;
for (auto *cur = block->head; cur != nullptr; cur = cur->next) {
if (cur->prev != prevBlock) {
DEBUG_FUNCTION_LINE_ERR("[Exp Heap Check] \"%s\" prev is invalid. expected %p actual %p", listName, prevBlock, cur->prev);
return false;
}
if (cur < heap->header.dataStart || cur > heap->header.dataEnd || ((uint32_t) cur + sizeof(MEMExpHeapBlock) + cur->blockSize) > (uint32_t) heap->header.dataEnd) {
DEBUG_FUNCTION_LINE_ERR("[Exp Heap Check] Block is not inside heap. block: %p size %d; heap start %p heap end %p", cur, sizeof(MEMExpHeapBlock) + cur->blockSize, heap->header.dataStart, heap->header.dataEnd);
return false;
}
if (cur->tag != tag) {
DEBUG_FUNCTION_LINE_ERR("[Exp Heap Check] Invalid block tag expected %04X, actual %04X", tag, cur->tag);
return false;
}
totalSizeOut = totalSizeOut + cur->blockSize + (cur->attribs >> 8 & 0x7fffff) + sizeof(MEMExpHeapBlock);
prevBlock = cur;
}
if (prevBlock != block->tail) {
DEBUG_FUNCTION_LINE_ERR("[Exp Heap Check] \"%s\" tail is unexpected! expected %p, actual %p", listName, heap->usedList.tail, prevBlock);
return false;
}
return true;
}
bool CheckMemExpHeapCore(MEMExpHeap *heap) {
uint32_t totalSize = 0;
#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
if (!CheckMemExpHeapBlock(heap, &heap->usedList, 0x5544, "used", totalSize)) {
return false;
}
#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
if (!CheckMemExpHeapBlock(heap, &heap->freeList, 0x4652, "free", totalSize)) {
return false;
}
if (totalSize != (uint32_t) heap->header.dataEnd - (uint32_t) heap->header.dataStart) {
DEBUG_FUNCTION_LINE_ERR("[Exp Heap Check] heap size is unexpected! expected %08X, actual %08X", (uint32_t) heap->header.dataEnd - (uint32_t) heap->header.dataStart, totalSize);
return false;
}
return true;
}
bool CheckMemExpHeap(MEMExpHeap *heap) {
OSMemoryBarrier();
if (heap->header.tag != MEM_EXPANDED_HEAP_TAG) {
DEBUG_FUNCTION_LINE_ERR("[Exp Heap Check] Invalid heap handle. - %08X", heap->header.tag);
return false;
}
if (heap->header.flags & MEM_HEAP_FLAG_USE_LOCK) {
#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
OSUninterruptibleSpinLock_Acquire(&(heap->header).lock);
}
auto result = CheckMemExpHeapCore(heap);
if (heap->header.flags & MEM_HEAP_FLAG_USE_LOCK) {
#pragma GCC diagnostic ignored "-Waddress-of-packed-member"
OSUninterruptibleSpinLock_Release(&(heap->header).lock);
}
return result;
}
void Memory_CheckHeaps() {
if (!CheckMemExpHeap(reinterpret_cast<MEMExpHeap *>(gHeapHandle))) {
DEBUG_FUNCTION_LINE_ERR("MemoryMapping heap %p is corrupted.", gHeapHandle);
#ifdef DEBUG
OSFatal("MemoryMappingModule: Heap is corrupted");
#endif
}
}