| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/test/test_file_util.h" | 5 #include "base/test/test_file_util.h" |
| 6 | 6 |
| 7 #include <sys/mman.h> | 7 #include <sys/mman.h> |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <stdint.h> |
| 9 | 10 |
| 10 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 11 #include "base/files/memory_mapped_file.h" | 12 #include "base/files/memory_mapped_file.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 | 14 |
| 14 namespace base { | 15 namespace base { |
| 15 | 16 |
| 16 bool EvictFileFromSystemCache(const FilePath& file) { | 17 bool EvictFileFromSystemCache(const FilePath& file) { |
| 17 // There aren't any really direct ways to purge a file from the UBC. From | 18 // There aren't any really direct ways to purge a file from the UBC. From |
| 18 // talking with Amit Singh, the safest is to mmap the file with MAP_FILE (the | 19 // talking with Amit Singh, the safest is to mmap the file with MAP_FILE (the |
| 19 // default) + MAP_SHARED, then do an msync to invalidate the memory. The next | 20 // default) + MAP_SHARED, then do an msync to invalidate the memory. The next |
| 20 // open should then have to load the file from disk. | 21 // open should then have to load the file from disk. |
| 21 | 22 |
| 22 int64 length; | 23 int64_t length; |
| 23 if (!GetFileSize(file, &length)) { | 24 if (!GetFileSize(file, &length)) { |
| 24 DLOG(ERROR) << "failed to get size of " << file.value(); | 25 DLOG(ERROR) << "failed to get size of " << file.value(); |
| 25 return false; | 26 return false; |
| 26 } | 27 } |
| 27 | 28 |
| 28 // When a file is empty, we do not need to evict it from cache. | 29 // When a file is empty, we do not need to evict it from cache. |
| 29 // In fact, an attempt to map it to memory will result in error. | 30 // In fact, an attempt to map it to memory will result in error. |
| 30 if (length == 0) { | 31 if (length == 0) { |
| 31 DLOG(WARNING) << "file size is zero, will not attempt to map to memory"; | 32 DLOG(WARNING) << "file size is zero, will not attempt to map to memory"; |
| 32 return true; | 33 return true; |
| 33 } | 34 } |
| 34 | 35 |
| 35 MemoryMappedFile mapped_file; | 36 MemoryMappedFile mapped_file; |
| 36 if (!mapped_file.Initialize(file)) { | 37 if (!mapped_file.Initialize(file)) { |
| 37 DLOG(WARNING) << "failed to memory map " << file.value(); | 38 DLOG(WARNING) << "failed to memory map " << file.value(); |
| 38 return false; | 39 return false; |
| 39 } | 40 } |
| 40 | 41 |
| 41 if (msync(const_cast<uint8*>(mapped_file.data()), mapped_file.length(), | 42 if (msync(const_cast<uint8_t*>(mapped_file.data()), mapped_file.length(), |
| 42 MS_INVALIDATE) != 0) { | 43 MS_INVALIDATE) != 0) { |
| 43 DLOG(WARNING) << "failed to invalidate memory map of " << file.value() | 44 DLOG(WARNING) << "failed to invalidate memory map of " << file.value() |
| 44 << ", errno: " << errno; | 45 << ", errno: " << errno; |
| 45 return false; | 46 return false; |
| 46 } | 47 } |
| 47 | 48 |
| 48 return true; | 49 return true; |
| 49 } | 50 } |
| 50 | 51 |
| 51 } // namespace base | 52 } // namespace base |
| OLD | NEW |