Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: base/files/memory_mapped_file.cc

Issue 1798203002: Support read/write memory-mapped files. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebased Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <utility> 7 #include <utility>
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 12 matching lines...) Expand all
23 bool MemoryMappedFile::Region::operator!=( 23 bool MemoryMappedFile::Region::operator!=(
24 const MemoryMappedFile::Region& other) const { 24 const MemoryMappedFile::Region& other) const {
25 return other.offset != offset || other.size != size; 25 return other.offset != offset || other.size != size;
26 } 26 }
27 27
28 MemoryMappedFile::~MemoryMappedFile() { 28 MemoryMappedFile::~MemoryMappedFile() {
29 CloseHandles(); 29 CloseHandles();
30 } 30 }
31 31
32 #if !defined(OS_NACL) 32 #if !defined(OS_NACL)
33 bool MemoryMappedFile::Initialize(const FilePath& file_name) { 33 bool MemoryMappedFile::Initialize(const FilePath& file_name, Access access) {
34 if (IsValid()) 34 if (IsValid())
35 return false; 35 return false;
36 36
37 file_.Initialize(file_name, File::FLAG_OPEN | File::FLAG_READ); 37 uint32_t flags = 0;
38 switch (access) {
39 case READ_ONLY:
40 flags = File::FLAG_OPEN | File::FLAG_READ;
41 break;
42 case READ_WRITE:
43 flags = File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE;
44 break;
45 case READ_WRITE_EXTEND:
46 // Can't open with "extend" because no maximum size is known.
47 NOTREACHED();
48 }
49 file_.Initialize(file_name, flags);
38 50
39 if (!file_.IsValid()) { 51 if (!file_.IsValid()) {
40 DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe(); 52 DLOG(ERROR) << "Couldn't open " << file_name.AsUTF8Unsafe();
41 return false; 53 return false;
42 } 54 }
43 55
44 if (!MapFileRegionToMemory(Region::kWholeFile)) { 56 if (!MapFileRegionToMemory(Region::kWholeFile, access)) {
45 CloseHandles(); 57 CloseHandles();
46 return false; 58 return false;
47 } 59 }
48 60
49 return true; 61 return true;
50 } 62 }
51 63
52 bool MemoryMappedFile::Initialize(File file) { 64 bool MemoryMappedFile::Initialize(File file, Access access) {
53 return Initialize(std::move(file), Region::kWholeFile); 65 DCHECK_NE(READ_WRITE_EXTEND, access);
66 return Initialize(std::move(file), Region::kWholeFile, access);
54 } 67 }
55 68
56 bool MemoryMappedFile::Initialize(File file, const Region& region) { 69 bool MemoryMappedFile::Initialize(File file,
70 const Region& region,
71 Access access) {
72 switch (access) {
73 case READ_WRITE_EXTEND:
74 // Ensure that the extended size is within limits of File.
75 if (region.size > std::numeric_limits<int64_t>::max() - region.offset) {
76 DLOG(ERROR) << "Region bounds exceed maximum for base::File.";
77 return false;
78 }
79 // Fall through.
80 case READ_ONLY:
81 case READ_WRITE:
82 // Ensure that the region values are valid.
83 if (region.offset < 0 || region.size < 0) {
84 DLOG(ERROR) << "Region bounds are not valid.";
85 return false;
86 }
87 break;
88 }
89
57 if (IsValid()) 90 if (IsValid())
58 return false; 91 return false;
59 92
60 if (region != Region::kWholeFile) { 93 if (region != Region::kWholeFile) {
61 DCHECK_GE(region.offset, 0); 94 DCHECK_GE(region.offset, 0);
62 DCHECK_GT(region.size, 0); 95 DCHECK_GT(region.size, 0);
63 } 96 }
64 97
65 file_ = std::move(file); 98 file_ = std::move(file);
66 99
67 if (!MapFileRegionToMemory(region)) { 100 if (!MapFileRegionToMemory(region, access)) {
68 CloseHandles(); 101 CloseHandles();
69 return false; 102 return false;
70 } 103 }
71 104
72 return true; 105 return true;
73 } 106 }
74 107
75 bool MemoryMappedFile::IsValid() const { 108 bool MemoryMappedFile::IsValid() const {
76 return data_ != NULL; 109 return data_ != NULL;
77 } 110 }
78 111
79 // static 112 // static
80 void MemoryMappedFile::CalculateVMAlignedBoundaries(int64_t start, 113 void MemoryMappedFile::CalculateVMAlignedBoundaries(int64_t start,
81 int64_t size, 114 int64_t size,
82 int64_t* aligned_start, 115 int64_t* aligned_start,
83 int64_t* aligned_size, 116 int64_t* aligned_size,
84 int32_t* offset) { 117 int32_t* offset) {
85 // Sadly, on Windows, the mmap alignment is not just equal to the page size. 118 // Sadly, on Windows, the mmap alignment is not just equal to the page size.
86 const int64_t mask = 119 const int64_t mask =
87 static_cast<int64_t>(SysInfo::VMAllocationGranularity()) - 1; 120 static_cast<int64_t>(SysInfo::VMAllocationGranularity()) - 1;
88 DCHECK_LT(mask, std::numeric_limits<int32_t>::max()); 121 DCHECK_LT(mask, std::numeric_limits<int32_t>::max());
89 *offset = start & mask; 122 *offset = start & mask;
90 *aligned_start = start & ~mask; 123 *aligned_start = start & ~mask;
91 *aligned_size = (size + *offset + mask) & ~mask; 124 *aligned_size = (size + *offset + mask) & ~mask;
92 } 125 }
93 #endif 126 #endif
94 127
95 } // namespace base 128 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698