Re-land: content: Refactor GPU memory buffer framework.
[chromium-blink-merge.git] / base / memory / discardable_memory_shmem.cc
blob77699f0739f30f4725330e95d002bfd0cfeb1ef5
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(shared_memory_);
92 return shared_memory_->memory();
95 bool DiscardableMemoryShmem::AllocateAndAcquireLock() {
96 if (shared_memory_ && shared_memory_->Lock())
97 return true;
99 // TODO(reveman): Allocate fixed size memory segments and use a free list to
100 // improve performance and limit the number of file descriptors used.
101 shared_memory_ = DiscardableMemoryShmemAllocator::GetInstance()
102 ->AllocateLockedDiscardableSharedMemory(bytes_);
103 DCHECK(shared_memory_);
104 return false;
107 void DiscardableMemoryShmem::ReleaseLock() {
108 shared_memory_->Unlock();
111 void DiscardableMemoryShmem::Purge() {
112 shared_memory_->Purge(Time());
113 shared_memory_.reset();
116 bool DiscardableMemoryShmem::IsMemoryResident() const {
117 return shared_memory_->IsMemoryResident();
120 } // namespace internal
121 } // namespace base