OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "base/memory/discardable_memory_ashmem.h" |
| 6 |
| 7 #include "base/memory/discardable_memory_allocator_android.h" |
| 8 |
| 9 namespace base { |
| 10 namespace internal { |
| 11 |
| 12 DiscardableMemoryAshmem::DiscardableMemoryAshmem( |
| 13 size_t size, |
| 14 DiscardableMemoryAllocator* allocator, |
| 15 DiscardableMemoryManager* manager) |
| 16 : allocator_(allocator), |
| 17 manager_(manager) { |
| 18 manager_->Register(this, size); |
| 19 } |
| 20 |
| 21 DiscardableMemoryAshmem::~DiscardableMemoryAshmem() { |
| 22 // Note that this will call Purge() below while the manager's mutex is |
| 23 // acquired which allows the ashmem allocator not to have any mutex. |
| 24 manager_->Unregister(this); |
| 25 DCHECK(!ashmem_chunk_); |
| 26 } |
| 27 |
| 28 bool DiscardableMemoryAshmem::Initialize() { |
| 29 return Lock() == DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; |
| 30 } |
| 31 |
| 32 DiscardableMemoryLockStatus DiscardableMemoryAshmem::Lock() { |
| 33 bool purged = false; |
| 34 if (!manager_->AcquireLock(this, &purged)) |
| 35 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; |
| 36 |
| 37 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED |
| 38 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; |
| 39 } |
| 40 |
| 41 void DiscardableMemoryAshmem::Unlock() { |
| 42 manager_->ReleaseLock(this); |
| 43 } |
| 44 |
| 45 void* DiscardableMemoryAshmem::Memory() const { |
| 46 DCHECK(ashmem_chunk_); |
| 47 return ashmem_chunk_->Memory(); |
| 48 } |
| 49 |
| 50 // Note that the methods below are called by the manager while its internal |
| 51 // mutex is acquired which means that they don't need extra synchronization. |
| 52 |
| 53 bool DiscardableMemoryAshmem::AllocateAndAcquireLock(size_t bytes) { |
| 54 if (ashmem_chunk_) |
| 55 return ashmem_chunk_->Lock(); |
| 56 |
| 57 ashmem_chunk_ = allocator_->Allocate(bytes); |
| 58 return !!ashmem_chunk_; |
| 59 } |
| 60 |
| 61 void DiscardableMemoryAshmem::ReleaseLock() { |
| 62 ashmem_chunk_->Unlock(); |
| 63 } |
| 64 |
| 65 void DiscardableMemoryAshmem::Purge() { |
| 66 ashmem_chunk_.reset(); |
| 67 } |
| 68 |
| 69 } // namespace internal |
| 70 } // namespace base |
OLD | NEW |