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_ashmem_allocator.h" |
| 8 |
| 9 namespace base { |
| 10 namespace internal { |
| 11 |
| 12 DiscardableMemoryAshmem::DiscardableMemoryAshmem( |
| 13 size_t bytes, |
| 14 DiscardableMemoryAshmemAllocator* allocator, |
| 15 DiscardableMemoryManager* manager) |
| 16 : bytes_(bytes), |
| 17 allocator_(allocator), |
| 18 manager_(manager) { |
| 19 manager_->Register(this, bytes_); |
| 20 } |
| 21 |
| 22 DiscardableMemoryAshmem::~DiscardableMemoryAshmem() { |
| 23 manager_->Unregister(this); |
| 24 } |
| 25 |
| 26 bool DiscardableMemoryAshmem::Initialize() { |
| 27 return Lock() == DISCARDABLE_MEMORY_LOCK_STATUS_PURGED; |
| 28 } |
| 29 |
| 30 DiscardableMemoryLockStatus DiscardableMemoryAshmem::Lock() { |
| 31 bool purged = false; |
| 32 if (!manager_->AcquireLock(this, &purged)) |
| 33 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; |
| 34 |
| 35 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED |
| 36 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; |
| 37 } |
| 38 |
| 39 void DiscardableMemoryAshmem::Unlock() { |
| 40 manager_->ReleaseLock(this); |
| 41 } |
| 42 |
| 43 void* DiscardableMemoryAshmem::Memory() const { |
| 44 DCHECK(ashmem_chunk_); |
| 45 return ashmem_chunk_->Memory(); |
| 46 } |
| 47 |
| 48 bool DiscardableMemoryAshmem::AllocateAndAcquireLock() { |
| 49 if (ashmem_chunk_) |
| 50 return ashmem_chunk_->Lock(); |
| 51 |
| 52 ashmem_chunk_ = allocator_->Allocate(bytes_); |
| 53 return false; |
| 54 } |
| 55 |
| 56 void DiscardableMemoryAshmem::ReleaseLock() { |
| 57 ashmem_chunk_->Unlock(); |
| 58 } |
| 59 |
| 60 void DiscardableMemoryAshmem::Purge() { |
| 61 ashmem_chunk_.reset(); |
| 62 } |
| 63 |
| 64 } // namespace internal |
| 65 } // namespace base |
OLD | NEW |