OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/discardable_memory_manager.h" | 5 #include "base/memory/discardable_memory_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 namespace { | 13 namespace { |
14 | 14 |
15 class TestAllocationImpl : public internal::DiscardableMemoryManagerAllocation { | 15 class TestAllocationImpl : public internal::DiscardableMemoryManagerAllocation { |
16 public: | 16 public: |
17 TestAllocationImpl() : is_allocated_(false), is_locked_(false) {} | 17 TestAllocationImpl() : is_allocated_(false), is_locked_(false) {} |
18 virtual ~TestAllocationImpl() { DCHECK(!is_locked_); } | 18 virtual ~TestAllocationImpl() { DCHECK(!is_locked_); } |
19 | 19 |
20 // Overridden from internal::DiscardableMemoryManagerAllocation: | 20 // Overridden from internal::DiscardableMemoryManagerAllocation: |
21 virtual bool AllocateAndAcquireLock() OVERRIDE { | 21 bool AllocateAndAcquireLock() override { |
22 bool was_allocated = is_allocated_; | 22 bool was_allocated = is_allocated_; |
23 is_allocated_ = true; | 23 is_allocated_ = true; |
24 DCHECK(!is_locked_); | 24 DCHECK(!is_locked_); |
25 is_locked_ = true; | 25 is_locked_ = true; |
26 return was_allocated; | 26 return was_allocated; |
27 } | 27 } |
28 virtual void ReleaseLock() OVERRIDE { | 28 void ReleaseLock() override { |
29 DCHECK(is_locked_); | 29 DCHECK(is_locked_); |
30 is_locked_ = false; | 30 is_locked_ = false; |
31 } | 31 } |
32 virtual void Purge() OVERRIDE { | 32 void Purge() override { |
33 DCHECK(is_allocated_); | 33 DCHECK(is_allocated_); |
34 is_allocated_ = false; | 34 is_allocated_ = false; |
35 } | 35 } |
36 | 36 |
37 bool is_locked() const { return is_locked_; } | 37 bool is_locked() const { return is_locked_; } |
38 | 38 |
39 private: | 39 private: |
40 bool is_allocated_; | 40 bool is_allocated_; |
41 bool is_locked_; | 41 bool is_locked_; |
42 }; | 42 }; |
43 | 43 |
44 // Tests can assume that the default limit is at least 1024. Tests that rely on | 44 // Tests can assume that the default limit is at least 1024. Tests that rely on |
45 // something else needs to explicit set the limit. | 45 // something else needs to explicit set the limit. |
46 const size_t kDefaultMemoryLimit = 1024; | 46 const size_t kDefaultMemoryLimit = 1024; |
47 const size_t kDefaultSoftMemoryLimit = kDefaultMemoryLimit; | 47 const size_t kDefaultSoftMemoryLimit = kDefaultMemoryLimit; |
48 | 48 |
49 class TestDiscardableMemoryManagerImpl | 49 class TestDiscardableMemoryManagerImpl |
50 : public internal::DiscardableMemoryManager { | 50 : public internal::DiscardableMemoryManager { |
51 public: | 51 public: |
52 TestDiscardableMemoryManagerImpl() | 52 TestDiscardableMemoryManagerImpl() |
53 : DiscardableMemoryManager(kDefaultMemoryLimit, | 53 : DiscardableMemoryManager(kDefaultMemoryLimit, |
54 kDefaultSoftMemoryLimit, | 54 kDefaultSoftMemoryLimit, |
55 TimeDelta::Max()) {} | 55 TimeDelta::Max()) {} |
56 | 56 |
57 void SetNow(TimeTicks now) { now_ = now; } | 57 void SetNow(TimeTicks now) { now_ = now; } |
58 | 58 |
59 private: | 59 private: |
60 // Overriden from internal::DiscardableMemoryManager: | 60 // Overriden from internal::DiscardableMemoryManager: |
61 virtual TimeTicks Now() const OVERRIDE { return now_; } | 61 TimeTicks Now() const override { return now_; } |
62 | 62 |
63 TimeTicks now_; | 63 TimeTicks now_; |
64 }; | 64 }; |
65 | 65 |
66 class DiscardableMemoryManagerTestBase { | 66 class DiscardableMemoryManagerTestBase { |
67 public: | 67 public: |
68 DiscardableMemoryManagerTestBase() {} | 68 DiscardableMemoryManagerTestBase() {} |
69 | 69 |
70 protected: | 70 protected: |
71 enum LockStatus { | 71 enum LockStatus { |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 Unregister(&allocation[2]); | 449 Unregister(&allocation[2]); |
450 } | 450 } |
451 | 451 |
452 class ThreadedDiscardableMemoryManagerTest | 452 class ThreadedDiscardableMemoryManagerTest |
453 : public DiscardableMemoryManagerTest { | 453 : public DiscardableMemoryManagerTest { |
454 public: | 454 public: |
455 ThreadedDiscardableMemoryManagerTest() | 455 ThreadedDiscardableMemoryManagerTest() |
456 : memory_usage_thread_("memory_usage_thread"), | 456 : memory_usage_thread_("memory_usage_thread"), |
457 thread_sync_(true, false) {} | 457 thread_sync_(true, false) {} |
458 | 458 |
459 virtual void SetUp() OVERRIDE { memory_usage_thread_.Start(); } | 459 void SetUp() override { memory_usage_thread_.Start(); } |
460 | 460 |
461 virtual void TearDown() OVERRIDE { memory_usage_thread_.Stop(); } | 461 void TearDown() override { memory_usage_thread_.Stop(); } |
462 | 462 |
463 void UseMemoryHelper() { | 463 void UseMemoryHelper() { |
464 size_t size = 1024; | 464 size_t size = 1024; |
465 TestAllocationImpl allocation; | 465 TestAllocationImpl allocation; |
466 RegisterAndLock(&allocation, size); | 466 RegisterAndLock(&allocation, size); |
467 Unlock(&allocation); | 467 Unlock(&allocation); |
468 Unregister(&allocation); | 468 Unregister(&allocation); |
469 } | 469 } |
470 | 470 |
471 void SignalHelper() { thread_sync_.Signal(); } | 471 void SignalHelper() { thread_sync_.Signal(); } |
472 | 472 |
473 Thread memory_usage_thread_; | 473 Thread memory_usage_thread_; |
474 WaitableEvent thread_sync_; | 474 WaitableEvent thread_sync_; |
475 }; | 475 }; |
476 | 476 |
477 TEST_F(ThreadedDiscardableMemoryManagerTest, UseMemoryOnThread) { | 477 TEST_F(ThreadedDiscardableMemoryManagerTest, UseMemoryOnThread) { |
478 memory_usage_thread_.message_loop()->PostTask( | 478 memory_usage_thread_.message_loop()->PostTask( |
479 FROM_HERE, | 479 FROM_HERE, |
480 Bind(&ThreadedDiscardableMemoryManagerTest::UseMemoryHelper, | 480 Bind(&ThreadedDiscardableMemoryManagerTest::UseMemoryHelper, |
481 Unretained(this))); | 481 Unretained(this))); |
482 memory_usage_thread_.message_loop()->PostTask( | 482 memory_usage_thread_.message_loop()->PostTask( |
483 FROM_HERE, | 483 FROM_HERE, |
484 Bind(&ThreadedDiscardableMemoryManagerTest::SignalHelper, | 484 Bind(&ThreadedDiscardableMemoryManagerTest::SignalHelper, |
485 Unretained(this))); | 485 Unretained(this))); |
486 thread_sync_.Wait(); | 486 thread_sync_.Wait(); |
487 } | 487 } |
488 | 488 |
489 } // namespace | 489 } // namespace |
490 } // namespace base | 490 } // namespace base |
OLD | NEW |