diff --git a/relocator/src/imports.h b/relocator/src/imports.h index a577723..98de8c4 100644 --- a/relocator/src/imports.h +++ b/relocator/src/imports.h @@ -6,6 +6,7 @@ IMPORT(OSDynLoad_FindExport); IMPORT(DCFlushRange); IMPORT(ICInvalidateRange); IMPORT(OSGetSystemInfo); +IMPORT(MEMGetSizeForMBlockExpHeap); IMPORT(OSSleepTicks); IMPORT(OSFatal); diff --git a/relocator/src/memory.c b/relocator/src/memory.c new file mode 100644 index 0000000..214726d --- /dev/null +++ b/relocator/src/memory.c @@ -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 . + ****************************************************************************/ +#include +#include +#include +#include +#include +#include + +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; +} diff --git a/relocator/src/memory.cpp b/relocator/src/memory.cpp deleted file mode 100644 index bd21201..0000000 --- a/relocator/src/memory.cpp +++ /dev/null @@ -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 . - ****************************************************************************/ -#include -#include -#include - -#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); -} diff --git a/relocator/src/memory.h b/relocator/src/memory.h new file mode 100644 index 0000000..db67a05 --- /dev/null +++ b/relocator/src/memory.h @@ -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 . + ****************************************************************************/ +#ifndef __MEMORY_H_ +#define __MEMORY_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +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_