forked from sqlumdash/psmalloc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsm_win.c
309 lines (234 loc) · 5.88 KB
/
psm_win.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
** SPDX-License-Identifier: MIT
**
** Copyright (c) 2019 Toshiba Corporation
*/
#include <Windows.h>
#include <stdarg.h>
#include "psm.h"
#ifdef TESTLOG
#define LOG( ...) logging(__VA_ARGS__)
#else
#define LOG(...) do {} while(0)
#endif
static void logging(const char *format, ...) {
va_list va;
printf("[%d] ", GetCurrentProcessId());
va_start(va, format);
vprintf(format, va);
va_end(va);
}
static int callCreateFileMapping(PSMHandle h, size_t size) {
int ret = 0;
HANDLE hMap;
DWORD high_size = 0;
uint64_t size64 = size;
LOG(" CreateFileMapping size %zu\n", size);
if (sizeof(size_t) > 4) {
high_size = size64 >> 32;
}
hMap = CreateFileMapping(h->hFile, NULL, PAGE_READWRITE, high_size, (DWORD)size, NULL);
if (hMap == NULL) ret = 1;
h->hMap = hMap;
LOG(" CreateFileMapping hMap %p\n", hMap);
LOG(" CreateFileMapping ret %d\n", ret);
return ret;
}
static void resetPSMHandle(PSMHandle h) {
LOG(" resetPSMHandle\n");
h->hFile = INVALID_HANDLE_VALUE;
h->hMap = NULL;
h->pMap = NULL;
h->name[0] = '\0';
h->pAllocator = NULL;
h->length = 0;
}
static int openPSM(PSMHandle h, const char *name) {
int ret = 0;
LOG(" openPSM\n", h, name);
h->hFile = CreateFileA(name, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_RANDOM_ACCESS, NULL);
if (h->hFile == INVALID_HANDLE_VALUE) ret = 1;
LOG(" openPSM open handle %p\n", h->hFile);
LOG(" openPSM ret %d\n", ret);
return ret;
}
static void closePSM(PSMHandle h) {
LOG(" closePSM\n");
if (h->hFile != INVALID_HANDLE_VALUE) {
LOG(" closePSM CloseHandle %p\n", h->hFile);
CloseHandle(h->hFile);
}
}
static int mapPSM(PSMHandle h, size_t maplen, void *pReqAddress) {
int ret = 0;
LOG(" mapPSM\n");
if (h->hMap == NULL) {
if (callCreateFileMapping(h, maplen)) ret = 1;
}
if (h->hMap != NULL) {
LOG(" mapPSM MapViewOfFileEx\n");
h->pMap = MapViewOfFileEx(h->hMap, FILE_MAP_WRITE, 0, 0, 0, pReqAddress);
if ((h->pMap == NULL) || (pReqAddress != NULL && h->pMap != pReqAddress)) ret = 1;
LOG(" mapPSM MapViewOfFileEx pMap %p\n", h->pMap);
}
if (ret == 0) {
h->length = maplen;
}
LOG(" mapPSM ret %d\n", ret);
return ret;
}
static void unmapPSM(PSMHandle h) {
LOG(" unmapPSM\n");
if (h->pMap != NULL) {
LOG(" unmapPSM UnmapViewOfFile %p\n", h->pMap);
UnmapViewOfFile(h->pMap);
h->pMap = NULL;
}
if (h->hMap != NULL) {
LOG(" unmapPSM CloseHandle %p\n", h->hMap);
CloseHandle(h->hMap);
h->hMap = NULL;
}
}
static uint32_t getProcessID(PSMProcess *proc) {
return *proc;
}
static int lockPSM(PSMHandle h) {
int ret = 0;
OVERLAPPED overlapped;
BOOL bret;
LOG(" lockPSM\n");
memset(&overlapped, 0, sizeof(overlapped));
bret = LockFileEx(h->hFile, LOCKFILE_EXCLUSIVE_LOCK, 0, MAXDWORD, MAXDWORD, &overlapped);
if (bret == 0) ret = 1;
LOG(" lockPSM ret %d\n", ret);
return ret;
}
static int unlockPSM(PSMHandle h) {
int ret = 0;
OVERLAPPED overlapped;
BOOL bret;
LOG(" unlockPSM\n");
memset(&overlapped, 0, sizeof(overlapped));
bret = UnlockFileEx(h->hFile, 0, MAXDWORD, MAXDWORD, &overlapped);
if (bret == 0) ret = 1;
LOG(" unlockPSM result %d\n", ret);
return ret;
}
/* Prepare file size. */
static int preparePSMFile(PSMHandle h, size_t size) {
int ret;
LOG(" preparePSMFile\n");
ret = callCreateFileMapping(h, size);
LOG(" preparePSMFile ret %d\n", ret);
return ret;
}
/* Empty file. */
static int emptyPSMFile(PSMHandle h) {
BOOL bret;
LARGE_INTEGER i64;
LARGE_INTEGER i64_cur;
int ret = 0;
LOG(" emptyPSMFile\n");
i64.QuadPart = 0;
bret = SetFilePointerEx(h->hFile, i64, &i64_cur, FILE_BEGIN);
if (bret == 0) {
ret = 1;
}
else {
bret = SetEndOfFile(h->hFile);
if (bret == 0) {
ret = 1;
}
}
LOG(" emptyPSMFile ret %d\n", ret);
return ret;
}
/* Remove file. */
static int removePSMFile(PSMHandle h) {
int ret = 0;
BOOL bret;
LOG(" removePSMFile\n");
bret = DeleteFileA(h->name);
if (bret == 0) ret = 1;
LOG(" removePSMFile ret %d\n", ret);
return ret;
}
/* Get file size from file descriptor. */
static int getPSMFileSize(PSMHandle h, size_t *size) {
int ret = 0;
LARGE_INTEGER i64;
BOOL bret;
uint64_t size64;
LOG(" getPSMFileSize\n");
bret = GetFileSizeEx(h->hFile, &i64);
if (bret == 0) ret = 1;
size64 = i64.QuadPart;
if (sizeof(size_t) > 4) {
*size = (size_t)size64;
} else {
*size = i64.LowPart;
}
LOG(" getPSMFileSize ret %d\n", ret);
return ret;
}
static void getCurrentProcess(PSMProcess* pProc) {
*pProc = GetCurrentProcessId();
}
static int isEqualProcess(PSMProcess proc1, PSMProcess proc2) {
int ret = 0;
if (proc1 == proc2)
ret = 1;
return ret;
}
static int isExistProcess(PSMProcess proc) {
int ret = 0;
HANDLE hProcess = INVALID_HANDLE_VALUE;
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, proc);
if (hProcess != INVALID_HANDLE_VALUE) {
DWORD status;
if (GetExitCodeProcess(hProcess, &status) != 0) {
if (status == STILL_ACTIVE) {
ret = 1;
}
}
CloseHandle(hProcess);
}
return ret;
}
static void copyString(char *dest, const char *src, size_t n) {
strncpy_s(dest, n, src, n-1);
}
static int normalizePath(char *dest, const char *src, size_t dn) {
int ret = 0;
DWORD dret;
LOG(" normalizePath\n");
dret = GetFullPathNameA(src, (DWORD)dn, dest, NULL);
if (dret == 0) {
ret = 1;
}
LOG(" normalizePath result %d\n", ret);
return ret;
}
PSM_EXPORT PSMPlatformFunctionsType PSMPlatformFunctions = {
resetPSMHandle,
lockPSM,
unlockPSM,
preparePSMFile,
emptyPSMFile,
removePSMFile,
getPSMFileSize,
openPSM,
closePSM,
mapPSM,
unmapPSM,
copyString,
normalizePath,
getProcessID,
getCurrentProcess,
isEqualProcess,
isExistProcess,
logging,
};