From 19f63f4794988124563e4add1a7c4ecbcc02806b Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Fri, 23 Jul 2021 11:26:10 +0200 Subject: [PATCH] Use HeapAlloc to allocate hunk memory for the engine on Xbox. This reserves memory from the title's pool as opposed to the system's pool, which allows us to get the full 128 MB (or more) that we desire. --- Assets/Scripts/QuakeParms.cs | 10 ++++++++++ Assets/Scripts/SystemLibrary.cs | 12 ++++++++++++ Assets/Scripts/UniQuake.cs | 4 ---- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Assets/Scripts/QuakeParms.cs b/Assets/Scripts/QuakeParms.cs index 7d5c845..cc1f25e 100644 --- a/Assets/Scripts/QuakeParms.cs +++ b/Assets/Scripts/QuakeParms.cs @@ -49,7 +49,13 @@ public class QuakeParms public void AllocateMemory(int memorySize) { memSize = memorySize; + +#if UNITY_GAMECORE + memBase = SystemLibrary.HeapAlloc(SystemLibrary.GetProcessHeap(), 0, new IntPtr(memorySize)); +#else memBase = Marshal.AllocHGlobal(memorySize); +#endif + if (memBase == IntPtr.Zero) { throw new OutOfMemoryException($"Could not allocate {memorySize / 1024 / 1024} MB of heap memory!"); @@ -66,7 +72,11 @@ public class QuakeParms if (memBase != IntPtr.Zero) { +#if UNITY_GAMECORE + SystemLibrary.HeapFree(SystemLibrary.GetProcessHeap(), 0, memBase); +#else Marshal.FreeHGlobal(memBase); +#endif memBase = IntPtr.Zero; } diff --git a/Assets/Scripts/SystemLibrary.cs b/Assets/Scripts/SystemLibrary.cs index 33beb41..ce90776 100644 --- a/Assets/Scripts/SystemLibrary.cs +++ b/Assets/Scripts/SystemLibrary.cs @@ -53,4 +53,16 @@ public static class SystemLibrary [DllImport("SceKernelShim.prx")] public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); #endif + +#if UNITY_GAMECORE + [DllImport("kernel32", SetLastError = true)] + public static extern IntPtr GetProcessHeap(); + + [DllImport("kernel32")] + public static extern IntPtr HeapAlloc(IntPtr hHeap, int dwFlags, IntPtr dwBytes); + + [DllImport("kernel32")] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool HeapFree(IntPtr hHeap, int dwFlags, IntPtr lpMem); +#endif } diff --git a/Assets/Scripts/UniQuake.cs b/Assets/Scripts/UniQuake.cs index 15cdbaf..f7565c1 100644 --- a/Assets/Scripts/UniQuake.cs +++ b/Assets/Scripts/UniQuake.cs @@ -5,11 +5,7 @@ using UnityEngine; public partial class UniQuake: MonoBehaviour { -#if UNITY_GAMECORE - private const int DefaultMemSize = 0x2000000; // Xbox is limited to 32 MB of heap space for now... -#else private const int DefaultMemSize = 0x8000000; // 128 MB of heap space -#endif public int memorySize = DefaultMemSize;