Created
October 16, 2018 22:35
-
-
Save mjs3339/77b810a0edff4473e5549ce252016268 to your computer and use it in GitHub Desktop.
C# AllocUmm Allocate Unmanaged Memory Wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static unsafe class AllocUmm | |
{ | |
[Flags] | |
public enum AllocationType : uint | |
{ | |
COMMIT = 0x1000, | |
RESERVE = 0x2000, | |
RESET = 0x80000, | |
LARGE_PAGES = 0x20000000, | |
PHYSICAL = 0x400000, | |
TOP_DOWN = 0x100000, | |
WRITE_WATCH = 0x200000 | |
} | |
[Flags] | |
public enum MemoryProtection : uint | |
{ | |
EXECUTE = 0x10, | |
EXECUTE_READ = 0x20, | |
EXECUTE_READWRITE = 0x40, | |
EXECUTE_WRITECOPY = 0x80, | |
NOACCESS = 0x01, | |
READONLY = 0x02, | |
READWRITE = 0x04, | |
WRITECOPY = 0x08, | |
GUARD_Modifierflag = 0x100, | |
NOCACHE_Modifierflag = 0x200, | |
WRITECOMBINE_Modifierflag = 0x400 | |
} | |
public static IntPtr New(int Size) | |
{ | |
if(MemoryInfo.GetValues().PhysicalAvailable < Size) | |
throw new Exception("Error: Requested size cannot exceed available physical memory."); | |
var p = Marshal.AllocHGlobal(Size); | |
if(p == IntPtr.Zero) | |
throw new Exception("Error: Out of physical memory."); | |
return p; | |
} | |
public static IntPtr NewAndZero(int Size) | |
{ | |
if(MemoryInfo.GetValues().PhysicalAvailable < Size) | |
throw new Exception("Error: Requested size cannot exceed available physical memory."); | |
var p = (byte*) Marshal.AllocHGlobal(Size).ToPointer(); | |
if(p == null) | |
throw new Exception("Error: Out of physical memory."); | |
for(var i = 0; i < Size; i++) | |
*(p + i) = 0; | |
return(IntPtr) p; | |
} | |
public static void Free(IntPtr pointerToUnmanagedMemory) | |
{ | |
Marshal.FreeHGlobal(pointerToUnmanagedMemory); | |
} | |
public static IntPtr Resize(IntPtr oldPointer, int newSize) | |
{ | |
if(MemoryInfo.GetValues().PhysicalAvailable < newSize) | |
throw new Exception("Error: Requested size cannot exceed available physical memory."); | |
var p = Marshal.ReAllocHGlobal(oldPointer, new IntPtr(newSize)); | |
if(p == IntPtr.Zero) | |
throw new Exception("Error: Out of physical memory."); | |
return p; | |
} | |
//////////////// VA /////////////////// | |
[DllImport("kernel32.dll", SetLastError = true)] | |
private static extern IntPtr VirtualAlloc(IntPtr lpAddress, ulong dwSize, AllocationType flAllocationType, MemoryProtection flProtect); | |
[DllImport("kernel32.dll")] | |
private static extern bool VirtualFree(IntPtr lpAddress, uint dwSize, uint dwFreeType); | |
public static IntPtr VAMemory(long Size) | |
{ | |
if(MemoryInfo.GetValues().PhysicalAvailable < Size) | |
throw new Exception("Error: Requested size cannot exceed available physical memory."); | |
var p = VirtualAlloc(IntPtr.Zero, (ulong) Size, AllocationType.COMMIT | AllocationType.RESERVE, MemoryProtection.READWRITE); | |
if(p == IntPtr.Zero) | |
throw new Exception("Error: Out of physical memory."); | |
return p; | |
} | |
public static byte* NewVa(long Size) | |
{ | |
return(byte*) VAMemory(Size).ToPointer(); | |
} | |
public static void StressTestMemory(float percentage) | |
{ | |
var rng = new CRng(); | |
var apm = MemoryInfo.GetValues().PhysicalAvailable; | |
var bta = (long) (apm * (percentage / 100)); | |
var p = VAMemory(bta); | |
var src = (byte*) p.ToPointer(); | |
for(var j = 0L; j < bta; j += 500) | |
try | |
{ | |
src[j] = rng.NextByte(255); | |
} | |
catch(Exception ex) | |
{ | |
break; | |
} | |
VAFree(p); | |
} | |
public static void VAFree(IntPtr p) | |
{ | |
VirtualFree(p, 0, 0x8000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment