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

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, 8 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) {
57 if (IsValid()) 72 if (IsValid())
58 return false; 73 return false;
59 74
60 if (region != Region::kWholeFile) { 75 if (region != Region::kWholeFile) {
61 DCHECK_GE(region.offset, 0); 76 DCHECK_GE(region.offset, 0);
62 DCHECK_GT(region.size, 0); 77 DCHECK_GT(region.size, 0);
63 } 78 }
64 79
65 file_ = std::move(file); 80 file_ = std::move(file);
66 81
67 if (!MapFileRegionToMemory(region)) { 82 if (!MapFileRegionToMemory(region, access)) {
68 CloseHandles(); 83 CloseHandles();
69 return false; 84 return false;
70 } 85 }
71 86
72 return true; 87 return true;
73 } 88 }
74 89
75 bool MemoryMappedFile::IsValid() const { 90 bool MemoryMappedFile::IsValid() const {
76 return data_ != NULL; 91 return data_ != NULL;
77 } 92 }
78 93
79 // static 94 // static
80 void MemoryMappedFile::CalculateVMAlignedBoundaries(int64_t start, 95 void MemoryMappedFile::CalculateVMAlignedBoundaries(int64_t start,
81 int64_t size, 96 int64_t size,
82 int64_t* aligned_start, 97 int64_t* aligned_start,
83 int64_t* aligned_size, 98 int64_t* aligned_size,
84 int32_t* offset) { 99 int32_t* offset) {
85 // Sadly, on Windows, the mmap alignment is not just equal to the page size. 100 // Sadly, on Windows, the mmap alignment is not just equal to the page size.
86 const int64_t mask = 101 const int64_t mask =
87 static_cast<int64_t>(SysInfo::VMAllocationGranularity()) - 1; 102 static_cast<int64_t>(SysInfo::VMAllocationGranularity()) - 1;
88 DCHECK_LT(mask, std::numeric_limits<int32_t>::max()); 103 DCHECK_LT(mask, std::numeric_limits<int32_t>::max());
89 *offset = start & mask; 104 *offset = start & mask;
90 *aligned_start = start & ~mask; 105 *aligned_start = start & ~mask;
91 *aligned_size = (size + *offset + mask) & ~mask; 106 *aligned_size = (size + *offset + mask) & ~mask;
92 } 107 }
93 #endif 108 #endif
94 109
95 } // namespace base 110 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698