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 #include "base/sys_info.h" |
10 | 10 |
(...skipping 24 matching lines...) Expand all Loading... |
35 if (IsValid()) | 35 if (IsValid()) |
36 return false; | 36 return false; |
37 | 37 |
38 file_.Initialize(file_name, File::FLAG_OPEN | File::FLAG_READ); | 38 file_.Initialize(file_name, File::FLAG_OPEN | File::FLAG_READ); |
39 | 39 |
40 if (!file_.IsValid()) { | 40 if (!file_.IsValid()) { |
41 DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe(); | 41 DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe(); |
42 return false; | 42 return false; |
43 } | 43 } |
44 | 44 |
45 if (!MapFileRegionToMemory(Region::kWholeFile)) { | 45 if (!MapFileRegionToMemory(Region::kWholeFile, false)) { |
46 CloseHandles(); | 46 CloseHandles(); |
47 return false; | 47 return false; |
48 } | 48 } |
49 | 49 |
50 return true; | 50 return true; |
51 } | 51 } |
52 | 52 |
53 bool MemoryMappedFile::Initialize(File file) { | 53 bool MemoryMappedFile::Initialize(File file, bool write) { |
54 return Initialize(file.Pass(), Region::kWholeFile); | 54 return Initialize(file.Pass(), Region::kWholeFile, write); |
55 } | 55 } |
56 | 56 |
57 bool MemoryMappedFile::Initialize(File file, const Region& region) { | 57 bool MemoryMappedFile::Initialize(File file, const Region& region, bool write) { |
58 if (IsValid()) | 58 if (IsValid()) |
59 return false; | 59 return false; |
60 | 60 |
61 file_ = file.Pass(); | 61 file_ = file.Pass(); |
62 | 62 |
63 if (!MapFileRegionToMemory(region)) { | 63 if (!MapFileRegionToMemory(region, write)) { |
64 CloseHandles(); | 64 CloseHandles(); |
65 return false; | 65 return false; |
66 } | 66 } |
67 | 67 |
68 return true; | 68 return true; |
69 } | 69 } |
70 | 70 |
71 bool MemoryMappedFile::IsValid() const { | 71 bool MemoryMappedFile::IsValid() const { |
72 return data_ != NULL; | 72 return data_ != NULL; |
73 } | 73 } |
74 | 74 |
75 // static | 75 // static |
76 void MemoryMappedFile::CalculateVMAlignedBoundaries(int64 start, | 76 void MemoryMappedFile::CalculateVMAlignedBoundaries(int64 start, |
77 int64 size, | 77 int64 size, |
78 int64* aligned_start, | 78 int64* aligned_start, |
79 int64* aligned_size, | 79 int64* aligned_size, |
80 int32* offset) { | 80 int32* offset) { |
81 // Sadly, on Windows, the mmap alignment is not just equal to the page size. | 81 // Sadly, on Windows, the mmap alignment is not just equal to the page size. |
82 const int64 mask = static_cast<int64>(SysInfo::VMAllocationGranularity()) - 1; | 82 const int64 mask = static_cast<int64>(SysInfo::VMAllocationGranularity()) - 1; |
83 DCHECK_LT(mask, std::numeric_limits<int32>::max()); | 83 DCHECK_LT(mask, std::numeric_limits<int32>::max()); |
84 *offset = start & mask; | 84 *offset = start & mask; |
85 *aligned_start = start & ~mask; | 85 *aligned_start = start & ~mask; |
86 *aligned_size = (size + *offset + mask) & ~mask; | 86 *aligned_size = (size + *offset + mask) & ~mask; |
87 } | 87 } |
88 | 88 |
89 } // namespace base | 89 } // namespace base |
OLD | NEW |