Fix a softlock by implementing the memory functions properly

This commit is contained in:
Maschell 2020-04-29 12:04:33 +02:00
parent 06cb0efe34
commit 13e31e31f3
4 changed files with 150 additions and 114 deletions

View File

@ -6,6 +6,7 @@ IMPORT(OSDynLoad_FindExport);
IMPORT(DCFlushRange);
IMPORT(ICInvalidateRange);
IMPORT(OSGetSystemInfo);
IMPORT(MEMGetSizeForMBlockExpHeap);
IMPORT(OSSleepTicks);
IMPORT(OSFatal);

107
relocator/src/memory.c Normal file
View File

@ -0,0 +1,107 @@
/****************************************************************************
* 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 <coreinit/memexpheap.h>
#include <coreinit/memdefaultheap.h>
#include <coreinit/memorymap.h>
#include <malloc.h>
#include <string.h>
#include <errno.h>
extern uint32_t * pMEMAllocFromDefaultHeapEx;
extern uint32_t * pMEMAllocFromDefaultHeap;
extern uint32_t * pMEMFreeToDefaultHeap;
//!-------------------------------------------------------------------------------------------
//! reent versions
//!-------------------------------------------------------------------------------------------
void *_malloc_r(struct _reent *r, size_t size) {
void *ptr = ((void * (*)(size_t))(*pMEMAllocFromDefaultHeap))(size);
if (!ptr) {
r->_errno = ENOMEM;
}
return ptr;
}
void *_calloc_r(struct _reent *r, size_t num, size_t size) {
void *ptr = ((void * (*)(size_t))(*pMEMAllocFromDefaultHeap))(size);
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 ((void * (*)(size_t, size_t))(*pMEMAllocFromDefaultHeapEx))(size, align);
}
void _free_r(struct _reent *r, void *ptr) {
if (ptr) {
((void (*)(void *))(*pMEMFreeToDefaultHeap))(ptr);
}
}
void *_realloc_r(struct _reent *r, void *p, size_t size) {
void *new_ptr = ((void * (*)(size_t))(*pMEMAllocFromDefaultHeap))(size);
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);
((void (*)(void *))(*pMEMFreeToDefaultHeap))(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 ((void * (*)(size_t, size_t))(*pMEMAllocFromDefaultHeapEx))(size, OS_PAGE_SIZE);
}
void *
_pvalloc_r(struct _reent *r, size_t size) {
return ((void * (*)(size_t, size_t))(*pMEMAllocFromDefaultHeapEx))((size + (OS_PAGE_SIZE - 1)) & ~(OS_PAGE_SIZE - 1), OS_PAGE_SIZE);
}
int
_malloc_trim_r(struct _reent *r, size_t pad) {
return 0;
}

View File

@ -1,114 +0,0 @@
/****************************************************************************
* 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 <malloc.h>
#include <string.h>
#include <stdint.h>
#include "memory.h"
//!----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//! Memory functions
//! This is the only place where those are needed so lets keep them more or less private
//!----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
extern uint32_t * pMEMAllocFromDefaultHeapEx;
extern uint32_t * pMEMAllocFromDefaultHeap;
extern uint32_t * pMEMFreeToDefaultHeap;
//!-------------------------------------------------------------------------------------------
//! wraps
//!-------------------------------------------------------------------------------------------
extern "C" void *__wrap_malloc(size_t size) {
return ((void * (*)(size_t))(*pMEMAllocFromDefaultHeap))(size);
}
extern "C" void *__wrap_memalign(size_t align, size_t size) {
if (align < 4){
align = 4;
}
return ((void * (*)(size_t, size_t))(*pMEMAllocFromDefaultHeapEx))(size, align);
}
extern "C" void __wrap_free(void *p) {
if(p != 0) {
((void (*)(void *))(*pMEMFreeToDefaultHeap))(p);
}
return;
}
extern "C" void *__wrap_calloc(size_t n, size_t size) {
void *p = __wrap_malloc(n * size);
if (p != 0) {
memset(p, 0, n * size);
}
return p;
}
extern "C" size_t __wrap_malloc_usable_size(void *p) {
//! TODO: this is totally wrong and needs to be addressed
return 0x7FFFFFFF;
}
extern "C" void *__wrap_realloc(void *ptr, size_t size) {
void *newPtr;
if (!ptr) {
newPtr = __wrap_malloc(size);
if (!newPtr) {
goto error;
}
} else {
newPtr = __wrap_malloc(size);
if (!newPtr) {
goto error;
}
memcpy(newPtr, ptr, size);
__wrap_free(ptr);
}
return newPtr;
error:
return NULL;
}
//!-------------------------------------------------------------------------------------------
//! reent versions
//!-------------------------------------------------------------------------------------------
extern "C" void *__wrap__malloc_r(struct _reent *r, size_t size) {
return __wrap_malloc(size);
}
extern "C" void *__wrap__calloc_r(struct _reent *r, size_t n, size_t size) {
return __wrap_calloc(n, size);
}
extern "C" void *__wrap__memalign_r(struct _reent *r, size_t align, size_t size) {
return __wrap_memalign(align, size);
}
extern "C" void __wrap__free_r(struct _reent *r, void *p) {
__wrap_free(p);
}
extern "C" size_t __wrap__malloc_usable_size_r(struct _reent *r, void *p) {
return __wrap_malloc_usable_size(p);
}
extern "C" void *__wrap__realloc_r(struct _reent *r, void *p, size_t size) {
return __wrap_realloc(p, size);
}

42
relocator/src/memory.h Normal file
View File

@ -0,0 +1,42 @@
/****************************************************************************
* 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/>.
****************************************************************************/
#ifndef __MEMORY_H_
#define __MEMORY_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <malloc.h>
void memoryInitialize(void);
void memoryRelease(void);
void * MEM2_alloc(u32 size, u32 align);
void MEM2_free(void *ptr);
void * MEM1_alloc(u32 size, u32 align);
void MEM1_free(void *ptr);
void * MEMBucket_alloc(u32 size, u32 align);
void MEMBucket_free(void *ptr);
#ifdef __cplusplus
}
#endif
#endif // __MEMORY_H_