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