mirror of
https://github.com/Fledge68/WiiFlow_Lite.git
synced 2024-11-01 09:05:06 +01:00
08a298b0b7
-clearing screen properly now, no green screens or things like this -added a few more low mem settings, fixed a missing flush in set video mode (thanks dimok) -partially clearing mem1 too now, using the code of cfg-loader for that (thanks) -using asm volatile instead of __asm__ to really call what we want (thanks dimok for explaination) -deiniting usb device properly -shutting down system properly now on game boot, games should boot more often now, its still not fully fixed
199 lines
3.2 KiB
C++
199 lines
3.2 KiB
C++
|
|
#include <malloc.h>
|
|
#include <string.h>
|
|
#include <ogc/system.h>
|
|
|
|
#include "mem2.hpp"
|
|
#include "mem2alloc.hpp"
|
|
#include "gecko.h"
|
|
#include "disc.h"
|
|
|
|
// Forbid the use of MEM2 through malloc
|
|
u32 MALLOC_MEM2 = 0;
|
|
|
|
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;
|
|
|
|
|
|
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);
|
|
}
|
|
|
|
void MEM1_free(void *p)
|
|
{
|
|
__real_free(p);
|
|
}
|
|
|
|
void MEM2_init(unsigned int mem2Size)
|
|
{
|
|
g_mem2gp.init(mem2Size);
|
|
g_mem2gp.clear();
|
|
}
|
|
|
|
void MEM2_cleanup(void)
|
|
{
|
|
g_mem2gp.cleanup();
|
|
}
|
|
|
|
void MEM2_clear(void)
|
|
{
|
|
g_mem2gp.clear();
|
|
}
|
|
|
|
void MEM2_free(void *p)
|
|
{
|
|
g_mem2gp.release(p);
|
|
}
|
|
|
|
void *MEM2_alloc(unsigned int s)
|
|
{
|
|
return g_mem2gp.allocate(s);
|
|
}
|
|
|
|
void *MEM2_realloc(void *p, unsigned int s)
|
|
{
|
|
return g_mem2gp.reallocate(p, s);
|
|
}
|
|
|
|
unsigned int MEM2_usableSize(void *p)
|
|
{
|
|
return CMEM2Alloc::usableSize(p);
|
|
}
|
|
|
|
unsigned int MEM2_freesize()
|
|
{
|
|
return g_mem2gp.FreeSize();
|
|
}
|
|
|
|
void *__wrap_malloc(size_t size)
|
|
{
|
|
void *p;
|
|
if ((SYS_GetArena1Lo() >= MAX_MEM1_ARENA_LO) || size >= MEM2_PRIORITY_SIZE)
|
|
{
|
|
p = MEM2_alloc(size);
|
|
if(p != 0)
|
|
return p;
|
|
return __real_malloc(size);
|
|
}
|
|
p = __real_malloc(size);
|
|
if(p != 0)
|
|
return p;
|
|
return MEM2_alloc(size);
|
|
}
|
|
|
|
void *__wrap_calloc(size_t n, size_t size)
|
|
{
|
|
void *p;
|
|
if ((SYS_GetArena1Lo() >= MAX_MEM1_ARENA_LO) || (n * size) >= MEM2_PRIORITY_SIZE)
|
|
{
|
|
p = MEM2_alloc(n * size);
|
|
if (p != 0)
|
|
{
|
|
memset(p, 0, n * size);
|
|
return p;
|
|
}
|
|
return __real_calloc(n, size);
|
|
}
|
|
|
|
p = __real_calloc(n, size);
|
|
if (p != 0) return p;
|
|
|
|
p = MEM2_alloc(n * size);
|
|
if (p != 0)
|
|
memset(p, 0, n * size);
|
|
return p;
|
|
}
|
|
|
|
void *__wrap_memalign(size_t a, size_t size)
|
|
{
|
|
void *p;
|
|
if ((SYS_GetArena1Lo() >= MAX_MEM1_ARENA_LO) || size >= MEM2_PRIORITY_SIZE)
|
|
{
|
|
if (a <= 32 && 32 % a == 0)
|
|
{
|
|
p = MEM2_alloc(size);
|
|
if (p != 0)
|
|
return p;
|
|
}
|
|
return __real_memalign(a, size);
|
|
}
|
|
p = __real_memalign(a, size);
|
|
if(p != 0)
|
|
return p;
|
|
return MEM2_alloc(size);
|
|
}
|
|
|
|
void __wrap_free(void *p)
|
|
{
|
|
if(!p)
|
|
return;
|
|
|
|
if (((u32)p & 0x10000000) != 0)
|
|
g_mem2gp.release(p);
|
|
else
|
|
__real_free(p);
|
|
}
|
|
|
|
void *__wrap_realloc(void *p, size_t size)
|
|
{
|
|
void *n;
|
|
// ptr from mem2
|
|
if (((u32)p & 0x10000000) != 0 || (p == 0 && size > MEM2_PRIORITY_SIZE))
|
|
{
|
|
n = MEM2_realloc(p, size);
|
|
if (n != 0)
|
|
return n;
|
|
n = __real_malloc(size);
|
|
if (n == 0)
|
|
return 0;
|
|
if (p != 0)
|
|
{
|
|
memcpy(n, p, MEM2_usableSize(p) < size ? MEM2_usableSize(p) : size);
|
|
MEM2_free(p);
|
|
}
|
|
return n;
|
|
}
|
|
// ptr from malloc
|
|
n = __real_realloc(p, size);
|
|
if (n != 0)
|
|
return n;
|
|
n = MEM2_alloc(size);
|
|
if (n == 0)
|
|
return 0;
|
|
if (p != 0)
|
|
{
|
|
memcpy(n, p, __real_malloc_usable_size(p) < size ? __real_malloc_usable_size(p) : size);
|
|
__real_free(p);
|
|
}
|
|
return n;
|
|
}
|
|
|
|
size_t __wrap_malloc_usable_size(void *p)
|
|
{
|
|
if(((u32)p & 0x10000000) != 0)
|
|
return MEM2_usableSize(p);
|
|
return __real_malloc_usable_size(p);
|
|
}
|
|
|
|
} ///extern "C"
|