-
Notifications
You must be signed in to change notification settings - Fork 0
/
smallist.c
116 lines (99 loc) · 2.79 KB
/
smallist.c
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
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tlhelp32.h>
#define PATH_MAX_LEN (4 * MAX_PATH)
#define DWORD_MAX 4294967295
#define DWORD_MAX_LEN 10
// comment this out to enable closing handles (pay 28 bytes)
// this does nothing because all handles are closed when the process exists
#define CloseHandle(_)
#if 1
// pay a penalty of 16 bytes for these
#define UNLIKELY(prop) __builtin_expect(prop, 0)
#define LIKELY(prop) __builtin_expect(prop, 1)
#else
#define UNLIKELY(prop) prop
#define LIKELY(prop) prop
#endif
// returns the length
DWORD dwToChars(DWORD dw, char *out) {
DWORD length = 1;
DWORD mul_10 = 10;
if (UNLIKELY(dw == 0)) {
*out = '0';
return 1;
}
while (LIKELY(dw >= mul_10)) {
length++;
mul_10 *= 10;
}
out += length;
while (LIKELY(dw > 0)) {
out--;
*out = '0' + dw % 10;
dw /= 10;
}
return length;
}
void start(void) {
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (UNLIKELY(hStdout == INVALID_HANDLE_VALUE || snap == INVALID_HANDLE_VALUE)) {
ExitProcess(1);
__builtin_unreachable();
}
PROCESSENTRY32W entry;
entry.dwSize = sizeof(PROCESSENTRY32W);
if (UNLIKELY(!Process32FirstW(snap, &entry))) {
CloseHandle(snap);
ExitProcess(1);
__builtin_unreachable();
}
// 16 pages of memory / 64 KiB
#define iobuf_sz 0x10000
char *const restrict iobuf = VirtualAlloc(
NULL,
iobuf_sz,
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE
);
DWORD offset = 0;
do {
DWORD max_size = 0
+ DWORD_MAX_LEN + 1
+ DWORD_MAX_LEN + 1
+ PATH_MAX_LEN + 1;
if (UNLIKELY(offset + max_size > iobuf_sz)) {
if (UNLIKELY(!WriteFile(hStdout, iobuf, offset, NULL, NULL))) {
CloseHandle(snap);
ExitProcess(1);
__builtin_unreachable();
}
offset = 0;
}
offset += dwToChars(entry.th32ProcessID, iobuf + offset);
iobuf[offset] = '\t'; offset += 1;
offset += dwToChars(entry.th32ParentProcessID, iobuf + offset);
iobuf[offset] = '\t'; offset += 1;
// codecvt
offset += WideCharToMultiByte(
CP_UTF8,
0,
entry.szExeFile,
-1, // infer size because szExeFile is null terminated
iobuf + offset,
PATH_MAX_LEN, // size of output buffer
NULL,
NULL
) - 1; // minus one because of the null byte
iobuf[offset] = '\n'; offset += 1;
} while (LIKELY(Process32NextW(snap, &entry)));
if (UNLIKELY(!WriteFile(hStdout, iobuf, offset, NULL, NULL))) {
CloseHandle(snap);
ExitProcess(1);
__builtin_unreachable();
}
CloseHandle(snap);
ExitProcess(0);
__builtin_unreachable();
}