Index: base/memory/discardable_shared_memory.cc |
diff --git a/base/memory/discardable_shared_memory.cc b/base/memory/discardable_shared_memory.cc |
index 1b50da260e09b566807fb40e72a1ab0638db11d3..5b0fc9437c4ee885e4eb9b29047b67c44f7ea6f7 100644 |
--- a/base/memory/discardable_shared_memory.cc |
+++ b/base/memory/discardable_shared_memory.cc |
@@ -21,6 +21,10 @@ |
#include "third_party/ashmem/ashmem.h" |
#endif |
+#if defined(OS_WIN) |
+#include "base/win/windows_version.h" |
+#endif |
+ |
namespace base { |
namespace { |
@@ -98,6 +102,38 @@ size_t AlignToPageSize(size_t size) { |
} // namespace |
+// Indicates whether Lock/Unlock pinning is supported on this platform. |
+// Call DoPinning() to access this information from within class functions. |
+// static |
+char DiscardableSharedMemory::pinning_supported_ = -1; |
+ |
+// Lazy initialize pinning_supported_ to 0 (false) or 1 (true). |
+// -1 when unchecked. |
+bool DiscardableSharedMemory::DoPinning() { |
+ bool supported = false; |
+ switch (pinning_supported_) { |
+ case 1: |
+ supported = true; |
+ case 0: |
+ supported = false; |
+ case -1: |
+ pinning_supported_ = 0; |
+#if defined(OS_ANDROID) |
+ // Ashmem support. |
+ pinning_supported_ = 1; |
+ supported = true; |
+#elif defined(OS_WIN) |
+ // Check windows version. |
+ // We need >= 8 for MEM_RESET_UNDO. |
+ if (base::win::GetVersion() >= base::win::VERSION_WIN8) { |
reveman
2015/11/03 00:23:57
base::win::GetVersion() is cheap and already cache
penny
2015/11/03 01:14:47
Thank for this! I missed the fact that OSInfo act
|
+ pinning_supported_ = 1; |
+ supported = true; |
+ } |
+#endif |
+ } |
+ return supported; |
+} |
+ |
DiscardableSharedMemory::DiscardableSharedMemory() |
: mapped_size_(0), locked_page_count_(0) { |
} |
@@ -214,15 +250,28 @@ DiscardableSharedMemory::LockResult DiscardableSharedMemory::Lock( |
DCHECK_EQ(locked_pages_.size(), locked_page_count_); |
#endif |
+ if (DoPinning()) { |
reveman
2015/11/03 00:23:57
this check is never false on android so unnecessar
penny
2015/11/03 01:14:47
Done.
|
#if defined(OS_ANDROID) |
- SharedMemoryHandle handle = shared_memory_.handle(); |
- if (SharedMemory::IsHandleValid(handle)) { |
- if (ashmem_pin_region( |
- handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { |
+ SharedMemoryHandle handle = shared_memory_.handle(); |
+ if (SharedMemory::IsHandleValid(handle)) { |
+ if (ashmem_pin_region( |
+ handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { |
+ return PURGED; |
+ } |
+ } |
+#elif defined(OS_WIN) |
+ if (nullptr == VirtualAlloc(reinterpret_cast<char*>( |
reveman
2015/11/03 00:23:57
nit: chromium style is "if (!VirtualAlloc(reinte..
penny
2015/11/03 01:14:47
Done.
|
+ shared_memory_.memory()) + |
+ AlignToPageSize( |
+ sizeof(SharedState)) + |
+ offset, |
+ length, |
+ MEM_RESET_UNDO, |
+ PAGE_READWRITE)) { |
return PURGED; |
} |
- } |
#endif |
+ } |
return SUCCESS; |
} |
@@ -240,15 +289,28 @@ void DiscardableSharedMemory::Unlock(size_t offset, size_t length) { |
DCHECK(shared_memory_.memory()); |
+ if (DoPinning()) { |
#if defined(OS_ANDROID) |
- SharedMemoryHandle handle = shared_memory_.handle(); |
- if (SharedMemory::IsHandleValid(handle)) { |
- if (ashmem_unpin_region( |
- handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { |
- DPLOG(ERROR) << "ashmem_unpin_region() failed"; |
+ SharedMemoryHandle handle = shared_memory_.handle(); |
+ if (SharedMemory::IsHandleValid(handle)) { |
+ if (ashmem_unpin_region( |
+ handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { |
+ DPLOG(ERROR) << "ashmem_unpin_region() failed"; |
+ } |
} |
- } |
+#elif defined(OS_WIN) |
+ if (nullptr == VirtualAlloc(reinterpret_cast<char*>( |
reveman
2015/11/03 00:23:57
if (!VirtualAlloc(reinte...
penny
2015/11/03 01:14:47
Done.
|
+ shared_memory_.memory()) + |
+ AlignToPageSize( |
+ sizeof(SharedState)) + |
+ offset, |
+ length, |
+ MEM_RESET, |
+ PAGE_READWRITE)) { |
+ DPLOG(ERROR) << "VirtualAlloc() MEM_RESET failed in Unlock()"; |
+ } |
#endif |
+ } |
size_t start = offset / base::GetPageSize(); |
size_t end = start + length / base::GetPageSize(); |
@@ -340,6 +402,17 @@ bool DiscardableSharedMemory::Purge(Time current_time) { |
AlignToPageSize(mapped_size_), MADV_PURGE_ARGUMENT)) { |
DPLOG(ERROR) << "madvise() failed"; |
} |
+#elif defined(OS_WIN) |
+ // MEM_DECOMMIT the purged pages to release the physical storage, |
+ // either in memory or in the paging file on disk. Pages remain RESERVED. |
+ // NOTE: this is the best that can be done from the browser, until the |
+ // child/renderer notices the purge and releases its own handles. |
+ if (!VirtualFree(reinterpret_cast<char*>(shared_memory_.memory()) + |
+ AlignToPageSize(sizeof(SharedState)), |
+ AlignToPageSize(mapped_size_), |
+ MEM_DECOMMIT)) { |
+ DPLOG(ERROR) << "VirtualFree() MEM_DECOMMIT failed in Purge()"; |
+ } |
#endif |
last_known_usage_ = Time(); |