| 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_manager.h" | 8 #include "base/memory/discardable_memory_manager.h" |
| 9 | 9 |
| 10 namespace base { | 10 namespace base { |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 // This is admittedly pretty magical. | 13 // This is admittedly pretty magical. |
| 14 const size_t kEmulatedMemoryLimit = 512 * 1024 * 1024; | 14 const size_t kEmulatedMemoryLimit = 512 * 1024 * 1024; |
| 15 const size_t kEmulatedSoftMemoryLimit = 32 * 1024 * 1024; | 15 const size_t kEmulatedSoftMemoryLimit = 32 * 1024 * 1024; |
| 16 const size_t kEmulatedHardMemoryLimitExpirationTimeMs = 1000; | 16 const size_t kEmulatedHardMemoryLimitExpirationTimeMs = 1000; |
| 17 | 17 |
| 18 struct SharedState { | 18 // internal::DiscardableMemoryManager has an explicit constructor that takes |
| 19 SharedState() | 19 // a number of memory limit parameters. The LeakyLazyInstanceTraits doesn't |
| 20 : manager(kEmulatedMemoryLimit, | 20 // handle the case. Thus, we need our own class here. |
| 21 kEmulatedSoftMemoryLimit, | 21 struct DiscardableMemoryManagerLazyInstanceTraits { |
| 22 TimeDelta::FromMilliseconds( | 22 // Leaky as discardable memory clients can use this after the exit handler |
| 23 kEmulatedHardMemoryLimitExpirationTimeMs)) {} | 23 // has been called. |
| 24 static const bool kRegisterOnExit = false; |
| 25 #ifndef NDEBUG |
| 26 static const bool kAllowedToAccessOnNonjoinableThread = true; |
| 27 #endif |
| 24 | 28 |
| 25 internal::DiscardableMemoryManager manager; | 29 static internal::DiscardableMemoryManager* New(void* instance) { |
| 30 return new (instance) internal::DiscardableMemoryManager( |
| 31 kEmulatedMemoryLimit, |
| 32 kEmulatedSoftMemoryLimit, |
| 33 TimeDelta::FromMilliseconds(kEmulatedHardMemoryLimitExpirationTimeMs)); |
| 34 } |
| 35 static void Delete(internal::DiscardableMemoryManager* instance) { |
| 36 instance->~DiscardableMemoryManager(); |
| 37 } |
| 26 }; | 38 }; |
| 27 LazyInstance<SharedState>::Leaky g_shared_state = LAZY_INSTANCE_INITIALIZER; | 39 |
| 40 LazyInstance<internal::DiscardableMemoryManager, |
| 41 DiscardableMemoryManagerLazyInstanceTraits> |
| 42 g_manager = LAZY_INSTANCE_INITIALIZER; |
| 28 | 43 |
| 29 } // namespace | 44 } // namespace |
| 30 | 45 |
| 31 namespace internal { | 46 namespace internal { |
| 32 | 47 |
| 33 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes) | 48 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes) |
| 34 : bytes_(bytes), | 49 : bytes_(bytes), |
| 35 is_locked_(false) { | 50 is_locked_(false) { |
| 36 g_shared_state.Pointer()->manager.Register(this, bytes); | 51 g_manager.Pointer()->Register(this, bytes); |
| 37 } | 52 } |
| 38 | 53 |
| 39 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() { | 54 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() { |
| 40 if (is_locked_) | 55 if (is_locked_) |
| 41 Unlock(); | 56 Unlock(); |
| 42 g_shared_state.Pointer()->manager.Unregister(this); | 57 g_manager.Pointer()->Unregister(this); |
| 43 } | 58 } |
| 44 | 59 |
| 45 // static | 60 // static |
| 46 bool DiscardableMemoryEmulated::ReduceMemoryUsage() { | 61 bool DiscardableMemoryEmulated::ReduceMemoryUsage() { |
| 47 return g_shared_state.Pointer()->manager.ReduceMemoryUsage(); | 62 return g_manager.Pointer()->ReduceMemoryUsage(); |
| 48 } | 63 } |
| 49 | 64 |
| 50 // static | 65 // static |
| 51 void DiscardableMemoryEmulated::ReduceMemoryUsageUntilWithinLimit( | 66 void DiscardableMemoryEmulated::ReduceMemoryUsageUntilWithinLimit( |
| 52 size_t bytes) { | 67 size_t bytes) { |
| 53 g_shared_state.Pointer()->manager.ReduceMemoryUsageUntilWithinLimit(bytes); | 68 g_manager.Pointer()->ReduceMemoryUsageUntilWithinLimit(bytes); |
| 54 } | 69 } |
| 55 | 70 |
| 56 // static | 71 // static |
| 57 void DiscardableMemoryEmulated::PurgeForTesting() { | 72 void DiscardableMemoryEmulated::PurgeForTesting() { |
| 58 g_shared_state.Pointer()->manager.PurgeAll(); | 73 g_manager.Pointer()->PurgeAll(); |
| 59 } | 74 } |
| 60 | 75 |
| 61 bool DiscardableMemoryEmulated::Initialize() { | 76 bool DiscardableMemoryEmulated::Initialize() { |
| 62 return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; | 77 return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; |
| 63 } | 78 } |
| 64 | 79 |
| 65 DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() { | 80 DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() { |
| 66 DCHECK(!is_locked_); | 81 DCHECK(!is_locked_); |
| 67 | 82 |
| 68 bool purged = false; | 83 bool purged = false; |
| 69 if (!g_shared_state.Pointer()->manager.AcquireLock(this, &purged)) | 84 if (!g_manager.Pointer()->AcquireLock(this, &purged)) |
| 70 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; | 85 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; |
| 71 | 86 |
| 72 is_locked_ = true; | 87 is_locked_ = true; |
| 73 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED | 88 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED |
| 74 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; | 89 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; |
| 75 } | 90 } |
| 76 | 91 |
| 77 void DiscardableMemoryEmulated::Unlock() { | 92 void DiscardableMemoryEmulated::Unlock() { |
| 78 DCHECK(is_locked_); | 93 DCHECK(is_locked_); |
| 79 g_shared_state.Pointer()->manager.ReleaseLock(this); | 94 g_manager.Pointer()->ReleaseLock(this); |
| 80 is_locked_ = false; | 95 is_locked_ = false; |
| 81 } | 96 } |
| 82 | 97 |
| 83 void* DiscardableMemoryEmulated::Memory() const { | 98 void* DiscardableMemoryEmulated::Memory() const { |
| 84 DCHECK(is_locked_); | 99 DCHECK(is_locked_); |
| 85 DCHECK(memory_); | 100 DCHECK(memory_); |
| 86 return memory_.get(); | 101 return memory_.get(); |
| 87 } | 102 } |
| 88 | 103 |
| 89 bool DiscardableMemoryEmulated::AllocateAndAcquireLock() { | 104 bool DiscardableMemoryEmulated::AllocateAndAcquireLock() { |
| 90 if (memory_) | 105 if (memory_) |
| 91 return true; | 106 return true; |
| 92 | 107 |
| 93 memory_.reset(new uint8[bytes_]); | 108 memory_.reset(new uint8[bytes_]); |
| 94 return false; | 109 return false; |
| 95 } | 110 } |
| 96 | 111 |
| 97 void DiscardableMemoryEmulated::Purge() { | 112 void DiscardableMemoryEmulated::Purge() { |
| 98 memory_.reset(); | 113 memory_.reset(); |
| 99 } | 114 } |
| 100 | 115 |
| 101 } // namespace internal | 116 } // namespace internal |
| 102 } // namespace base | 117 } // namespace base |
| OLD | NEW |