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
3 // found in the LICENSE file.
5 #include "base/memory/discardable_memory_emulated.h"
7 #include "base/lazy_instance.h"
8 #include "base/memory/discardable_memory_manager.h"
13 // This is admittedly pretty magical.
14 const size_t kEmulatedMemoryLimit
= 512 * 1024 * 1024;
15 const size_t kEmulatedSoftMemoryLimit
= 32 * 1024 * 1024;
16 const size_t kEmulatedHardMemoryLimitExpirationTimeMs
= 1000;
20 : manager(kEmulatedMemoryLimit
,
21 kEmulatedSoftMemoryLimit
,
22 TimeDelta::FromMilliseconds(
23 kEmulatedHardMemoryLimitExpirationTimeMs
)) {}
25 internal::DiscardableMemoryManager manager
;
27 LazyInstance
<SharedState
>::Leaky g_shared_state
= LAZY_INSTANCE_INITIALIZER
;
33 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes
)
36 g_shared_state
.Pointer()->manager
.Register(this, bytes
);
39 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() {
42 g_shared_state
.Pointer()->manager
.Unregister(this);
46 bool DiscardableMemoryEmulated::ReduceMemoryUsage() {
47 return g_shared_state
.Pointer()->manager
.ReduceMemoryUsage();
51 void DiscardableMemoryEmulated::ReduceMemoryUsageUntilWithinLimit(
53 g_shared_state
.Pointer()->manager
.ReduceMemoryUsageUntilWithinLimit(bytes
);
57 void DiscardableMemoryEmulated::PurgeForTesting() {
58 g_shared_state
.Pointer()->manager
.PurgeAll();
61 bool DiscardableMemoryEmulated::Initialize() {
62 return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED
;
65 DiscardableMemoryLockStatus
DiscardableMemoryEmulated::Lock() {
69 if (!g_shared_state
.Pointer()->manager
.AcquireLock(this, &purged
))
70 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED
;
73 return purged
? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
74 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS
;
77 void DiscardableMemoryEmulated::Unlock() {
79 g_shared_state
.Pointer()->manager
.ReleaseLock(this);
83 void* DiscardableMemoryEmulated::Memory() const {
89 bool DiscardableMemoryEmulated::AllocateAndAcquireLock() {
93 memory_
.reset(new uint8
[bytes_
]);
97 void DiscardableMemoryEmulated::Purge() {
101 } // namespace internal