Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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 "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 | 11 |
| 12 namespace file_util { | 12 namespace file_util { |
| 13 | 13 |
| 14 bool EvictFileFromSystemCache(const FilePath& file) { | 14 bool EvictFileFromSystemCache(const FilePath& file) { |
| 15 // There aren't any really direct ways to purge a file from the UBC. From | 15 // There aren't any really direct ways to purge a file from the UBC. From |
| 16 // talking with Amit Singh, the safest is to mmap the file with MAP_FILE (the | 16 // talking with Amit Singh, the safest is to mmap the file with MAP_FILE (the |
| 17 // default) + MAP_SHARED, then do an msync to invalidate the memory. The next | 17 // default) + MAP_SHARED, then do an msync to invalidate the memory. The next |
| 18 // open should then have to load the file from disk. | 18 // open should then have to load the file from disk. |
| 19 | 19 |
| 20 int64 length; | |
| 21 if (!file_util::GetFileSize(file, &length)) { | |
| 22 DLOG(WARNING) << "failed to get size of " << file.value(); | |
|
Paweł Hajdan Jr.
2011/07/30 00:05:09
LOG(ERROR) here please.
Huyen
2011/08/02 22:02:28
Done.
| |
| 23 return false; | |
| 24 } | |
| 25 | |
| 26 // When a file is empty, we do not need to evict it from cache. | |
| 27 // In fact, an attempt to map it to memory will result in error. | |
| 28 if (!length) | |
|
Paweł Hajdan Jr.
2011/07/30 00:05:09
LOG(WARNING) here please.
Paweł Hajdan Jr.
2011/07/30 00:05:09
Please explicitly compare with 0 instead of an imp
Huyen
2011/08/02 22:02:28
Done.
Huyen
2011/08/02 22:02:28
Done.
| |
| 29 return true; | |
| 30 | |
| 20 file_util::MemoryMappedFile mapped_file; | 31 file_util::MemoryMappedFile mapped_file; |
| 21 if (!mapped_file.Initialize(file)) { | 32 if (!mapped_file.Initialize(file)) { |
| 22 DLOG(WARNING) << "failed to memory map " << file.value(); | 33 DLOG(WARNING) << "failed to memory map " << file.value(); |
|
Paweł Hajdan Jr.
2011/07/30 00:05:09
Oh, and also change those DLOG -> LOG.
Huyen
2011/08/02 22:02:28
Done.
| |
| 23 return false; | 34 return false; |
| 24 } | 35 } |
| 25 | 36 |
| 26 if (msync(const_cast<uint8*>(mapped_file.data()), mapped_file.length(), | 37 if (msync(const_cast<uint8*>(mapped_file.data()), mapped_file.length(), |
| 27 MS_INVALIDATE) != 0) { | 38 MS_INVALIDATE) != 0) { |
| 28 DLOG(WARNING) << "failed to invalidate memory map of " << file.value() | 39 DLOG(WARNING) << "failed to invalidate memory map of " << file.value() |
| 29 << ", errno: " << errno; | 40 << ", errno: " << errno; |
| 30 return false; | 41 return false; |
| 31 } | 42 } |
| 32 | 43 |
| 33 return true; | 44 return true; |
| 34 } | 45 } |
| 35 | 46 |
| 36 } // namespace file_util | 47 } // namespace file_util |
| OLD | NEW |