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
#include "page_routines.h"
#include <assert.h>
#include <windows.h>
void* platform_map_pages(void const* addr, size_t length, enum page_location location) {
LPVOID base_address = NULL;
if (location == PAGE_FIXED) {
base_address = (LPVOID) addr; /* use address hint from argument */
}
/* split length (64-bit) into two DWORDs (32-bit) parameters because Win32 */
DWORD len_low = (DWORD)(length & 0xFFFFFFFFL);
DWORD len_high = (DWORD)((length >> 32) & 0xFFFFFFFFL);
HANDLE anon_handle = CreateFileMapping(
/* Anonymous mapping */ INVALID_HANDLE_VALUE,
/* Whatever, security stuff*/ NULL,
PAGE_EXECUTE_READWRITE,
len_high, len_low,
/* Do not give a name to the mapping */ NULL);
if (anon_handle == NULL) {
return MAP_PAGES_FAILURE;
}
void* map = MAP_PAGES_FAILURE;
/* if base_address is not NULL, API call will fail if it cannot create a mapping */
map = MapViewOfFileEx(anon_handle,
FILE_MAP_READ | FILE_MAP_WRITE,
/* offset DWORDs */ 0, 0,
length,
(LPVOID) addr);
CloseHandle(anon_handle);
if (map == NULL) {
return MAP_PAGES_FAILURE;
}
return map;
}
int platform_unmap_pages(void const* addr, size_t length) {
(void) length;
if (UnmapViewOfFile(addr)) {
return 0;
}
return -1;
}
int platform_page_size() {
SYSTEM_INFO si;
GetSystemInfo(&si);
return (int)si.dwPageSize;
}