| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_emulated.h" | 5 #include "base/memory/discardable_memory_emulated.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/memory/discardable_memory_provider.h" | 8 #include "base/memory/discardable_memory_provider.h" |
| 9 | 9 |
| 10 namespace base { | 10 namespace base { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 : is_locked_(false) { | 22 : is_locked_(false) { |
| 23 g_provider.Pointer()->Register(this, size); | 23 g_provider.Pointer()->Register(this, size); |
| 24 } | 24 } |
| 25 | 25 |
| 26 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() { | 26 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() { |
| 27 if (is_locked_) | 27 if (is_locked_) |
| 28 Unlock(); | 28 Unlock(); |
| 29 g_provider.Pointer()->Unregister(this); | 29 g_provider.Pointer()->Unregister(this); |
| 30 } | 30 } |
| 31 | 31 |
| 32 // static | |
| 33 void DiscardableMemoryEmulated::RegisterMemoryPressureListeners() { | |
| 34 g_provider.Pointer()->RegisterMemoryPressureListener(); | |
| 35 } | |
| 36 | |
| 37 // static | |
| 38 void DiscardableMemoryEmulated::UnregisterMemoryPressureListeners() { | |
| 39 g_provider.Pointer()->UnregisterMemoryPressureListener(); | |
| 40 } | |
| 41 | |
| 42 // static | |
| 43 void DiscardableMemoryEmulated::PurgeForTesting() { | |
| 44 g_provider.Pointer()->PurgeAll(); | |
| 45 } | |
| 46 | |
| 47 bool DiscardableMemoryEmulated::Initialize() { | 32 bool DiscardableMemoryEmulated::Initialize() { |
| 48 return Lock() == DISCARDABLE_MEMORY_LOCK_STATUS_PURGED; | 33 return Lock() == DISCARDABLE_MEMORY_LOCK_STATUS_PURGED; |
| 49 } | 34 } |
| 50 | 35 |
| 51 DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() { | 36 DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() { |
| 52 DCHECK(!is_locked_); | 37 DCHECK(!is_locked_); |
| 53 | 38 |
| 54 bool purged = false; | 39 bool purged = false; |
| 55 memory_ = g_provider.Pointer()->Acquire(this, &purged); | 40 memory_ = g_provider.Pointer()->Acquire(this, &purged); |
| 56 if (!memory_) | 41 if (!memory_) |
| 57 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; | 42 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; |
| 58 | 43 |
| 59 is_locked_ = true; | 44 is_locked_ = true; |
| 60 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED | 45 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED |
| 61 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; | 46 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; |
| 62 } | 47 } |
| 63 | 48 |
| 64 void DiscardableMemoryEmulated::Unlock() { | 49 void DiscardableMemoryEmulated::Unlock() { |
| 65 DCHECK(is_locked_); | 50 DCHECK(is_locked_); |
| 66 g_provider.Pointer()->Release(this, memory_.Pass()); | 51 g_provider.Pointer()->Release(this, memory_.Pass()); |
| 67 is_locked_ = false; | 52 is_locked_ = false; |
| 68 } | 53 } |
| 69 | 54 |
| 70 void* DiscardableMemoryEmulated::Memory() const { | 55 void* DiscardableMemoryEmulated::Memory() const { |
| 71 DCHECK(memory_); | 56 DCHECK(memory_); |
| 72 return memory_.get(); | 57 return memory_.get(); |
| 73 } | 58 } |
| 74 | 59 |
| 60 // static |
| 61 void DiscardableMemoryEmulated::PurgeForTesting() { |
| 62 g_provider.Pointer()->PurgeAll(); |
| 63 } |
| 64 |
| 75 } // namespace internal | 65 } // namespace internal |
| 76 } // namespace base | 66 } // namespace base |
| OLD | NEW |