System: Simplify memory allocation

This commit is contained in:
Stenzek
2023-10-10 18:01:30 +10:00
committed by Connor McLaughlin
parent 377746f155
commit 606cbb3883
38 changed files with 446 additions and 1161 deletions
-5
View File
@@ -187,11 +187,6 @@ private:
#endif
};
// Safe version of Munmap -- NULLs the pointer variable immediately after free'ing it.
#define SafeSysMunmap(ptr, size) \
((void)(HostSys::Munmap(ptr, size), (ptr) = 0))
extern u64 GetTickFrequency();
extern u64 GetCPUTicks();
extern u64 GetPhysicalMemory();
@@ -24,6 +24,8 @@
#include "pcsx2/GameList.h"
#include "pcsx2/Patch.h"
#include "common/Assertions.h"
#include <algorithm>
GamePatchDetailsWidget::GamePatchDetailsWidget(std::string name, const std::string& author,
-2
View File
@@ -140,7 +140,6 @@ set(pcsx2Sources
Vif_Codes.cpp
Vif_Transfer.cpp
Vif_Unpack.cpp
VirtualMemory.cpp
VMManager.cpp
vtlb.cpp
VU0.cpp
@@ -217,7 +216,6 @@ set(pcsx2Headers
Vif_Dma.h
Vif.h
Vif_Unpack.h
VirtualMemory.h
VMManager.h
vtlb.h
VUflags.h
+1
View File
@@ -15,6 +15,7 @@
#pragma once
#include "common/Assertions.h"
#include "common/StringUtil.h"
// Useful enums for some of the fields.
+16 -22
View File
@@ -17,39 +17,33 @@
#include "GS/Renderers/Common/GSFunctionMap.h"
#include "System.h"
static GSCodeReserve s_instance;
GSCodeReserve::GSCodeReserve()
: RecompiledCodeReserve("GS Software Renderer")
namespace GSCodeReserve
{
static u8* s_memory_base;
static u8* s_memory_end;
static u8* s_memory_ptr;
}
GSCodeReserve::~GSCodeReserve() = default;
GSCodeReserve& GSCodeReserve::GetInstance()
void GSCodeReserve::ResetMemory()
{
return s_instance;
s_memory_base = SysMemory::GetSWRec();
s_memory_end = SysMemory::GetSWRecEnd();
s_memory_ptr = s_memory_base;
}
void GSCodeReserve::Assign(VirtualMemoryManagerPtr allocator)
size_t GSCodeReserve::GetMemoryUsed()
{
RecompiledCodeReserve::Assign(std::move(allocator), HostMemoryMap::SWrecOffset, HostMemoryMap::SWrecSize);
return s_memory_ptr - s_memory_base;
}
void GSCodeReserve::Reset()
u8* GSCodeReserve::ReserveMemory(size_t size)
{
RecompiledCodeReserve::Reset();
m_memory_used = 0;
pxAssert((s_memory_ptr + size) <= s_memory_end);
return s_memory_ptr;
}
u8* GSCodeReserve::Reserve(size_t size)
void GSCodeReserve::CommitMemory(size_t size)
{
pxAssert((m_memory_used + size) <= m_size);
return m_baseptr + m_memory_used;
}
void GSCodeReserve::Commit(size_t size)
{
pxAssert((m_memory_used + size) <= m_size);
m_memory_used += size;
pxAssert((s_memory_ptr + size) <= s_memory_end);
s_memory_ptr += size;
}
+9 -19
View File
@@ -17,7 +17,7 @@
#include "GS/GSExtra.h"
#include "GS/Renderers/SW/GSScanlineEnvironment.h"
#include "VirtualMemory.h"
#include "System.h"
#include "common/emitter/tools.h"
template <class KEY, class VALUE>
@@ -147,25 +147,15 @@ public:
// --------------------------------------------------------------------------------------
// Stores code buffers for the GS software JIT.
//
class GSCodeReserve : public RecompiledCodeReserve
namespace GSCodeReserve
{
public:
GSCodeReserve();
~GSCodeReserve();
void ResetMemory();
static GSCodeReserve& GetInstance();
size_t GetMemoryUsed();
size_t GetMemoryUsed() const { return m_memory_used; }
void Assign(VirtualMemoryManagerPtr allocator);
void Reset();
u8* Reserve(size_t size);
void Commit(size_t size);
private:
size_t m_memory_used = 0;
};
u8* ReserveMemory(size_t size);
void CommitMemory(size_t size);
}
template <class CG, class KEY, class VALUE>
class GSCodeGeneratorFunctionMap : public GSFunctionMap<KEY, VALUE>
@@ -200,7 +190,7 @@ public:
}
else
{
u8* code_ptr = GSCodeReserve::GetInstance().Reserve(MAX_SIZE);
u8* code_ptr = GSCodeReserve::ReserveMemory(MAX_SIZE);
CG cg(key, code_ptr, MAX_SIZE);
ASSERT(cg.getSize() < MAX_SIZE);
@@ -210,7 +200,7 @@ public:
sel.Print();
#endif
GSCodeReserve::GetInstance().Commit(cg.getSize());
GSCodeReserve::CommitMemory(cg.getSize());
ret = (VALUE)cg.getCode();
+3 -3
View File
@@ -38,12 +38,12 @@ GSDrawScanline::GSDrawScanline()
: m_sp_map("GSSetupPrim")
, m_ds_map("GSDrawScanline")
{
GSCodeReserve::GetInstance().Reset();
GSCodeReserve::ResetMemory();
}
GSDrawScanline::~GSDrawScanline()
{
if (const size_t used = GSCodeReserve::GetInstance().GetMemoryUsed(); used > 0)
if (const size_t used = GSCodeReserve::GetMemoryUsed(); used > 0)
DevCon.WriteLn("SW JIT generated %zu bytes of code", used);
}
@@ -82,7 +82,7 @@ void GSDrawScanline::ResetCodeCache()
Console.Warning("GS Software JIT cache overflow, resetting.");
m_sp_map.Clear();
m_ds_map.Clear();
GSCodeReserve::GetInstance().Reset();
GSCodeReserve::ResetMemory();
}
bool GSDrawScanline::SetupDraw(GSRasterizerData& data)
-28
View File
@@ -20,7 +20,6 @@
#include "Hardware.h"
#include "SPU2/spu2.h"
#include "USB/USB.h"
#include "x86/newVif.h"
#include "common/WrappedMemCopy.h"
@@ -31,35 +30,8 @@ using namespace R5900;
const int rdram_devices = 2; // put 8 for TOOL and 2 for PS2 and PSX
int rdram_sdevid = 0;
static bool hwInitialized = false;
void hwInit()
{
// [TODO] / FIXME: PCSX2 no longer works on an Init system. It assumes that the
// static global vars for the process will be initialized when the process is created, and
// then issues *resets only* from then on. (reset code for various S2 components should do
// NULL checks and allocate memory and such if the pointers are NULL only).
if( hwInitialized ) return;
VifUnpackSSE_Init();
hwInitialized = true;
}
void hwShutdown()
{
if (!hwInitialized) return;
VifUnpackSSE_Destroy();
hwInitialized = false;
}
void hwReset()
{
hwInit();
std::memset(eeHw, 0, sizeof(eeHw));
psHu32(SBUS_F260) = 0x1D000060;
-1
View File
@@ -364,7 +364,6 @@ enum GSRegisterAddresses
};
extern void hwReset();
extern void hwShutdown();
extern const int rdram_devices;
extern int rdram_sdevid;
+5 -22
View File
@@ -30,35 +30,20 @@ IopVM_MemoryAllocMess* iopMem = NULL;
alignas(__pagesize) u8 iopHw[Ps2MemSize::IopHardware];
// --------------------------------------------------------------------------------------
// iopMemoryReserve
// --------------------------------------------------------------------------------------
iopMemoryReserve::iopMemoryReserve()
: _parent("IOP Main Memory (2mb)")
{
}
iopMemoryReserve::~iopMemoryReserve()
{
Release();
}
void iopMemoryReserve::Assign(VirtualMemoryManagerPtr allocator)
void iopMemAlloc()
{
// TODO: Move to memmap
psxMemWLUT = (uptr*)_aligned_malloc(0x2000 * sizeof(uptr) * 2, 16);
if (!psxMemWLUT)
pxFailRel("Failed to allocate IOP memory lookup table");
psxMemRLUT = psxMemWLUT + 0x2000; //(uptr*)_aligned_malloc(0x10000 * sizeof(uptr),16);
VtlbMemoryReserve::Assign(std::move(allocator), HostMemoryMap::IOPmemOffset, sizeof(*iopMem));
iopMem = reinterpret_cast<IopVM_MemoryAllocMess*>(GetPtr());
iopMem = reinterpret_cast<IopVM_MemoryAllocMess*>(SysMemory::GetCodePtr(HostMemoryMap::IOPmemOffset));
}
void iopMemoryReserve::Release()
void iopMemRelease()
{
_parent::Release();
safe_aligned_free(psxMemWLUT);
psxMemRLUT = nullptr;
iopMem = nullptr;
@@ -66,10 +51,8 @@ void iopMemoryReserve::Release()
// Note! Resetting the IOP's memory state is dependent on having *all* psx memory allocated,
// which is performed by MemInit and PsxMemInit()
void iopMemoryReserve::Reset()
void iopMemReset()
{
_parent::Reset();
pxAssert( iopMem );
DbgCon.WriteLn("IOP resetting main memory...");
+3 -4
View File
@@ -71,10 +71,9 @@ static __fi u8* iopPhysMem( u32 addr )
#define psxHu16(mem) (*(u16*)&iopHw[(mem) & 0xffff])
#define psxHu32(mem) (*(u32*)&iopHw[(mem) & 0xffff])
extern void psxMemReserve();
extern void psxMemAlloc();
extern void psxMemReset();
extern void psxMemShutdown();
extern void iopMemAlloc();
extern void iopMemReset();
extern void iopMemRelease();
extern u8 iopMemRead8 (u32 mem);
extern u16 iopMemRead16(u32 mem);
+4 -20
View File
@@ -701,28 +701,13 @@ void memBindConditionalHandlers()
// --------------------------------------------------------------------------------------
// eeMemoryReserve (implementations)
// --------------------------------------------------------------------------------------
eeMemoryReserve::eeMemoryReserve()
: _parent("EE Main Memory")
void memAllocate()
{
eeMem = reinterpret_cast<EEVM_MemoryAllocMess*>(SysMemory::GetEEMem());
}
eeMemoryReserve::~eeMemoryReserve()
void memReset()
{
Release();
}
void eeMemoryReserve::Assign(VirtualMemoryManagerPtr allocator)
{
_parent::Assign(std::move(allocator), HostMemoryMap::EEmemOffset, sizeof(*eeMem));
eeMem = reinterpret_cast<EEVM_MemoryAllocMess*>(GetPtr());
}
// Resets memory mappings, unmaps TLBs, reloads bios roms, etc.
void eeMemoryReserve::Reset()
{
_parent::Reset();
// Note!! Ideally the vtlb should only be initialized once, and then subsequent
// resets of the system hardware would only clear vtlb mappings, but since the
// rest of the emu is not really set up to support a "soft" reset of that sort
@@ -842,8 +827,7 @@ void eeMemoryReserve::Reset()
CopyBIOSToMemory();
}
void eeMemoryReserve::Release()
void memRelease()
{
eeMem = nullptr;
_parent::Release();
}
+3
View File
@@ -97,6 +97,9 @@ static __fi void ZeroQWC( u128& dest )
#define psSu64(mem) (*(u64 *)&eeMem->Scratch[(mem) & 0x3fff])
#define psSu128(mem) (*(u128*)&eeMem->Scratch[(mem) & 0x3fff])
extern void memAllocate();
extern void memReset();
extern void memRelease();
extern void memSetKernelMode();
//extern void memSetSupervisorMode();
+21 -19
View File
@@ -1,5 +1,5 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2010 PCSX2 Dev Team
* Copyright (C) 2002-2023 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
@@ -18,18 +18,19 @@
namespace Ps2MemSize
{
static const uint MainRam = _32mb; // 32 MB main memory!
static const uint Rom = _1mb * 4; // 4 MB main rom
static const uint Rom1 = _1mb * 4; // DVD player
static const uint Rom2 = 0x00080000; // Chinese rom extension
static const uint Hardware = _64kb;
static const uint Scratch = _16kb;
static constexpr u32 MainRam = _32mb; // 32 MB main memory.
static constexpr u32 ExtraRam = _1mb * 96; // 32+96 MB devkit memory.
static constexpr u32 Rom = _1mb * 4; // 4 MB main rom
static constexpr u32 Rom1 = _1mb * 4; // DVD player
static constexpr u32 Rom2 = 0x00080000; // Chinese rom extension
static constexpr u32 Hardware = _64kb;
static constexpr u32 Scratch = _16kb;
static const uint IopRam = _1mb * 2; // 2MB main ram on the IOP.
static const uint IopHardware = _64kb;
static constexpr u32 IopRam = _1mb * 2; // 2MB main ram on the IOP.
static constexpr u32 IopHardware = _64kb;
static const uint GSregs = 0x00002000; // 8k for the GS registers and stuff.
}
static constexpr u32 GSregs = 0x00002000; // 8k for the GS registers and stuff.
} // namespace Ps2MemSize
typedef u8 mem8_t;
typedef u16 mem16_t;
@@ -39,11 +40,12 @@ typedef u128 mem128_t;
struct EEVM_MemoryAllocMess
{
u8 Main[Ps2MemSize::MainRam]; // Main memory (hard-wired to 32MB)
u8 Scratch[Ps2MemSize::Scratch]; // Scratchpad!
u8 ROM[Ps2MemSize::Rom]; // Boot rom (4MB)
u8 ROM1[Ps2MemSize::Rom1]; // DVD player (4MB)
u8 ROM2[Ps2MemSize::Rom2]; // Chinese extensions
u8 Main[Ps2MemSize::MainRam]; // Main memory (hard-wired to 32MB)
u8 ExtraMemory[Ps2MemSize::ExtraRam]; // Extra memory (32MB up to 128MB => 96MB).
u8 Scratch[Ps2MemSize::Scratch]; // Scratchpad!
u8 ROM[Ps2MemSize::Rom]; // Boot rom (4MB)
u8 ROM1[Ps2MemSize::Rom1]; // DVD player (4MB)
u8 ROM2[Ps2MemSize::Rom2]; // Chinese extensions
// Two 1 megabyte (max DMA) buffers for reading and writing to high memory (>32MB).
// Such accesses are not documented as causing bus errors but as the memory does
@@ -56,9 +58,9 @@ struct EEVM_MemoryAllocMess
struct IopVM_MemoryAllocMess
{
u8 Main[Ps2MemSize::IopRam]; // Main memory (hard-wired to 2MB)
u8 P[_64kb]; // I really have no idea what this is... --air
u8 Sif[0x100]; // a few special SIF/SBUS registers (likely not needed)
u8 Main[Ps2MemSize::IopRam]; // Main memory (hard-wired to 2MB)
u8 P[_64kb]; // I really have no idea what this is... --air
u8 Sif[0x100]; // a few special SIF/SBUS registers (likely not needed)
};
+1
View File
@@ -17,6 +17,7 @@
#define _PC_ // disables MIPS opcode macros.
#include "common/Assertions.h"
#include "common/ByteSwap.h"
#include "common/FileSystem.h"
#include "common/Path.h"
-1
View File
@@ -207,7 +207,6 @@ extern R3000Acpu psxRec;
extern void psxReset();
extern void psxException(u32 code, u32 step);
extern void iopEventTest();
extern void psxMemReset();
int psxIsBreakpointNeeded(u32 addr);
int psxIsMemcheckNeeded(u32 pc);
-6
View File
@@ -67,12 +67,8 @@ const int kMaxArgs = 16;
uptr g_argPtrs[kMaxArgs];
#define DEBUG_LAUNCHARG 0 // show lots of helpful console messages as the launch arguments are passed to the game
extern SysMainMemory& GetVmMemory();
void cpuReset()
{
GetVmMemory().Reset();
std::memset(&cpuRegs, 0, sizeof(cpuRegs));
std::memset(&fpuRegs, 0, sizeof(fpuRegs));
std::memset(&tlb, 0, sizeof(tlb));
@@ -91,8 +87,6 @@ void cpuReset()
psxReset();
pgifInit();
hwReset();
extern void Deci2Reset(); // lazy, no good header for it yet.
Deci2Reset();
-41
View File
@@ -248,48 +248,19 @@ struct R5900cpu
// the virtual cpu provider. Allocating additional heap memory from this method is
// NOT recommended. Heap allocations should be performed by Reset only. This
// maximizes the likeliness of reservations claiming addresses they prefer.
//
// Thread Affinity:
// Called from the main/UI thread only. Cpu execution status is guaranteed to
// be inactive. No locking is necessary.
//
// Exception Throws:
// HardwareDeficiency - The host machine's hardware does not support this CPU provider.
// OutOfMemory - Not enough memory, or the memory areas required were already
// reserved.
void (*Reserve)();
// Deallocates ram allocated by Allocate, Reserve, and/or by runtime code execution.
//
// Thread Affinity:
// Called from the main/UI thread only. Cpu execution status is guaranteed to
// be inactive. No locking is necessary.
//
// Exception Throws: None. This function is a destructor, and should not throw.
//
void (*Shutdown)();
// Initializes / Resets code execution states. Typically implementation is only
// needed for recompilers, as interpreters have no internal execution states and
// rely on the CPU/VM states almost entirely.
//
// Thread Affinity:
// Can be called from any thread. CPU execution status is indeterminate and may
// already be in progress. Implementations should be sure to queue and execute
// resets at the earliest safe convenience (typically right before recompiling a
// new block of code, or after a vsync event).
//
// Exception Throws: Emulator-defined. Common exception types to expect are
// OutOfMemory, Stream Exceptions
//
void (*Reset)();
// Steps a single instruction. Meant to be used by debuggers. Is currently unused
// and unimplemented. Future note: recompiler "step" should *always* fall back
// on interpreters.
//
// Exception Throws: [TODO] (possible execution-related throws to be added)
//
void (*Step)();
// Executes code until a break is signaled. Execution can be paused or suspended
@@ -297,11 +268,6 @@ struct R5900cpu
// Execution Breakages are handled the same way, where-by a signal causes the Execute
// call to return at the nearest state check (typically handled internally using
// either C++ exceptions or setjmp/longjmp).
//
// Exception Throws:
// Throws BaseR5900Exception and all derivatives.
// Throws FileNotFound or other Streaming errors (typically related to BIOS MEC/NVM)
//
void (*Execute)();
// Immediately exits execution of recompiled code if we are in a state to do so, or
@@ -319,13 +285,6 @@ struct R5900cpu
// Also: the calls from COP0's TLB remap code should be replaced with full recompiler
// resets, since TLB remaps affect more than just the code they contain (code that
// may reference the remapped blocks via memory loads/stores, for example).
//
// Thread Affinity Rule:
// Can be called from any thread (namely for being called from debugging threads)
//
// Exception Throws: [TODO] Emulator defined? (probably shouldn't throw, probably
// doesn't matter if we're stripping it out soon. ;)
//
void (*Clear)(u32 Addr, u32 Size);
};
+1
View File
@@ -20,6 +20,7 @@
#include "SIO/Memcard/MemoryCardFolder.h"
#include "SIO/Sio.h"
#include "common/Assertions.h"
#include "common/FileSystem.h"
#include "common/Path.h"
#include "common/StringUtil.h"
+1
View File
@@ -18,6 +18,7 @@
#include "SIO/Memcard/MemoryCardFile.h"
#include "SIO/Memcard/MemoryCardFolder.h"
#include "common/Assertions.h"
#include "common/Path.h"
#include "System.h"

Some files were not shown because too many files have changed in this diff Show More