usbloadergx/source/memory/mem2.cpp

198 lines
4.2 KiB
C++
Raw Normal View History

#include "mem2.h"
#include "mem2alloc.hpp"
#include <malloc.h>
#include <string.h>
#define MEM2_PRIORITY_SIZE 2097152 //2MB
// Forbid the use of MEM2 through malloc
u32 MALLOC_MEM2 = 0;
static CMEM2Alloc g_mem2gp;
static bool g_bigGoesToMem2 = false;
extern "C"
{
2010-09-24 02:48:03 +02:00
void MEM2_takeBigOnes(bool b)
{
g_bigGoesToMem2 = b;
}
2010-09-24 02:48:03 +02:00
void MEM2_init(unsigned int mem2Size)
{
2010-09-24 02:48:03 +02:00
g_mem2gp.init(mem2Size);
}
2010-09-24 02:48:03 +02:00
void MEM2_cleanup(void)
{
g_mem2gp.cleanup();
}
2010-09-24 02:48:03 +02:00
void *MEM2_alloc(unsigned int s)
{
2010-09-24 02:48:03 +02:00
return g_mem2gp.allocate(s);
}
2010-09-24 02:48:03 +02:00
void MEM2_free(void *p)
{
2010-09-24 02:48:03 +02:00
g_mem2gp.release(p);
}
2010-09-24 02:48:03 +02:00
void *MEM2_realloc(void *p, unsigned int s)
{
2010-09-24 02:48:03 +02:00
return g_mem2gp.reallocate(p, s);
}
2010-09-24 02:48:03 +02:00
unsigned int MEM2_usableSize(void *p)
{
2010-09-24 02:48:03 +02:00
return CMEM2Alloc::usableSize(p);
}
unsigned int MEM2_freesize()
{
return g_mem2gp.FreeSize();
}
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;
2010-09-24 02:48:03 +02:00
void *__wrap_malloc(size_t size)
{
void *p;
2010-09-24 02:48:03 +02:00
if (g_bigGoesToMem2 && size > MEM2_PRIORITY_SIZE)
{
2010-09-24 02:48:03 +02:00
p = MEM2_alloc(size);
if (p != 0)
{
return p;
}
2010-09-24 02:48:03 +02:00
return __real_malloc(size);
}
2010-09-24 02:48:03 +02:00
p = __real_malloc(size);
if (p != 0)
{
return p;
}
2010-09-24 02:48:03 +02:00
return MEM2_alloc(size);
}
2010-09-24 02:48:03 +02:00
void *__wrap_calloc(size_t n, size_t size)
{
void *p;
2010-09-24 02:48:03 +02:00
if (g_bigGoesToMem2 && size > MEM2_PRIORITY_SIZE)
{
2010-09-24 02:48:03 +02:00
p = MEM2_alloc(n * size);
if (p != 0)
{
2010-09-24 02:48:03 +02:00
memset(p, 0, n * size);
return p;
}
2010-09-24 02:48:03 +02:00
return __real_calloc(n, size);
}
2010-09-24 02:48:03 +02:00
p = __real_calloc(n, size);
if (p != 0)
{
return p;
}
2010-09-24 02:48:03 +02:00
p = MEM2_alloc(n * size);
if (p != 0)
{
2010-09-24 02:48:03 +02:00
memset(p, 0, n * size);
}
return p;
}
2010-09-24 02:48:03 +02:00
void *__wrap_memalign(size_t a, size_t size)
{
void *p;
2010-09-24 02:48:03 +02:00
if (g_bigGoesToMem2 && size > MEM2_PRIORITY_SIZE)
{
2010-09-24 02:48:03 +02:00
if (a <= 32 && 32 % a == 0)
{
2010-09-24 02:48:03 +02:00
p = MEM2_alloc(size);
if (p != 0)
{
return p;
}
}
2010-09-24 02:48:03 +02:00
return __real_memalign(a, size);
}
2010-09-24 02:48:03 +02:00
p = __real_memalign(a, size);
if (p != 0 || a > 32 || 32 % a != 0)
{
return p;
}
2010-09-24 02:48:03 +02:00
return MEM2_alloc(size);
}
2010-09-24 02:48:03 +02:00
void __wrap_free(void *p)
{
2010-09-24 02:48:03 +02:00
if (!p) return;
2010-09-24 02:48:03 +02:00
if (((u32) p & 0x10000000) != 0)
{
2010-09-24 02:48:03 +02:00
MEM2_free(p);
}
else
{
2010-09-24 02:48:03 +02:00
__real_free(p);
}
}
2010-09-24 02:48:03 +02:00
void *__wrap_realloc(void *p, size_t size)
{
void *n;
// ptr from mem2
2010-09-24 02:48:03 +02:00
if (((u32) p & 0x10000000) != 0 || (p == 0 && g_bigGoesToMem2 && size > MEM2_PRIORITY_SIZE))
{
2010-09-24 02:48:03 +02:00
n = MEM2_realloc(p, size);
if (n != 0)
{
return n;
}
2010-09-24 02:48:03 +02:00
n = __real_malloc(size);
if (n == 0)
{
return 0;
}
2010-09-24 02:48:03 +02:00
if (p != 0)
{
2010-09-24 02:48:03 +02:00
memcpy(n, p, MEM2_usableSize(p) < size ? MEM2_usableSize(p) : size);
MEM2_free(p);
}
return n;
}
// ptr from malloc
2010-09-24 02:48:03 +02:00
n = __real_realloc(p, size);
if (n != 0)
{
return n;
}
2010-09-24 02:48:03 +02:00
n = MEM2_alloc(size);
if (n == 0)
{
return 0;
}
2010-09-24 02:48:03 +02:00
if (p != 0)
{
2010-09-24 02:48:03 +02:00
memcpy(n, p, __real_malloc_usable_size(p) < size ? __real_malloc_usable_size(p) : size);
__real_free(p);
}
return n;
}
2010-09-24 02:48:03 +02:00
size_t __wrap_malloc_usable_size(void *p)
{
2010-09-24 02:48:03 +02:00
if (((u32) p & 0x10000000) != 0) return MEM2_usableSize(p);
return __real_malloc_usable_size(p);
}
} ///extern "C"