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 is_locked_(false) { | |
20 manager_->Register(this, bytes_); | |
21 } | |
22 | |
23 DiscardableMemoryAshmem::~DiscardableMemoryAshmem() { | |
24 if (is_locked_) | |
25 manager_->ReleaseLock(this); | |
26 | |
27 manager_->Unregister(this); | |
28 } | |
29 | |
30 bool DiscardableMemoryAshmem::Initialize() { | |
31 return Lock() == DISCARDABLE_MEMORY_LOCK_STATUS_PURGED; | |
32 } | |
33 | |
34 DiscardableMemoryLockStatus DiscardableMemoryAshmem::Lock() { | |
35 bool purged = false; | |
36 if (!manager_->AcquireLock(this, &purged)) | |
37 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; | |
38 | |
39 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED | |
40 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; | |
41 } | |
42 | |
43 void DiscardableMemoryAshmem::Unlock() { | |
44 manager_->ReleaseLock(this); | |
45 } | |
46 | |
47 void* DiscardableMemoryAshmem::Memory() const { | |
48 DCHECK(ashmem_chunk_); | |
49 return ashmem_chunk_->Memory(); | |
50 } | |
51 | |
52 bool DiscardableMemoryAshmem::AllocateAndAcquireLock() { | |
53 DCHECK(!is_locked_); | |
54 is_locked_ = true; | |
55 | |
56 if (ashmem_chunk_) | |
57 return ashmem_chunk_->Lock(); | |
58 | |
59 ashmem_chunk_ = allocator_->Allocate(bytes_); | |
60 return false; | |
61 } | |
62 | |
63 void DiscardableMemoryAshmem::ReleaseLock() { | |
64 DCHECK(is_locked_); | |
65 ashmem_chunk_->Unlock(); | |
66 is_locked_ = false; | |
67 } | |
68 | |
69 void DiscardableMemoryAshmem::Purge() { | |
70 DCHECK(!is_locked_); | |
71 ashmem_chunk_.reset(); | |
72 } | |
73 | |
74 } // namespace internal | |
75 } // namespace base | |
OLD | NEW |