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/strings/string16.h"
9 #include "base/threading/thread_restrictions.h"
13 MemoryMappedFile::MemoryMappedFile() : data_(NULL
), length_(0), image_(false) {
16 bool MemoryMappedFile::InitializeAsImageSection(const FilePath
& file_name
) {
18 return Initialize(file_name
);
21 bool MemoryMappedFile::MapFileToMemory() {
22 ThreadRestrictions::AssertIOAllowed();
27 int64 len
= file_
.GetLength();
28 if (len
<= 0 || len
> kint32max
)
30 length_
= static_cast<size_t>(len
);
32 int flags
= image_
? SEC_IMAGE
| PAGE_READONLY
: PAGE_READONLY
;
34 file_mapping_
.Set(::CreateFileMapping(file_
.GetPlatformFile(), NULL
,
36 if (!file_mapping_
.IsValid())
39 data_
= static_cast<uint8
*>(::MapViewOfFile(file_mapping_
.Get(),
40 FILE_MAP_READ
, 0, 0, 0));
44 void MemoryMappedFile::CloseHandles() {
46 ::UnmapViewOfFile(data_
);
47 if (file_mapping_
.IsValid())
48 file_mapping_
.Close();