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

Unified Diff: base/memory/discardable_memory_manager_unittest.cc

Issue 204733003: base: Refactor DiscardableMemoryManager for use with different type of allocations. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add missing unlock call and remove unnecessary unlock call Created 6 years, 9 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
Index: base/memory/discardable_memory_manager_unittest.cc
diff --git a/base/memory/discardable_memory_manager_unittest.cc b/base/memory/discardable_memory_manager_unittest.cc
index 9c1237ac2c6110700f99762c8390a4a402fc257f..8cbe8745cf16f1cb86f25a8bedfd7e4584ec771f 100644
--- a/base/memory/discardable_memory_manager_unittest.cc
+++ b/base/memory/discardable_memory_manager_unittest.cc
@@ -5,102 +5,95 @@
#include "base/memory/discardable_memory_manager.h"
#include "base/bind.h"
-#include "base/memory/discardable_memory.h"
-#include "base/run_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
-class DiscardableMemoryManagerTestBase {
+class DiscardableMemoryAllocationImpl
+ : public internal::DiscardableMemoryAllocation {
public:
- class TestDiscardableMemory : public DiscardableMemory {
- public:
- TestDiscardableMemory(
- internal::DiscardableMemoryManager* manager, size_t size)
- : manager_(manager),
- is_locked_(false) {
- manager_->Register(this, size);
- }
-
- virtual ~TestDiscardableMemory() {
- if (is_locked_)
- Unlock();
- manager_->Unregister(this);
- }
-
- // Overridden from DiscardableMemory:
- virtual DiscardableMemoryLockStatus Lock() OVERRIDE {
- DCHECK(!is_locked_);
-
- bool purged = false;
- memory_ = manager_->Acquire(this, &purged);
- if (!memory_)
- return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
+ // Overridden from internal::DiscardableMemoryAllocation:
+ virtual bool Lock() OVERRIDE { return true; }
+ virtual void Unlock() OVERRIDE {}
+ virtual void* Memory() OVERRIDE {
+ return reinterpret_cast<void*>(0xdeadbeef);
+ }
+};
- is_locked_ = true;
- return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
- : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
- }
- virtual void Unlock() OVERRIDE {
- DCHECK(is_locked_);
- manager_->Release(this, memory_.Pass());
- is_locked_ = false;
- }
- virtual void* Memory() const OVERRIDE {
- DCHECK(memory_);
- return memory_.get();
- }
+const size_t kDefaultDiscardableMemoryLimit = 8192u;
- private:
- internal::DiscardableMemoryManager* manager_;
- scoped_ptr<uint8, FreeDeleter> memory_;
- bool is_locked_;
+class DiscardableMemoryManagerTestBase
+ : public internal::DiscardableMemoryAllocation::Factory {
+ public:
+ struct Allocation {
+ Allocation(internal::DiscardableMemoryManager* manager, size_t bytes)
+ : manager(manager), id(manager->Register(bytes)) {}
+ ~Allocation() { manager->Unregister(id); }
- DISALLOW_COPY_AND_ASSIGN(TestDiscardableMemory);
+ internal::DiscardableMemoryManager* manager;
+ internal::DiscardableMemoryManager::AllocationId id;
};
DiscardableMemoryManagerTestBase()
- : manager_(new internal::DiscardableMemoryManager) {
- manager_->RegisterMemoryPressureListener();
+ : manager_(new internal::DiscardableMemoryManager(
+ this,
+ kDefaultDiscardableMemoryLimit)) {}
+
+ // Overridden from internal::DiscardableMemoryAllocation::Factory:
+ virtual scoped_ptr<internal::DiscardableMemoryAllocation>
+ CreateLockedAllocation(size_t bytes) OVERRIDE {
+ return make_scoped_ptr<internal::DiscardableMemoryAllocation>(
+ new DiscardableMemoryAllocationImpl);
}
protected:
- bool IsRegistered(const DiscardableMemory* discardable) {
- return manager_->IsRegisteredForTest(discardable);
+ bool IsRegistered(Allocation* allocation) {
+ return manager_->IsRegisteredForTest(allocation->id);
}
- bool CanBePurged(const DiscardableMemory* discardable) {
- return manager_->CanBePurgedForTest(discardable);
+ bool CanBePurged(Allocation* allocation) {
+ return manager_->CanBePurgedForTest(allocation->id);
}
size_t BytesAllocated() const {
return manager_->GetBytesAllocatedForTest();
}
- void* Memory(const DiscardableMemory* discardable) const {
- return discardable->Memory();
+ bool Lock(Allocation* allocation) {
+ bool purged;
+ return !!manager_->Lock(allocation->id, &purged);
}
+ bool Lock(Allocation* allocation, bool expected_to_be_purged) {
+ bool purged;
+ return !!manager_->Lock(allocation->id, &purged) &&
+ expected_to_be_purged == purged;
+ }
+
+ void Unlock(Allocation* allocation) { manager_->Unlock(allocation->id); }
+
void SetDiscardableMemoryLimit(size_t bytes) {
manager_->SetDiscardableMemoryLimit(bytes);
}
- void SetBytesToKeepUnderModeratePressure(size_t bytes) {
- manager_->SetBytesToKeepUnderModeratePressure(bytes);
+ scoped_ptr<Allocation> Create(size_t bytes) {
+ return make_scoped_ptr(new Allocation(manager_.get(), bytes));
}
- scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size) {
- scoped_ptr<TestDiscardableMemory> memory(
- new TestDiscardableMemory(manager_.get(), size));
- if (memory->Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_PURGED)
- return scoped_ptr<DiscardableMemory>();
- return memory.PassAs<DiscardableMemory>();
+ scoped_ptr<Allocation> CreateLocked(size_t bytes) {
+ scoped_ptr<Allocation> allocation(Create(bytes));
+ EXPECT_TRUE(IsRegistered(allocation.get()));
+ EXPECT_TRUE(Lock(allocation.get(), true));
+ return allocation.Pass();
+ }
+
+ void PurgeUntilUsageIsWithin(size_t limit) {
+ manager_->PurgeUntilUsageIsWithin(limit);
}
private:
- MessageLoopForIO message_loop_;
scoped_ptr<internal::DiscardableMemoryManager> manager_;
};
@@ -111,94 +104,81 @@ class DiscardableMemoryManagerTest
DiscardableMemoryManagerTest() {}
};
-TEST_F(DiscardableMemoryManagerTest, CreateLockedMemory) {
- size_t size = 1024;
- const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size));
- EXPECT_TRUE(IsRegistered(discardable.get()));
- EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get()));
+TEST_F(DiscardableMemoryManagerTest, CreateAndLock) {
+ size_t size = 1024u;
+ const scoped_ptr<Allocation> allocation(Create(size));
+ EXPECT_TRUE(IsRegistered(allocation.get()));
+ EXPECT_TRUE(Lock(allocation.get(), true));
EXPECT_EQ(1024u, BytesAllocated());
- EXPECT_FALSE(CanBePurged(discardable.get()));
+ EXPECT_FALSE(CanBePurged(allocation.get()));
}
-TEST_F(DiscardableMemoryManagerTest, CreateLockedMemoryZeroSize) {
- size_t size = 0;
- const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size));
- EXPECT_FALSE(discardable);
- EXPECT_FALSE(IsRegistered(discardable.get()));
+TEST_F(DiscardableMemoryManagerTest, CreateZeroSize) {
+ size_t size = 0u;
+ const scoped_ptr<Allocation> allocation(Create(size));
+ EXPECT_TRUE(IsRegistered(allocation.get()));
+ EXPECT_FALSE(Lock(allocation.get(), true));
EXPECT_EQ(0u, BytesAllocated());
}
TEST_F(DiscardableMemoryManagerTest, LockAfterUnlock) {
- size_t size = 1024;
- const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size));
- EXPECT_TRUE(IsRegistered(discardable.get()));
- EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get()));
+ size_t size = 1024u;
+ const scoped_ptr<Allocation> allocation(CreateLocked(size));
EXPECT_EQ(1024u, BytesAllocated());
- EXPECT_FALSE(CanBePurged(discardable.get()));
+ EXPECT_FALSE(CanBePurged(allocation.get()));
// Now unlock so we can lock later.
- discardable->Unlock();
- EXPECT_TRUE(CanBePurged(discardable.get()));
+ Unlock(allocation.get());
+ EXPECT_TRUE(CanBePurged(allocation.get()));
- EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS, discardable->Lock());
- EXPECT_FALSE(CanBePurged(discardable.get()));
+ EXPECT_TRUE(Lock(allocation.get(), false));
+ EXPECT_FALSE(CanBePurged(allocation.get()));
}
TEST_F(DiscardableMemoryManagerTest, LockAfterPurge) {
- size_t size = 1024;
- const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size));
- EXPECT_TRUE(IsRegistered(discardable.get()));
- EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get()));
+ size_t size = 1024u;
+ const scoped_ptr<Allocation> allocation(CreateLocked(size));
EXPECT_EQ(1024u, BytesAllocated());
- EXPECT_FALSE(CanBePurged(discardable.get()));
+ EXPECT_FALSE(CanBePurged(allocation.get()));
// Now unlock so we can lock later.
- discardable->Unlock();
- EXPECT_TRUE(CanBePurged(discardable.get()));
+ Unlock(allocation.get());
+ EXPECT_TRUE(CanBePurged(allocation.get()));
// Force the system to purge.
- MemoryPressureListener::NotifyMemoryPressure(
- MemoryPressureListener::MEMORY_PRESSURE_CRITICAL);
-
- // Required because ObserverListThreadSafe notifies via PostTask.
- RunLoop().RunUntilIdle();
+ PurgeUntilUsageIsWithin(0u);
- EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, discardable->Lock());
- EXPECT_FALSE(CanBePurged(discardable.get()));
+ EXPECT_TRUE(Lock(allocation.get(), true));
+ EXPECT_FALSE(CanBePurged(allocation.get()));
}
TEST_F(DiscardableMemoryManagerTest, LockAfterPurgeAndCannotReallocate) {
- size_t size = 1024;
- const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size));
- EXPECT_TRUE(IsRegistered(discardable.get()));
- EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get()));
+ size_t size = 1024u;
+ const scoped_ptr<Allocation> allocation(CreateLocked(size));
EXPECT_EQ(1024u, BytesAllocated());
- EXPECT_FALSE(CanBePurged(discardable.get()));
+ EXPECT_FALSE(CanBePurged(allocation.get()));
// Now unlock so we can lock later.
- discardable->Unlock();
- EXPECT_TRUE(CanBePurged(discardable.get()));
+ Unlock(allocation.get());
+ EXPECT_TRUE(CanBePurged(allocation.get()));
- // Set max allowed allocation to 1 byte. This will make cause the memory
- // to be purged.
+ // Set max allowed allocation to 1 byte. This will cause the memory to be
+ // purged.
SetDiscardableMemoryLimit(1);
- EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, discardable->Lock());
- EXPECT_FALSE(CanBePurged(discardable.get()));
+ EXPECT_TRUE(Lock(allocation.get(), true));
+ EXPECT_FALSE(CanBePurged(allocation.get()));
}
TEST_F(DiscardableMemoryManagerTest, Overflow) {
{
- size_t size = 1024;
- const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size));
- EXPECT_TRUE(IsRegistered(discardable.get()));
- EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get()));
+ size_t size = 1024u;
+ const scoped_ptr<Allocation> allocation(CreateLocked(size));
EXPECT_EQ(1024u, BytesAllocated());
size_t massive_size = std::numeric_limits<size_t>::max();
- const scoped_ptr<DiscardableMemory> massive_discardable(
- CreateLockedMemory(massive_size));
- EXPECT_FALSE(massive_discardable);
+ const scoped_ptr<Allocation> massive_allocation(Create(massive_size));
+ EXPECT_FALSE(Lock(massive_allocation.get(), true));
EXPECT_EQ(1024u, BytesAllocated());
}
EXPECT_EQ(0u, BytesAllocated());
@@ -228,27 +208,24 @@ class DiscardableMemoryManagerPermutationTest
// Use discardable memory in order specified by ordering parameter.
void CreateAndUseDiscardableMemory() {
for (int i = 0; i < 3; ++i) {
- discardables_[i] = CreateLockedMemory(1024);
- EXPECT_TRUE(discardables_[i]);
- EXPECT_NE(static_cast<void*>(NULL), Memory(discardables_[i].get()));
- discardables_[i]->Unlock();
+ allocations_[i] = CreateLocked(1024u);
+ Unlock(allocations_[i].get());
}
for (int i = 0; i < 3; ++i) {
int index = GetParam().ordering()[i];
- EXPECT_NE(DISCARDABLE_MEMORY_LOCK_STATUS_FAILED,
- discardables_[index]->Lock());
+ EXPECT_TRUE(Lock(allocations_[index].get()));
// Leave i == 0 locked.
if (i > 0)
- discardables_[index]->Unlock();
+ Unlock(allocations_[index].get());
}
}
- DiscardableMemory* discardable(unsigned position) {
- return discardables_[GetParam().ordering()[position]].get();
+ Allocation* allocation(unsigned position) {
+ return allocations_[GetParam().ordering()[position]].get();
}
private:
- scoped_ptr<DiscardableMemory> discardables_[3];
+ scoped_ptr<Allocation> allocations_[3];
};
// Verify that memory was discarded in the correct order after applying
@@ -256,17 +233,14 @@ class DiscardableMemoryManagerPermutationTest
TEST_P(DiscardableMemoryManagerPermutationTest, LRUDiscardedModeratePressure) {
CreateAndUseDiscardableMemory();
- SetBytesToKeepUnderModeratePressure(1024);
- SetDiscardableMemoryLimit(2048);
+ SetDiscardableMemoryLimit(2048u);
+ // Simulate moderate pressure by purging until usage is within 1024u bytes.
+ PurgeUntilUsageIsWithin(1024u);
- MemoryPressureListener::NotifyMemoryPressure(
- MemoryPressureListener::MEMORY_PRESSURE_MODERATE);
- RunLoop().RunUntilIdle();
-
- EXPECT_NE(DISCARDABLE_MEMORY_LOCK_STATUS_FAILED, discardable(2)->Lock());
- EXPECT_NE(DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS, discardable(1)->Lock());
+ EXPECT_TRUE(Lock(allocation(2)));
+ EXPECT_TRUE(Lock(allocation(1), true));
// 0 should still be locked.
- EXPECT_NE(static_cast<void*>(NULL), Memory(discardable(0)));
+ EXPECT_FALSE(CanBePurged(allocation(0)));
}
// Verify that memory was discarded in the correct order after changing
@@ -274,44 +248,37 @@ TEST_P(DiscardableMemoryManagerPermutationTest, LRUDiscardedModeratePressure) {
TEST_P(DiscardableMemoryManagerPermutationTest, LRUDiscardedExceedLimit) {
CreateAndUseDiscardableMemory();
- SetBytesToKeepUnderModeratePressure(1024);
- SetDiscardableMemoryLimit(2048);
+ SetDiscardableMemoryLimit(2048u);
- EXPECT_NE(DISCARDABLE_MEMORY_LOCK_STATUS_FAILED, discardable(2)->Lock());
- EXPECT_NE(DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS, discardable(1)->Lock());
+ EXPECT_TRUE(Lock(allocation(2)));
+ EXPECT_TRUE(Lock(allocation(1), true));
// 0 should still be locked.
- EXPECT_NE(static_cast<void*>(NULL), Memory(discardable(0)));
+ EXPECT_FALSE(CanBePurged(allocation(0)));
}
// Verify that no more memory than necessary was discarded after changing
// memory limit.
TEST_P(DiscardableMemoryManagerPermutationTest, LRUDiscardedAmount) {
- SetBytesToKeepUnderModeratePressure(2048);
- SetDiscardableMemoryLimit(4096);
-
CreateAndUseDiscardableMemory();
- SetDiscardableMemoryLimit(2048);
+ SetDiscardableMemoryLimit(2048u);
- EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS, discardable(2)->Lock());
- EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, discardable(1)->Lock());
+ EXPECT_TRUE(Lock(allocation(2), false));
+ EXPECT_TRUE(Lock(allocation(1), true));
// 0 should still be locked.
- EXPECT_NE(static_cast<void*>(NULL), Memory(discardable(0)));
+ EXPECT_FALSE(CanBePurged(allocation(0)));
}
-TEST_P(DiscardableMemoryManagerPermutationTest,
- CriticalPressureFreesAllUnlocked) {
+TEST_P(DiscardableMemoryManagerPermutationTest, PurgeFreesAllUnlocked) {
CreateAndUseDiscardableMemory();
- MemoryPressureListener::NotifyMemoryPressure(
- MemoryPressureListener::MEMORY_PRESSURE_CRITICAL);
- RunLoop().RunUntilIdle();
+ PurgeUntilUsageIsWithin(0u);
for (int i = 0; i < 3; ++i) {
if (i == 0)
- EXPECT_NE(static_cast<void*>(NULL), Memory(discardable(i)));
+ EXPECT_FALSE(CanBePurged(allocation(i)));
else
- EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, discardable(i)->Lock());
+ EXPECT_TRUE(Lock(allocation(i), true));
}
}
@@ -326,43 +293,25 @@ INSTANTIATE_TEST_CASE_P(DiscardableMemoryManagerPermutationTests,
TEST_F(DiscardableMemoryManagerTest, NormalDestruction) {
{
- size_t size = 1024;
- const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size));
- EXPECT_TRUE(IsRegistered(discardable.get()));
+ size_t size = 1024u;
+ const scoped_ptr<Allocation> allocation(CreateLocked(size));
EXPECT_EQ(1024u, BytesAllocated());
+ Unlock(allocation.get());
}
EXPECT_EQ(0u, BytesAllocated());
}
TEST_F(DiscardableMemoryManagerTest, DestructionWhileLocked) {
{
- size_t size = 1024;
- const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size));
- EXPECT_TRUE(IsRegistered(discardable.get()));
- EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get()));
+ size_t size = 1024u;
+ const scoped_ptr<Allocation> allocation(CreateLocked(size));
EXPECT_EQ(1024u, BytesAllocated());
- EXPECT_FALSE(CanBePurged(discardable.get()));
+ EXPECT_FALSE(CanBePurged(allocation.get()));
}
// Should have ignored the "locked" status and freed the discardable memory.
EXPECT_EQ(0u, BytesAllocated());
}
-#if !defined(NDEBUG) && !defined(OS_ANDROID) && !defined(OS_IOS)
-// Death tests are not supported with Android APKs.
-TEST_F(DiscardableMemoryManagerTest, UnlockedMemoryAccessCrashesInDebugMode) {
- size_t size = 1024;
- const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size));
- EXPECT_TRUE(IsRegistered(discardable.get()));
- EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get()));
- EXPECT_EQ(1024u, BytesAllocated());
- EXPECT_FALSE(CanBePurged(discardable.get()));
- discardable->Unlock();
- EXPECT_TRUE(CanBePurged(discardable.get()));
- // We *must* die if we are asked to vend a pointer to unlocked memory.
- EXPECT_DEATH(discardable->Memory(), ".*Check failed.*");
-}
-#endif
-
class ThreadedDiscardableMemoryManagerTest
: public DiscardableMemoryManagerTest {
public:
@@ -380,11 +329,9 @@ class ThreadedDiscardableMemoryManagerTest
}
void UseMemoryHelper() {
- size_t size = 1024;
- const scoped_ptr<DiscardableMemory> discardable(CreateLockedMemory(size));
- EXPECT_TRUE(IsRegistered(discardable.get()));
- EXPECT_NE(static_cast<void*>(NULL), Memory(discardable.get()));
- discardable->Unlock();
+ size_t size = 1024u;
+ const scoped_ptr<Allocation> allocation(CreateLocked(size));
+ Unlock(allocation.get());
}
void SignalHelper() {
« base/memory/discardable_memory_manager.h ('K') | « base/memory/discardable_memory_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698