Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(393)

Unified Diff: base/memory/discardable_shared_memory.cc

Issue 1413163006: Revert of base: Use MADV_REMOVE instead of ftruncate to purge discardable memory segments. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/memory/discardable_shared_memory.h ('k') | base/memory/discardable_shared_memory_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..d0eaca1a66219eafee72db30d3e91170e4055e98 100644
--- a/base/memory/discardable_shared_memory.cc
+++ b/base/memory/discardable_shared_memory.cc
@@ -4,9 +4,8 @@
#include "base/memory/discardable_shared_memory.h"
-#if defined(OS_POSIX) && !defined(OS_NACL)
-// For madvise() which is available on all POSIX compatible systems.
-#include <sys/mman.h>
+#if defined(OS_POSIX)
+#include <unistd.h>
#endif
#include <algorithm>
@@ -302,7 +301,11 @@
bool DiscardableSharedMemory::Purge(Time current_time) {
// Calls to this function must be synchronized properly.
DFAKE_SCOPED_LOCK(thread_collision_warner_);
- DCHECK(shared_memory_.memory());
+
+ // 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;
SharedState old_state(SharedState::UNLOCKED, last_known_usage_);
SharedState new_state(SharedState::UNLOCKED, Time());
@@ -323,25 +326,6 @@
return false;
}
-#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),
-// provide MADV_FREE which has the same result but memory is purged lazily.
-#if defined(OS_LINUX) || defined(OS_ANDROID)
-#define MADV_PURGE_ARGUMENT MADV_REMOVE
-#else
-#define MADV_PURGE_ARGUMENT MADV_FREE
-#endif
- // Advise the kernel to remove resources associated with purged pages.
- // Subsequent accesses of memory pages will succeed, but might result in
- // zero-fill-on-demand pages.
- if (madvise(reinterpret_cast<char*>(shared_memory_.memory()) +
- AlignToPageSize(sizeof(SharedState)),
- AlignToPageSize(mapped_size_), MADV_PURGE_ARGUMENT)) {
- DPLOG(ERROR) << "madvise() failed";
- }
-#endif
-
last_known_usage_ = Time();
return true;
}
@@ -369,6 +353,26 @@
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();
}
« no previous file with comments | « base/memory/discardable_shared_memory.h ('k') | base/memory/discardable_shared_memory_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698