-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExecutableMemory.cs
42 lines (38 loc) · 1.22 KB
/
ExecutableMemory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.IO;
using Iced.Intel;
public static unsafe partial class ExecutableMemory
{
public static void* Allocate(ReadOnlySpan<byte> code)
{
if (OperatingSystem.IsWindows())
return AllocateWindows(code);
else if (OperatingSystem.IsLinux())
return AllocateUnix(code);
else
throw new PlatformNotSupportedException();
}
public static void* Allocate(Assembler asm)
{
ArgumentNullException.ThrowIfNull(asm);
using var ms = new MemoryStream();
asm.Assemble(new StreamCodeWriter(ms), 0);
return Allocate(ms.GetBuffer().AsSpan(0, (int)ms.Length));
}
public static void* Allocate(Action<Assembler> generator)
{
ArgumentNullException.ThrowIfNull(generator);
var asm = new Assembler(Environment.Is64BitProcess ? 64 : 32);
generator(asm);
return Allocate(asm);
}
public static void Free(void* address)
{
if (OperatingSystem.IsWindows())
FreeWindows(address);
else if (OperatingSystem.IsLinux())
FreeUnix(address);
else
throw new PlatformNotSupportedException();
}
}