blob: 891fcffc4cb434b74729657b7981a400729f2e61 [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
[email protected]815b9052014-07-30 07:02:1013const MemoryMappedFile::Region MemoryMappedFile::Region::kWholeFile(
14 base::LINKER_INITIALIZED);
[email protected]3fa9ff82014-07-28 17:57:4015
[email protected]815b9052014-07-30 07:02:1016MemoryMappedFile::Region::Region(base::LinkerInitialized) : offset(0), size(0) {
[email protected]3fa9ff82014-07-28 17:57:4017}
18
19MemoryMappedFile::Region::Region(int64 offset, int64 size)
20 : offset(offset), size(size) {
21 DCHECK_GE(offset, 0);
22 DCHECK_GT(size, 0);
23}
24
25bool MemoryMappedFile::Region::operator==(
26 const MemoryMappedFile::Region& other) const {
27 return other.offset == offset && other.size == size;
28}
29
[email protected]c3088cb2013-02-24 21:55:4530MemoryMappedFile::~MemoryMappedFile() {
31 CloseHandles();
32}
33
mkosiba3c766cc2015-01-09 13:10:2234#if !defined(OS_NACL)
[email protected]c3088cb2013-02-24 21:55:4535bool MemoryMappedFile::Initialize(const FilePath& file_name) {
36 if (IsValid())
37 return false;
38
[email protected]58ba3ea2014-01-03 22:14:1539 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]3fa9ff82014-07-28 17:57:4046 if (!MapFileRegionToMemory(Region::kWholeFile)) {
[email protected]c3088cb2013-02-24 21:55:4547 CloseHandles();
48 return false;
49 }
50
51 return true;
52}
53
[email protected]58ba3ea2014-01-03 22:14:1554bool MemoryMappedFile::Initialize(File file) {
[email protected]3fa9ff82014-07-28 17:57:4055 return Initialize(file.Pass(), Region::kWholeFile);
56}
57
58bool MemoryMappedFile::Initialize(File file, const Region& region) {
[email protected]c3088cb2013-02-24 21:55:4559 if (IsValid())
60 return false;
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
77void 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}
mkosiba3c766cc2015-01-09 13:10:2289#endif
[email protected]3fa9ff82014-07-28 17:57:4090
[email protected]c3088cb2013-02-24 21:55:4591} // namespace base