2020-04-29 12:04:33 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* 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/>.
|
|
|
|
****************************************************************************/
|
2022-02-04 21:44:03 +01:00
|
|
|
#include "logger.h"
|
|
|
|
#include <coreinit/cache.h>
|
2020-04-29 12:04:33 +02:00
|
|
|
#include <coreinit/memdefaultheap.h>
|
2022-02-04 21:44:03 +01:00
|
|
|
#include <coreinit/memexpheap.h>
|
2020-04-29 12:04:33 +02:00
|
|
|
#include <coreinit/memorymap.h>
|
2022-02-04 21:44:03 +01:00
|
|
|
#include <errno.h>
|
2020-04-29 12:04:33 +02:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-02-19 10:05:02 +01:00
|
|
|
extern MEMHeapHandle gHeapHandle;
|
|
|
|
|
|
|
|
void *MEMAllocSafe(uint32_t size, uint32_t align) {
|
2022-02-04 21:44:03 +01:00
|
|
|
void *res = nullptr;
|
2021-02-19 10:05:02 +01:00
|
|
|
MEMHeapHandle heapHandle = gHeapHandle;
|
2022-02-04 21:44:03 +01:00
|
|
|
res = MEMAllocFromExpHeapEx(heapHandle, size, align);
|
2021-02-19 10:05:02 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *MemoryAlloc(uint32_t size) {
|
|
|
|
void *res = MEMAllocSafe(size, 4);
|
|
|
|
if (res == nullptr) {
|
|
|
|
OSFatal_printf("Failed to MemoryAlloc %d", size);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *MemoryAllocEx(uint32_t size, uint32_t align) {
|
|
|
|
void *res = MEMAllocSafe(size, align);
|
|
|
|
if (res == nullptr) {
|
|
|
|
OSFatal_printf("Failed to MemoryAllocEX %d %d", size, align);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryFree(void *ptr) {
|
|
|
|
if (ptr) {
|
|
|
|
MEMFreeToExpHeap(gHeapHandle, ptr);
|
|
|
|
} else {
|
|
|
|
OSFatal_printf("Failed to free");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 21:44:03 +01:00
|
|
|
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;
|
2020-04-29 12:04:33 +02:00
|
|
|
|
|
|
|
//!-------------------------------------------------------------------------------------------
|
|
|
|
//! reent versions
|
|
|
|
//!-------------------------------------------------------------------------------------------
|
|
|
|
void *_malloc_r(struct _reent *r, size_t size) {
|
2021-02-19 10:05:02 +01:00
|
|
|
void *ptr = MemoryAllocEx(size, 4);
|
2020-04-29 12:04:33 +02:00
|
|
|
if (!ptr) {
|
|
|
|
r->_errno = ENOMEM;
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *_calloc_r(struct _reent *r, size_t num, size_t size) {
|
2021-02-19 10:05:02 +01:00
|
|
|
void *ptr = MemoryAllocEx(num * size, 4);
|
2020-04-29 12:04:33 +02:00
|
|
|
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) {
|
2021-02-19 10:05:02 +01:00
|
|
|
return MemoryAllocEx(size, align);
|
2020-04-29 12:04:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void _free_r(struct _reent *r, void *ptr) {
|
|
|
|
if (ptr) {
|
2021-02-19 10:05:02 +01:00
|
|
|
MemoryFree(ptr);
|
2020-04-29 12:04:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *_realloc_r(struct _reent *r, void *p, size_t size) {
|
2021-02-19 10:05:02 +01:00
|
|
|
void *new_ptr = MemoryAllocEx(size, 4);
|
2020-04-29 12:04:33 +02:00
|
|
|
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);
|
2021-02-19 10:05:02 +01:00
|
|
|
MemoryFree(p);
|
2020-04-29 12:04:33 +02:00
|
|
|
}
|
|
|
|
return new_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mallinfo _mallinfo_r(struct _reent *r) {
|
2020-05-17 19:05:51 +02:00
|
|
|
struct mallinfo info = {0};
|
2020-04-29 12:04:33 +02:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2022-02-04 21:44:03 +01:00
|
|
|
void _malloc_stats_r(struct _reent *r) {
|
2020-04-29 12:04:33 +02:00
|
|
|
}
|
|
|
|
|
2022-02-04 21:44:03 +01:00
|
|
|
int _mallopt_r(struct _reent *r, int param, int value) {
|
2020-04-29 12:04:33 +02:00
|
|
|
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) {
|
2021-02-19 10:05:02 +01:00
|
|
|
return MemoryAllocEx(size, OS_PAGE_SIZE);
|
2020-04-29 12:04:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
_pvalloc_r(struct _reent *r, size_t size) {
|
2021-02-19 10:05:02 +01:00
|
|
|
return MemoryAllocEx((size + (OS_PAGE_SIZE - 1)) & ~(OS_PAGE_SIZE - 1), OS_PAGE_SIZE);
|
2020-04-29 12:04:33 +02:00
|
|
|
}
|
|
|
|
|
2022-02-04 21:44:03 +01:00
|
|
|
int _malloc_trim_r(struct _reent *r, size_t pad) {
|
2020-04-29 12:04:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|