2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ogc/system.h>
|
|
|
|
|
2012-05-12 18:03:14 +02:00
|
|
|
#include "mem2.hpp"
|
|
|
|
#include "mem2alloc.hpp"
|
2012-08-05 15:48:15 +02:00
|
|
|
#include "gecko/gecko.h"
|
|
|
|
#include "loader/utils.h"
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
// Forbid the use of MEM2 through malloc
|
|
|
|
u32 MALLOC_MEM2 = 0;
|
2012-05-12 18:03:14 +02:00
|
|
|
|
2012-01-21 21:57:41 +01:00
|
|
|
static CMEM2Alloc g_mem2gp;
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
extern __typeof(malloc) __real_malloc;
|
|
|
|
extern __typeof(calloc) __real_calloc;
|
|
|
|
extern __typeof(realloc) __real_realloc;
|
|
|
|
extern __typeof(memalign) __real_memalign;
|
|
|
|
extern __typeof(free) __real_free;
|
|
|
|
extern __typeof(malloc_usable_size) __real_malloc_usable_size;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-08-09 21:50:59 +02:00
|
|
|
extern __typeof(_malloc_r) __real__malloc_r;
|
|
|
|
extern __typeof(_calloc_r) __real__calloc_r;
|
|
|
|
extern __typeof(_realloc_r) __real__realloc_r;
|
|
|
|
extern __typeof(_memalign_r) __real__memalign_r;
|
|
|
|
extern __typeof(_free_r) __real__free_r;
|
|
|
|
extern __typeof(_malloc_usable_size_r) __real__malloc_usable_size_r;
|
|
|
|
|
|
|
|
bool first_mem = true;
|
|
|
|
|
|
|
|
void DisableMEM1allocR()
|
|
|
|
{
|
|
|
|
first_mem = false;
|
|
|
|
}
|
|
|
|
|
2012-05-18 16:10:37 +02:00
|
|
|
void *MEM1_alloc(unsigned int s)
|
2012-05-07 21:57:37 +02:00
|
|
|
{
|
2012-06-07 02:34:47 +02:00
|
|
|
return __real_malloc(s);
|
2012-05-07 21:57:37 +02:00
|
|
|
}
|
|
|
|
|
2012-05-18 18:55:04 +02:00
|
|
|
void *MEM1_memalign(unsigned int a, unsigned int s)
|
|
|
|
{
|
2012-06-07 02:34:47 +02:00
|
|
|
return __real_memalign(a, s);
|
2012-05-18 18:55:04 +02:00
|
|
|
}
|
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
void *MEM1_realloc(void *p, unsigned int s)
|
|
|
|
{
|
2012-06-07 02:34:47 +02:00
|
|
|
return __real_realloc(p, s);
|
2012-05-07 21:45:25 +02:00
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
void MEM1_free(void *p)
|
|
|
|
{
|
2012-06-07 18:02:23 +02:00
|
|
|
__real_free(p);
|
2012-05-18 18:55:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int MEM1_freesize()
|
|
|
|
{
|
2012-06-07 02:34:47 +02:00
|
|
|
return SYS_GetArena1Size();
|
2012-05-18 18:55:04 +02:00
|
|
|
}
|
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
void MEM2_init(unsigned int mem2Size)
|
|
|
|
{
|
|
|
|
g_mem2gp.init(mem2Size);
|
2012-05-17 02:43:42 +02:00
|
|
|
g_mem2gp.clear();
|
2012-05-07 21:45:25 +02:00
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
void MEM2_cleanup(void)
|
|
|
|
{
|
|
|
|
g_mem2gp.cleanup();
|
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
void MEM2_clear(void)
|
|
|
|
{
|
|
|
|
g_mem2gp.clear();
|
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
void MEM2_free(void *p)
|
|
|
|
{
|
|
|
|
g_mem2gp.release(p);
|
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
void *MEM2_alloc(unsigned int s)
|
|
|
|
{
|
|
|
|
return g_mem2gp.allocate(s);
|
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-08-05 15:48:15 +02:00
|
|
|
/* Placeholder, will be needed with new memory manager */
|
|
|
|
void *MEM2_memalign(unsigned int /* alignment */, unsigned int s)
|
|
|
|
{
|
|
|
|
return MEM2_alloc(s);
|
|
|
|
}
|
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
void *MEM2_realloc(void *p, unsigned int s)
|
|
|
|
{
|
|
|
|
return g_mem2gp.reallocate(p, s);
|
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
unsigned int MEM2_usableSize(void *p)
|
|
|
|
{
|
2012-05-18 18:55:04 +02:00
|
|
|
return g_mem2gp.usableSize(p);
|
2012-05-07 21:45:25 +02:00
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
unsigned int MEM2_freesize()
|
|
|
|
{
|
|
|
|
return g_mem2gp.FreeSize();
|
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
void *__wrap_malloc(size_t size)
|
|
|
|
{
|
|
|
|
void *p;
|
2012-07-18 18:43:28 +02:00
|
|
|
if(SYS_GetArena1Lo() >= MAX_MEM1_ARENA_LO || size >= MEM2_PRIORITY_SIZE)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-05-16 16:48:01 +02:00
|
|
|
p = g_mem2gp.allocate(size);
|
2012-05-07 21:45:25 +02:00
|
|
|
if(p != 0)
|
|
|
|
return p;
|
|
|
|
return __real_malloc(size);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
2012-05-07 21:45:25 +02:00
|
|
|
p = __real_malloc(size);
|
|
|
|
if(p != 0)
|
|
|
|
return p;
|
2012-05-16 16:48:01 +02:00
|
|
|
return g_mem2gp.allocate(size);
|
2012-05-07 21:45:25 +02:00
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-08-09 21:50:59 +02:00
|
|
|
void *__wrap__malloc_r(struct _reent *r, size_t size)
|
|
|
|
{
|
|
|
|
if(first_mem)
|
|
|
|
return __real__malloc_r(r, size);
|
|
|
|
|
|
|
|
return MEM2_alloc(size);
|
|
|
|
}
|
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
void *__wrap_calloc(size_t n, size_t size)
|
|
|
|
{
|
|
|
|
void *p;
|
2012-07-18 18:43:28 +02:00
|
|
|
if(SYS_GetArena1Lo() >= MAX_MEM1_ARENA_LO || (n * size) >= MEM2_PRIORITY_SIZE)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-05-16 16:48:01 +02:00
|
|
|
p = g_mem2gp.allocate(n * size);
|
2012-05-07 21:45:25 +02:00
|
|
|
if (p != 0)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-05-07 21:45:25 +02:00
|
|
|
memset(p, 0, n * size);
|
|
|
|
return p;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
2012-05-07 21:45:25 +02:00
|
|
|
return __real_calloc(n, size);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
p = __real_calloc(n, size);
|
|
|
|
if (p != 0) return p;
|
|
|
|
|
2012-05-16 16:48:01 +02:00
|
|
|
p = g_mem2gp.allocate(n * size);
|
2012-05-07 21:45:25 +02:00
|
|
|
if (p != 0)
|
|
|
|
memset(p, 0, n * size);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2012-08-09 21:50:59 +02:00
|
|
|
void *__wrap__calloc_r(struct _reent *r, size_t n, size_t size)
|
|
|
|
{
|
|
|
|
if(first_mem)
|
|
|
|
return __real__calloc_r(r, n, size);
|
|
|
|
|
|
|
|
void *p = MEM2_alloc(n*size);
|
|
|
|
if(p)
|
|
|
|
memset(p, 0, n*size);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
void *__wrap_memalign(size_t a, size_t size)
|
|
|
|
{
|
|
|
|
void *p;
|
2012-07-18 18:43:28 +02:00
|
|
|
if(SYS_GetArena1Lo() >= MAX_MEM1_ARENA_LO || size >= MEM2_PRIORITY_SIZE)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-07-27 19:26:49 +02:00
|
|
|
if(a <= 32 && 32 % a == 0)
|
|
|
|
{
|
|
|
|
p = g_mem2gp.allocate(size);
|
|
|
|
if (p != 0)
|
|
|
|
return p;
|
|
|
|
}
|
2012-05-07 21:45:25 +02:00
|
|
|
return __real_memalign(a, size);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
2012-05-07 21:45:25 +02:00
|
|
|
p = __real_memalign(a, size);
|
2012-07-27 19:26:49 +02:00
|
|
|
if(p != 0 || a > 32 || 32 % a != 0)
|
2012-05-07 21:45:25 +02:00
|
|
|
return p;
|
2012-07-27 19:26:49 +02:00
|
|
|
|
|
|
|
return g_mem2gp.allocate(size);
|
2012-05-07 21:45:25 +02:00
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-08-09 21:50:59 +02:00
|
|
|
void *__wrap__memalign_r(struct _reent *r, size_t a, size_t size)
|
|
|
|
{
|
|
|
|
if(first_mem)
|
|
|
|
return __real__memalign_r(r, a, size);
|
|
|
|
|
|
|
|
return MEM2_alloc(size);
|
|
|
|
}
|
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
void __wrap_free(void *p)
|
|
|
|
{
|
|
|
|
if(!p)
|
|
|
|
return;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-05-19 14:29:24 +02:00
|
|
|
if(((u32)p & 0x10000000) != 0)
|
2012-05-07 21:45:25 +02:00
|
|
|
g_mem2gp.release(p);
|
|
|
|
else
|
|
|
|
__real_free(p);
|
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-08-09 21:50:59 +02:00
|
|
|
void __wrap__free_r(struct _reent *r, void *p)
|
|
|
|
{
|
|
|
|
if(first_mem)
|
|
|
|
__real__free_r(r, p);
|
|
|
|
else
|
|
|
|
MEM2_free(p);
|
|
|
|
}
|
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
void *__wrap_realloc(void *p, size_t size)
|
|
|
|
{
|
|
|
|
void *n;
|
|
|
|
// ptr from mem2
|
|
|
|
if (((u32)p & 0x10000000) != 0 || (p == 0 && size > MEM2_PRIORITY_SIZE))
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-05-16 16:48:01 +02:00
|
|
|
n = g_mem2gp.reallocate(p, size);
|
2012-05-07 21:45:25 +02:00
|
|
|
if (n != 0)
|
2012-01-21 21:57:41 +01:00
|
|
|
return n;
|
2012-05-07 21:45:25 +02:00
|
|
|
n = __real_malloc(size);
|
|
|
|
if (n == 0)
|
2012-01-21 21:57:41 +01:00
|
|
|
return 0;
|
|
|
|
if (p != 0)
|
|
|
|
{
|
2012-05-07 21:45:25 +02:00
|
|
|
memcpy(n, p, MEM2_usableSize(p) < size ? MEM2_usableSize(p) : size);
|
2012-05-16 16:48:01 +02:00
|
|
|
g_mem2gp.release(p);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
2012-05-07 21:45:25 +02:00
|
|
|
// ptr from malloc
|
|
|
|
n = __real_realloc(p, size);
|
|
|
|
if (n != 0)
|
|
|
|
return n;
|
2012-05-16 16:48:01 +02:00
|
|
|
n = g_mem2gp.allocate(size);
|
2012-05-07 21:45:25 +02:00
|
|
|
if (n == 0)
|
|
|
|
return 0;
|
|
|
|
if (p != 0)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-05-07 21:45:25 +02:00
|
|
|
memcpy(n, p, __real_malloc_usable_size(p) < size ? __real_malloc_usable_size(p) : size);
|
|
|
|
__real_free(p);
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
2012-05-07 21:45:25 +02:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2012-08-09 21:50:59 +02:00
|
|
|
void *__wrap__realloc_r(struct _reent *r, void *p, size_t size)
|
|
|
|
{
|
|
|
|
if(first_mem)
|
|
|
|
return __real__realloc_r(r, p, size);
|
|
|
|
|
|
|
|
return MEM2_realloc(p, size);
|
|
|
|
}
|
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
size_t __wrap_malloc_usable_size(void *p)
|
|
|
|
{
|
|
|
|
if(((u32)p & 0x10000000) != 0)
|
2012-05-16 16:48:01 +02:00
|
|
|
return CMEM2Alloc::usableSize(p);
|
2012-05-07 21:45:25 +02:00
|
|
|
return __real_malloc_usable_size(p);
|
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-08-09 21:50:59 +02:00
|
|
|
size_t __wrap__malloc_usable_size_r(struct _reent *r, void *p)
|
|
|
|
{
|
|
|
|
if(first_mem)
|
|
|
|
return __real__malloc_usable_size_r(r, p);
|
|
|
|
|
|
|
|
return CMEM2Alloc::usableSize(p);
|
|
|
|
}
|
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
} ///extern "C"
|