2016-07-21 07:45:53 +02:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <coreinit/baseheap.h>
|
2016-10-11 21:01:53 +02:00
|
|
|
#include <coreinit/memheap.h>
|
2016-07-21 07:45:53 +02:00
|
|
|
#include <coreinit/expandedheap.h>
|
|
|
|
|
2016-07-23 12:23:32 +02:00
|
|
|
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);
|
2016-07-21 07:45:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-23 12:23:32 +02:00
|
|
|
void *
|
2016-07-24 02:57:04 +02:00
|
|
|
__wrap_malloc(size_t size) {
|
|
|
|
return __wrap_memalign(4, size);
|
2016-07-21 07:45:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-23 12:23:32 +02:00
|
|
|
void
|
2016-07-24 02:57:04 +02:00
|
|
|
__wrap_free(void *ptr) {
|
|
|
|
if (ptr) {
|
|
|
|
MEMFreeToExpHeap(MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2), ptr);
|
2016-07-23 12:23:32 +02:00
|
|
|
}
|
2016-07-21 07:45:53 +02:00
|
|
|
}
|
|
|
|
|
2016-08-05 21:10:19 +02:00
|
|
|
size_t
|
|
|
|
__wrap_malloc_usable_size(void *ptr) {
|
|
|
|
return MEMGetSizeForMBlockExpHeap(ptr);
|
|
|
|
}
|
|
|
|
|
2016-07-23 12:23:32 +02:00
|
|
|
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-07-23 12:23:32 +02:00
|
|
|
}
|
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-23 12:23:32 +02:00
|
|
|
|
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;
|
2016-07-21 07:45:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-23 12:23:32 +02:00
|
|
|
void *
|
2016-07-24 02:57:04 +02:00
|
|
|
__wrap_valloc(size_t size) {
|
|
|
|
return __wrap_memalign(64, size);
|
|
|
|
}
|
|
|
|
|
2016-07-23 12:23:32 +02:00
|
|
|
|
|
|
|
|
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);
|
2016-07-21 07:45:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-23 12:23:32 +02:00
|
|
|
void *
|
2016-07-24 02:57:04 +02:00
|
|
|
__wrap__malloc_r(struct _reent *r, size_t size) {
|
2016-07-23 12:23:32 +02:00
|
|
|
return __wrap_malloc(size);
|
2016-07-21 07:45:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-24 02:57:04 +02:00
|
|
|
void
|
|
|
|
__wrap__free_r(struct _reent *r, void *ptr) {
|
|
|
|
return __wrap_free(ptr);
|
2016-07-21 07:45:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-23 12:23:32 +02:00
|
|
|
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-21 07:45:53 +02:00
|
|
|
}
|
|
|
|
|
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-21 07:45:53 +02:00
|
|
|
}
|
|
|
|
|
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-21 07:45:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-24 02:57:04 +02:00
|
|
|
void *
|
|
|
|
__wrap__valloc_r(struct _reent *r, size_t size) {
|
|
|
|
return __wrap_valloc(size);
|
2016-07-21 07:45:53 +02:00
|
|
|
}
|