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

Side by Side Diff: base/files/memory_mapped_file_win.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 <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <limits> 10 #include <limits>
11 11
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/strings/string16.h" 13 #include "base/strings/string16.h"
14 #include "base/threading/thread_restrictions.h" 14 #include "base/threading/thread_restrictions.h"
15 15
16 namespace base { 16 namespace base {
17 17
18 MemoryMappedFile::MemoryMappedFile() : data_(NULL), length_(0), image_(false) { 18 MemoryMappedFile::MemoryMappedFile() : data_(NULL), length_(0), image_(false) {
19 } 19 }
20 20
21 bool MemoryMappedFile::InitializeAsImageSection(const FilePath& file_name) { 21 bool MemoryMappedFile::InitializeAsImageSection(const FilePath& file_name) {
22 image_ = true; 22 image_ = true;
23 return Initialize(file_name); 23 return Initialize(file_name);
24 } 24 }
25 25
26 bool MemoryMappedFile::MapFileRegionToMemory( 26 bool MemoryMappedFile::MapFileRegionToMemory(
27 const MemoryMappedFile::Region& region) { 27 const MemoryMappedFile::Region& region,
28 Access access) {
28 ThreadRestrictions::AssertIOAllowed(); 29 ThreadRestrictions::AssertIOAllowed();
29 30
30 if (!file_.IsValid()) 31 if (!file_.IsValid())
31 return false; 32 return false;
32 33
33 int flags = image_ ? SEC_IMAGE | PAGE_READONLY : PAGE_READONLY; 34 int flags = image_ ? SEC_IMAGE : 0;
forshaw 2016/05/09 21:23:19 While we're changing this I'd highly recommend we
bcwhite 2016/05/10 00:44:41 Hmmm.... How about I work towards that in a diffe
forshaw 2016/05/10 07:04:30 Well we could do it in another CL, sure. I'd argue
bcwhite 2016/05/10 21:00:54 Since it's not actually in-use anywhere, I guess t
35 uint32_t size_low = 0;
36 uint32_t size_high = 0;
37 switch (access) {
38 case READ_ONLY:
39 flags |= PAGE_READONLY;
40 break;
41 case READ_WRITE:
42 flags |= PAGE_READWRITE;
43 break;
44 case READ_WRITE_EXTEND:
45 flags |= PAGE_READWRITE;
46 size_high = static_cast<uint32_t>(region.size >> 32);
47 size_low = static_cast<uint32_t>(region.size & 0xFFFFFFFF);
48 break;
49 }
34 50
35 file_mapping_.Set(::CreateFileMapping(file_.GetPlatformFile(), NULL, 51 file_mapping_.Set(::CreateFileMapping(file_.GetPlatformFile(), NULL, flags,
36 flags, 0, 0, NULL)); 52 size_high, size_low, NULL));
erikchen 2016/05/10 02:21:51 A question, mostly for forshaw: Should we be remo
forshaw 2016/05/10 07:04:30 I don't believe so, at least as far as I can tell
37 if (!file_mapping_.IsValid()) 53 if (!file_mapping_.IsValid())
38 return false; 54 return false;
39 55
40 LARGE_INTEGER map_start = {}; 56 LARGE_INTEGER map_start = {};
41 SIZE_T map_size = 0; 57 SIZE_T map_size = 0;
42 int32_t data_offset = 0; 58 int32_t data_offset = 0;
43 59
44 if (region == MemoryMappedFile::Region::kWholeFile) { 60 if (region == MemoryMappedFile::Region::kWholeFile) {
61 DCHECK_NE(READ_WRITE_EXTEND, access);
45 int64_t file_len = file_.GetLength(); 62 int64_t file_len = file_.GetLength();
46 if (file_len <= 0 || file_len > std::numeric_limits<int32_t>::max()) 63 if (file_len <= 0 || file_len > std::numeric_limits<int32_t>::max())
47 return false; 64 return false;
48 length_ = static_cast<size_t>(file_len); 65 length_ = static_cast<size_t>(file_len);
49 } else { 66 } else {
50 // The region can be arbitrarily aligned. MapViewOfFile, instead, requires 67 // The region can be arbitrarily aligned. MapViewOfFile, instead, requires
51 // that the start address is aligned to the VM granularity (which is 68 // that the start address is aligned to the VM granularity (which is
52 // typically larger than a page size, for instance 32k). 69 // typically larger than a page size, for instance 32k).
53 // Also, conversely to POSIX's mmap, the |map_size| doesn't have to be 70 // Also, conversely to POSIX's mmap, the |map_size| doesn't have to be
54 // aligned and must be less than or equal the mapped file size. 71 // aligned and must be less than or equal the mapped file size.
(...skipping 10 matching lines...) Expand all
65 static_cast<uint64_t>(size) > std::numeric_limits<SIZE_T>::max()) { 82 static_cast<uint64_t>(size) > std::numeric_limits<SIZE_T>::max()) {
66 DLOG(ERROR) << "Region bounds are not valid for MapViewOfFile"; 83 DLOG(ERROR) << "Region bounds are not valid for MapViewOfFile";
67 return false; 84 return false;
68 } 85 }
69 map_start.QuadPart = aligned_start; 86 map_start.QuadPart = aligned_start;
70 map_size = static_cast<SIZE_T>(size); 87 map_size = static_cast<SIZE_T>(size);
71 length_ = static_cast<size_t>(region.size); 88 length_ = static_cast<size_t>(region.size);
72 } 89 }
73 90
74 data_ = static_cast<uint8_t*>( 91 data_ = static_cast<uint8_t*>(
75 ::MapViewOfFile(file_mapping_.Get(), FILE_MAP_READ, map_start.HighPart, 92 ::MapViewOfFile(file_mapping_.Get(),
76 map_start.LowPart, map_size)); 93 (flags & PAGE_READONLY) ? FILE_MAP_READ : FILE_MAP_WRITE,
94 map_start.HighPart, map_start.LowPart, map_size));
77 if (data_ == NULL) 95 if (data_ == NULL)
78 return false; 96 return false;
79 data_ += data_offset; 97 data_ += data_offset;
80 return true; 98 return true;
81 } 99 }
82 100
83 void MemoryMappedFile::CloseHandles() { 101 void MemoryMappedFile::CloseHandles() {
84 if (data_) 102 if (data_)
85 ::UnmapViewOfFile(data_); 103 ::UnmapViewOfFile(data_);
86 if (file_mapping_.IsValid()) 104 if (file_mapping_.IsValid())
87 file_mapping_.Close(); 105 file_mapping_.Close();
88 if (file_.IsValid()) 106 if (file_.IsValid())
89 file_.Close(); 107 file_.Close();
90 108
91 data_ = NULL; 109 data_ = NULL;
92 length_ = 0; 110 length_ = 0;
93 } 111 }
94 112
95 } // namespace base 113 } // namespace base
OLDNEW
« base/files/memory_mapped_file.cc ('K') | « base/files/memory_mapped_file_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698