forked from DaxxTrias/Process.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryFactory.cs
More file actions
124 lines (114 loc) · 4.72 KB
/
MemoryFactory.cs
File metadata and controls
124 lines (114 loc) · 4.72 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Linq;
using ProcessNET.Native.Types;
using ProcessNET.Utilities;
namespace ProcessNET.Memory
{
/// <summary>
/// Class providing tools for manipulating memory space.
/// </summary>
public class MemoryFactory : IMemoryFactory
{
/// <summary>
/// The list containing all allocated memory.
/// </summary>
protected readonly List<IAllocatedMemory> InternalRemoteAllocations;
/// <summary>
/// The reference of the <see cref="Process" /> object.
/// </summary>
protected readonly IProcess Process;
protected bool isDisposed = false;
/// <summary>
/// Initializes a new instance of the <see cref="MemoryFactory" /> class.
/// </summary>
/// <param name="process">The reference of the <see cref="Process" /> object.</param>
public MemoryFactory(IProcess process)
{
// Save the parameter
Process = process;
// Create a list containing all allocated memory
InternalRemoteAllocations = new List<IAllocatedMemory>();
}
/// <summary>
/// A collection containing all allocated memory in the remote process.
/// </summary>
public IEnumerable<IAllocatedMemory> Allocations => InternalRemoteAllocations.AsReadOnly();
/// <summary>
/// Gets the <see cref="IAllocatedMemory" /> with the specified name.
/// </summary>
/// <value>
/// The <see cref="IAllocatedMemory" />.
/// </value>
/// <param name="name">The name.</param>
/// <returns></returns>
public IAllocatedMemory this[string name]
{
get { return InternalRemoteAllocations.FirstOrDefault(am => am.Identifier == name); }
}
/// <summary>
/// Gets all blocks of memory allocated in the remote process.
/// </summary>
public IEnumerable<MemoryRegion> Regions
{
get
{
var size = Process.Memory.Is32Bit ? 4 : 8;
var adresseTo = size == 8 ? new IntPtr(0x7fffffffffffffff) : new IntPtr(0x7fffffff);
return
MemoryHelper.Query(Process.Handle, IntPtr.Zero, adresseTo)
.Select(page => new MemoryRegion(Process, page.BaseAddress));
}
}
/// <summary>
/// Allocates a region of memory within the virtual address space of the remote process.
/// </summary>
/// <param name="name"></param>
/// <param name="size">The size of the memory to allocate.</param>
/// <param name="protection">The protection of the memory to allocate.</param>
/// <param name="mustBeDisposed">The allocated memory will be released when the finalizer collects the object.</param>
public IAllocatedMemory Allocate(string name, int size,
MemoryProtectionFlags protection = MemoryProtectionFlags.ExecuteReadWrite, bool mustBeDisposed = true)
{
// Allocate a memory space
var memory = new AllocatedMemory(Process, name, size, protection, mustBeDisposed);
// Add the memory in the list
InternalRemoteAllocations.Add(memory);
return memory;
}
/// <summary>
/// Deallocates a region of memory previously allocated within the virtual address space of the remote process.
/// </summary>
/// <param name="allocation">The allocated memory to release.</param>
public void Deallocate(IAllocatedMemory allocation)
{
// Dispose the element
if (!allocation.IsDisposed)
allocation.Dispose();
// Remove the element from the allocated memory list
if (InternalRemoteAllocations.Contains(allocation))
InternalRemoteAllocations.Remove(allocation);
}
/// <summary>
/// Releases all resources used by the <see cref="MemoryFactory" /> object.
/// </summary>
public virtual void Dispose()
{
if (isDisposed)
return;
isDisposed = true;
// Release all allocated memories which must be disposed
foreach (var allocatedMemory in InternalRemoteAllocations.Where(m => m.MustBeDisposed).ToArray())
allocatedMemory.Dispose();
// Avoid the finalizer
GC.SuppressFinalize(this);
}
/// <summary>
/// Frees resources and perform other cleanup operations before it is reclaimed by garbage collection.
/// </summary>
~MemoryFactory()
{
Dispose();
}
}
}