Chromium Code Reviews| Index: base/memory/discardable_memory_emulated.cc |
| diff --git a/base/memory/discardable_memory_emulated.cc b/base/memory/discardable_memory_emulated.cc |
| index f9097b186e1405897f2a85c17f52398f0e90a1ac..609c6aed4e4dcde75d822f6dffdf78198c56ef1a 100644 |
| --- a/base/memory/discardable_memory_emulated.cc |
| +++ b/base/memory/discardable_memory_emulated.cc |
| @@ -15,16 +15,29 @@ const size_t kEmulatedMemoryLimit = 512 * 1024 * 1024; |
| const size_t kEmulatedSoftMemoryLimit = 32 * 1024 * 1024; |
| const size_t kEmulatedHardMemoryLimitExpirationTimeMs = 1000; |
| -struct SharedState { |
| - SharedState() |
| - : manager(kEmulatedMemoryLimit, |
| - kEmulatedSoftMemoryLimit, |
| - TimeDelta::FromMilliseconds( |
| - kEmulatedHardMemoryLimitExpirationTimeMs)) {} |
| - |
| - internal::DiscardableMemoryManager manager; |
| +// internal::DiscardableMemoryManager has an explicit constructor that takes |
| +// a number of memory limit parameters. The DefaultLazyInstanceTraits doesn't |
| +// handle the case. Thus, we need our own class here. |
| +struct DiscardableMemoryManagerLazyInstanceTraits { |
| + 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
|
| +#ifndef NDEBUG |
| + 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
|
| +#endif |
| + |
| + static internal::DiscardableMemoryManager* New(void* instance) { |
| + return new (instance) internal::DiscardableMemoryManager( |
| + 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
|
| + kEmulatedSoftMemoryLimit, |
| + TimeDelta::FromMilliseconds(kEmulatedHardMemoryLimitExpirationTimeMs)); |
| + } |
| + static void Delete(internal::DiscardableMemoryManager* instance) { |
| + instance->~DiscardableMemoryManager(); |
| + } |
| }; |
| -LazyInstance<SharedState>::Leaky g_shared_state = LAZY_INSTANCE_INITIALIZER; |
| + |
| +LazyInstance<internal::DiscardableMemoryManager, |
| + DiscardableMemoryManagerLazyInstanceTraits> |
| + g_manager = LAZY_INSTANCE_INITIALIZER; |
| } // namespace |
| @@ -33,29 +46,29 @@ namespace internal { |
| DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes) |
| : bytes_(bytes), |
| is_locked_(false) { |
| - g_shared_state.Pointer()->manager.Register(this, bytes); |
| + 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 :)
|
| } |
| DiscardableMemoryEmulated::~DiscardableMemoryEmulated() { |
| if (is_locked_) |
| Unlock(); |
| - g_shared_state.Pointer()->manager.Unregister(this); |
| + g_manager.Pointer()->Unregister(this); |
| } |
| // static |
| bool DiscardableMemoryEmulated::ReduceMemoryUsage() { |
| - return g_shared_state.Pointer()->manager.ReduceMemoryUsage(); |
| + return g_manager.Pointer()->ReduceMemoryUsage(); |
| } |
| // static |
| void DiscardableMemoryEmulated::ReduceMemoryUsageUntilWithinLimit( |
| size_t bytes) { |
| - g_shared_state.Pointer()->manager.ReduceMemoryUsageUntilWithinLimit(bytes); |
| + g_manager.Pointer()->ReduceMemoryUsageUntilWithinLimit(bytes); |
| } |
| // static |
| void DiscardableMemoryEmulated::PurgeForTesting() { |
| - g_shared_state.Pointer()->manager.PurgeAll(); |
| + g_manager.Pointer()->PurgeAll(); |
| } |
| bool DiscardableMemoryEmulated::Initialize() { |
| @@ -66,7 +79,7 @@ DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() { |
| DCHECK(!is_locked_); |
| bool purged = false; |
| - if (!g_shared_state.Pointer()->manager.AcquireLock(this, &purged)) |
| + if (!g_manager.Pointer()->AcquireLock(this, &purged)) |
| return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; |
| is_locked_ = true; |
| @@ -76,7 +89,7 @@ DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() { |
| void DiscardableMemoryEmulated::Unlock() { |
| DCHECK(is_locked_); |
| - g_shared_state.Pointer()->manager.ReleaseLock(this); |
| + g_manager.Pointer()->ReleaseLock(this); |
| is_locked_ = false; |
| } |