-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmemory_statistics.cpp
More file actions
61 lines (44 loc) · 2.07 KB
/
memory_statistics.cpp
File metadata and controls
61 lines (44 loc) · 2.07 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
#include "memory_statistics.h"
#include <intrin.h>
#include <Windows.h>
uint32_t DetailedMemoryReport::FillIn() {
__stosb(reinterpret_cast<unsigned char*>(this), 0, sizeof(*this));
MEMORY_BASIC_INFORMATION info;
// Windows never maps memory at the first or last 64 KB of the address-space.
uintptr_t address = 64 << 10;
goto initialIteration;
for (;;) {
if (address >= static_cast<uintptr_t>(-(64 << 10))) { break; }
initialIteration:
if (!VirtualQuery(reinterpret_cast<const void*>(address), &info, sizeof(info))) { return GetLastError(); }
address = reinterpret_cast<uintptr_t>(info.BaseAddress) + info.RegionSize;
size_t pageCount = info.RegionSize >> pageSizeLog2;
if (info.State == MEM_FREE) {
DWORD regionSizeLog2;
_BitScanReverse(®ionSizeLog2, info.RegionSize);
++freeSpanHistogram[regionSizeLog2 - pageSizeLog2];
freePageCount += pageCount;
} else {
static_assert(offsetof(DetailedMemoryReport, imagePageCount) == offsetof(DetailedMemoryReport, mappedPageCount) + 4);
static_assert(offsetof(DetailedMemoryReport, privatePageCount) == offsetof(DetailedMemoryReport, mappedPageCount) + 8);
uint32_t* typePageCount = &mappedPageCount;
typePageCount += info.Type != MEM_MAPPED;
typePageCount += info.Type == MEM_PRIVATE;
*typePageCount += pageCount;
if (info.State == MEM_RESERVE) {
reservedPageCount += pageCount;
} else {
guardPageCount += (info.Protect & PAGE_GUARD) != 0 ? pageCount : 0;
static_assert(PAGE_EXECUTE_WRITECOPY == 0x80);
uint32_t protection = info.Protect & 0x7F;
committedPageCount += pageCount;
if (protection != 0) {
DWORD protectionBit;
_BitScanForward(&protectionBit, protection);
pageCountByProtection[protectionBit] += pageCount;
}
}
}
}
return 0;
}