wut/libraries/wutnewlib/wut_sbrk.c
James Benton d5effaaf88 coreinit: Cleanup memory heap functions.
Move files to mem*heap.h.
Use MEMHeapHandle everywhere to reduce need for unecessary casts.
Rename types to match the function names better (e.g. Frame -> Frm).
2018-05-30 13:13:37 +01:00

47 lines
1.0 KiB
C

#include "wut_newlib.h"
#include <coreinit/atomic.h>
#include <coreinit/memheap.h>
#include <coreinit/memexpheap.h>
static uint8_t *sHeapBase = NULL;
static uint32_t sHeapMaxSize = 0;
static volatile uint32_t sHeapSize = 0;
void *
__wut_sbrk_r(struct _reent *r,
ptrdiff_t incr)
{
uint32_t oldSize, newSize;
do {
oldSize = sHeapSize;
newSize = oldSize + incr;
if (newSize > sHeapMaxSize) {
r->_errno = ENOMEM;
return (void *)-1;
}
} while (!OSCompareAndSwapAtomic(&sHeapSize, oldSize, newSize));
return sHeapBase + oldSize;
}
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);
sHeapSize = 0;
}
void
__fini_wut_sbrk_heap()
{
MEMHeapHandle heap = MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2);
MEMFreeToExpHeap(heap, sHeapBase);
}