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