2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ogc/system.h>
|
2013-12-21 18:02:36 +01:00
|
|
|
#include <ogc/machine/processor.h>
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-05-12 18:03:14 +02:00
|
|
|
#include "mem2.hpp"
|
2013-12-21 18:02:36 +01:00
|
|
|
#include "mem_manager.hpp"
|
2012-12-08 17:17:35 +01:00
|
|
|
#include "gecko/gecko.hpp"
|
2012-08-05 15:48:15 +02:00
|
|
|
#include "loader/utils.h"
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2020-06-26 01:11:58 +02:00
|
|
|
static const u32 MEM2_PRIORITY_SIZE = 0x1000;// 4k or 4096
|
2012-09-07 20:13:04 +02:00
|
|
|
|
2020-06-26 01:11:58 +02:00
|
|
|
// Forbid the use of MEM2 through malloc by setting MALLOC_MEM2 = 0; if not 0 then malloc can use mem2
|
|
|
|
// calls to malloc, memalign, calloc, free are handled below by the wrappers which decide whether to use mem1 or mem2.
|
2012-01-21 21:57:41 +01:00
|
|
|
u32 MALLOC_MEM2 = 0;
|
2012-05-12 18:03:14 +02:00
|
|
|
|
2013-12-21 18:02:36 +01:00
|
|
|
u8 *MEM1_lo_start = (u8*)0x80004000;
|
2014-03-01 00:11:02 +01:00
|
|
|
u8 *MEM1_lo_end = (u8*)0x8061ff00;
|
2014-03-17 18:38:32 +01:00
|
|
|
u8 *MEM1_lo_list = (u8*)0x90080800;
|
2012-09-07 20:13:04 +02:00
|
|
|
|
2014-03-01 00:11:02 +01:00
|
|
|
u8 *MEM2_lo_start = (u8*)0x90200000;
|
|
|
|
u8 *MEM2_lo_end = (u8*)0x905fff00;
|
2014-03-17 18:38:32 +01:00
|
|
|
u8 *MEM2_lo_list = (u8*)0x900a0800;
|
2014-03-01 00:11:02 +01:00
|
|
|
|
|
|
|
u8 *MEM2_start = (u8*)0x90600000;
|
2014-03-17 18:38:32 +01:00
|
|
|
u8 *MEM2_end = (u8*)0x932fff00;
|
|
|
|
u8 *MEM2_list = (u8*)0x90000800;
|
2012-09-07 20:13:04 +02:00
|
|
|
|
2014-03-17 18:38:32 +01:00
|
|
|
static MemManager g_mem1lo;
|
|
|
|
static MemManager g_mem2lo;
|
|
|
|
static MemManager g_mem2gp;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
|
|
|
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-09-07 20:13:04 +02:00
|
|
|
void MEM_init()
|
|
|
|
{
|
2013-12-21 18:02:36 +01:00
|
|
|
g_mem1lo.Init(MEM1_lo_start, MEM1_lo_list, (u32)(MEM1_lo_end-MEM1_lo_start)); //about 6mb
|
|
|
|
g_mem1lo.ClearMem();
|
|
|
|
|
2014-03-01 00:11:02 +01:00
|
|
|
g_mem2lo.Init(MEM2_lo_start, MEM2_lo_list, (u32)(MEM2_lo_end-MEM2_lo_start)); //about 4mb
|
|
|
|
g_mem2lo.ClearMem();
|
|
|
|
|
2014-03-17 18:38:32 +01:00
|
|
|
g_mem2gp.Init(MEM2_start, MEM2_list, (u32)(MEM2_end-MEM2_start)); //about 45mb
|
2013-12-21 18:02:36 +01:00
|
|
|
g_mem2gp.ClearMem();
|
2012-09-07 20:13:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void *MEM1_lo_alloc(unsigned int s)
|
|
|
|
{
|
2013-12-21 18:02:36 +01:00
|
|
|
return g_mem1lo.Alloc(s);
|
2012-09-07 20:13:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MEM1_lo_free(void *p)
|
|
|
|
{
|
|
|
|
if(!p)
|
|
|
|
return;
|
2013-12-21 18:02:36 +01:00
|
|
|
g_mem1lo.Free(p);
|
2012-09-07 20:13:04 +02:00
|
|
|
}
|
|
|
|
|
2013-01-21 00:30:28 +01:00
|
|
|
unsigned int MEM1_lo_freesize()
|
|
|
|
{
|
|
|
|
return g_mem1lo.FreeSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-18 16:10:37 +02:00
|
|
|
void *MEM1_alloc(unsigned int s)
|
2012-05-07 21:57:37 +02:00
|
|
|
{
|
2012-09-23 14:10:25 +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-09-23 14:10:25 +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-09-07 20:13:04 +02:00
|
|
|
if(!p)
|
|
|
|
return;
|
2012-09-23 14:10:25 +02:00
|
|
|
__real_free(p);
|
2012-05-18 18:55:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int MEM1_freesize()
|
|
|
|
{
|
2013-01-21 00:30:28 +01:00
|
|
|
return SYS_GetArena1Size();
|
2012-05-18 18:55:04 +02:00
|
|
|
}
|
|
|
|
|
2013-01-19 17:09:56 +01:00
|
|
|
|
2014-03-01 00:11:02 +01:00
|
|
|
void MEM2_lo_free(void *p)
|
|
|
|
{
|
|
|
|
if(!p)
|
|
|
|
return;
|
|
|
|
g_mem2lo.Free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *MEM2_lo_alloc(unsigned int s)
|
|
|
|
{
|
|
|
|
return g_mem2lo.Alloc(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
void MEM2_free(void *p)
|
|
|
|
{
|
2012-09-07 20:13:04 +02:00
|
|
|
if(!p)
|
|
|
|
return;
|
2013-12-21 18:02:36 +01:00
|
|
|
g_mem2gp.Free(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
|
|
|
void *MEM2_alloc(unsigned int s)
|
|
|
|
{
|
2013-12-21 18:02:36 +01:00
|
|
|
return g_mem2gp.Alloc(s);
|
2012-05-07 21:45:25 +02:00
|
|
|
}
|
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)
|
|
|
|
{
|
2013-12-21 18:02:36 +01:00
|
|
|
return g_mem2gp.Alloc(s);
|
2012-08-05 15:48:15 +02:00
|
|
|
}
|
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
void *MEM2_realloc(void *p, unsigned int s)
|
|
|
|
{
|
2013-12-21 18:02:36 +01:00
|
|
|
return g_mem2gp.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
|
|
|
unsigned int MEM2_usableSize(void *p)
|
|
|
|
{
|
2013-12-21 18:02:36 +01:00
|
|
|
return g_mem2gp.MemBlockSize(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
|
|
|
|
2020-06-26 01:11:58 +02:00
|
|
|
// these wrap's are used when malloc, calloc, memalign, free are called in wiiflow.
|
|
|
|
// they decide based on if the size >= mem2_prio_size (4k) to use mem2 or mem1. using the __real means to use mem1 because MALLOC_MEM2 = 0.
|
2012-05-07 21:45:25 +02:00
|
|
|
void *__wrap_malloc(size_t size)
|
|
|
|
{
|
|
|
|
void *p;
|
2012-09-07 20:13:04 +02:00
|
|
|
if(size >= MEM2_PRIORITY_SIZE)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2013-12-21 18:02:36 +01:00
|
|
|
p = g_mem2gp.Alloc(size);
|
2012-05-07 21:45:25 +02:00
|
|
|
if(p != 0)
|
2012-09-07 20:13:04 +02:00
|
|
|
return p;
|
2012-05-07 21:45:25 +02:00
|
|
|
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;
|
2013-12-21 18:02:36 +01:00
|
|
|
return g_mem2gp.Alloc(size);
|
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 *__wrap_calloc(size_t n, size_t size)
|
|
|
|
{
|
|
|
|
void *p;
|
2012-09-07 20:13:04 +02:00
|
|
|
if((n * size) >= MEM2_PRIORITY_SIZE)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2013-12-21 18:02:36 +01:00
|
|
|
p = g_mem2gp.Alloc(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;
|
|
|
|
|
2013-12-21 18:02:36 +01:00
|
|
|
p = g_mem2gp.Alloc(n * size);
|
2012-05-07 21:45:25 +02:00
|
|
|
if (p != 0)
|
|
|
|
memset(p, 0, n * size);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *__wrap_memalign(size_t a, size_t size)
|
|
|
|
{
|
|
|
|
void *p;
|
2012-09-07 20:13:04 +02:00
|
|
|
if(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)
|
|
|
|
{
|
2013-12-21 18:02:36 +01:00
|
|
|
p = g_mem2gp.Alloc(size);
|
2012-07-27 19:26:49 +02:00
|
|
|
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
|
|
|
|
2013-12-21 18:02:36 +01:00
|
|
|
return g_mem2gp.Alloc(size);
|
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 __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)
|
2013-01-19 17:09:56 +01:00
|
|
|
{
|
2014-03-01 00:11:02 +01:00
|
|
|
if(p > MEM2_start)
|
|
|
|
g_mem2gp.Free(p);
|
|
|
|
else
|
|
|
|
g_mem2lo.Free(p);
|
2013-01-19 17:09:56 +01:00
|
|
|
}
|
2012-05-07 21:45:25 +02:00
|
|
|
else
|
2012-09-07 20:13:04 +02:00
|
|
|
MEM1_free(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
|
|
|
void *__wrap_realloc(void *p, size_t size)
|
|
|
|
{
|
|
|
|
void *n;
|
|
|
|
// ptr from mem2
|
2012-09-21 21:46:58 +02:00
|
|
|
if(((u32)p & 0x10000000) != 0 || (p == 0 && size > MEM2_PRIORITY_SIZE))
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2013-12-21 18:02:36 +01:00
|
|
|
n = g_mem2gp.ReAlloc(p, size);
|
2012-09-21 21:46:58 +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);
|
2012-09-21 21:46:58 +02:00
|
|
|
if(n == 0)
|
2012-01-21 21:57:41 +01:00
|
|
|
return 0;
|
2012-09-21 21:46:58 +02:00
|
|
|
if(p != 0)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-05-07 21:45:25 +02:00
|
|
|
memcpy(n, p, MEM2_usableSize(p) < size ? MEM2_usableSize(p) : size);
|
2013-12-21 18:02:36 +01:00
|
|
|
g_mem2gp.Free(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);
|
2012-09-21 21:46:58 +02:00
|
|
|
if(n != 0)
|
2012-05-07 21:45:25 +02:00
|
|
|
return n;
|
2013-12-21 18:02:36 +01:00
|
|
|
n = g_mem2gp.Alloc(size);
|
2012-09-21 21:46:58 +02:00
|
|
|
if(n == 0)
|
2012-05-07 21:45:25 +02:00
|
|
|
return 0;
|
2012-09-21 21:46:58 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t __wrap_malloc_usable_size(void *p)
|
|
|
|
{
|
|
|
|
if(((u32)p & 0x10000000) != 0)
|
2014-03-01 00:11:02 +01:00
|
|
|
{
|
|
|
|
if(p > MEM2_start)
|
|
|
|
return g_mem2gp.MemBlockSize(p);
|
|
|
|
else
|
|
|
|
return g_mem2lo.MemBlockSize(p);
|
|
|
|
}
|
2012-05-07 21:45:25 +02:00
|
|
|
return __real_malloc_usable_size(p);
|
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-05-07 21:45:25 +02:00
|
|
|
} ///extern "C"
|