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. It's approximately enough memory for four
15 const size_t kEmulatedMemoryLimit = 64 * 1024 * 1024;
16 const size_t kEmulatedBytesToKeepUnderModeratePressure =
17 kEmulatedMemoryLimit / 4;
21 : manager(kEmulatedMemoryLimit,
22 kEmulatedBytesToKeepUnderModeratePressure) {}
24 internal::DiscardableMemoryManager manager;
26 LazyInstance<SharedState>::Leaky g_shared_state = LAZY_INSTANCE_INITIALIZER;
32 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes)
35 g_shared_state.Pointer()->manager.Register(this, bytes);
38 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() {
41 g_shared_state.Pointer()->manager.Unregister(this);
45 void DiscardableMemoryEmulated::RegisterMemoryPressureListeners() {
46 g_shared_state.Pointer()->manager.RegisterMemoryPressureListener();
50 void DiscardableMemoryEmulated::UnregisterMemoryPressureListeners() {
51 g_shared_state.Pointer()->manager.UnregisterMemoryPressureListener();
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() {
67 if (!g_shared_state.Pointer()->manager.AcquireLock(this, &purged))
68 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
71 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
72 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
75 void DiscardableMemoryEmulated::Unlock() {
77 g_shared_state.Pointer()->manager.ReleaseLock(this);
81 void* DiscardableMemoryEmulated::Memory() const {
87 bool DiscardableMemoryEmulated::AllocateAndAcquireLock() {
91 memory_.reset(new uint8[bytes_]);
95 void DiscardableMemoryEmulated::Purge() {
99 } // namespace internal