[email protected] | c3088cb | 2013-02-24 21:55:45 | [diff] [blame] | 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 | |
[email protected] | 06b6650 | 2013-02-27 03:01:13 | [diff] [blame] | 7 | #include "base/files/file_path.h" |
[email protected] | c3088cb | 2013-02-24 21:55:45 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | 3fa9ff8 | 2014-07-28 17:57:40 | [diff] [blame] | 9 | #include "base/sys_info.h" |
[email protected] | c3088cb | 2013-02-24 21:55:45 | [diff] [blame] | 10 | |
| 11 | namespace base { |
| 12 | |
[email protected] | 815b905 | 2014-07-30 07:02:10 | [diff] [blame] | 13 | const MemoryMappedFile::Region MemoryMappedFile::Region::kWholeFile( |
| 14 | base::LINKER_INITIALIZED); |
[email protected] | 3fa9ff8 | 2014-07-28 17:57:40 | [diff] [blame] | 15 | |
[email protected] | 815b905 | 2014-07-30 07:02:10 | [diff] [blame] | 16 | MemoryMappedFile::Region::Region(base::LinkerInitialized) : offset(0), size(0) { |
[email protected] | 3fa9ff8 | 2014-07-28 17:57:40 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | MemoryMappedFile::Region::Region(int64 offset, int64 size) |
| 20 | : offset(offset), size(size) { |
| 21 | DCHECK_GE(offset, 0); |
| 22 | DCHECK_GT(size, 0); |
| 23 | } |
| 24 | |
| 25 | bool MemoryMappedFile::Region::operator==( |
| 26 | const MemoryMappedFile::Region& other) const { |
| 27 | return other.offset == offset && other.size == size; |
| 28 | } |
| 29 | |
[email protected] | c3088cb | 2013-02-24 21:55:45 | [diff] [blame] | 30 | MemoryMappedFile::~MemoryMappedFile() { |
| 31 | CloseHandles(); |
| 32 | } |
| 33 | |
mkosiba | 3c766cc | 2015-01-09 13:10:22 | [diff] [blame^] | 34 | #if !defined(OS_NACL) |
[email protected] | c3088cb | 2013-02-24 21:55:45 | [diff] [blame] | 35 | bool MemoryMappedFile::Initialize(const FilePath& file_name) { |
| 36 | if (IsValid()) |
| 37 | return false; |
| 38 | |
[email protected] | 58ba3ea | 2014-01-03 22:14:15 | [diff] [blame] | 39 | file_.Initialize(file_name, File::FLAG_OPEN | File::FLAG_READ); |
| 40 | |
| 41 | if (!file_.IsValid()) { |
| 42 | DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe(); |
| 43 | return false; |
| 44 | } |
| 45 | |
[email protected] | 3fa9ff8 | 2014-07-28 17:57:40 | [diff] [blame] | 46 | if (!MapFileRegionToMemory(Region::kWholeFile)) { |
[email protected] | c3088cb | 2013-02-24 21:55:45 | [diff] [blame] | 47 | CloseHandles(); |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | return true; |
| 52 | } |
| 53 | |
[email protected] | 58ba3ea | 2014-01-03 22:14:15 | [diff] [blame] | 54 | bool MemoryMappedFile::Initialize(File file) { |
[email protected] | 3fa9ff8 | 2014-07-28 17:57:40 | [diff] [blame] | 55 | return Initialize(file.Pass(), Region::kWholeFile); |
| 56 | } |
| 57 | |
| 58 | bool MemoryMappedFile::Initialize(File file, const Region& region) { |
[email protected] | c3088cb | 2013-02-24 21:55:45 | [diff] [blame] | 59 | if (IsValid()) |
| 60 | return false; |
| 61 | |
[email protected] | 58ba3ea | 2014-01-03 22:14:15 | [diff] [blame] | 62 | file_ = file.Pass(); |
[email protected] | c3088cb | 2013-02-24 21:55:45 | [diff] [blame] | 63 | |
[email protected] | 3fa9ff8 | 2014-07-28 17:57:40 | [diff] [blame] | 64 | if (!MapFileRegionToMemory(region)) { |
[email protected] | c3088cb | 2013-02-24 21:55:45 | [diff] [blame] | 65 | CloseHandles(); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool MemoryMappedFile::IsValid() const { |
| 73 | return data_ != NULL; |
| 74 | } |
| 75 | |
[email protected] | 3fa9ff8 | 2014-07-28 17:57:40 | [diff] [blame] | 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 | } |
mkosiba | 3c766cc | 2015-01-09 13:10:22 | [diff] [blame^] | 89 | #endif |
[email protected] | 3fa9ff8 | 2014-07-28 17:57:40 | [diff] [blame] | 90 | |
[email protected] | c3088cb | 2013-02-24 21:55:45 | [diff] [blame] | 91 | } // namespace base |