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::MapFileToMemory() { | 19 bool MemoryMappedFile::MapFileRegionToMemory(const File::Region& region) { |
20 ThreadRestrictions::AssertIOAllowed(); | 20 ThreadRestrictions::AssertIOAllowed(); |
21 | 21 |
22 struct stat file_stat; | 22 off_t map_start = 0; |
23 if (fstat(file_.GetPlatformFile(), &file_stat) == -1 ) { | 23 size_t map_size = 0; |
24 DPLOG(ERROR) << "fstat " << file_.GetPlatformFile(); | 24 int32 data_offset = 0; |
25 | |
26 if (region == File::Region::kWholeFile) { | |
27 int64 file_len = file_.GetLength(); | |
28 if (file_len == -1) { | |
29 DPLOG(ERROR) << "fstat " << file_.GetPlatformFile(); | |
30 return false; | |
31 } | |
32 map_size = static_cast<size_t>(file_len); | |
33 length_ = map_size; | |
34 } else { | |
35 // The region can be arbitrarily aligned. mmap, instead, requires both the | |
36 // start and size to be page-aligned. Hence, we map here the page-aligned | |
37 // outer region [|aligned_start|, |aligned_start| + |size|] which contains | |
38 // |region| and then add up the |data_offset| displacement. | |
39 int64 aligned_start = 0; | |
40 int64 aligned_size = 0; | |
41 CalculateVMAlignedBoundaries(region.offset, | |
42 region.size, | |
43 &aligned_start, | |
44 &aligned_size, | |
45 &data_offset); | |
46 | |
47 // Ensure that the casts in the mmap call below are sane. | |
48 if (aligned_start < 0 || aligned_size < 0 || | |
49 aligned_start > std::numeric_limits<off_t>::max() || | |
50 static_cast<uint64>(aligned_size) > | |
51 std::numeric_limits<size_t>::max() || | |
52 static_cast<uint64>(region.size) > std::numeric_limits<size_t>::max()) { | |
53 DLOG(ERROR) << "Region bounds are not valid for mmap"; | |
willchan no longer on Chromium
2014/07/25 21:21:24
How about NOTREACHED()?
Primiano Tucci (use gerrit)
2014/07/28 12:52:11
Hmm technically that is reachable if the client ca
| |
54 return false; | |
55 } | |
56 | |
57 map_start = static_cast<off_t>(aligned_start); | |
58 map_size = static_cast<size_t>(aligned_size); | |
59 length_ = static_cast<size_t>(region.size); | |
60 } | |
61 | |
62 data_ = static_cast<uint8*>(mmap(NULL, | |
63 map_size, | |
64 PROT_READ, | |
65 MAP_SHARED, | |
66 file_.GetPlatformFile(), | |
67 map_start)); | |
68 if (data_ == MAP_FAILED) { | |
69 DPLOG(ERROR) << "mmap " << file_.GetPlatformFile(); | |
25 return false; | 70 return false; |
26 } | 71 } |
27 length_ = file_stat.st_size; | |
28 | 72 |
29 data_ = static_cast<uint8*>( | 73 data_ += data_offset; |
30 mmap(NULL, length_, PROT_READ, MAP_SHARED, file_.GetPlatformFile(), 0)); | 74 return true; |
31 if (data_ == MAP_FAILED) | |
32 DPLOG(ERROR) << "mmap " << file_.GetPlatformFile(); | |
33 | |
34 return data_ != MAP_FAILED; | |
35 } | 75 } |
36 | 76 |
37 void MemoryMappedFile::CloseHandles() { | 77 void MemoryMappedFile::CloseHandles() { |
38 ThreadRestrictions::AssertIOAllowed(); | 78 ThreadRestrictions::AssertIOAllowed(); |
39 | 79 |
40 if (data_ != NULL) | 80 if (data_ != NULL) |
41 munmap(data_, length_); | 81 munmap(data_, length_); |
42 file_.Close(); | 82 file_.Close(); |
43 | 83 |
44 data_ = NULL; | 84 data_ = NULL; |
45 length_ = 0; | 85 length_ = 0; |
46 } | 86 } |
47 | 87 |
48 } // namespace base | 88 } // namespace base |
OLD | NEW |