OLD | NEW |
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 <sys/mman.h> | 7 #include <sys/mman.h> |
8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
9 #include <unistd.h> | 9 #include <unistd.h> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 | 15 |
16 MemoryMappedFile::MemoryMappedFile() : data_(NULL), length_(0) { | 16 MemoryMappedFile::MemoryMappedFile() : data_(NULL), length_(0) { |
17 } | 17 } |
18 | 18 |
19 bool MemoryMappedFile::MapFileRegionToMemory( | 19 bool MemoryMappedFile::MapFileRegionToMemory( |
20 const MemoryMappedFile::Region& region) { | 20 const MemoryMappedFile::Region& region, bool write) { |
21 ThreadRestrictions::AssertIOAllowed(); | 21 ThreadRestrictions::AssertIOAllowed(); |
22 | 22 |
23 off_t map_start = 0; | 23 off_t map_start = 0; |
24 size_t map_size = 0; | 24 size_t map_size = 0; |
25 int32 data_offset = 0; | 25 int32 data_offset = 0; |
26 | 26 |
27 if (region == MemoryMappedFile::Region::kWholeFile) { | 27 if (region == MemoryMappedFile::Region::kWholeFile) { |
28 int64 file_len = file_.GetLength(); | 28 int64 file_len = file_.GetLength(); |
29 if (file_len == -1) { | 29 if (file_len == -1) { |
30 DPLOG(ERROR) << "fstat " << file_.GetPlatformFile(); | 30 DPLOG(ERROR) << "fstat " << file_.GetPlatformFile(); |
(...skipping 22 matching lines...) Expand all Loading... |
53 static_cast<uint64>(region.size) > std::numeric_limits<size_t>::max()) { | 53 static_cast<uint64>(region.size) > std::numeric_limits<size_t>::max()) { |
54 DLOG(ERROR) << "Region bounds are not valid for mmap"; | 54 DLOG(ERROR) << "Region bounds are not valid for mmap"; |
55 return false; | 55 return false; |
56 } | 56 } |
57 | 57 |
58 map_start = static_cast<off_t>(aligned_start); | 58 map_start = static_cast<off_t>(aligned_start); |
59 map_size = static_cast<size_t>(aligned_size); | 59 map_size = static_cast<size_t>(aligned_size); |
60 length_ = static_cast<size_t>(region.size); | 60 length_ = static_cast<size_t>(region.size); |
61 } | 61 } |
62 | 62 |
| 63 int flags = PROT_READ; |
| 64 if (write) |
| 65 flags |= PROT_WRITE; |
63 data_ = static_cast<uint8*>(mmap(NULL, | 66 data_ = static_cast<uint8*>(mmap(NULL, |
64 map_size, | 67 map_size, |
65 PROT_READ, | 68 flags, |
66 MAP_SHARED, | 69 MAP_SHARED, |
67 file_.GetPlatformFile(), | 70 file_.GetPlatformFile(), |
68 map_start)); | 71 map_start)); |
69 if (data_ == MAP_FAILED) { | 72 if (data_ == MAP_FAILED) { |
70 DPLOG(ERROR) << "mmap " << file_.GetPlatformFile(); | 73 DPLOG(ERROR) << "mmap " << file_.GetPlatformFile(); |
71 return false; | 74 return false; |
72 } | 75 } |
73 | 76 |
74 data_ += data_offset; | 77 data_ += data_offset; |
75 return true; | 78 return true; |
76 } | 79 } |
77 | 80 |
78 void MemoryMappedFile::CloseHandles() { | 81 void MemoryMappedFile::CloseHandles() { |
79 ThreadRestrictions::AssertIOAllowed(); | 82 ThreadRestrictions::AssertIOAllowed(); |
80 | 83 |
81 if (data_ != NULL) | 84 if (data_ != NULL) |
82 munmap(data_, length_); | 85 munmap(data_, length_); |
83 file_.Close(); | 86 file_.Close(); |
84 | 87 |
85 data_ = NULL; | 88 data_ = NULL; |
86 length_ = 0; | 89 length_ = 0; |
87 } | 90 } |
88 | 91 |
89 } // namespace base | 92 } // namespace base |
OLD | NEW |