forked from AlexanderBagel/ProcessMemoryMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryMap.Workset.pas
More file actions
117 lines (102 loc) · 2.86 KB
/
Copy pathMemoryMap.Workset.pas
File metadata and controls
117 lines (102 loc) · 2.86 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
////////////////////////////////////////////////////////////////////////////////
//
// ****************************************************************************
// * Project : MemoryMap
// * Unit Name : MemoryMap.Workset.pas
// * Purpose : Класс собирает данные о Workset процесса
// * Author : Александр (Rouse_) Багель
// * Copyright : © Fangorn Wizards Lab 1998 - 2022.
// * Version : 1.3.21
// * Home Page : http://rouse.drkb.ru
// * Home Blog : http://alexander-bagel.blogspot.ru
// ****************************************************************************
// * Stable Release : http://rouse.drkb.ru/winapi.php#pmm2
// * Latest Source : https://github.com/AlexanderBagel/ProcessMemoryMap
// ****************************************************************************
//
unit MemoryMap.Workset;
interface
uses
Winapi.Windows,
Winapi.PsAPI,
System.SysUtils,
Generics.Collections;
type
TShareInfo = record
Shared: Boolean;
SharedCount: Byte;
end;
TWorkset = class
private
FData: TDictionary<Pointer, TShareInfo>;
protected
procedure InitWorksetData(hProcess: THandle);
public
constructor Create(hProcess: THandle);
destructor Destroy; override;
function GetPageSharedInfo(pPage: Pointer; var Shared: Boolean;
var SharedCount: Byte): Boolean;
end;
implementation
{ TWorkset }
constructor TWorkset.Create(hProcess: THandle);
begin
FData := TDictionary<Pointer, TShareInfo>.Create;
InitWorksetData(hProcess);
end;
destructor TWorkset.Destroy;
begin
FData.Free;
inherited;
end;
function TWorkset.GetPageSharedInfo(pPage: Pointer; var Shared: Boolean;
var SharedCount: Byte): Boolean;
var
ShareInfo: TShareInfo;
begin
Result := FData.TryGetValue(pPage, ShareInfo);
if Result then
begin
Shared := ShareInfo.Shared;
SharedCount := ShareInfo.SharedCount;
end;
end;
procedure TWorkset.InitWorksetData(hProcess: THandle);
const
{$IFDEF WIN64}
AddrMask = $FFFFFFFFFFFFF000;
{$ELSE}
AddrMask = $FFFFF000;
{$ENDIF}
SharedBitMask = $100;
SharedCountMask = $E0;
function GetSharedCount(Value: ULONG_PTR): Byte; inline;
begin
Result := (Value and SharedCountMask) shr 5;
end;
var
WorksetBuff: array of ULONG_PTR;
I: Integer;
ShareInfo: TShareInfo;
begin
SetLength(WorksetBuff, $400000);
while not QueryWorkingSet(hProcess, @WorksetBuff[0],
Length(WorksetBuff) * SizeOf(ULONG_PTR)) do
begin
if WorksetBuff[0] = 0 then Exit;
SetLength(WorksetBuff, WorksetBuff[0] * 2);
end;
for I := 0 to WorksetBuff[0] - 1 do
begin
ShareInfo.Shared := WorksetBuff[I] and SharedBitMask <> 0;
ShareInfo.SharedCount := GetSharedCount(WorksetBuff[I]);
try
FData.AddOrSetValue(Pointer(WorksetBuff[I] and AddrMask), ShareInfo);
except
on E: EListError do ;
else
raise;
end;
end;
end;
end.