WiiFlow_Lite/source/memory/mem2.cpp

259 lines
4.3 KiB
C++
Raw Normal View History

2012-01-21 21:57:41 +01:00
#include <malloc.h>
#include <string.h>
#include <ogc/system.h>
#include "mem2.hpp"
#include "mem2alloc.hpp"
#include "gecko/gecko.hpp"
#include "loader/utils.h"
2012-01-21 21:57:41 +01:00
#define MEM2_PRIORITY_SIZE 0x1000
2012-01-21 21:57:41 +01:00
// Forbid the use of MEM2 through malloc
u32 MALLOC_MEM2 = 0;
void *MEM1_lo_start = (void*)0x80004000;
void *MEM1_lo_end = (void*)0x80620000;
void *MEM2_lo_start = (void*)0x90000000;
void *MEM2_lo_end = (void*)0x90600000;
void *MEM2_start = (void*)0x90600000;
void *MEM2_end = (void*)0x93200000;
static CMEM2Alloc g_mem1lo;
static CMEM2Alloc g_mem2lo_gp;
2012-01-21 21:57:41 +01:00
static CMEM2Alloc g_mem2gp;
extern "C"
{
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
void MEM_init()
{
g_mem1lo.init(MEM1_lo_start, MEM1_lo_end); //about 6mb
g_mem1lo.clear();
g_mem2lo_gp.init(MEM2_lo_start, MEM2_lo_end); //about 6mb
g_mem2lo_gp.clear();
g_mem2gp.init(MEM2_start, MEM2_end); //about 44mb
g_mem2gp.clear();
}
void *MEM1_lo_alloc(unsigned int s)
{
return g_mem1lo.allocate(s);
}
void MEM1_lo_free(void *p)
{
if(!p)
return;
g_mem1lo.release(p);
}
void *MEM1_alloc(unsigned int s)
{
return __real_malloc(s);
}
void *MEM1_memalign(unsigned int a, unsigned int s)
{
return __real_memalign(a, s);
}
void *MEM1_realloc(void *p, unsigned int s)
{
return __real_realloc(p, s);
}
2012-01-21 21:57:41 +01:00
void MEM1_free(void *p)
{
if(!p)
return;
__real_free(p);
}
unsigned int MEM1_freesize()
{
return (g_mem1lo.FreeSize() + SYS_GetArena1Size());
}
void *MEM2_lo_alloc(unsigned int s)
{
return g_mem2lo_gp.allocate(s);
}
2012-01-21 21:57:41 +01:00
void *MEM2_lo_realloc(void *p, unsigned int s)
{
return g_mem2lo_gp.reallocate(p, s);
}
2012-01-21 21:57:41 +01:00
void MEM2_lo_free(void *p)
{
if(!p)
return;
g_mem2lo_gp.release(p);
}
void MEM2_free(void *p)
{
if(!p)
return;
g_mem2gp.release(p);
}
2012-01-21 21:57:41 +01:00
void *MEM2_alloc(unsigned int s)
{
return g_mem2gp.allocate(s);
}
2012-01-21 21:57:41 +01:00
/* Placeholder, will be needed with new memory manager */
void *MEM2_memalign(unsigned int /* alignment */, unsigned int s)
{
return MEM2_alloc(s);
}
void *MEM2_realloc(void *p, unsigned int s)
{
return g_mem2gp.reallocate(p, s);
}
2012-01-21 21:57:41 +01:00
unsigned int MEM2_usableSize(void *p)
{
return g_mem2gp.usableSize(p);
}
2012-01-21 21:57:41 +01:00
unsigned int MEM2_freesize()
{
return g_mem2gp.FreeSize();
}
2012-01-21 21:57:41 +01:00
void *__wrap_malloc(size_t size)
{
void *p;
if(size >= MEM2_PRIORITY_SIZE)
2012-01-21 21:57:41 +01:00
{
p = g_mem2gp.allocate(size);
if(p != 0)
return p;
return __real_malloc(size);
2012-01-21 21:57:41 +01:00
}
p = __real_malloc(size);
if(p != 0)
return p;
return g_mem2gp.allocate(size);
}
2012-01-21 21:57:41 +01:00
void *__wrap_calloc(size_t n, size_t size)
{
void *p;
if((n * size) >= MEM2_PRIORITY_SIZE)
2012-01-21 21:57:41 +01:00
{
p = g_mem2gp.allocate(n * size);
if (p != 0)
2012-01-21 21:57:41 +01:00
{
memset(p, 0, n * size);
return p;
2012-01-21 21:57:41 +01:00
}
return __real_calloc(n, size);
2012-01-21 21:57:41 +01:00
}
p = __real_calloc(n, size);
if (p != 0) return p;
p = g_mem2gp.allocate(n * size);
if (p != 0)
memset(p, 0, n * size);
return p;
}
void *__wrap_memalign(size_t a, size_t size)
{
void *p;
if(size >= MEM2_PRIORITY_SIZE)
2012-01-21 21:57:41 +01:00
{
if(a <= 32 && 32 % a == 0)
{
p = g_mem2gp.allocate(size);
if (p != 0)
return p;
}
return __real_memalign(a, size);
2012-01-21 21:57:41 +01:00
}
p = __real_memalign(a, size);
if(p != 0 || a > 32 || 32 % a != 0)
return p;
return g_mem2gp.allocate(size);
}
2012-01-21 21:57:41 +01:00
void __wrap_free(void *p)
{
if(!p)
return;
2012-01-21 21:57:41 +01:00
if(((u32)p & 0x10000000) != 0)
{
if(p > MEM2_start)
g_mem2gp.release(p);
else
g_mem2lo_gp.release(p);
}
else
MEM1_free(p);
}
2012-01-21 21:57:41 +01: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
{
n = g_mem2gp.reallocate(p, size);
2012-09-21 21:46:58 +02:00
if(n != 0)
2012-01-21 21:57:41 +01:00
return n;
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
{
memcpy(n, p, MEM2_usableSize(p) < size ? MEM2_usableSize(p) : size);
g_mem2gp.release(p);
2012-01-21 21:57:41 +01:00
}
return n;
}
// ptr from malloc
n = __real_realloc(p, size);
2012-09-21 21:46:58 +02:00
if(n != 0)
return n;
n = g_mem2gp.allocate(size);
2012-09-21 21:46:58 +02:00
if(n == 0)
return 0;
2012-09-21 21:46:58 +02:00
if(p != 0)
2012-01-21 21:57:41 +01: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
}
return n;
}
size_t __wrap_malloc_usable_size(void *p)
{
if(((u32)p & 0x10000000) != 0)
return CMEM2Alloc::usableSize(p);
return __real_malloc_usable_size(p);
}
2012-01-21 21:57:41 +01:00
} ///extern "C"