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