| 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 |
| 13 const MemoryMappedFile::Region MemoryMappedFile::Region::kWholeFile; |
| 14 |
| 15 MemoryMappedFile::Region::Region() : offset(0), size(0) { |
| 16 } |
| 17 |
| 18 MemoryMappedFile::Region::Region(int64 offset, int64 size) |
| 19 : offset(offset), size(size) { |
| 20 DCHECK_GE(offset, 0); |
| 21 DCHECK_GT(size, 0); |
| 22 } |
| 23 |
| 24 bool MemoryMappedFile::Region::operator==( |
| 25 const MemoryMappedFile::Region& other) const { |
| 26 return other.offset == offset && other.size == size; |
| 27 } |
| 28 |
| 12 MemoryMappedFile::~MemoryMappedFile() { | 29 MemoryMappedFile::~MemoryMappedFile() { |
| 13 CloseHandles(); | 30 CloseHandles(); |
| 14 } | 31 } |
| 15 | 32 |
| 16 bool MemoryMappedFile::Initialize(const FilePath& file_name) { | 33 bool MemoryMappedFile::Initialize(const FilePath& file_name) { |
| 17 if (IsValid()) | 34 if (IsValid()) |
| 18 return false; | 35 return false; |
| 19 | 36 |
| 20 file_.Initialize(file_name, File::FLAG_OPEN | File::FLAG_READ); | 37 file_.Initialize(file_name, File::FLAG_OPEN | File::FLAG_READ); |
| 21 | 38 |
| 22 if (!file_.IsValid()) { | 39 if (!file_.IsValid()) { |
| 23 DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe(); | 40 DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe(); |
| 24 return false; | 41 return false; |
| 25 } | 42 } |
| 26 | 43 |
| 27 if (!MapFileToMemory()) { | 44 if (!MapFileRegionToMemory(Region::kWholeFile)) { |
| 28 CloseHandles(); | 45 CloseHandles(); |
| 29 return false; | 46 return false; |
| 30 } | 47 } |
| 31 | 48 |
| 32 return true; | 49 return true; |
| 33 } | 50 } |
| 34 | 51 |
| 35 bool MemoryMappedFile::Initialize(File file) { | 52 bool MemoryMappedFile::Initialize(File file) { |
| 53 return Initialize(file.Pass(), Region::kWholeFile); |
| 54 } |
| 55 |
| 56 bool MemoryMappedFile::Initialize(File file, const Region& region) { |
| 36 if (IsValid()) | 57 if (IsValid()) |
| 37 return false; | 58 return false; |
| 38 | 59 |
| 39 file_ = file.Pass(); | 60 file_ = file.Pass(); |
| 40 | 61 |
| 41 if (!MapFileToMemory()) { | 62 if (!MapFileRegionToMemory(region)) { |
| 42 CloseHandles(); | 63 CloseHandles(); |
| 43 return false; | 64 return false; |
| 44 } | 65 } |
| 45 | 66 |
| 46 return true; | 67 return true; |
| 47 } | 68 } |
| 48 | 69 |
| 49 bool MemoryMappedFile::IsValid() const { | 70 bool MemoryMappedFile::IsValid() const { |
| 50 return data_ != NULL; | 71 return data_ != NULL; |
| 51 } | 72 } |
| 52 | 73 |
| 74 // static |
| 75 void MemoryMappedFile::CalculateVMAlignedBoundaries(int64 start, |
| 76 int64 size, |
| 77 int64* aligned_start, |
| 78 int64* aligned_size, |
| 79 int32* offset) { |
| 80 // Sadly, on Windows, the mmap alignment is not just equal to the page size. |
| 81 const int64 mask = static_cast<int64>(SysInfo::VMAllocationGranularity()) - 1; |
| 82 DCHECK_LT(mask, std::numeric_limits<int32>::max()); |
| 83 *offset = start & mask; |
| 84 *aligned_start = start & ~mask; |
| 85 *aligned_size = (size + *offset + mask) & ~mask; |
| 86 } |
| 87 |
| 53 } // namespace base | 88 } // namespace base |
| OLD | NEW |