1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/files/memory_mapped_file.h"
7 #include "base/files/file_path.h"
8 #include "base/logging.h"
9 #include "base/metrics/histogram.h"
10 #include "base/string16.h"
11 #include "base/threading/thread_restrictions.h"
15 MemoryMappedFile::MemoryMappedFile()
16 : file_(INVALID_HANDLE_VALUE
),
17 file_mapping_(INVALID_HANDLE_VALUE
),
19 length_(INVALID_FILE_SIZE
) {
22 bool MemoryMappedFile::InitializeAsImageSection(const FilePath
& file_name
) {
25 file_
= CreatePlatformFile(file_name
, PLATFORM_FILE_OPEN
| PLATFORM_FILE_READ
,
28 if (file_
== kInvalidPlatformFileValue
) {
29 DLOG(ERROR
) << "Couldn't open " << file_name
.AsUTF8Unsafe();
33 if (!MapFileToMemoryInternalEx(SEC_IMAGE
)) {
41 bool MemoryMappedFile::MapFileToMemoryInternal() {
42 return MapFileToMemoryInternalEx(0);
45 bool MemoryMappedFile::MapFileToMemoryInternalEx(int flags
) {
46 ThreadRestrictions::AssertIOAllowed();
48 if (file_
== INVALID_HANDLE_VALUE
)
51 length_
= ::GetFileSize(file_
, NULL
);
52 if (length_
== INVALID_FILE_SIZE
)
55 file_mapping_
= ::CreateFileMapping(file_
, NULL
, PAGE_READONLY
| flags
,
58 // According to msdn, system error codes are only reserved up to 15999.
59 // http://msdn.microsoft.com/en-us/library/ms681381(v=VS.85).aspx.
60 UMA_HISTOGRAM_ENUMERATION("MemoryMappedFile.CreateFileMapping",
61 logging::GetLastSystemErrorCode(), 16000);
65 data_
= static_cast<uint8
*>(
66 ::MapViewOfFile(file_mapping_
, FILE_MAP_READ
, 0, 0, 0));
68 UMA_HISTOGRAM_ENUMERATION("MemoryMappedFile.MapViewOfFile",
69 logging::GetLastSystemErrorCode(), 16000);
74 void MemoryMappedFile::CloseHandles() {
76 ::UnmapViewOfFile(data_
);
77 if (file_mapping_
!= INVALID_HANDLE_VALUE
)
78 ::CloseHandle(file_mapping_
);
79 if (file_
!= INVALID_HANDLE_VALUE
)
83 file_mapping_
= file_
= INVALID_HANDLE_VALUE
;
84 length_
= INVALID_FILE_SIZE
;