mirror of
https://github.com/wiiu-env/wut.git
synced 2025-01-06 16:28:14 +01:00
Change wutnewlib to implement malloc family of functions.
This allows us to forward them straight to default heap functions, this is better than having to allocate a fixed heap for sbrk as it allows people to better mix usage malloc & default heap functions.
This commit is contained in:
parent
c4214cef35
commit
1471ea4338
@ -5,6 +5,7 @@ add_library(wutnewlib
|
||||
wut_clock.c
|
||||
wut_gettod_r.c
|
||||
wut_lock.c
|
||||
wut_malloc.c
|
||||
wut_malloc_lock.c
|
||||
wut_nanosleep.c
|
||||
wut_newlib.c
|
||||
|
88
libraries/wutnewlib/wut_malloc.c
Normal file
88
libraries/wutnewlib/wut_malloc.c
Normal file
@ -0,0 +1,88 @@
|
||||
#include <coreinit/memexpheap.h>
|
||||
#include <coreinit/memdefaultheap.h>
|
||||
#include <coreinit/memorymap.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
|
||||
void *
|
||||
_malloc_r(struct _reent *r, size_t size)
|
||||
{
|
||||
return MEMAllocFromDefaultHeap(size);
|
||||
}
|
||||
|
||||
void
|
||||
_free_r(struct _reent *r, void *ptr)
|
||||
{
|
||||
MEMFreeToDefaultHeap(ptr);
|
||||
}
|
||||
|
||||
void *
|
||||
_realloc_r(struct _reent *r, void *ptr, size_t size)
|
||||
{
|
||||
void *new_ptr = _malloc_r(r, size);
|
||||
if (!ptr || !new_ptr) {
|
||||
return new_ptr;
|
||||
}
|
||||
|
||||
memcpy(new_ptr, ptr, MEMGetSizeForMBlockExpHeap(ptr));
|
||||
_free_r(r, ptr);
|
||||
return new_ptr;
|
||||
}
|
||||
|
||||
void *
|
||||
_calloc_r(struct _reent *r, size_t num, size_t size)
|
||||
{
|
||||
void *ptr = _malloc_r(r, num * size);
|
||||
if (ptr) {
|
||||
memset(ptr, 0, num * size);
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void *
|
||||
_memalign_r(struct _reent *r, size_t align, size_t size)
|
||||
{
|
||||
return MEMAllocFromDefaultHeapEx(size, align);
|
||||
}
|
||||
|
||||
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 _memalign_r(r, OS_PAGE_SIZE, size);
|
||||
}
|
||||
|
||||
void *
|
||||
_pvalloc_r(struct _reent *r, size_t size)
|
||||
{
|
||||
return _memalign_r(r, OS_PAGE_SIZE, (size + (OS_PAGE_SIZE - 1)) & ~(OS_PAGE_SIZE - 1));
|
||||
}
|
||||
|
||||
int
|
||||
_malloc_trim_r(struct _reent *r, size_t pad)
|
||||
{
|
||||
return 0;
|
||||
}
|
@ -1,11 +1,10 @@
|
||||
#include "wut_newlib.h"
|
||||
|
||||
#include <coreinit/atomic.h>
|
||||
#include <coreinit/memheap.h>
|
||||
#include <coreinit/memexpheap.h>
|
||||
#include <coreinit/memdefaultheap.h>
|
||||
|
||||
static uint8_t *sHeapBase = NULL;
|
||||
static uint32_t sHeapMaxSize = 0;
|
||||
static uint32_t sHeapMaxSize = 128 * 1024;
|
||||
static volatile uint32_t sHeapSize = 0;
|
||||
|
||||
void *
|
||||
@ -30,17 +29,16 @@ __wut_sbrk_r(struct _reent *r,
|
||||
void
|
||||
__init_wut_sbrk_heap()
|
||||
{
|
||||
MEMHeapHandle heap = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2);
|
||||
uint32_t freeSize = MEMGetAllocatableSizeForExpHeapEx(heap, 0x1000);
|
||||
|
||||
sHeapMaxSize = (uint32_t)(0.9f * (float)freeSize) & ~0xFFF;
|
||||
sHeapBase = (uint8_t *)MEMAllocFromExpHeapEx(heap, sHeapMaxSize, 0x1000);
|
||||
sHeapMaxSize = 128 * 1024;
|
||||
sHeapBase = (uint8_t *)MEMAllocFromDefaultHeap(sHeapMaxSize);
|
||||
sHeapSize = 0;
|
||||
}
|
||||
|
||||
void
|
||||
__fini_wut_sbrk_heap()
|
||||
{
|
||||
MEMHeapHandle heap = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2);
|
||||
MEMFreeToExpHeap(heap, sHeapBase);
|
||||
MEMFreeToDefaultHeap(sHeapBase);
|
||||
sHeapBase = NULL;
|
||||
sHeapSize = 0;
|
||||
sHeapMaxSize = 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user