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() {
27 manager_
->Unregister(this);
30 bool DiscardableMemoryAshmem::Initialize() {
31 return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED
;
34 DiscardableMemoryLockStatus
DiscardableMemoryAshmem::Lock() {
38 if (!manager_
->AcquireLock(this, &purged
))
39 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED
;
42 return purged
? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
43 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS
;
46 void DiscardableMemoryAshmem::Unlock() {
48 manager_
->ReleaseLock(this);
52 void* DiscardableMemoryAshmem::Memory() const {
54 DCHECK(ashmem_chunk_
);
55 return ashmem_chunk_
->Memory();
58 bool DiscardableMemoryAshmem::AllocateAndAcquireLock() {
60 return ashmem_chunk_
->Lock();
62 ashmem_chunk_
= allocator_
->Allocate(bytes_
);
66 void DiscardableMemoryAshmem::ReleaseLock() {
67 ashmem_chunk_
->Unlock();
70 void DiscardableMemoryAshmem::Purge() {
71 ashmem_chunk_
.reset();
74 } // namespace internal