blob: 6c3f4023d12014806dbd6db037c5ad9548b4318d [file] [log] [blame]
[email protected]c3088cb2013-02-24 21:55:451// 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]06b66502013-02-27 03:01:137#include "base/files/file_path.h"
[email protected]c3088cb2013-02-24 21:55:458#include "base/logging.h"
[email protected]3fa9ff82014-07-28 17:57:409#include "base/sys_info.h"
[email protected]c3088cb2013-02-24 21:55:4510
11namespace base {
12
agrievefd2d44ab2015-06-19 04:33:0313const MemoryMappedFile::Region MemoryMappedFile::Region::kWholeFile = {0, 0};
[email protected]3fa9ff82014-07-28 17:57:4014
15bool MemoryMappedFile::Region::operator==(
16 const MemoryMappedFile::Region& other) const {
17 return other.offset == offset && other.size == size;
18}
19
agrieve0f9183132015-06-03 13:41:5320bool MemoryMappedFile::Region::operator!=(
21 const MemoryMappedFile::Region& other) const {
22 return other.offset != offset || other.size != size;
23}
24
[email protected]c3088cb2013-02-24 21:55:4525MemoryMappedFile::~MemoryMappedFile() {
26 CloseHandles();
27}
28
mkosiba3c766cc2015-01-09 13:10:2229#if !defined(OS_NACL)
[email protected]c3088cb2013-02-24 21:55:4530bool MemoryMappedFile::Initialize(const FilePath& file_name) {
31 if (IsValid())
32 return false;
33
[email protected]58ba3ea2014-01-03 22:14:1534 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
[email protected]3fa9ff82014-07-28 17:57:4041 if (!MapFileRegionToMemory(Region::kWholeFile)) {
[email protected]c3088cb2013-02-24 21:55:4542 CloseHandles();
43 return false;
44 }
45
46 return true;
47}
48
[email protected]58ba3ea2014-01-03 22:14:1549bool MemoryMappedFile::Initialize(File file) {
[email protected]3fa9ff82014-07-28 17:57:4050 return Initialize(file.Pass(), Region::kWholeFile);
51}
52
53bool MemoryMappedFile::Initialize(File file, const Region& region) {
[email protected]c3088cb2013-02-24 21:55:4554 if (IsValid())
55 return false;
56
agrieve0f9183132015-06-03 13:41:5357 if (region != Region::kWholeFile) {
58 DCHECK_GE(region.offset, 0);
59 DCHECK_GT(region.size, 0);
60 }
61
[email protected]58ba3ea2014-01-03 22:14:1562 file_ = file.Pass();
[email protected]c3088cb2013-02-24 21:55:4563
[email protected]3fa9ff82014-07-28 17:57:4064 if (!MapFileRegionToMemory(region)) {
[email protected]c3088cb2013-02-24 21:55:4565 CloseHandles();
66 return false;
67 }
68
69 return true;
70}
71
72bool MemoryMappedFile::IsValid() const {
73 return data_ != NULL;
74}
75
[email protected]3fa9ff82014-07-28 17:57:4076// static
avid0181f32015-12-10 19:41:4777void MemoryMappedFile::CalculateVMAlignedBoundaries(int64_t start,
78 int64_t size,
79 int64_t* aligned_start,
80 int64_t* aligned_size,
81 int32_t* offset) {
[email protected]3fa9ff82014-07-28 17:57:4082 // Sadly, on Windows, the mmap alignment is not just equal to the page size.
avid0181f32015-12-10 19:41:4783 const int64_t mask =
84 static_cast<int64_t>(SysInfo::VMAllocationGranularity()) - 1;
85 DCHECK_LT(mask, std::numeric_limits<int32_t>::max());
[email protected]3fa9ff82014-07-28 17:57:4086 *offset = start & mask;
87 *aligned_start = start & ~mask;
88 *aligned_size = (size + *offset + mask) & ~mask;
89}
mkosiba3c766cc2015-01-09 13:10:2290#endif
[email protected]3fa9ff82014-07-28 17:57:4091
[email protected]c3088cb2013-02-24 21:55:4592} // namespace base