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/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/sys_info.h" |
9 | 10 |
10 namespace base { | 11 namespace base { |
11 | 12 |
12 MemoryMappedFile::~MemoryMappedFile() { | 13 MemoryMappedFile::~MemoryMappedFile() { |
13 CloseHandles(); | 14 CloseHandles(); |
14 } | 15 } |
15 | 16 |
16 bool MemoryMappedFile::Initialize(const FilePath& file_name) { | 17 bool MemoryMappedFile::Initialize(const FilePath& file_name) { |
17 if (IsValid()) | 18 if (IsValid()) |
18 return false; | 19 return false; |
19 | 20 |
20 file_.Initialize(file_name, File::FLAG_OPEN | File::FLAG_READ); | 21 file_.Initialize(file_name, File::FLAG_OPEN | File::FLAG_READ); |
21 | 22 |
22 if (!file_.IsValid()) { | 23 if (!file_.IsValid()) { |
23 DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe(); | 24 DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe(); |
24 return false; | 25 return false; |
25 } | 26 } |
26 | 27 |
27 if (!MapFileToMemory()) { | 28 if (!MapFileRegionToMemory(File::Region::WholeFile())) { |
28 CloseHandles(); | 29 CloseHandles(); |
29 return false; | 30 return false; |
30 } | 31 } |
31 | 32 |
32 return true; | 33 return true; |
33 } | 34 } |
34 | 35 |
35 bool MemoryMappedFile::Initialize(File file) { | 36 bool MemoryMappedFile::Initialize(File file) { |
| 37 return Initialize(file.Pass(), base::File::Region::WholeFile()); |
| 38 } |
| 39 |
| 40 bool MemoryMappedFile::Initialize(File file, const base::File::Region& region) { |
36 if (IsValid()) | 41 if (IsValid()) |
37 return false; | 42 return false; |
38 | 43 |
39 file_ = file.Pass(); | 44 file_ = file.Pass(); |
40 | 45 |
41 if (!MapFileToMemory()) { | 46 if (!MapFileRegionToMemory(region)) { |
42 CloseHandles(); | 47 CloseHandles(); |
43 return false; | 48 return false; |
44 } | 49 } |
45 | 50 |
46 return true; | 51 return true; |
47 } | 52 } |
48 | 53 |
49 bool MemoryMappedFile::IsValid() const { | 54 bool MemoryMappedFile::IsValid() const { |
50 return data_ != NULL; | 55 return data_ != NULL; |
51 } | 56 } |
52 | 57 |
| 58 // static |
| 59 void MemoryMappedFile::CalculateVMAlignedBoundaries(int64 start, |
| 60 int64 size, |
| 61 int64* aligned_start, |
| 62 int64* aligned_size, |
| 63 int32* offset) { |
| 64 // Sadly, on Windows, the mmap alignment is not just equal to the page size. |
| 65 const int64 mask = static_cast<int64>(SysInfo::VMAllocationGranularity()) - 1; |
| 66 *offset = start & mask; |
| 67 *aligned_start = start & ~mask; |
| 68 *aligned_size = (size + *offset + mask) & ~mask; |
| 69 } |
| 70 |
53 } // namespace base | 71 } // namespace base |
OLD | NEW |