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" |
(...skipping 15 matching lines...) Expand all Loading... |
26 return was_allocated; | 26 return was_allocated; |
27 } | 27 } |
28 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 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 virtual bool IsMemoryResident() const override { | 36 bool IsMemoryResident() const override { |
37 DCHECK(is_allocated_); | 37 DCHECK(is_allocated_); |
38 return true; | 38 return true; |
39 } | 39 } |
40 | 40 |
41 bool is_locked() const { return is_locked_; } | 41 bool is_locked() const { return is_locked_; } |
42 | 42 |
43 private: | 43 private: |
44 bool is_allocated_; | 44 bool is_allocated_; |
45 bool is_locked_; | 45 bool is_locked_; |
46 }; | 46 }; |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 Unregister(&allocation[2]); | 453 Unregister(&allocation[2]); |
454 } | 454 } |
455 | 455 |
456 class ThreadedDiscardableMemoryManagerTest | 456 class ThreadedDiscardableMemoryManagerTest |
457 : public DiscardableMemoryManagerTest { | 457 : public DiscardableMemoryManagerTest { |
458 public: | 458 public: |
459 ThreadedDiscardableMemoryManagerTest() | 459 ThreadedDiscardableMemoryManagerTest() |
460 : memory_usage_thread_("memory_usage_thread"), | 460 : memory_usage_thread_("memory_usage_thread"), |
461 thread_sync_(true, false) {} | 461 thread_sync_(true, false) {} |
462 | 462 |
463 virtual void SetUp() override { memory_usage_thread_.Start(); } | 463 void SetUp() override { memory_usage_thread_.Start(); } |
464 | 464 |
465 virtual void TearDown() override { memory_usage_thread_.Stop(); } | 465 void TearDown() override { memory_usage_thread_.Stop(); } |
466 | 466 |
467 void UseMemoryHelper() { | 467 void UseMemoryHelper() { |
468 size_t size = 1024; | 468 size_t size = 1024; |
469 TestAllocationImpl allocation; | 469 TestAllocationImpl allocation; |
470 RegisterAndLock(&allocation, size); | 470 RegisterAndLock(&allocation, size); |
471 Unlock(&allocation); | 471 Unlock(&allocation); |
472 Unregister(&allocation); | 472 Unregister(&allocation); |
473 } | 473 } |
474 | 474 |
475 void SignalHelper() { thread_sync_.Signal(); } | 475 void SignalHelper() { thread_sync_.Signal(); } |
476 | 476 |
477 Thread memory_usage_thread_; | 477 Thread memory_usage_thread_; |
478 WaitableEvent thread_sync_; | 478 WaitableEvent thread_sync_; |
479 }; | 479 }; |
480 | 480 |
481 TEST_F(ThreadedDiscardableMemoryManagerTest, UseMemoryOnThread) { | 481 TEST_F(ThreadedDiscardableMemoryManagerTest, UseMemoryOnThread) { |
482 memory_usage_thread_.message_loop()->PostTask( | 482 memory_usage_thread_.message_loop()->PostTask( |
483 FROM_HERE, | 483 FROM_HERE, |
484 Bind(&ThreadedDiscardableMemoryManagerTest::UseMemoryHelper, | 484 Bind(&ThreadedDiscardableMemoryManagerTest::UseMemoryHelper, |
485 Unretained(this))); | 485 Unretained(this))); |
486 memory_usage_thread_.message_loop()->PostTask( | 486 memory_usage_thread_.message_loop()->PostTask( |
487 FROM_HERE, | 487 FROM_HERE, |
488 Bind(&ThreadedDiscardableMemoryManagerTest::SignalHelper, | 488 Bind(&ThreadedDiscardableMemoryManagerTest::SignalHelper, |
489 Unretained(this))); | 489 Unretained(this))); |
490 thread_sync_.Wait(); | 490 thread_sync_.Wait(); |
491 } | 491 } |
492 | 492 |
493 } // namespace | 493 } // namespace |
494 } // namespace base | 494 } // namespace base |
OLD | NEW |