Chromium Code Reviews| 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/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace base { | 13 namespace base { |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 class TestAllocationImpl : public internal::DiscardableMemoryManagerAllocation { | 16 class TestAllocationImpl : public internal::DiscardableMemoryManagerAllocation { |
| 17 public: | 17 public: |
| 18 TestAllocationImpl() : is_allocated_(false), is_locked_(false) {} | 18 TestAllocationImpl() : is_allocated_(false), is_locked_(false), bytes_(0) {} |
| 19 virtual ~TestAllocationImpl() { DCHECK(!is_locked_); } | 19 virtual ~TestAllocationImpl() { DCHECK(!is_locked_); } |
| 20 | 20 |
| 21 // Overridden from internal::DiscardableMemoryManagerAllocation: | 21 // Overridden from internal::DiscardableMemoryManagerAllocation: |
| 22 virtual bool AllocateAndAcquireLock(size_t bytes) OVERRIDE { | 22 virtual bool AllocateAndAcquireLock(size_t bytes) OVERRIDE { |
| 23 bool was_allocated = is_allocated_; | 23 bool was_allocated = is_allocated_; |
| 24 is_allocated_ = true; | 24 is_allocated_ = true; |
| 25 DCHECK(!is_locked_); | 25 DCHECK(!is_locked_); |
| 26 is_locked_ = true; | 26 is_locked_ = true; |
| 27 DCHECK(!bytes_ || bytes_ == bytes); | |
|
reveman
2014/04/26 00:13:37
how about we remove the |bytes| parameter from All
Philippe
2014/04/28 12:23:14
Good idea, I was tempted to make the change too.
| |
| 28 bytes_ = bytes; | |
| 27 return was_allocated; | 29 return was_allocated; |
| 28 } | 30 } |
| 29 virtual void ReleaseLock() OVERRIDE { | 31 virtual void ReleaseLock() OVERRIDE { |
| 30 DCHECK(is_locked_); | 32 DCHECK_EQ(is_locked_, is_allocated_); |
|
reveman
2014/04/26 00:13:37
hm, this seems unnecessary. What sequence of event
Philippe
2014/04/28 12:23:14
I think this was needed for the zero allocation (w
| |
| 31 is_locked_ = false; | 33 is_locked_ = false; |
| 32 } | 34 } |
| 33 virtual void Purge() OVERRIDE { | 35 virtual void Purge() OVERRIDE { |
| 34 DCHECK(is_allocated_); | 36 // Allocation only happens for non-zero sizes. |
| 37 DCHECK_EQ(is_allocated_, !!bytes_); | |
| 35 is_allocated_ = false; | 38 is_allocated_ = false; |
| 39 bytes_ = 0; | |
| 36 } | 40 } |
| 37 | 41 |
| 38 bool is_locked() const { return is_locked_; } | 42 bool is_locked() const { return is_locked_; } |
| 39 | 43 |
| 40 private: | 44 private: |
| 41 bool is_allocated_; | 45 bool is_allocated_; |
| 42 bool is_locked_; | 46 bool is_locked_; |
| 47 size_t bytes_; | |
| 43 }; | 48 }; |
| 44 | 49 |
| 45 class DiscardableMemoryManagerTestBase { | 50 class DiscardableMemoryManagerTestBase { |
| 46 public: | 51 public: |
| 47 DiscardableMemoryManagerTestBase() { | 52 DiscardableMemoryManagerTestBase() { |
| 48 manager_.RegisterMemoryPressureListener(); | 53 manager_.RegisterMemoryPressureListener(); |
| 49 } | 54 } |
| 50 | 55 |
| 51 protected: | 56 protected: |
| 52 enum LockStatus { | 57 enum LockStatus { |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 417 Unretained(this))); | 422 Unretained(this))); |
| 418 memory_usage_thread_.message_loop()->PostTask( | 423 memory_usage_thread_.message_loop()->PostTask( |
| 419 FROM_HERE, | 424 FROM_HERE, |
| 420 Bind(&ThreadedDiscardableMemoryManagerTest::SignalHelper, | 425 Bind(&ThreadedDiscardableMemoryManagerTest::SignalHelper, |
| 421 Unretained(this))); | 426 Unretained(this))); |
| 422 thread_sync_.Wait(); | 427 thread_sync_.Wait(); |
| 423 } | 428 } |
| 424 | 429 |
| 425 } // namespace | 430 } // namespace |
| 426 } // namespace base | 431 } // namespace base |
| OLD | NEW |