Chromium Code Reviews| 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 DefaultLazyInstanceTraits 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 static const bool kRegisterOnExit = false; |
|
danakj
2014/10/15 18:04:08
why false?
reveman
2014/10/15 18:38:56
There's no guarantee that this is not be used afte
| |
| 23 kEmulatedHardMemoryLimitExpirationTimeMs)) {} | 23 #ifndef NDEBUG |
| 24 static const bool kAllowedToAccessOnNonjoinableThread = true; | |
|
danakj
2014/10/15 18:04:08
why true?
Sounds like you're making a LeakyLazyIn
reveman
2014/10/15 18:38:57
This is what the code used to do. It used LazyInst
| |
| 25 #endif | |
| 24 | 26 |
| 25 internal::DiscardableMemoryManager manager; | 27 static internal::DiscardableMemoryManager* New(void* instance) { |
| 28 return new (instance) internal::DiscardableMemoryManager( | |
| 29 kEmulatedMemoryLimit, | |
|
danakj
2014/10/15 18:04:08
What about adding an Initialize(pass, constants, h
reveman
2014/10/15 18:38:57
That would not be thread safe unless I misundersto
danakj
2014/10/15 18:48:47
Oh, Register holds a mutex I guess? I see.
reveman
2014/10/15 18:54:36
hm, I probably misunderstood your suggestion. When
| |
| 30 kEmulatedSoftMemoryLimit, | |
| 31 TimeDelta::FromMilliseconds(kEmulatedHardMemoryLimitExpirationTimeMs)); | |
| 32 } | |
| 33 static void Delete(internal::DiscardableMemoryManager* instance) { | |
| 34 instance->~DiscardableMemoryManager(); | |
| 35 } | |
| 26 }; | 36 }; |
| 27 LazyInstance<SharedState>::Leaky g_shared_state = LAZY_INSTANCE_INITIALIZER; | 37 |
| 38 LazyInstance<internal::DiscardableMemoryManager, | |
| 39 DiscardableMemoryManagerLazyInstanceTraits> | |
| 40 g_manager = LAZY_INSTANCE_INITIALIZER; | |
| 28 | 41 |
| 29 } // namespace | 42 } // namespace |
| 30 | 43 |
| 31 namespace internal { | 44 namespace internal { |
| 32 | 45 |
| 33 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes) | 46 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes) |
| 34 : bytes_(bytes), | 47 : bytes_(bytes), |
| 35 is_locked_(false) { | 48 is_locked_(false) { |
| 36 g_shared_state.Pointer()->manager.Register(this, bytes); | 49 g_manager.Pointer()->Register(this, bytes); |
|
danakj
2014/10/15 18:59:33
before/after this register call, in this method.
reveman
2014/10/15 19:02:04
Two threads can call this ctor at the same time. B
danakj
2014/10/15 19:11:53
Right, sketchy. Carry on then :)
| |
| 37 } | 50 } |
| 38 | 51 |
| 39 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() { | 52 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() { |
| 40 if (is_locked_) | 53 if (is_locked_) |
| 41 Unlock(); | 54 Unlock(); |
| 42 g_shared_state.Pointer()->manager.Unregister(this); | 55 g_manager.Pointer()->Unregister(this); |
| 43 } | 56 } |
| 44 | 57 |
| 45 // static | 58 // static |
| 46 bool DiscardableMemoryEmulated::ReduceMemoryUsage() { | 59 bool DiscardableMemoryEmulated::ReduceMemoryUsage() { |
| 47 return g_shared_state.Pointer()->manager.ReduceMemoryUsage(); | 60 return g_manager.Pointer()->ReduceMemoryUsage(); |
| 48 } | 61 } |
| 49 | 62 |
| 50 // static | 63 // static |
| 51 void DiscardableMemoryEmulated::ReduceMemoryUsageUntilWithinLimit( | 64 void DiscardableMemoryEmulated::ReduceMemoryUsageUntilWithinLimit( |
| 52 size_t bytes) { | 65 size_t bytes) { |
| 53 g_shared_state.Pointer()->manager.ReduceMemoryUsageUntilWithinLimit(bytes); | 66 g_manager.Pointer()->ReduceMemoryUsageUntilWithinLimit(bytes); |
| 54 } | 67 } |
| 55 | 68 |
| 56 // static | 69 // static |
| 57 void DiscardableMemoryEmulated::PurgeForTesting() { | 70 void DiscardableMemoryEmulated::PurgeForTesting() { |
| 58 g_shared_state.Pointer()->manager.PurgeAll(); | 71 g_manager.Pointer()->PurgeAll(); |
| 59 } | 72 } |
| 60 | 73 |
| 61 bool DiscardableMemoryEmulated::Initialize() { | 74 bool DiscardableMemoryEmulated::Initialize() { |
| 62 return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; | 75 return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; |
| 63 } | 76 } |
| 64 | 77 |
| 65 DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() { | 78 DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() { |
| 66 DCHECK(!is_locked_); | 79 DCHECK(!is_locked_); |
| 67 | 80 |
| 68 bool purged = false; | 81 bool purged = false; |
| 69 if (!g_shared_state.Pointer()->manager.AcquireLock(this, &purged)) | 82 if (!g_manager.Pointer()->AcquireLock(this, &purged)) |
| 70 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; | 83 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; |
| 71 | 84 |
| 72 is_locked_ = true; | 85 is_locked_ = true; |
| 73 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED | 86 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED |
| 74 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; | 87 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; |
| 75 } | 88 } |
| 76 | 89 |
| 77 void DiscardableMemoryEmulated::Unlock() { | 90 void DiscardableMemoryEmulated::Unlock() { |
| 78 DCHECK(is_locked_); | 91 DCHECK(is_locked_); |
| 79 g_shared_state.Pointer()->manager.ReleaseLock(this); | 92 g_manager.Pointer()->ReleaseLock(this); |
| 80 is_locked_ = false; | 93 is_locked_ = false; |
| 81 } | 94 } |
| 82 | 95 |
| 83 void* DiscardableMemoryEmulated::Memory() const { | 96 void* DiscardableMemoryEmulated::Memory() const { |
| 84 DCHECK(is_locked_); | 97 DCHECK(is_locked_); |
| 85 DCHECK(memory_); | 98 DCHECK(memory_); |
| 86 return memory_.get(); | 99 return memory_.get(); |
| 87 } | 100 } |
| 88 | 101 |
| 89 bool DiscardableMemoryEmulated::AllocateAndAcquireLock() { | 102 bool DiscardableMemoryEmulated::AllocateAndAcquireLock() { |
| 90 if (memory_) | 103 if (memory_) |
| 91 return true; | 104 return true; |
| 92 | 105 |
| 93 memory_.reset(new uint8[bytes_]); | 106 memory_.reset(new uint8[bytes_]); |
| 94 return false; | 107 return false; |
| 95 } | 108 } |
| 96 | 109 |
| 97 void DiscardableMemoryEmulated::Purge() { | 110 void DiscardableMemoryEmulated::Purge() { |
| 98 memory_.reset(); | 111 memory_.reset(); |
| 99 } | 112 } |
| 100 | 113 |
| 101 } // namespace internal | 114 } // namespace internal |
| 102 } // namespace base | 115 } // namespace base |
| OLD | NEW |