summaryrefslogtreecommitdiffstats
path: root/base/memory
diff options
context:
space:
mode:
authorreveman <reveman@chromium.org>2015-03-25 12:21:26 -0700
committerCommit bot <commit-bot@chromium.org>2015-03-25 19:21:54 +0000
commit2d15df9ef3f9b982aef650d0be0be64a1c5d34a5 (patch)
treeacda139ba1ea27a83799c2d05c660127cc7cbd65 /base/memory
parent2c90787990638e31794cf94c2cf13dcc50ad2327 (diff)
downloadchromium_src-2d15df9ef3f9b982aef650d0be0be64a1c5d34a5.zip
chromium_src-2d15df9ef3f9b982aef650d0be0be64a1c5d34a5.tar.gz
chromium_src-2d15df9ef3f9b982aef650d0be0be64a1c5d34a5.tar.bz2
base: Replace PurgeAndTruncate with Shrink function.
This cleans up the API a bit and allows the code to be unit tested. This also prevents ftruncate from being used on Android where shared memory is backed by Ashmem. Ashmem doesn't support ftruncate. BUG=466509 TEST=base_unittests --gtest_filter=DiscardableSharedMemoryTest.Shrink Review URL: https://codereview.chromium.org/1018743003 Cr-Commit-Position: refs/heads/master@{#322207}
Diffstat (limited to 'base/memory')
-rw-r--r--base/memory/discardable_shared_memory.cc36
-rw-r--r--base/memory/discardable_shared_memory.h18
-rw-r--r--base/memory/discardable_shared_memory_unittest.cc16
3 files changed, 48 insertions, 22 deletions
diff --git a/base/memory/discardable_shared_memory.cc b/base/memory/discardable_shared_memory.cc
index 49e93cd..830d6b9 100644
--- a/base/memory/discardable_shared_memory.cc
+++ b/base/memory/discardable_shared_memory.cc
@@ -325,22 +325,6 @@ bool DiscardableSharedMemory::Purge(Time current_time) {
return true;
}
-bool DiscardableSharedMemory::PurgeAndTruncate(Time current_time) {
- if (!Purge(current_time))
- return false;
-
-#if defined(OS_POSIX)
- // Truncate shared memory to size of SharedState.
- SharedMemoryHandle handle = shared_memory_.handle();
- if (SharedMemory::IsHandleValid(handle)) {
- if (HANDLE_EINTR(ftruncate(handle.fd, sizeof(SharedState))) != 0)
- DPLOG(ERROR) << "ftruncate() failed";
- }
-#endif
-
- return true;
-}
-
bool DiscardableSharedMemory::IsMemoryResident() const {
DCHECK(shared_memory_.memory());
@@ -357,6 +341,26 @@ void DiscardableSharedMemory::Close() {
mapped_size_ = 0;
}
+#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(handle.fd, AlignToPageSize(sizeof(SharedState)))) != 0) {
+ DPLOG(ERROR) << "ftruncate() failed";
+ return;
+ }
+ mapped_size_ = 0;
+#else
+ NOTIMPLEMENTED();
+#endif
+}
+#endif
+
Time DiscardableSharedMemory::Now() const {
return Time::Now();
}
diff --git a/base/memory/discardable_shared_memory.h b/base/memory/discardable_shared_memory.h
index e3b437c..892d556 100644
--- a/base/memory/discardable_shared_memory.h
+++ b/base/memory/discardable_shared_memory.h
@@ -15,6 +15,12 @@
#include <set>
#endif
+// Define DISCARDABLE_SHARED_MEMORY_SHRINKING if platform supports shrinking
+// of discardable shared memory segments.
+#if defined(OS_POSIX) && !defined(OS_ANDROID)
+#define DISCARDABLE_SHARED_MEMORY_SHRINKING
+#endif
+
namespace base {
// Platform abstraction for discardable shared memory.
@@ -93,12 +99,6 @@ class BASE_EXPORT DiscardableSharedMemory {
// each call.
bool Purge(Time current_time);
- // Purge and release as much memory as possible to the OS.
- // Note: The amount of memory that can be released to the OS is platform
- // specific. Best case, all but one page is released. Worst case, nothing
- // is released.
- bool PurgeAndTruncate(Time current_time);
-
// Returns true if memory is still resident.
bool IsMemoryResident() const;
@@ -116,6 +116,12 @@ class BASE_EXPORT DiscardableSharedMemory {
return shared_memory_.ShareToProcess(process_handle, new_handle);
}
+#if defined(DISCARDABLE_SHARED_MEMORY_SHRINKING)
+ // Release as much memory as possible to the OS. The change in size will
+ // be reflected by the return value of mapped_size().
+ void Shrink();
+#endif
+
private:
// Virtual for tests.
virtual Time Now() const;
diff --git a/base/memory/discardable_shared_memory_unittest.cc b/base/memory/discardable_shared_memory_unittest.cc
index 74d19a6..ae7235d 100644
--- a/base/memory/discardable_shared_memory_unittest.cc
+++ b/base/memory/discardable_shared_memory_unittest.cc
@@ -311,5 +311,21 @@ TEST(DiscardableSharedMemoryTest, MappedSize) {
EXPECT_EQ(0u, memory.mapped_size());
}
+#if defined(DISCARDABLE_SHARED_MEMORY_SHRINKING)
+TEST(DiscardableSharedMemoryTest, Shrink) {
+ const uint32 kDataSize = 1024;
+
+ TestDiscardableSharedMemory memory;
+ bool rv = memory.CreateAndMap(kDataSize);
+ ASSERT_TRUE(rv);
+
+ EXPECT_NE(0u, memory.mapped_size());
+
+ // Mapped size should be 0 after shrinking memory segment.
+ memory.Shrink();
+ EXPECT_EQ(0u, memory.mapped_size());
+}
+#endif
+
} // namespace
} // namespace base