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"
12 DiscardableMemoryAshmem::DiscardableMemoryAshmem(
14 DiscardableMemoryAshmemAllocator* allocator,
15 DiscardableMemoryManager* manager)
17 allocator_(allocator),
20 manager_->Register(this, bytes_);
23 DiscardableMemoryAshmem::~DiscardableMemoryAshmem() {
25 manager_->ReleaseLock(this);
27 manager_->Unregister(this);
30 bool DiscardableMemoryAshmem::Initialize() {
31 return Lock() == DISCARDABLE_MEMORY_LOCK_STATUS_PURGED;
34 DiscardableMemoryLockStatus DiscardableMemoryAshmem::Lock() {
36 if (!manager_->AcquireLock(this, &purged))
37 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
39 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
40 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS;
43 void DiscardableMemoryAshmem::Unlock() {
44 manager_->ReleaseLock(this);
47 void* DiscardableMemoryAshmem::Memory() const {
48 DCHECK(ashmem_chunk_);
49 return ashmem_chunk_->Memory();
52 bool DiscardableMemoryAshmem::AllocateAndAcquireLock() {
57 return ashmem_chunk_->Lock();
59 ashmem_chunk_ = allocator_->Allocate(bytes_);
63 void DiscardableMemoryAshmem::ReleaseLock() {
65 ashmem_chunk_->Unlock();
69 void DiscardableMemoryAshmem::Purge() {
71 ashmem_chunk_.reset();
74 } // namespace internal