Support for unpacked ARM packed relocations.
[chromium-blink-merge.git] / base / memory / discardable_memory_emulated.cc
blob140fdb01e28952acd80025c5bda9591fa67276c3
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. It's approximately enough memory for four
14 // 2560x1600 images.
15 const size_t kEmulatedMemoryLimit = 64 * 1024 * 1024;
16 const size_t kEmulatedBytesToKeepUnderModeratePressure =
17 kEmulatedMemoryLimit / 4;
19 struct SharedState {
20 SharedState()
21 : manager(kEmulatedMemoryLimit,
22 kEmulatedBytesToKeepUnderModeratePressure) {}
24 internal::DiscardableMemoryManager manager;
26 LazyInstance<SharedState>::Leaky g_shared_state = LAZY_INSTANCE_INITIALIZER;
28 } // namespace
30 namespace internal {
32 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes)
33 : bytes_(bytes),
34 is_locked_(false) {
35 g_shared_state.Pointer()->manager.Register(this, bytes);
38 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() {
39 if (is_locked_)
40 Unlock();
41 g_shared_state.Pointer()->manager.Unregister(this);
44 // static
45 void DiscardableMemoryEmulated::RegisterMemoryPressureListeners() {
46 g_shared_state.Pointer()->manager.RegisterMemoryPressureListener();
49 // static
50 void DiscardableMemoryEmulated::UnregisterMemoryPressureListeners() {
51 g_shared_state.Pointer()->manager.UnregisterMemoryPressureListener();
54 // static
55 void DiscardableMemoryEmulated::PurgeForTesting() {
56 g_shared_state.Pointer()->manager.PurgeAll();
59 bool DiscardableMemoryEmulated::Initialize() {
60 return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
63 DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() {
64 DCHECK(!is_locked_);
66 bool purged = false;
67 if (!g_shared_state.Pointer()->manager.AcquireLock(this, &purged))
68 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
70 is_locked_ = true;
71 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
72 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
75 void DiscardableMemoryEmulated::Unlock() {
76 DCHECK(is_locked_);
77 g_shared_state.Pointer()->manager.ReleaseLock(this);
78 is_locked_ = false;
81 void* DiscardableMemoryEmulated::Memory() const {
82 DCHECK(is_locked_);
83 DCHECK(memory_);
84 return memory_.get();
87 bool DiscardableMemoryEmulated::AllocateAndAcquireLock() {
88 if (memory_)
89 return true;
91 memory_.reset(new uint8[bytes_]);
92 return false;
95 void DiscardableMemoryEmulated::Purge() {
96 memory_.reset();
99 } // namespace internal
100 } // namespace base