Roll google-toolbax-for-mac a095262:17eee69 (svn 685:703).
[chromium-blink-merge.git] / base / memory / discardable_memory_shmem.cc
blob416fcf2aa9fede8388c4895ec4084307d84c75ae
1 // Copyright 2014 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_shmem.h"
7 #include "base/lazy_instance.h"
8 #include "base/memory/discardable_memory_shmem_allocator.h"
9 #include "base/memory/discardable_shared_memory.h"
11 namespace base {
12 namespace {
14 // Have the DiscardableMemoryManager trigger in-process eviction
15 // when address space usage gets too high (e.g. 512 MBytes).
16 const size_t kMemoryLimit = 512 * 1024 * 1024;
18 // 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.
21 struct 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
29 static internal::DiscardableMemoryManager* New(void* instance) {
30 return new (instance) internal::DiscardableMemoryManager(
31 kMemoryLimit, kMemoryLimit, TimeDelta::Max());
33 static void Delete(internal::DiscardableMemoryManager* instance) {
34 instance->~DiscardableMemoryManager();
38 LazyInstance<internal::DiscardableMemoryManager,
39 DiscardableMemoryManagerLazyInstanceTraits> g_manager =
40 LAZY_INSTANCE_INITIALIZER;
42 } // namespace
44 namespace internal {
46 DiscardableMemoryShmem::DiscardableMemoryShmem(size_t bytes)
47 : bytes_(bytes), is_locked_(false) {
48 g_manager.Pointer()->Register(this, bytes);
51 DiscardableMemoryShmem::~DiscardableMemoryShmem() {
52 if (is_locked_)
53 Unlock();
54 g_manager.Pointer()->Unregister(this);
57 // static
58 void DiscardableMemoryShmem::ReleaseFreeMemory() {
59 g_manager.Pointer()->ReleaseFreeMemory();
62 // static
63 void DiscardableMemoryShmem::PurgeForTesting() {
64 g_manager.Pointer()->PurgeAll();
67 bool DiscardableMemoryShmem::Initialize() {
68 return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
71 DiscardableMemoryLockStatus DiscardableMemoryShmem::Lock() {
72 DCHECK(!is_locked_);
74 bool purged = false;
75 if (!g_manager.Pointer()->AcquireLock(this, &purged))
76 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
78 is_locked_ = true;
79 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
80 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
83 void DiscardableMemoryShmem::Unlock() {
84 DCHECK(is_locked_);
85 g_manager.Pointer()->ReleaseLock(this);
86 is_locked_ = false;
89 void* DiscardableMemoryShmem::Memory() const {
90 DCHECK(is_locked_);
91 DCHECK(chunk_);
92 return chunk_->Memory();
95 bool DiscardableMemoryShmem::AllocateAndAcquireLock() {
96 if (chunk_ && chunk_->Lock())
97 return true;
99 chunk_ = DiscardableMemoryShmemAllocator::GetInstance()
100 ->AllocateLockedDiscardableMemory(bytes_);
101 DCHECK(chunk_);
102 return false;
105 void DiscardableMemoryShmem::ReleaseLock() {
106 chunk_->Unlock();
109 void DiscardableMemoryShmem::Purge() {
110 DCHECK(!is_locked_);
111 chunk_.reset();
114 bool DiscardableMemoryShmem::IsMemoryResident() const {
115 return chunk_->IsMemoryResident();
118 } // namespace internal
119 } // namespace base