mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-10-16 07:50:28 +02:00
Clean up MemArena and MemMap. Remove Symbian from MemArena (shouldn't have more ifdefs than code)
This commit is contained in:
1 parent
bf5946203d
commit
9b7ab3816f
5 files changed
+236
-250
No files matched your search
+203
-4
@@ -19,7 +19,9 @@
|
||||
|
||||
#include "Common/Common.h"
|
||||
#include "Common/MemoryUtil.h"
|
||||
#ifndef __SYMBIAN32__
|
||||
#include "Common/MemArena.h"
|
||||
#endif
|
||||
#include "Common/ChunkFile.h"
|
||||
|
||||
#include "Core/MemMap.h"
|
||||
@@ -37,10 +39,14 @@
|
||||
namespace Memory {
|
||||
|
||||
// The base pointer to the auto-mirrored arena.
|
||||
u8* base = NULL;
|
||||
u8* base = NULL;
|
||||
|
||||
#ifdef __SYMBIAN32__
|
||||
RChunk* memmap;
|
||||
#else
|
||||
// The MemArena class
|
||||
MemArena g_arena;
|
||||
#endif
|
||||
// ==============
|
||||
|
||||
// 64-bit: Pointers to low-mem (sub-0x10000000) mirror
|
||||
@@ -114,6 +120,199 @@ static MemoryView views[] =
|
||||
|
||||
static const int num_views = sizeof(views) / sizeof(MemoryView);
|
||||
|
||||
// yeah, this could also be done in like two bitwise ops...
|
||||
#define SKIP(a_flags, b_flags)
|
||||
// if (!(a_flags & MV_WII_ONLY) && (b_flags & MV_WII_ONLY))
|
||||
// continue;
|
||||
// if (!(a_flags & MV_FAKE_VMEM) && (b_flags & MV_FAKE_VMEM))
|
||||
// continue;
|
||||
|
||||
static bool Memory_TryBase(u32 flags) {
|
||||
// OK, we know where to find free space. Now grab it!
|
||||
// We just mimic the popular BAT setup.
|
||||
|
||||
#if defined(_XBOX)
|
||||
void *ptr;
|
||||
#elif !defined(__SYMBIAN32__)
|
||||
size_t position = 0;
|
||||
size_t last_position = 0;
|
||||
#endif
|
||||
|
||||
// Zero all the pointers to be sure.
|
||||
for (int i = 0; i < num_views; i++)
|
||||
{
|
||||
if (views[i].out_ptr_low)
|
||||
*views[i].out_ptr_low = 0;
|
||||
if (views[i].out_ptr)
|
||||
*views[i].out_ptr = 0;
|
||||
}
|
||||
|
||||
int i;
|
||||
for (i = 0; i < num_views; i++)
|
||||
{
|
||||
const MemoryView &view = views[i];
|
||||
if (view.size == 0)
|
||||
continue;
|
||||
SKIP(flags, view.flags);
|
||||
|
||||
#ifdef __SYMBIAN32__
|
||||
if (!(view.flags & MV_MIRROR_PREVIOUS)) {
|
||||
*(view.out_ptr_low) = (u8*)((int)memmap->Base() + view.virtual_address);
|
||||
// Over allocate VRAM to span the mirrors. Hopefully resolves crash.
|
||||
if (i == 2)
|
||||
memmap->Commit(view.virtual_address & 0x3FFFFFFF, view.size * 4);
|
||||
else
|
||||
memmap->Commit(view.virtual_address & 0x3FFFFFFF, view.size);
|
||||
}
|
||||
*(view.out_ptr) = (u8*)((int)memmap->Base() + view.virtual_address & 0x3FFFFFFF);
|
||||
#elif defined(_XBOX)
|
||||
if (!(view.flags & MV_MIRROR_PREVIOUS)) {
|
||||
*(view.out_ptr_low) = (u8*)(base + view.virtual_address);
|
||||
ptr = VirtualAlloc(base + (view.virtual_address & 0x3FFFFFFF), view.size, MEM_COMMIT, PAGE_READWRITE);
|
||||
}
|
||||
*(view.out_ptr) = (u8*)base + (view.virtual_address & 0x3FFFFFFF);
|
||||
#else
|
||||
if (view.flags & MV_MIRROR_PREVIOUS) {
|
||||
position = last_position;
|
||||
} else {
|
||||
*(view.out_ptr_low) = (u8*)g_arena.CreateView(position, view.size);
|
||||
if (!*view.out_ptr_low)
|
||||
goto bail;
|
||||
}
|
||||
#ifdef _M_X64
|
||||
*view.out_ptr = (u8*)g_arena.CreateView(
|
||||
position, view.size, base + view.virtual_address);
|
||||
#else
|
||||
if (view.flags & MV_MIRROR_PREVIOUS) { // TODO: should check if the two & 0x3FFFFFFF are identical.
|
||||
// No need to create multiple identical views.
|
||||
*view.out_ptr = *views[i - 1].out_ptr;
|
||||
} else {
|
||||
*view.out_ptr = (u8*)g_arena.CreateView(
|
||||
position, view.size, base + (view.virtual_address & 0x3FFFFFFF));
|
||||
if (!*view.out_ptr)
|
||||
goto bail;
|
||||
}
|
||||
#endif
|
||||
last_position = position;
|
||||
position += g_arena.roundup(view.size);
|
||||
#endif
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
#if !defined(_XBOX) && !defined(__SYMBIAN32__)
|
||||
bail:
|
||||
// Argh! ERROR! Free what we grabbed so far so we can try again.
|
||||
for (int j = 0; j <= i; j++)
|
||||
{
|
||||
if (views[i].size == 0)
|
||||
continue;
|
||||
SKIP(flags, views[i].flags);
|
||||
if (views[j].out_ptr_low && *views[j].out_ptr_low)
|
||||
{
|
||||
g_arena.ReleaseView(*views[j].out_ptr_low, views[j].size);
|
||||
*views[j].out_ptr_low = NULL;
|
||||
}
|
||||
if (*views[j].out_ptr)
|
||||
{
|
||||
#ifdef _M_X64
|
||||
g_arena.ReleaseView(*views[j].out_ptr, views[j].size);
|
||||
#else
|
||||
if (!(views[j].flags & MV_MIRROR_PREVIOUS))
|
||||
{
|
||||
g_arena.ReleaseView(*views[j].out_ptr, views[j].size);
|
||||
}
|
||||
#endif
|
||||
*views[j].out_ptr = NULL;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void MemoryMap_Setup(u32 flags)
|
||||
{
|
||||
// Find a base to reserve 256MB
|
||||
#if defined(_XBOX)
|
||||
base = (u8*)VirtualAlloc(0, 0x10000000, MEM_RESERVE|MEM_LARGE_PAGES, PAGE_READWRITE);
|
||||
#elif defined(__SYMBIAN32__)
|
||||
memmap = new RChunk();
|
||||
memmap->CreateDisconnectedLocal(0 , 0, 0x10000000);
|
||||
base = memmap->Base();
|
||||
#elif defined(_M_X64) || !defined(_WIN32)
|
||||
size_t total_mem = 0;
|
||||
|
||||
for (int i = 0; i < num_views; i++)
|
||||
{
|
||||
if (views[i].size == 0)
|
||||
continue;
|
||||
SKIP(flags, views[i].flags);
|
||||
if ((views[i].flags & MV_MIRROR_PREVIOUS) == 0)
|
||||
total_mem += g_arena.roundup(views[i].size);
|
||||
}
|
||||
// Grab some pagefile backed memory out of the void ...
|
||||
g_arena.GrabLowMemSpace(total_mem);
|
||||
// This really shouldn't fail - in 64-bit, there will always be enough address space.
|
||||
// Linux32 is fine with the x64 method, although limited to 32-bit with no automirrors.
|
||||
base = MemArena::Find4GBBase();
|
||||
#endif
|
||||
|
||||
|
||||
// Now, create views in high memory where there's plenty of space.
|
||||
#ifdef _WIN32
|
||||
// Try a whole range of possible bases. Return once we got a valid one.
|
||||
int base_attempts = 0;
|
||||
u32 max_base_addr = 0x7FFF0000 - 0x10000000;
|
||||
|
||||
for (u32 base_addr = 0x01000000; base_addr < max_base_addr; base_addr += 0x400000)
|
||||
{
|
||||
base_attempts++;
|
||||
base = (u8 *)base_addr;
|
||||
if (Memory_TryBase(flags))
|
||||
{
|
||||
INFO_LOG(MEMMAP, "Found valid memory base at %p after %i tries.", base, base_attempts);
|
||||
base_attempts = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (base_attempts)
|
||||
PanicAlert("No possible memory base pointer found!");
|
||||
#else
|
||||
// Try base we retrieved earlier
|
||||
if (!Memory_TryBase(flags))
|
||||
{
|
||||
ERROR_LOG(MEMMAP, "MemoryMap_Setup: Failed finding a memory base.");
|
||||
PanicAlert("MemoryMap_Setup: Failed finding a memory base.");
|
||||
}
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
void MemoryMap_Shutdown(u32 flags)
|
||||
{
|
||||
#ifdef __SYMBIAN32__
|
||||
memmap->Decommit(0, memmap->MaxSize());
|
||||
memmap->Close();
|
||||
delete memmap;
|
||||
#else
|
||||
for (int i = 0; i < num_views; i++)
|
||||
{
|
||||
if (views[i].size == 0)
|
||||
continue;
|
||||
SKIP(flags, views[i].flags);
|
||||
if (views[i].out_ptr_low && *views[i].out_ptr_low)
|
||||
g_arena.ReleaseView(*views[i].out_ptr_low, views[i].size);
|
||||
if (*views[i].out_ptr && (!views[i].out_ptr_low || *views[i].out_ptr != *views[i].out_ptr_low))
|
||||
g_arena.ReleaseView(*views[i].out_ptr, views[i].size);
|
||||
*views[i].out_ptr = NULL;
|
||||
if (views[i].out_ptr_low)
|
||||
*views[i].out_ptr_low = NULL;
|
||||
}
|
||||
g_arena.ReleaseSpace();
|
||||
#endif
|
||||
}
|
||||
|
||||
void Init()
|
||||
{
|
||||
int flags = 0;
|
||||
@@ -129,7 +328,7 @@ void Init()
|
||||
if (views[i].flags & MV_IS_EXTRA2_RAM)
|
||||
views[i].size = std::min(std::max((int)g_MemorySize - MAX_MMAP_SIZE * 2, 0), MAX_MMAP_SIZE);
|
||||
}
|
||||
base = MemoryMap_Setup(views, num_views, flags, &g_arena);
|
||||
MemoryMap_Setup(flags);
|
||||
|
||||
INFO_LOG(MEMMAP, "Memory system initialized. RAM at %p (mirror at 0 @ %p, uncached @ %p)",
|
||||
m_pRAM, m_pPhysicalRAM, m_pUncachedRAM);
|
||||
@@ -170,8 +369,8 @@ void DoState(PointerWrap &p)
|
||||
void Shutdown()
|
||||
{
|
||||
u32 flags = 0;
|
||||
MemoryMap_Shutdown(views, num_views, flags, &g_arena);
|
||||
g_arena.ReleaseSpace();
|
||||
|
||||
MemoryMap_Shutdown(flags);
|
||||
base = NULL;
|
||||
DEBUG_LOG(MEMMAP, "Memory system shut down.");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user