| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/files/memory_mapped_file.h" | 5 #include "base/files/memory_mapped_file.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
| 9 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
| 10 | 10 |
| 11 namespace base { | 11 namespace base { |
| 12 | 12 |
| 13 MemoryMappedFile::MemoryMappedFile() : data_(NULL), length_(0), image_(false) { | 13 MemoryMappedFile::MemoryMappedFile() : data_(NULL), length_(0), image_(false) { |
| 14 } | 14 } |
| 15 | 15 |
| 16 bool MemoryMappedFile::InitializeAsImageSection(const FilePath& file_name) { | 16 bool MemoryMappedFile::InitializeAsImageSection(const FilePath& file_name) { |
| 17 image_ = true; | 17 image_ = true; |
| 18 return Initialize(file_name); | 18 return Initialize(file_name); |
| 19 } | 19 } |
| 20 | 20 |
| 21 bool MemoryMappedFile::MapFileRegionToMemory( | 21 bool MemoryMappedFile::MapFileRegionToMemory( |
| 22 const MemoryMappedFile::Region& region) { | 22 const MemoryMappedFile::Region& region) { |
| 23 ThreadRestrictions::AssertIOAllowed(); | 23 ThreadRestrictions::AssertIOAllowed(); |
| 24 | 24 |
| 25 if (!file_.IsValid()) | 25 if (!file_.IsValid()) |
| 26 return false; | 26 return false; |
| 27 | 27 |
| 28 int flags = image_ ? SEC_IMAGE | PAGE_READONLY : PAGE_READONLY; | 28 int flags = 0; |
| 29 if (image_) |
| 30 flags = SEC_IMAGE | PAGE_READONLY; |
| 31 else |
| 32 flags = (file_.flags() & File::FLAG_WRITE || |
| 33 file_.flags() & File::FLAG_APPEND) ? |
| 34 PAGE_READWRITE : PAGE_READONLY; |
| 29 | 35 |
| 30 file_mapping_.Set(::CreateFileMapping(file_.GetPlatformFile(), NULL, | 36 file_mapping_.Set(::CreateFileMapping(file_.GetPlatformFile(), NULL, |
| 31 flags, 0, 0, NULL)); | 37 flags, 0, 0, NULL)); |
| 32 if (!file_mapping_.IsValid()) | 38 if (!file_mapping_.IsValid()) |
| 33 return false; | 39 return false; |
| 34 | 40 |
| 35 LARGE_INTEGER map_start = {0}; | 41 LARGE_INTEGER map_start = {0}; |
| 36 SIZE_T map_size = 0; | 42 SIZE_T map_size = 0; |
| 37 int32 data_offset = 0; | 43 int32 data_offset = 0; |
| 38 | 44 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 59 if (aligned_start < 0 || size < 0 || | 65 if (aligned_start < 0 || size < 0 || |
| 60 static_cast<uint64>(size) > std::numeric_limits<SIZE_T>::max()) { | 66 static_cast<uint64>(size) > std::numeric_limits<SIZE_T>::max()) { |
| 61 DLOG(ERROR) << "Region bounds are not valid for MapViewOfFile"; | 67 DLOG(ERROR) << "Region bounds are not valid for MapViewOfFile"; |
| 62 return false; | 68 return false; |
| 63 } | 69 } |
| 64 map_start.QuadPart = aligned_start; | 70 map_start.QuadPart = aligned_start; |
| 65 map_size = static_cast<SIZE_T>(size); | 71 map_size = static_cast<SIZE_T>(size); |
| 66 length_ = static_cast<size_t>(region.size); | 72 length_ = static_cast<size_t>(region.size); |
| 67 } | 73 } |
| 68 | 74 |
| 75 int map_flags = (flags & PAGE_READONLY) ? FILE_MAP_READ : FILE_MAP_WRITE; |
| 69 data_ = static_cast<uint8*>(::MapViewOfFile(file_mapping_.Get(), | 76 data_ = static_cast<uint8*>(::MapViewOfFile(file_mapping_.Get(), |
| 70 FILE_MAP_READ, | 77 map_flags, |
| 71 map_start.HighPart, | 78 map_start.HighPart, |
| 72 map_start.LowPart, | 79 map_start.LowPart, |
| 73 map_size)); | 80 map_size)); |
| 74 if (data_ == NULL) | 81 if (data_ == NULL) |
| 75 return false; | 82 return false; |
| 76 data_ += data_offset; | 83 data_ += data_offset; |
| 77 return true; | 84 return true; |
| 78 } | 85 } |
| 79 | 86 |
| 80 void MemoryMappedFile::CloseHandles() { | 87 void MemoryMappedFile::CloseHandles() { |
| 81 if (data_) | 88 if (data_) |
| 82 ::UnmapViewOfFile(data_); | 89 ::UnmapViewOfFile(data_); |
| 83 if (file_mapping_.IsValid()) | 90 if (file_mapping_.IsValid()) |
| 84 file_mapping_.Close(); | 91 file_mapping_.Close(); |
| 85 if (file_.IsValid()) | 92 if (file_.IsValid()) |
| 86 file_.Close(); | 93 file_.Close(); |
| 87 | 94 |
| 88 data_ = NULL; | 95 data_ = NULL; |
| 89 length_ = 0; | 96 length_ = 0; |
| 90 } | 97 } |
| 91 | 98 |
| 92 } // namespace base | 99 } // namespace base |
| OLD | NEW |