2014-08-01 18:27:48 +02:00
|
|
|
#include "stdafx.h"
|
2014-08-31 11:54:12 +02:00
|
|
|
#include "Memory.h"
|
|
|
|
|
|
2014-11-19 15:16:30 +01:00
|
|
|
namespace vm
|
|
|
|
|
{
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
void* const g_base_addr = VirtualAlloc(nullptr, 0x100000000, MEM_RESERVE, PAGE_NOACCESS);
|
|
|
|
|
#else
|
|
|
|
|
#include <sys/mman.h>
|
2014-08-31 11:54:12 +02:00
|
|
|
|
2014-11-19 15:16:30 +01:00
|
|
|
/* OS X uses MAP_ANON instead of MAP_ANONYMOUS */
|
|
|
|
|
#ifndef MAP_ANONYMOUS
|
|
|
|
|
#define MAP_ANONYMOUS MAP_ANON
|
|
|
|
|
#endif
|
2014-08-31 11:54:12 +02:00
|
|
|
|
2014-11-19 15:16:30 +01:00
|
|
|
void* const g_base_addr = ::mmap(nullptr, 0x100000000, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
|
|
|
|
|
#endif
|
2014-08-01 18:27:48 +02:00
|
|
|
|
|
|
|
|
bool check_addr(u32 addr)
|
|
|
|
|
{
|
2014-08-31 01:06:43 +02:00
|
|
|
// Checking address before using it is unsafe.
|
|
|
|
|
// The only safe way to check it is to protect both actions (checking and using) with mutex that is used for mapping/allocation.
|
2014-08-01 18:27:48 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-31 01:06:43 +02:00
|
|
|
//TODO
|
2014-08-01 18:27:48 +02:00
|
|
|
bool map(u32 addr, u32 size, u32 flags)
|
|
|
|
|
{
|
2014-11-19 15:16:30 +01:00
|
|
|
return Memory.Map(addr, size);
|
2014-08-01 18:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool unmap(u32 addr, u32 size, u32 flags)
|
|
|
|
|
{
|
2014-11-19 15:16:30 +01:00
|
|
|
return Memory.Unmap(addr);
|
2014-08-01 18:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
2014-11-19 15:16:30 +01:00
|
|
|
u32 alloc(u32 addr, u32 size, memory_location location)
|
2014-08-01 18:27:48 +02:00
|
|
|
{
|
2014-11-19 15:16:30 +01:00
|
|
|
return g_locations[location].fixed_allocator(addr, size);
|
2014-08-01 18:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
2014-11-19 15:16:30 +01:00
|
|
|
u32 alloc(u32 size, memory_location location)
|
|
|
|
|
{
|
|
|
|
|
return g_locations[location].allocator(size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void dealloc(u32 addr, memory_location location)
|
|
|
|
|
{
|
|
|
|
|
return g_locations[location].deallocator(addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace ps3
|
|
|
|
|
{
|
|
|
|
|
u32 main_alloc(u32 size)
|
|
|
|
|
{
|
|
|
|
|
return Memory.MainMem.AllocAlign(size, 1);
|
|
|
|
|
}
|
|
|
|
|
u32 main_fixed_alloc(u32 addr, u32 size)
|
|
|
|
|
{
|
|
|
|
|
return Memory.MainMem.AllocFixed(addr, size) ? addr : 0;
|
|
|
|
|
}
|
|
|
|
|
void main_dealloc(u32 addr)
|
|
|
|
|
{
|
|
|
|
|
Memory.MainMem.Free(addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u32 g_stack_offset = 0;
|
|
|
|
|
|
|
|
|
|
u32 stack_alloc(u32 size)
|
|
|
|
|
{
|
|
|
|
|
return Memory.StackMem.AllocAlign(size, 0x10);
|
|
|
|
|
}
|
|
|
|
|
u32 stack_fixed_alloc(u32 addr, u32 size)
|
|
|
|
|
{
|
|
|
|
|
return Memory.StackMem.AllocFixed(addr, size) ? addr : 0;
|
|
|
|
|
}
|
|
|
|
|
void stack_dealloc(u32 addr)
|
|
|
|
|
{
|
|
|
|
|
Memory.StackMem.Free(addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u32 sprx_alloc(u32 size)
|
|
|
|
|
{
|
|
|
|
|
return Memory.SPRXMem.AllocAlign(size, 1);
|
|
|
|
|
}
|
|
|
|
|
u32 sprx_fixed_alloc(u32 addr, u32 size)
|
|
|
|
|
{
|
|
|
|
|
return Memory.SPRXMem.AllocFixed(Memory.SPRXMem.GetStartAddr() + addr, size) ? Memory.SPRXMem.GetStartAddr() + addr : 0;
|
|
|
|
|
}
|
|
|
|
|
void sprx_dealloc(u32 addr)
|
|
|
|
|
{
|
|
|
|
|
Memory.SPRXMem.Free(addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u32 user_space_alloc(u32 size)
|
|
|
|
|
{
|
|
|
|
|
return Memory.PRXMem.AllocAlign(size, 1);
|
|
|
|
|
}
|
|
|
|
|
u32 user_space_fixed_alloc(u32 addr, u32 size)
|
|
|
|
|
{
|
|
|
|
|
return Memory.PRXMem.AllocFixed(addr, size) ? addr : 0;
|
|
|
|
|
}
|
|
|
|
|
void user_space_dealloc(u32 addr)
|
|
|
|
|
{
|
|
|
|
|
Memory.PRXMem.Free(addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void init()
|
|
|
|
|
{
|
|
|
|
|
Memory.Init(Memory_PS3);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace psv
|
|
|
|
|
{
|
|
|
|
|
void init()
|
|
|
|
|
{
|
|
|
|
|
Memory.Init(Memory_PSV);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace psp
|
|
|
|
|
{
|
|
|
|
|
void init()
|
|
|
|
|
{
|
|
|
|
|
Memory.Init(Memory_PSP);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
location_info g_locations[memory_location_count] =
|
|
|
|
|
{
|
|
|
|
|
{ 0x00010000, 0x2FFF0000, ps3::main_alloc, ps3::main_fixed_alloc, ps3::main_dealloc },
|
|
|
|
|
{ 0x00010000, 0x2FFF0000, ps3::stack_alloc, ps3::stack_fixed_alloc, ps3::stack_dealloc },
|
|
|
|
|
{ 0x00010000, 0x2FFF0000, ps3::sprx_alloc, ps3::sprx_fixed_alloc, ps3::sprx_dealloc },
|
|
|
|
|
{ 0x00010000, 0x2FFF0000, ps3::user_space_alloc, ps3::user_space_fixed_alloc, ps3::user_space_dealloc },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void close()
|
2014-08-01 18:27:48 +02:00
|
|
|
{
|
2014-11-19 15:16:30 +01:00
|
|
|
Memory.Close();
|
2014-08-01 18:27:48 +02:00
|
|
|
}
|
|
|
|
|
}
|