blob: 9820e2ea73842431f1ea32a1638c994c33c681d1 [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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.
4
5#include "base/memory/discardable_memory_emulated.h"
6
7#include "base/lazy_instance.h"
8#include "base/memory/discardable_memory_manager.h"
9
10namespace base {
11namespace {
12
13// This is admittedly pretty magical.
14const size_t kEmulatedMemoryLimit = 512 * 1024 * 1024;
15const size_t kEmulatedSoftMemoryLimit = 32 * 1024 * 1024;
16const size_t kEmulatedHardMemoryLimitExpirationTimeMs = 1000;
17
James Robinson80d418c2014-10-16 16:00:02 -070018// internal::DiscardableMemoryManager has an explicit constructor that takes
19// a number of memory limit parameters. The LeakyLazyInstanceTraits doesn't
20// handle the case. Thus, we need our own class here.
21struct DiscardableMemoryManagerLazyInstanceTraits {
22 // Leaky as discardable memory clients can use this after the exit handler
23 // has been called.
24 static const bool kRegisterOnExit = false;
25#ifndef NDEBUG
26 static const bool kAllowedToAccessOnNonjoinableThread = true;
27#endif
James Robinson646469d2014-10-03 15:33:28 -070028
James Robinson80d418c2014-10-16 16:00:02 -070029 static internal::DiscardableMemoryManager* New(void* instance) {
30 return new (instance) internal::DiscardableMemoryManager(
31 kEmulatedMemoryLimit,
32 kEmulatedSoftMemoryLimit,
33 TimeDelta::FromMilliseconds(kEmulatedHardMemoryLimitExpirationTimeMs));
34 }
35 static void Delete(internal::DiscardableMemoryManager* instance) {
36 instance->~DiscardableMemoryManager();
37 }
James Robinson646469d2014-10-03 15:33:28 -070038};
James Robinson80d418c2014-10-16 16:00:02 -070039
40LazyInstance<internal::DiscardableMemoryManager,
41 DiscardableMemoryManagerLazyInstanceTraits>
42 g_manager = LAZY_INSTANCE_INITIALIZER;
James Robinson646469d2014-10-03 15:33:28 -070043
44} // namespace
45
46namespace internal {
47
48DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes)
49 : bytes_(bytes),
50 is_locked_(false) {
James Robinson80d418c2014-10-16 16:00:02 -070051 g_manager.Pointer()->Register(this, bytes);
James Robinson646469d2014-10-03 15:33:28 -070052}
53
54DiscardableMemoryEmulated::~DiscardableMemoryEmulated() {
55 if (is_locked_)
56 Unlock();
James Robinson80d418c2014-10-16 16:00:02 -070057 g_manager.Pointer()->Unregister(this);
James Robinson646469d2014-10-03 15:33:28 -070058}
59
60// static
61bool DiscardableMemoryEmulated::ReduceMemoryUsage() {
James Robinson80d418c2014-10-16 16:00:02 -070062 return g_manager.Pointer()->ReduceMemoryUsage();
James Robinson646469d2014-10-03 15:33:28 -070063}
64
65// static
66void DiscardableMemoryEmulated::ReduceMemoryUsageUntilWithinLimit(
67 size_t bytes) {
James Robinson80d418c2014-10-16 16:00:02 -070068 g_manager.Pointer()->ReduceMemoryUsageUntilWithinLimit(bytes);
James Robinson646469d2014-10-03 15:33:28 -070069}
70
71// static
72void DiscardableMemoryEmulated::PurgeForTesting() {
James Robinson80d418c2014-10-16 16:00:02 -070073 g_manager.Pointer()->PurgeAll();
James Robinson646469d2014-10-03 15:33:28 -070074}
75
76bool DiscardableMemoryEmulated::Initialize() {
77 return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
78}
79
80DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() {
81 DCHECK(!is_locked_);
82
83 bool purged = false;
James Robinson80d418c2014-10-16 16:00:02 -070084 if (!g_manager.Pointer()->AcquireLock(this, &purged))
James Robinson646469d2014-10-03 15:33:28 -070085 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
86
87 is_locked_ = true;
88 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
89 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
90}
91
92void DiscardableMemoryEmulated::Unlock() {
93 DCHECK(is_locked_);
James Robinson80d418c2014-10-16 16:00:02 -070094 g_manager.Pointer()->ReleaseLock(this);
James Robinson646469d2014-10-03 15:33:28 -070095 is_locked_ = false;
96}
97
98void* DiscardableMemoryEmulated::Memory() const {
99 DCHECK(is_locked_);
100 DCHECK(memory_);
101 return memory_.get();
102}
103
104bool DiscardableMemoryEmulated::AllocateAndAcquireLock() {
105 if (memory_)
106 return true;
107
108 memory_.reset(new uint8[bytes_]);
109 return false;
110}
111
112void DiscardableMemoryEmulated::Purge() {
113 memory_.reset();
114}
115
116} // namespace internal
117} // namespace base