Re-land: content: Refactor GPU memory buffer framework.
[chromium-blink-merge.git] / base / memory / discardable_memory_ashmem.cc
blobdf0697c9ae9a7238f2be844748633ef603c5887b
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_ashmem.h"
7 #include "base/memory/discardable_memory_ashmem_allocator.h"
9 namespace base {
10 namespace internal {
12 DiscardableMemoryAshmem::DiscardableMemoryAshmem(
13 size_t bytes,
14 DiscardableMemoryAshmemAllocator* allocator,
15 DiscardableMemoryManager* manager)
16 : bytes_(bytes),
17 allocator_(allocator),
18 manager_(manager),
19 is_locked_(false) {
20 manager_->Register(this, bytes_);
23 DiscardableMemoryAshmem::~DiscardableMemoryAshmem() {
24 if (is_locked_)
25 Unlock();
27 manager_->Unregister(this);
30 bool DiscardableMemoryAshmem::Initialize() {
31 return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
34 DiscardableMemoryLockStatus DiscardableMemoryAshmem::Lock() {
35 DCHECK(!is_locked_);
37 bool purged = false;
38 if (!manager_->AcquireLock(this, &purged))
39 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
41 is_locked_ = true;
42 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
43 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
46 void DiscardableMemoryAshmem::Unlock() {
47 DCHECK(is_locked_);
48 manager_->ReleaseLock(this);
49 is_locked_ = false;
52 void* DiscardableMemoryAshmem::Memory() const {
53 DCHECK(is_locked_);
54 DCHECK(ashmem_chunk_);
55 return ashmem_chunk_->Memory();
58 bool DiscardableMemoryAshmem::AllocateAndAcquireLock() {
59 if (ashmem_chunk_)
60 return ashmem_chunk_->Lock();
62 ashmem_chunk_ = allocator_->Allocate(bytes_);
63 return false;
66 void DiscardableMemoryAshmem::ReleaseLock() {
67 ashmem_chunk_->Unlock();
70 void DiscardableMemoryAshmem::Purge() {
71 ashmem_chunk_.reset();
74 bool DiscardableMemoryAshmem::IsMemoryResident() const {
75 return true;
78 } // namespace internal
79 } // namespace base