From 173804cbe1e2d980b58f0f25081ea0c8b8df9927 Mon Sep 17 00:00:00 2001 From: goaaats Date: Fri, 28 Jan 2022 20:13:13 +0100 Subject: [PATCH] feat(MemoryHelper): add helpers for game allocators --- Dalamud/Memory/MemoryHelper.cs | 77 ++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/Dalamud/Memory/MemoryHelper.cs b/Dalamud/Memory/MemoryHelper.cs index 0a4b840ef..e8a32d2c3 100644 --- a/Dalamud/Memory/MemoryHelper.cs +++ b/Dalamud/Memory/MemoryHelper.cs @@ -5,6 +5,7 @@ using System.Text; using Dalamud.Game.Text.SeStringHandling; using Dalamud.Memory.Exceptions; +using FFXIVClientStructs.FFXIV.Client.System.Memory; using FFXIVClientStructs.FFXIV.Client.System.String; using static Dalamud.NativeFunctions; @@ -653,5 +654,81 @@ namespace Dalamud.Memory => SizeOf(marshal) * elementCount; #endregion + + #region Game + + /// + /// Allocate memory in the game's UI memory space. + /// + /// Amount of bytes to allocate. + /// The alignment of the allocation. + /// Pointer to the allocated region. + public static IntPtr GameAllocateUi(ulong size, ulong alignment = 0) + { + return new IntPtr(IMemorySpace.GetUISpace()->Malloc(size, alignment)); + } + + /// + /// Allocate memory in the game's default memory space. + /// + /// Amount of bytes to allocate. + /// The alignment of the allocation. + /// Pointer to the allocated region. + public static IntPtr GameAllocateDefault(ulong size, ulong alignment = 0) + { + return new IntPtr(IMemorySpace.GetDefaultSpace()->Malloc(size, alignment)); + } + + /// + /// Allocate memory in the game's animation memory space. + /// + /// Amount of bytes to allocate. + /// The alignment of the allocation. + /// Pointer to the allocated region. + public static IntPtr GameAllocateAnimation(ulong size, ulong alignment = 0) + { + return new IntPtr(IMemorySpace.GetAnimationSpace()->Malloc(size, alignment)); + } + + /// + /// Allocate memory in the game's apricot memory space. + /// + /// Amount of bytes to allocate. + /// The alignment of the allocation. + /// Pointer to the allocated region. + public static IntPtr GameAllocateApricot(ulong size, ulong alignment = 0) + { + return new IntPtr(IMemorySpace.GetApricotSpace()->Malloc(size, alignment)); + } + + /// + /// Allocate memory in the game's sound memory space. + /// + /// Amount of bytes to allocate. + /// The alignment of the allocation. + /// Pointer to the allocated region. + public static IntPtr GameAllocateSound(ulong size, ulong alignment = 0) + { + return new IntPtr(IMemorySpace.GetSoundSpace()->Malloc(size, alignment)); + } + + /// + /// Free memory in the game's memory space. + /// + /// The memory you are freeing must be allocated with game allocators. + /// Position at which the memory to be freed is located. + /// Amount of bytes to free. + public static void GameFree(ref IntPtr ptr, ulong size) + { + if (ptr == IntPtr.Zero) + { + return; + } + + IMemorySpace.Free((void*)ptr, size); + ptr = IntPtr.Zero; + } + + #endregion } }