This repository has been archived by the owner on Sep 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
memmanager.pas
113 lines (96 loc) · 2.49 KB
/
memmanager.pas
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
unit MemManager;
{$mode delphi}
interface
uses
Classes, SysUtils,
Generics.Collections,Generics.Defaults,
Unicorn_dyn, UnicornConst,Utils,
QuickJS;
type
TMemory = record
RealPtr : Pointer;
Base : Int64;
size : UInt64;
perms : UInt32;
IsFree : Boolean;
end;
{ TCmuMemoryManager }
TCmuMemoryManager = Class
private
uc : uc_engine;
Status : THeapStatus; // Memory Status.
Base_Address, // Base Address for Heap.
CurrHeapUsed,
CurrHeapSize,
Size_limit : Int64; // Max size of Heap.
Heap : Generics.Collections.TList<TMemory>;
public
constructor Create(uc : uc_engine; BaseAddress, SizeLimit : Int64);
function GetHeapStatus : THeapStatus;
function GetPrems(HPtr : Int64) : UInt32;
function Alloc(Size : Int64; perms : UInt32) : Int64;
end;
implementation
uses
Globals;
{ TCmuMemoryManager }
constructor TCmuMemoryManager.Create(uc : uc_engine;
BaseAddress, SizeLimit : Int64);
begin
Self.Heap := Generics.Collections.TList<TMemory>.Create; // init heap list.
Self.Base_Address := BaseAddress;
Self.Size_limit := SizeLimit;
Self.CurrHeapUsed := 0;
Self.CurrHeapSize := SizeLimit;
Self.uc := uc;
FillByte(Status,SizeOf(Status),0);
end;
function TCmuMemoryManager.GetHeapStatus : THeapStatus;
begin
FillByte(Status,SizeOf(result),0);
result.TotalAllocated := CurrHeapUsed;
result.TotalFree := CurrHeapSize - CurrHeapUsed;
result.TotalAddrSpace := CurrHeapSize;
result.TotalUncommitted := 0;
result.TotalCommitted := 0;
result.Unused := 0;
result.Overhead := 0;
result.HeapErrorCode := 0;
end;
function TCmuMemoryManager.GetPrems(HPtr : Int64) : UInt32;
begin
Result := 0;
end;
function TCmuMemoryManager.Alloc(Size : Int64; perms : UInt32) : Int64;
var
item,MemItem : TMemory;
LPtr : Pointer;
begin
Result := 0;
while Heap.GetEnumerator.MoveNext do
begin
if Heap.GetEnumerator.Current.IsFree and (Heap.GetEnumerator.Current.size >= Size) then
begin
item := Heap.GetEnumerator.Current;
item.IsFree := False;
Result := item.Base;
Break;
end;
end;
if Result = 0 then
begin
LPtr := AllocMem(Size);
if LPtr <> nil then
begin
Emulator.err := uc_mem_map_ptr(uc, Result, UC_PAGE_SIZE, UC_PROT_ALL,LPtr);
if Emulator.err = UC_ERR_OK then
begin
item.Base := Result;
end
else
begin
end;
end;
end;
end;
end.