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/posix/eintr_wrapper.h" | 12 #include "base/posix/eintr_wrapper.h" |
13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 | 16 |
17 MemoryMappedFile::MemoryMappedFile() | 17 MemoryMappedFile::MemoryMappedFile() |
18 : file_(kInvalidPlatformFileValue), | 18 : file_(kInvalidPlatformFileValue), |
19 data_(NULL), | 19 data_(NULL), |
20 length_(0) { | 20 length_(0) { |
21 } | 21 } |
22 | 22 |
23 bool MemoryMappedFile::MapFileToMemoryInternal() { | 23 bool MemoryMappedFile::MapFileToMemoryInternal() { |
24 ThreadRestrictions::AssertIOAllowed(); | 24 ThreadRestrictions::AssertIOAllowed(); |
25 | 25 |
26 struct stat file_stat; | 26 struct stat file_stat; |
27 if (fstat(file_, &file_stat) == kInvalidPlatformFileValue) { | 27 if (fstat(file_, &file_stat) == kInvalidPlatformFileValue) { |
28 DLOG(ERROR) << "Couldn't fstat " << file_ << ", errno " << errno; | 28 DPLOG(ERROR) << "fstat " << file_; |
29 return false; | 29 return false; |
30 } | 30 } |
31 length_ = file_stat.st_size; | 31 length_ = file_stat.st_size; |
32 | 32 |
33 data_ = static_cast<uint8*>( | 33 data_ = static_cast<uint8*>( |
34 mmap(NULL, length_, PROT_READ, MAP_SHARED, file_, 0)); | 34 mmap(NULL, length_, PROT_READ, MAP_SHARED, file_, 0)); |
35 if (data_ == MAP_FAILED) | 35 if (data_ == MAP_FAILED) |
36 DLOG(ERROR) << "Couldn't mmap " << file_ << ", errno " << errno; | 36 DPLOG(ERROR) << "mmap " << file_; |
37 | 37 |
38 return data_ != MAP_FAILED; | 38 return data_ != MAP_FAILED; |
39 } | 39 } |
40 | 40 |
41 void MemoryMappedFile::CloseHandles() { | 41 void MemoryMappedFile::CloseHandles() { |
42 ThreadRestrictions::AssertIOAllowed(); | 42 ThreadRestrictions::AssertIOAllowed(); |
43 | 43 |
44 if (data_ != NULL) | 44 if (data_ != NULL) |
45 munmap(data_, length_); | 45 munmap(data_, length_); |
46 if (file_ != kInvalidPlatformFileValue) | 46 if (file_ != kInvalidPlatformFileValue) |
47 ignore_result(HANDLE_EINTR(close(file_))); | 47 ignore_result(HANDLE_EINTR(close(file_))); |
48 | 48 |
49 data_ = NULL; | 49 data_ = NULL; |
50 length_ = 0; | 50 length_ = 0; |
51 file_ = kInvalidPlatformFileValue; | 51 file_ = kInvalidPlatformFileValue; |
52 } | 52 } |
53 | 53 |
54 } // namespace base | 54 } // namespace base |
OLD | NEW |