wut/crt/memory.c

103 lines
1.9 KiB
C
Raw Normal View History

#include <malloc.h>
#include <string.h>
#include <coreinit/baseheap.h>
2016-10-11 21:01:53 +02:00
#include <coreinit/memheap.h>
#include <coreinit/expandedheap.h>
void *
2016-07-24 02:57:04 +02:00
__wrap_memalign(size_t alignment, size_t size) {
return MEMAllocFromExpHeapEx(MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2), size, alignment);
}
void *
2016-07-24 02:57:04 +02:00
__wrap_malloc(size_t size) {
return __wrap_memalign(4, size);
}
void
2016-07-24 02:57:04 +02:00
__wrap_free(void *ptr) {
if (ptr) {
MEMFreeToExpHeap(MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2), ptr);
}
}
2016-08-05 21:10:19 +02:00
size_t
__wrap_malloc_usable_size(void *ptr) {
return MEMGetSizeForMBlockExpHeap(ptr);
}
void *
2016-07-24 02:57:04 +02:00
__wrap_realloc(void *ptr, size_t size) {
2016-08-05 21:10:19 +02:00
if (!ptr) {
return __wrap_malloc(size);
}
2016-10-11 21:01:53 +02:00
2016-08-05 21:10:19 +02:00
if (__wrap_malloc_usable_size(ptr) >= size) {
return ptr;
}
2016-10-11 21:01:53 +02:00
2016-07-24 02:57:04 +02:00
void *realloc_ptr = __wrap_malloc(size);
2016-10-11 21:01:53 +02:00
2016-08-05 21:10:19 +02:00
if(!realloc_ptr) {
return NULL;
}
2016-10-11 21:01:53 +02:00
2016-08-05 21:10:19 +02:00
memcpy(realloc_ptr, ptr, __wrap_malloc_usable_size(ptr));
__wrap_free(ptr);
2016-10-11 21:01:53 +02:00
2016-07-24 02:57:04 +02:00
return realloc_ptr;
}
2016-07-24 02:57:04 +02:00
void *
__wrap_calloc(size_t num, size_t size) {
void *ptr = __wrap_malloc(num*size);
2016-10-11 21:01:53 +02:00
2016-07-24 02:57:04 +02:00
if(ptr) {
memset(ptr, 0, num*size);
}
2016-10-11 21:01:53 +02:00
2016-07-24 02:57:04 +02:00
return ptr;
}
void *
2016-07-24 02:57:04 +02:00
__wrap_valloc(size_t size) {
return __wrap_memalign(64, size);
}
2016-07-24 02:57:04 +02:00
void *
__wrap__memalign_r(struct _reent *r, size_t alignment, size_t size) {
return __wrap_memalign(alignment, size);
}
void *
2016-07-24 02:57:04 +02:00
__wrap__malloc_r(struct _reent *r, size_t size) {
return __wrap_malloc(size);
}
2016-07-24 02:57:04 +02:00
void
__wrap__free_r(struct _reent *r, void *ptr) {
return __wrap_free(ptr);
}
void *
2016-07-24 02:57:04 +02:00
__wrap__realloc_r(struct _reent *r, void *ptr, size_t size) {
return __wrap_realloc(ptr, size);
}
2016-07-24 02:57:04 +02:00
void *
__wrap__calloc_r(struct _reent *r, size_t num, size_t size) {
return __wrap_calloc(num, size);
}
2016-07-24 02:57:04 +02:00
size_t
__wrap__malloc_usable_size_r(struct _reent *r, void *ptr) {
return __wrap_malloc_usable_size(ptr);
}
2016-07-24 02:57:04 +02:00
void *
__wrap__valloc_r(struct _reent *r, size_t size) {
return __wrap_valloc(size);
}