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/lazy_instance.h" |
| 8 #include "base/memory/discardable_memory_allocation_ashmem_factory.h" |
| 9 #include "base/memory/discardable_memory_manager.h" |
| 10 |
| 11 namespace base { |
| 12 namespace internal { |
| 13 |
| 14 DiscardableMemoryAshmem::DiscardableMemoryAshmem( |
| 15 DiscardableMemoryManager* manager, |
| 16 size_t size) |
| 17 : manager_(manager), |
| 18 allocation_id_(manager->Register(size)), |
| 19 memory_(NULL) { |
| 20 } |
| 21 |
| 22 DiscardableMemoryAshmem::~DiscardableMemoryAshmem() { |
| 23 manager_->Unregister(allocation_id_); |
| 24 } |
| 25 |
| 26 bool DiscardableMemoryAshmem::Initialize() { |
| 27 return Lock() == DISCARDABLE_MEMORY_LOCK_STATUS_PURGED; |
| 28 } |
| 29 |
| 30 DiscardableMemoryLockStatus DiscardableMemoryAshmem::Lock() { |
| 31 DCHECK(!memory_); |
| 32 |
| 33 bool purged = false; |
| 34 memory_ = manager_->Lock(allocation_id_, &purged); |
| 35 if (!memory_) |
| 36 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; |
| 37 |
| 38 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED |
| 39 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; |
| 40 } |
| 41 |
| 42 void DiscardableMemoryAshmem::Unlock() { |
| 43 DCHECK(memory_); |
| 44 manager_->Unlock(allocation_id_); |
| 45 memory_ = NULL; |
| 46 } |
| 47 |
| 48 void* DiscardableMemoryAshmem::Memory() const { |
| 49 DCHECK(memory_); |
| 50 return memory_; |
| 51 } |
| 52 |
| 53 } // namespace internal |
| 54 } // namespace base |
OLD | NEW |