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..0721740231aab71c8ffca88062d2bc816f846d33 100644 |
--- a/base/memory/discardable_shared_memory.cc |
+++ b/base/memory/discardable_shared_memory.cc |
@@ -214,6 +214,7 @@ DiscardableSharedMemory::LockResult DiscardableSharedMemory::Lock( |
DCHECK_EQ(locked_pages_.size(), locked_page_count_); |
#endif |
+// Pin pages if supported. |
#if defined(OS_ANDROID) |
SharedMemoryHandle handle = shared_memory_.handle(); |
if (SharedMemory::IsHandleValid(handle)) { |
@@ -222,6 +223,17 @@ DiscardableSharedMemory::LockResult DiscardableSharedMemory::Lock( |
return PURGED; |
} |
} |
+#elif defined(OS_WIN) |
+ // If osVersion is 0, we need to lazy init. |
+ if ((osVersion >= base::win::VERSION_WIN8) || |
+ ((!osVersion) && |
+ ((osVersion = base::win::GetVersion()) >= base::win::VERSION_WIN8))) { |
reveman
2015/11/05 19:13:47
I'm not a fan of this. It's hard to read. There's
penny
2015/11/05 19:51:29
Bruce, Justin and I don't feel strongly about this
|
+ if (!VirtualAlloc(reinterpret_cast<char*>(shared_memory_.memory()) + |
+ AlignToPageSize(sizeof(SharedState)) + offset, |
+ length, MEM_RESET_UNDO, PAGE_READWRITE)) { |
+ return PURGED; |
+ } |
+ } |
#endif |
return SUCCESS; |
@@ -240,6 +252,7 @@ void DiscardableSharedMemory::Unlock(size_t offset, size_t length) { |
DCHECK(shared_memory_.memory()); |
+// Unpin pages if supported. |
#if defined(OS_ANDROID) |
SharedMemoryHandle handle = shared_memory_.handle(); |
if (SharedMemory::IsHandleValid(handle)) { |
@@ -248,6 +261,21 @@ void DiscardableSharedMemory::Unlock(size_t offset, size_t length) { |
DPLOG(ERROR) << "ashmem_unpin_region() failed"; |
} |
} |
+#elif defined(OS_WIN) |
+ // If osVersion is 0, we need to lazy init. |
+ if ((osVersion >= base::win::VERSION_WIN8) || |
+ ((!osVersion) && |
+ ((osVersion = base::win::GetVersion()) >= base::win::VERSION_WIN8))) { |
+ // Note: MEM_RESET is not technically gated on Win8. However, this Unlock |
+ // function needs to match the Lock behaviour (MEM_RESET_UNDO) to properly |
+ // implement memory pinning. It needs to bias towards preserving the |
+ // contents of memory between an Unlock and next Lock. |
+ if (!VirtualAlloc(reinterpret_cast<char*>(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(); |
@@ -323,6 +351,12 @@ bool DiscardableSharedMemory::Purge(Time current_time) { |
return false; |
} |
+// The next section will release as much resource as can be done |
+// from the purging process, until the client process notices the |
+// purge and releases its own references. |
+// Note: this memory will not be accessed again. The segment will be |
+// freed asynchronously at a later time, so just do the best |
+// immediately. |
#if defined(OS_POSIX) && !defined(OS_NACL) |
// Linux and Android provide MADV_REMOVE which is preferred as it has a |
// behavior that can be verified in tests. Other POSIX flavors (MacOSX, BSDs), |
@@ -340,6 +374,14 @@ 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. |
+ 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(); |
@@ -373,4 +415,12 @@ Time DiscardableSharedMemory::Now() const { |
return Time::Now(); |
} |
+#if defined(OS_WIN) |
+// Initialize our static Windows version to 0. |
+// Lazy init will happen when first checked in Lock/Unlock. |
+// static |
+base::win::Version DiscardableSharedMemory::osVersion = |
+ base::win::VERSION_PRE_XP; |
+#endif |
+ |
} // namespace base |