Index: base/memory/discardable_shared_memory.cc |
diff --git a/base/memory/discardable_shared_memory.cc b/base/memory/discardable_shared_memory.cc |
index d0eaca1a66219eafee72db30d3e91170e4055e98..9078c8fce3efa457960caaeb126d9c707b76fb9a 100644 |
--- a/base/memory/discardable_shared_memory.cc |
+++ b/base/memory/discardable_shared_memory.cc |
@@ -4,8 +4,8 @@ |
#include "base/memory/discardable_shared_memory.h" |
-#if defined(OS_POSIX) |
-#include <unistd.h> |
+#if defined(OS_POSIX) && !defined(OS_NACL) |
danakj
2015/10/20 18:04:03
I'm still kinda uncomfortable with this statement
reveman
2015/10/21 14:50:57
madvise() is part of the POSIX standard. It was ad
|
+#include <sys/mman.h> |
#endif |
#include <algorithm> |
@@ -20,6 +20,14 @@ |
#include "third_party/ashmem/ashmem.h" |
#endif |
+// Use MADV_FREE when MADV_REMOVE is not available. MADV_FREE doesn't give us |
Primiano Tucci (use gerrit)
2015/10/20 13:33:39
I'd probably make this comment a bit more explicit
reveman
2015/10/21 14:50:57
Done.
|
+// the guarantee that pages will be zero-filled after a call which makes it |
+// hard to test but it shouldn't hurt to use it. Worst case, it has the effect |
+// of not calling madvise. |
+#if !defined(DISCARDABLE_SHARED_MEMORY_REMOVE) |
+#define MADV_REMOVE MADV_FREE |
+#endif |
+ |
namespace base { |
namespace { |
@@ -301,11 +309,7 @@ void* DiscardableSharedMemory::memory() const { |
bool DiscardableSharedMemory::Purge(Time current_time) { |
// Calls to this function must be synchronized properly. |
DFAKE_SCOPED_LOCK(thread_collision_warner_); |
- |
- // Early out if not mapped. This can happen if the segment was previously |
- // unmapped using a call to Close(). |
- if (!shared_memory_.memory()) |
- return true; |
+ DCHECK(shared_memory_.memory()); |
SharedState old_state(SharedState::UNLOCKED, last_known_usage_); |
SharedState new_state(SharedState::UNLOCKED, Time()); |
@@ -326,6 +330,17 @@ bool DiscardableSharedMemory::Purge(Time current_time) { |
return false; |
} |
+#if defined(OS_POSIX) && !defined(OS_NACL) |
+ // Advise the kernel to remove resources associated with purged pages. |
+ // Subsequent accesses of memory pages will succeed, but will result in |
+ // zero-fill-on-demand pages. |
+ if (madvise(reinterpret_cast<char*>(shared_memory_.memory()) + |
+ AlignToPageSize(sizeof(SharedState)), |
+ AlignToPageSize(mapped_size_), MADV_REMOVE)) { |
+ DPLOG(ERROR) << "madvise(MADV_REMOVE) failed"; |
Primiano Tucci (use gerrit)
2015/10/20 13:33:39
On mac if this fails you will say that madvise(MAD
reveman
2015/10/21 14:50:57
Done.
|
+ } |
+#endif |
+ |
last_known_usage_ = Time(); |
return true; |
} |
@@ -353,26 +368,6 @@ void DiscardableSharedMemory::Close() { |
shared_memory_.Close(); |
} |
-#if defined(DISCARDABLE_SHARED_MEMORY_SHRINKING) |
-void DiscardableSharedMemory::Shrink() { |
-#if defined(OS_POSIX) |
- SharedMemoryHandle handle = shared_memory_.handle(); |
- if (!SharedMemory::IsHandleValid(handle)) |
- return; |
- |
- // Truncate shared memory to size of SharedState. |
- if (HANDLE_EINTR(ftruncate(SharedMemory::GetFdFromSharedMemoryHandle(handle), |
- AlignToPageSize(sizeof(SharedState)))) != 0) { |
- DPLOG(ERROR) << "ftruncate() failed"; |
- return; |
- } |
- mapped_size_ = 0; |
-#else |
- NOTIMPLEMENTED(); |
-#endif |
-} |
-#endif |
- |
Time DiscardableSharedMemory::Now() const { |
return Time::Now(); |
} |