-
Notifications
You must be signed in to change notification settings - Fork 15
/
win32.c
237 lines (194 loc) · 6.76 KB
/
win32.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
/*
* AUTHOR
*
* Ash Berlin <[email protected]>
*
* Based on code by
* Rob Mueller <[email protected]>
*
* COPYRIGHT AND LICENSE
*
* Copyright (C) 2007 by Ash Berlin
*
* This library is free software; you can redistribute it and/or modify
* it under the same terms as Perl itself.
*
*/
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdarg.h>
#include "mmap_cache.h"
#include "mmap_cache_internals.h"
#ifdef _MSC_VER
#if _MSC_VER <= 1310
#define vsnprintf _vsnprintf
#endif
#endif
char* _mmc_get_def_share_filename(mmap_cache * cache)
{
int ret;
static char buf[MAX_PATH];
ret = GetTempPath(MAX_PATH, buf);
if (ret > MAX_PATH)
{
_mmc_set_error(cache, GetLastError(), "Unable to get temp path");
return NULL;
}
return strcat(buf, "sharefile");
}
int mmc_open_cache_file(mmap_cache* cache, int* do_init) {
int i;
void *tmp;
HANDLE fh, fileMap, findHandle;
WIN32_FIND_DATA statbuf;
*do_init = 0;
findHandle = FindFirstFile(cache->share_file, &statbuf);
/* Create file if it doesn't exist */
if (findHandle == INVALID_HANDLE_VALUE) {
fh = CreateFile(cache->share_file, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL);
if (fh == INVALID_HANDLE_VALUE) {
_mmc_set_error(cache, GetLastError(), "Create of share file %s failed", cache->share_file);
return -1;
}
/* Fill file with 0's */
tmp = calloc(1, cache->c_page_size);
if (!tmp) {
_mmc_set_error(cache, GetLastError(), "Calloc of tmp space failed");
return -1;
}
for (i = 0; i < cache->c_num_pages; i++) {
DWORD tmpOut;
WriteFile(fh, tmp, cache->c_page_size, &tmpOut, NULL);
}
free(tmp);
/* Later on initialise page structures */
*do_init = 1;
CloseHandle(fh);
} else {
FindClose(findHandle);
if (cache->init_file || (statbuf.nFileSizeLow != cache->c_size)) {
*do_init = 1;
fh = CreateFile(cache->share_file, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL);
if (fh == INVALID_HANDLE_VALUE) {
_mmc_set_error(cache, GetLastError(), "Truncate of existing share file %s failed", cache->share_file);
return -1;
}
CloseHandle(fh);
}
}
fh = CreateFile(cache->share_file, // File Name
GENERIC_READ|GENERIC_WRITE, // Desired Access
FILE_SHARE_READ|FILE_SHARE_WRITE, // Share mode
NULL, // Security Rights
OPEN_EXISTING, // Creation Mode
FILE_ATTRIBUTE_TEMPORARY, // File Attribs
NULL); // Template File
if (fh == INVALID_HANDLE_VALUE) {
_mmc_set_error(cache, GetLastError(), "Open of share file \"%s\" failed", cache->share_file);
return -1;
}
cache->fh = fh;
return 0;
}
int mmc_map_memory(mmap_cache * cache) {
HANDLE fileMap = CreateFileMapping(cache->fh, NULL, PAGE_READWRITE, 0, cache->c_size, NULL);
if (fileMap == NULL) {
_mmc_set_error(cache, GetLastError(), "CreateFileMapping of %s failed", cache->share_file);
CloseHandle(cache->fh);
return -1;
}
cache->mm_var = MapViewOfFile(fileMap, FILE_MAP_WRITE|FILE_MAP_READ, 0,0,0);
if (cache->mm_var == NULL) {
_mmc_set_error(cache, GetLastError(), "Mmap of shared file %s failed", cache->share_file);
CloseHandle(fileMap);
CloseHandle(cache->fh);
return -1;
}
/* If I read the docs right, this will do nothing untill the mm_var is unmapped */
if (CloseHandle(fileMap) == FALSE) {
_mmc_set_error(cache, GetLastError(), "CloseHandle(fileMap) on shared file %s failed", cache->share_file);
UnmapViewOfFile(cache->mm_var);
CloseHandle(fileMap);
CloseHandle(cache->fh);
return -1;
}
return 0;
}
int mmc_close_fh(mmap_cache* cache) {
int ret = CloseHandle(cache->fh);
cache->fh = NULL;
return ret;
}
int mmc_unmap_memory(mmap_cache* cache) {
int res = UnmapViewOfFile(cache->mm_var);
if (res == -1) {
_mmc_set_error(cache, GetLastError(), "Unmmap of shared file %s failed", cache->share_file);
}
return res;
}
int mmc_lock_page(mmap_cache* cache, MU64 p_offset) {
OVERLAPPED lock;
DWORD lock_res, bytesTransfered;
memset(&lock, 0, sizeof(lock));
lock.Offset = (DWORD)(p_offset & 0xffffffff);
lock.OffsetHigh = (DWORD)((p_offset >> 32) & 0xffffffff);
lock.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (LockFileEx(cache->fh, 0, 0, cache->c_page_size, 0, &lock) == 0) {
_mmc_set_error(cache, GetLastError(), "LockFileEx failed");
return -1;
}
lock_res = WaitForSingleObjectEx(lock.hEvent, 10000, FALSE);
if (lock_res != WAIT_OBJECT_0 || GetOverlappedResult(cache->fh, &lock, &bytesTransfered, FALSE) == FALSE) {
CloseHandle(lock.hEvent);
_mmc_set_error(cache, GetLastError(), "Overlapped Lock failed");
return -1;
}
return 0;
}
int mmc_unlock_page(mmap_cache* cache) {
OVERLAPPED lock;
memset(&lock, 0, sizeof(lock));
lock.Offset = cache->p_offset;
lock.hEvent = 0;
UnlockFileEx(cache->fh, 0, cache->c_page_size, 0, &lock);
/* Set to bad value while page not locked */
cache->p_cur = NOPAGE;
}
/*
* int _mmc_set_error(mmap_cache *cache, int err, char * error_string, ...)
*
* Set internal error string/state
*
*/
int _mmc_set_error(mmap_cache *cache, int err, char * error_string, ...) {
va_list ap;
static char errbuf[1024];
char *msgBuff;
va_start(ap, error_string);
/* Make sure it's terminated */
errbuf[1023] = '\0';
/* Start with error string passed */
vsnprintf(errbuf, 1023, error_string, ap);
/* Add system error code if passed */
if (err) {
strncat(errbuf, ": ", 1023);
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &msgBuff,
0, NULL );
strncat(errbuf, msgBuff, 1023);
LocalFree(msgBuff);
}
/* Save in cache object */
cache->last_error = errbuf;
va_end(ap);
return -1;
}