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

Side by Side Diff: base/files/memory_mapped_file_posix.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
« no previous file with comments | « base/files/memory_mapped_file.cc ('k') | base/files/memory_mapped_file_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include <sys/mman.h> 9 #include <sys/mman.h>
10 #include <sys/stat.h> 10 #include <sys/stat.h>
11 #include <unistd.h> 11 #include <unistd.h>
12 12
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/threading/thread_restrictions.h" 14 #include "base/threading/thread_restrictions.h"
15 #include "build/build_config.h" 15 #include "build/build_config.h"
16 16
17 namespace base { 17 namespace base {
18 18
19 MemoryMappedFile::MemoryMappedFile() : data_(NULL), length_(0) { 19 MemoryMappedFile::MemoryMappedFile() : data_(NULL), length_(0) {
20 } 20 }
21 21
22 #if !defined(OS_NACL) 22 #if !defined(OS_NACL)
23 bool MemoryMappedFile::MapFileRegionToMemory( 23 bool MemoryMappedFile::MapFileRegionToMemory(
24 const MemoryMappedFile::Region& region) { 24 const MemoryMappedFile::Region& region,
25 Access access) {
25 ThreadRestrictions::AssertIOAllowed(); 26 ThreadRestrictions::AssertIOAllowed();
26 27
27 off_t map_start = 0; 28 off_t map_start = 0;
28 size_t map_size = 0; 29 size_t map_size = 0;
29 int32_t data_offset = 0; 30 int32_t data_offset = 0;
30 31
31 if (region == MemoryMappedFile::Region::kWholeFile) { 32 if (region == MemoryMappedFile::Region::kWholeFile) {
32 int64_t file_len = file_.GetLength(); 33 int64_t file_len = file_.GetLength();
33 if (file_len == -1) { 34 if (file_len == -1) {
34 DPLOG(ERROR) << "fstat " << file_.GetPlatformFile(); 35 DPLOG(ERROR) << "fstat " << file_.GetPlatformFile();
(...skipping 23 matching lines...) Expand all
58 std::numeric_limits<size_t>::max()) { 59 std::numeric_limits<size_t>::max()) {
59 DLOG(ERROR) << "Region bounds are not valid for mmap"; 60 DLOG(ERROR) << "Region bounds are not valid for mmap";
60 return false; 61 return false;
61 } 62 }
62 63
63 map_start = static_cast<off_t>(aligned_start); 64 map_start = static_cast<off_t>(aligned_start);
64 map_size = static_cast<size_t>(aligned_size); 65 map_size = static_cast<size_t>(aligned_size);
65 length_ = static_cast<size_t>(region.size); 66 length_ = static_cast<size_t>(region.size);
66 } 67 }
67 68
68 data_ = static_cast<uint8_t*>(mmap(NULL, map_size, PROT_READ, MAP_SHARED, 69 int flags = 0;
70 switch (access) {
71 case READ_ONLY:
72 flags |= PROT_READ;
73 break;
74 case READ_WRITE:
75 flags |= PROT_READ | PROT_WRITE;
76 break;
77 case READ_WRITE_EXTEND:
78 // POSIX won't auto-extend the file when it is written so it must first
Mark Mentovai 2016/05/19 14:57:06 What kinds of sizes are we talking about here? HFS
bcwhite 2016/05/19 15:40:25 That's annoying. For my immediate use (sharing Sy
79 // be explicitly extended to the maximum size. Zeros will fill the new
80 // space.
81 file_.SetLength(std::max(file_.GetLength(), region.offset + region.size));
82 flags |= PROT_READ | PROT_WRITE;
83 break;
84 }
85 data_ = static_cast<uint8_t*>(mmap(NULL, map_size, flags, MAP_SHARED,
69 file_.GetPlatformFile(), map_start)); 86 file_.GetPlatformFile(), map_start));
70 if (data_ == MAP_FAILED) { 87 if (data_ == MAP_FAILED) {
71 DPLOG(ERROR) << "mmap " << file_.GetPlatformFile(); 88 DPLOG(ERROR) << "mmap " << file_.GetPlatformFile();
72 return false; 89 return false;
73 } 90 }
74 91
75 data_ += data_offset; 92 data_ += data_offset;
76 return true; 93 return true;
77 } 94 }
78 #endif 95 #endif
79 96
80 void MemoryMappedFile::CloseHandles() { 97 void MemoryMappedFile::CloseHandles() {
81 ThreadRestrictions::AssertIOAllowed(); 98 ThreadRestrictions::AssertIOAllowed();
82 99
83 if (data_ != NULL) 100 if (data_ != NULL)
84 munmap(data_, length_); 101 munmap(data_, length_);
85 file_.Close(); 102 file_.Close();
86 103
87 data_ = NULL; 104 data_ = NULL;
88 length_ = 0; 105 length_ = 0;
89 } 106 }
90 107
91 } // namespace base 108 } // namespace base
OLDNEW
« no previous file with comments | « base/files/memory_mapped_file.cc ('k') | base/files/memory_mapped_file_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698