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 <limits> |
| 8 |
7 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
8 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
9 #include "base/threading/thread_restrictions.h" | 11 #include "base/threading/thread_restrictions.h" |
10 | 12 |
11 namespace base { | 13 namespace base { |
12 | 14 |
13 MemoryMappedFile::MemoryMappedFile() : data_(NULL), length_(0), image_(false) { | 15 MemoryMappedFile::MemoryMappedFile() : data_(NULL), length_(0), image_(false) { |
14 } | 16 } |
15 | 17 |
16 bool MemoryMappedFile::InitializeAsImageSection(const FilePath& file_name) { | 18 bool MemoryMappedFile::InitializeAsImageSection(const FilePath& file_name) { |
(...skipping 10 matching lines...) Expand all Loading... |
27 | 29 |
28 int flags = image_ ? SEC_IMAGE | PAGE_READONLY : PAGE_READONLY; | 30 int flags = image_ ? SEC_IMAGE | PAGE_READONLY : PAGE_READONLY; |
29 | 31 |
30 file_mapping_.Set(::CreateFileMapping(file_.GetPlatformFile(), NULL, | 32 file_mapping_.Set(::CreateFileMapping(file_.GetPlatformFile(), NULL, |
31 flags, 0, 0, NULL)); | 33 flags, 0, 0, NULL)); |
32 if (!file_mapping_.IsValid()) | 34 if (!file_mapping_.IsValid()) |
33 return false; | 35 return false; |
34 | 36 |
35 LARGE_INTEGER map_start = {}; | 37 LARGE_INTEGER map_start = {}; |
36 SIZE_T map_size = 0; | 38 SIZE_T map_size = 0; |
37 int32 data_offset = 0; | 39 int32_t data_offset = 0; |
38 | 40 |
39 if (region == MemoryMappedFile::Region::kWholeFile) { | 41 if (region == MemoryMappedFile::Region::kWholeFile) { |
40 int64 file_len = file_.GetLength(); | 42 int64_t file_len = file_.GetLength(); |
41 if (file_len <= 0 || file_len > kint32max) | 43 if (file_len <= 0 || file_len > std::numeric_limits<int32_t>::max()) |
42 return false; | 44 return false; |
43 length_ = static_cast<size_t>(file_len); | 45 length_ = static_cast<size_t>(file_len); |
44 } else { | 46 } else { |
45 // The region can be arbitrarily aligned. MapViewOfFile, instead, requires | 47 // The region can be arbitrarily aligned. MapViewOfFile, instead, requires |
46 // that the start address is aligned to the VM granularity (which is | 48 // that the start address is aligned to the VM granularity (which is |
47 // typically larger than a page size, for instance 32k). | 49 // typically larger than a page size, for instance 32k). |
48 // Also, conversely to POSIX's mmap, the |map_size| doesn't have to be | 50 // Also, conversely to POSIX's mmap, the |map_size| doesn't have to be |
49 // aligned and must be less than or equal the mapped file size. | 51 // aligned and must be less than or equal the mapped file size. |
50 // We map here the outer region [|aligned_start|, |aligned_start+size|] | 52 // We map here the outer region [|aligned_start|, |aligned_start+size|] |
51 // which contains |region| and then add up the |data_offset| displacement. | 53 // which contains |region| and then add up the |data_offset| displacement. |
52 int64 aligned_start = 0; | 54 int64_t aligned_start = 0; |
53 int64 ignored = 0; | 55 int64_t ignored = 0; |
54 CalculateVMAlignedBoundaries( | 56 CalculateVMAlignedBoundaries( |
55 region.offset, region.size, &aligned_start, &ignored, &data_offset); | 57 region.offset, region.size, &aligned_start, &ignored, &data_offset); |
56 int64 size = region.size + data_offset; | 58 int64_t size = region.size + data_offset; |
57 | 59 |
58 // Ensure that the casts below in the MapViewOfFile call are sane. | 60 // Ensure that the casts below in the MapViewOfFile call are sane. |
59 if (aligned_start < 0 || size < 0 || | 61 if (aligned_start < 0 || size < 0 || |
60 static_cast<uint64>(size) > std::numeric_limits<SIZE_T>::max()) { | 62 static_cast<uint64_t>(size) > std::numeric_limits<SIZE_T>::max()) { |
61 DLOG(ERROR) << "Region bounds are not valid for MapViewOfFile"; | 63 DLOG(ERROR) << "Region bounds are not valid for MapViewOfFile"; |
62 return false; | 64 return false; |
63 } | 65 } |
64 map_start.QuadPart = aligned_start; | 66 map_start.QuadPart = aligned_start; |
65 map_size = static_cast<SIZE_T>(size); | 67 map_size = static_cast<SIZE_T>(size); |
66 length_ = static_cast<size_t>(region.size); | 68 length_ = static_cast<size_t>(region.size); |
67 } | 69 } |
68 | 70 |
69 data_ = static_cast<uint8*>(::MapViewOfFile(file_mapping_.Get(), | 71 data_ = static_cast<uint8_t*>( |
70 FILE_MAP_READ, | 72 ::MapViewOfFile(file_mapping_.Get(), FILE_MAP_READ, map_start.HighPart, |
71 map_start.HighPart, | 73 map_start.LowPart, map_size)); |
72 map_start.LowPart, | |
73 map_size)); | |
74 if (data_ == NULL) | 74 if (data_ == NULL) |
75 return false; | 75 return false; |
76 data_ += data_offset; | 76 data_ += data_offset; |
77 return true; | 77 return true; |
78 } | 78 } |
79 | 79 |
80 void MemoryMappedFile::CloseHandles() { | 80 void MemoryMappedFile::CloseHandles() { |
81 if (data_) | 81 if (data_) |
82 ::UnmapViewOfFile(data_); | 82 ::UnmapViewOfFile(data_); |
83 if (file_mapping_.IsValid()) | 83 if (file_mapping_.IsValid()) |
84 file_mapping_.Close(); | 84 file_mapping_.Close(); |
85 if (file_.IsValid()) | 85 if (file_.IsValid()) |
86 file_.Close(); | 86 file_.Close(); |
87 | 87 |
88 data_ = NULL; | 88 data_ = NULL; |
89 length_ = 0; | 89 length_ = 0; |
90 } | 90 } |
91 | 91 |
92 } // namespace base | 92 } // namespace base |
OLD | NEW |