/
MemoryMappedFile.cpp
154 lines (131 loc) · 3.97 KB
/
MemoryMappedFile.cpp
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
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
#include "stdafx.h"
#include "MemoryMappedFile.h"
MemoryMappedFile::MemoryMappedFile() :
m_mapFile(INVALID_HANDLE_VALUE),
m_file(INVALID_HANDLE_VALUE),
m_mapAddress(nullptr),
m_currentFileSize(0)
{
}
MemoryMappedFile::~MemoryMappedFile()
{
}
void MemoryMappedFile::Init(std::wstring filename, UINT fileSize)
{
m_filename = filename;
WIN32_FIND_DATA findFileData;
HANDLE handle = FindFirstFileEx(filename.c_str(), FindExInfoBasic, &findFileData, FindExSearchNameMatch, nullptr, 0);
bool found = handle != INVALID_HANDLE_VALUE;
if (found)
{
FindClose(handle);
}
m_file = CreateFile2(
filename.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
(found) ? OPEN_EXISTING : CREATE_NEW,
nullptr);
if (m_file == INVALID_HANDLE_VALUE)
{
std::cerr << (L"m_file is invalid. Error %ld.\n", GetLastError());
std::cerr << (L"Target file is %s\n", filename.c_str());
return;
}
LARGE_INTEGER realFileSize = {};
BOOL flag = GetFileSizeEx(m_file, &realFileSize);
if (!flag)
{
std::cerr << (L"\nError %ld occurred in GetFileSizeEx!", GetLastError());
assert(false);
return;
}
assert(realFileSize.HighPart == 0);
m_currentFileSize = realFileSize.LowPart;
if (m_currentFileSize == 0)
{
// File mapping files with a size of 0 produces an error.
m_currentFileSize = DefaultFileSize;
}
else if(fileSize > m_currentFileSize)
{
// Grow to the specified size.
m_currentFileSize = fileSize;
}
m_mapFile = CreateFileMapping(m_file, nullptr, PAGE_READWRITE, 0, m_currentFileSize, nullptr);
if (m_mapFile == nullptr)
{
std::cerr << (L"m_mapFile is NULL: last error: %d\n", GetLastError());
assert(false);
return;
}
m_mapAddress = MapViewOfFile(m_mapFile, FILE_MAP_ALL_ACCESS, 0, 0, m_currentFileSize);
if (m_mapAddress == nullptr)
{
std::cerr << (L"m_mapAddress is NULL: last error: %d\n", GetLastError());
assert(false);
return;
}
}
void MemoryMappedFile::Destroy(bool deleteFile)
{
if (m_mapAddress)
{
BOOL flag = UnmapViewOfFile(m_mapAddress);
if (!flag)
{
std::cerr << (L"\nError %ld occurred unmapping the view!", GetLastError());
assert(false);
}
m_mapAddress = nullptr;
flag = CloseHandle(m_mapFile); // Close the file mapping object.
if (!flag)
{
std::cerr << (L"\nError %ld occurred closing the mapping object!", GetLastError());
assert(false);
}
flag = CloseHandle(m_file); // Close the file itself.
if (!flag)
{
std::cerr << (L"\nError %ld occurred closing the file!", GetLastError());
assert(false);
}
}
if (deleteFile)
{
DeleteFile(m_filename.c_str());
}
}
void MemoryMappedFile::GrowMapping(UINT size)
{
// Add space for the extra size at the beginning of the file.
size += sizeof(UINT);
// Check the size.
if (size <= m_currentFileSize)
{
// Don't shrink.
return;
}
// Flush.
BOOL flag = FlushViewOfFile(m_mapAddress, 0);
if (!flag)
{
std::cerr << (L"\nError %ld occurred flushing the mapping object!", GetLastError());
assert(false);
}
// Close the current mapping.
Destroy(false);
// Update the size and create a new mapping.
m_currentFileSize = size;
Init(m_filename, m_currentFileSize);
}