Chromium Code Reviews| 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 |
| 11 namespace base { | 11 namespace base { |
| 12 | 12 |
| 13 const MemoryMappedFile::Region MemoryMappedFile::Region::kWholeFile( | 13 const MemoryMappedFile::Region MemoryMappedFile::Region::kWholeFile( |
| 14 base::LINKER_INITIALIZED); | 14 base::LINKER_INITIALIZED); |
| 15 | 15 |
| 16 MemoryMappedFile::Region::Region(base::LinkerInitialized) : offset(0), size(0) { | 16 MemoryMappedFile::Region::Region(base::LinkerInitialized) : offset(0), size(0) { |
| 17 } | 17 } |
| 18 | 18 |
| 19 MemoryMappedFile::Region::Region() : offset(-1), size(-1) { | |
| 20 } | |
| 21 | |
| 19 MemoryMappedFile::Region::Region(int64 offset, int64 size) | 22 MemoryMappedFile::Region::Region(int64 offset, int64 size) |
| 20 : offset(offset), size(size) { | 23 : offset(offset), size(size) { |
| 21 DCHECK_GE(offset, 0); | |
| 22 DCHECK_GT(size, 0); | |
| 23 } | 24 } |
| 24 | 25 |
| 25 bool MemoryMappedFile::Region::operator==( | 26 bool MemoryMappedFile::Region::operator==( |
| 26 const MemoryMappedFile::Region& other) const { | 27 const MemoryMappedFile::Region& other) const { |
| 27 return other.offset == offset && other.size == size; | 28 return other.offset == offset && other.size == size; |
| 28 } | 29 } |
| 29 | 30 |
| 31 bool MemoryMappedFile::Region::operator!=( | |
| 32 const MemoryMappedFile::Region& other) const { | |
| 33 return other.offset != offset || other.size != size; | |
|
Lei Zhang
2015/06/02 20:25:43
Some people just do !(this == other) here, especia
| |
| 34 } | |
| 35 | |
| 30 MemoryMappedFile::~MemoryMappedFile() { | 36 MemoryMappedFile::~MemoryMappedFile() { |
| 31 CloseHandles(); | 37 CloseHandles(); |
| 32 } | 38 } |
| 33 | 39 |
| 34 #if !defined(OS_NACL) | 40 #if !defined(OS_NACL) |
| 35 bool MemoryMappedFile::Initialize(const FilePath& file_name) { | 41 bool MemoryMappedFile::Initialize(const FilePath& file_name) { |
| 36 if (IsValid()) | 42 if (IsValid()) |
| 37 return false; | 43 return false; |
| 38 | 44 |
| 39 file_.Initialize(file_name, File::FLAG_OPEN | File::FLAG_READ); | 45 file_.Initialize(file_name, File::FLAG_OPEN | File::FLAG_READ); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 52 } | 58 } |
| 53 | 59 |
| 54 bool MemoryMappedFile::Initialize(File file) { | 60 bool MemoryMappedFile::Initialize(File file) { |
| 55 return Initialize(file.Pass(), Region::kWholeFile); | 61 return Initialize(file.Pass(), Region::kWholeFile); |
| 56 } | 62 } |
| 57 | 63 |
| 58 bool MemoryMappedFile::Initialize(File file, const Region& region) { | 64 bool MemoryMappedFile::Initialize(File file, const Region& region) { |
| 59 if (IsValid()) | 65 if (IsValid()) |
| 60 return false; | 66 return false; |
| 61 | 67 |
| 68 if (region != Region::kWholeFile) { | |
| 69 DCHECK_GE(region.offset, 0); | |
| 70 DCHECK_GT(region.size, 0); | |
| 71 } | |
| 72 | |
| 62 file_ = file.Pass(); | 73 file_ = file.Pass(); |
| 63 | 74 |
| 64 if (!MapFileRegionToMemory(region)) { | 75 if (!MapFileRegionToMemory(region)) { |
| 65 CloseHandles(); | 76 CloseHandles(); |
| 66 return false; | 77 return false; |
| 67 } | 78 } |
| 68 | 79 |
| 69 return true; | 80 return true; |
| 70 } | 81 } |
| 71 | 82 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 82 // Sadly, on Windows, the mmap alignment is not just equal to the page size. | 93 // Sadly, on Windows, the mmap alignment is not just equal to the page size. |
| 83 const int64 mask = static_cast<int64>(SysInfo::VMAllocationGranularity()) - 1; | 94 const int64 mask = static_cast<int64>(SysInfo::VMAllocationGranularity()) - 1; |
| 84 DCHECK_LT(mask, std::numeric_limits<int32>::max()); | 95 DCHECK_LT(mask, std::numeric_limits<int32>::max()); |
| 85 *offset = start & mask; | 96 *offset = start & mask; |
| 86 *aligned_start = start & ~mask; | 97 *aligned_start = start & ~mask; |
| 87 *aligned_size = (size + *offset + mask) & ~mask; | 98 *aligned_size = (size + *offset + mask) & ~mask; |
| 88 } | 99 } |
| 89 #endif | 100 #endif |
| 90 | 101 |
| 91 } // namespace base | 102 } // namespace base |
| OLD | NEW |