Files
Upsilon/liba/src/malloc.c
Romain Goyet e3c6264068 [ion] Remove _liba_heap_start, simply use _heap_start
Change-Id: I395762275f3aa1832210dfc3faae9c0eefb91d9e
2016-09-21 13:50:58 +02:00

48 lines
997 B
C

#include <stdlib.h>
#include <string.h>
#include <private/memconfig.h>
extern char _heap_start;
extern char _heap_end;
heap_config_t HeapConfig = {
.nHeap = 0
};
// Memsys headers cannot be included easily so we rewrite them here
int memsys5Init(void *NotUsed);
void memsys5FreeUnsafe(void *pOld);
void * memsys5MallocUnsafe(int nByte);
void * memsys5Realloc(void *pPrior, int nBytes);
int memsys5Roundup(int n);
static void configure_heap() {
HeapConfig.nHeap = (&_heap_end - &_heap_start);
HeapConfig.pHeap = &_heap_start;
HeapConfig.mnReq = 1;
HeapConfig.bMemstat = 0;
HeapConfig.xLog = 0;
memsys5Init(0);
}
void free(void *ptr) {
if (ptr != NULL) {
memsys5FreeUnsafe(ptr);
}
}
void * malloc(size_t size) {
void * p = NULL;
if (HeapConfig.nHeap == 0) {
configure_heap();
}
if (size > 0) {
p = memsys5MallocUnsafe(memsys5Roundup(size));
}
return p;
}
void * realloc(void *ptr, size_t size) {
return memsys5Realloc(ptr, memsys5Roundup(size));
}