summaryrefslogtreecommitdiff
path: root/src/os_win/os_map.c
blob: c0aa6dac28f367c6788a5a340fd4f08ddb0ef144 (plain)
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
/*-
 * Copyright (c) 2014-2017 MongoDB, Inc.
 * Copyright (c) 2008-2014 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

#include "wt_internal.h"

/*
 * __wt_win_map --
 *	Map a file into memory.
 */
int
__wt_win_map(WT_FILE_HANDLE *file_handle, WT_SESSION *wt_session,
    void *mapped_regionp, size_t *lenp, void *mapped_cookiep)
{
	DWORD windows_error;
	WT_FILE_HANDLE_WIN *win_fh;
	WT_SESSION_IMPL *session;
	size_t len;
	wt_off_t file_size;
	void *map, *mapped_cookie;

	win_fh = (WT_FILE_HANDLE_WIN *)file_handle;
	session = (WT_SESSION_IMPL *)wt_session;

	/*
	 * There's no locking here to prevent the underlying file from changing
	 * underneath us, our caller needs to ensure consistency of the mapped
	 * region vs. any other file activity.
	 */
	WT_RET(__wt_win_fs_size(file_handle->file_system,
		wt_session, file_handle->name, &file_size));
	len = (size_t)file_size;

	__wt_verbose(session, WT_VERB_HANDLEOPS,
	    "%s: memory-map: %" WT_SIZET_FMT " bytes", file_handle->name, len);

	mapped_cookie = CreateFileMappingW(
	    win_fh->filehandle, NULL, PAGE_READONLY, 0, 0, NULL);
	if (mapped_cookie == NULL) {
		windows_error = __wt_getlasterror();
		__wt_errx(session,
		    "%s: memory-map: CreateFileMappingW: %s",
		    file_handle->name,
		    __wt_formatmessage(session, windows_error));
		return (__wt_map_windows_error(windows_error));
	}

	if ((map =
	    MapViewOfFile(mapped_cookie, FILE_MAP_READ, 0, 0, len)) == NULL) {
		/* Retrieve the error before cleaning up. */
		windows_error = __wt_getlasterror();

		(void)CloseHandle(mapped_cookie);

		__wt_errx(session,
		    "%s: memory-map: MapViewOfFile: %s",
		    file_handle->name,
		    __wt_formatmessage(session, windows_error));
		return (__wt_map_windows_error(windows_error));
	}

	*(void **)mapped_cookiep = mapped_cookie;
	*(void **)mapped_regionp = map;
	*lenp = len;
	return (0);
}

/*
 * __wt_win_unmap --
 *	Remove a memory mapping.
 */
int
__wt_win_unmap(WT_FILE_HANDLE *file_handle, WT_SESSION *wt_session,
    void *mapped_region, size_t length, void *mapped_cookie)
{
	DWORD windows_error;
	WT_DECL_RET;
	WT_FILE_HANDLE_WIN *win_fh;
	WT_SESSION_IMPL *session;

	win_fh = (WT_FILE_HANDLE_WIN *)file_handle;
	session = (WT_SESSION_IMPL *)wt_session;

	__wt_verbose(session, WT_VERB_HANDLEOPS,
	    "%s: memory-unmap: %" WT_SIZET_FMT " bytes",
	    file_handle->name, length);

	if (UnmapViewOfFile(mapped_region) == 0) {
		windows_error = __wt_getlasterror();
		__wt_errx(session,
		    "%s: memory-unmap: UnmapViewOfFile: %s",
		    file_handle->name,
		    __wt_formatmessage(session, windows_error));
		ret = __wt_map_windows_error(windows_error);
	}

	if (CloseHandle(*(void **)mapped_cookie) == 0) {
		windows_error = __wt_getlasterror();
		__wt_errx(session,
		    "%s: memory-unmap: CloseHandle: %s",
		    file_handle->name,
		    __wt_formatmessage(session, windows_error));
		ret = __wt_map_windows_error(windows_error);
	}

	return (ret);
}