2018-05-28 12:40:22 +02:00
|
|
|
#include "wut_newlib.h"
|
|
|
|
|
|
|
|
#include <coreinit/atomic.h>
|
2018-06-13 19:38:53 +02:00
|
|
|
#include <coreinit/memdefaultheap.h>
|
2018-05-28 12:40:22 +02:00
|
|
|
|
|
|
|
static uint8_t *sHeapBase = NULL;
|
2018-06-13 19:38:53 +02:00
|
|
|
static uint32_t sHeapMaxSize = 128 * 1024;
|
2018-05-28 12:40:22 +02:00
|
|
|
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()
|
|
|
|
{
|
2018-06-13 19:38:53 +02:00
|
|
|
sHeapMaxSize = 128 * 1024;
|
|
|
|
sHeapBase = (uint8_t *)MEMAllocFromDefaultHeap(sHeapMaxSize);
|
2018-05-28 12:40:22 +02:00
|
|
|
sHeapSize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
__fini_wut_sbrk_heap()
|
|
|
|
{
|
2018-06-13 19:38:53 +02:00
|
|
|
MEMFreeToDefaultHeap(sHeapBase);
|
|
|
|
sHeapBase = NULL;
|
|
|
|
sHeapSize = 0;
|
|
|
|
sHeapMaxSize = 0;
|
2018-05-28 12:40:22 +02:00
|
|
|
}
|