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 base::LazyInstance<internal::DiscardableMemoryManager>::Leaky g_manager =
14 LAZY_INSTANCE_INITIALIZER;
20 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes)
23 g_manager.Pointer()->Register(this, bytes);
26 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() {
29 g_manager.Pointer()->Unregister(this);
33 void DiscardableMemoryEmulated::RegisterMemoryPressureListeners() {
34 g_manager.Pointer()->RegisterMemoryPressureListener();
38 void DiscardableMemoryEmulated::UnregisterMemoryPressureListeners() {
39 g_manager.Pointer()->UnregisterMemoryPressureListener();
43 void DiscardableMemoryEmulated::PurgeForTesting() {
44 g_manager.Pointer()->PurgeAll();
47 bool DiscardableMemoryEmulated::Initialize() {
48 return Lock() == DISCARDABLE_MEMORY_LOCK_STATUS_PURGED;
51 DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() {
55 if (!g_manager.Pointer()->AcquireLock(this, &purged))
56 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
59 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
60 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
63 void DiscardableMemoryEmulated::Unlock() {
65 g_manager.Pointer()->ReleaseLock(this);
69 void* DiscardableMemoryEmulated::Memory() const {
75 bool DiscardableMemoryEmulated::AllocateAndAcquireLock() {
79 memory_.reset(new uint8[bytes_]);
83 void DiscardableMemoryEmulated::Purge() {
87 } // namespace internal