Cleanup: Only build extensions renderer code when extensions are enabled.
[chromium-blink-merge.git] / base / memory / discardable_memory_emulated.cc
blobf9097b186e1405897f2a85c17f52398f0e90a1ac
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"
10 namespace base {
11 namespace {
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;
18 struct SharedState {
19 SharedState()
20 : manager(kEmulatedMemoryLimit,
21 kEmulatedSoftMemoryLimit,
22 TimeDelta::FromMilliseconds(
23 kEmulatedHardMemoryLimitExpirationTimeMs)) {}
25 internal::DiscardableMemoryManager manager;
27 LazyInstance<SharedState>::Leaky g_shared_state = LAZY_INSTANCE_INITIALIZER;
29 } // namespace
31 namespace internal {
33 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes)
34 : bytes_(bytes),
35 is_locked_(false) {
36 g_shared_state.Pointer()->manager.Register(this, bytes);
39 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() {
40 if (is_locked_)
41 Unlock();
42 g_shared_state.Pointer()->manager.Unregister(this);
45 // static
46 bool DiscardableMemoryEmulated::ReduceMemoryUsage() {
47 return g_shared_state.Pointer()->manager.ReduceMemoryUsage();
50 // static
51 void DiscardableMemoryEmulated::ReduceMemoryUsageUntilWithinLimit(
52 size_t bytes) {
53 g_shared_state.Pointer()->manager.ReduceMemoryUsageUntilWithinLimit(bytes);
56 // static
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() {
66 DCHECK(!is_locked_);
68 bool purged = false;
69 if (!g_shared_state.Pointer()->manager.AcquireLock(this, &purged))
70 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
72 is_locked_ = true;
73 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
74 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
77 void DiscardableMemoryEmulated::Unlock() {
78 DCHECK(is_locked_);
79 g_shared_state.Pointer()->manager.ReleaseLock(this);
80 is_locked_ = false;
83 void* DiscardableMemoryEmulated::Memory() const {
84 DCHECK(is_locked_);
85 DCHECK(memory_);
86 return memory_.get();
89 bool DiscardableMemoryEmulated::AllocateAndAcquireLock() {
90 if (memory_)
91 return true;
93 memory_.reset(new uint8[bytes_]);
94 return false;
97 void DiscardableMemoryEmulated::Purge() {
98 memory_.reset();
101 } // namespace internal
102 } // namespace base