OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/discardable_memory_shmem.h" | 5 #include "base/memory/discardable_memory_shmem.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/memory/discardable_memory_shmem_allocator.h" | 8 #include "base/memory/discardable_memory_shmem_allocator.h" |
9 | 9 |
10 namespace base { | 10 namespace base { |
11 namespace internal { | 11 namespace internal { |
12 | 12 |
13 DiscardableMemoryShmem::DiscardableMemoryShmem(size_t bytes) | 13 DiscardableMemoryShmem::DiscardableMemoryShmem(size_t bytes) |
14 : bytes_(bytes), is_locked_(false) { | 14 : chunk_(DiscardableMemoryShmemAllocator::GetInstance() |
| 15 ->AllocateLockedDiscardableMemory(bytes)), |
| 16 is_locked_(true) { |
| 17 DCHECK(chunk_); |
15 } | 18 } |
16 | 19 |
17 DiscardableMemoryShmem::~DiscardableMemoryShmem() { | 20 DiscardableMemoryShmem::~DiscardableMemoryShmem() { |
18 if (is_locked_) | 21 if (is_locked_) |
19 Unlock(); | 22 Unlock(); |
20 } | 23 } |
21 | 24 |
22 bool DiscardableMemoryShmem::Initialize() { | 25 bool DiscardableMemoryShmem::Lock() { |
23 return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; | 26 DCHECK(!is_locked_); |
24 } | 27 DCHECK(chunk_); |
25 | 28 |
26 DiscardableMemoryLockStatus DiscardableMemoryShmem::Lock() { | 29 if (!chunk_->Lock()) { |
27 DCHECK(!is_locked_); | 30 chunk_.reset(); |
28 | 31 return false; |
29 if (chunk_ && chunk_->Lock()) { | |
30 is_locked_ = true; | |
31 return DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; | |
32 } | 32 } |
33 | 33 |
34 chunk_ = DiscardableMemoryShmemAllocator::GetInstance() | |
35 ->AllocateLockedDiscardableMemory(bytes_); | |
36 if (!chunk_) | |
37 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; | |
38 | |
39 is_locked_ = true; | 34 is_locked_ = true; |
40 return DISCARDABLE_MEMORY_LOCK_STATUS_PURGED; | 35 return true; |
41 } | 36 } |
42 | 37 |
43 void DiscardableMemoryShmem::Unlock() { | 38 void DiscardableMemoryShmem::Unlock() { |
44 DCHECK(is_locked_); | 39 DCHECK(is_locked_); |
45 DCHECK(chunk_); | |
46 chunk_->Unlock(); | 40 chunk_->Unlock(); |
47 is_locked_ = false; | 41 is_locked_ = false; |
48 } | 42 } |
49 | 43 |
50 void* DiscardableMemoryShmem::Memory() const { | 44 void* DiscardableMemoryShmem::Memory() const { |
51 DCHECK(is_locked_); | 45 DCHECK(is_locked_); |
52 DCHECK(chunk_); | |
53 return chunk_->Memory(); | 46 return chunk_->Memory(); |
54 } | 47 } |
55 | 48 |
56 } // namespace internal | 49 } // namespace internal |
57 } // namespace base | 50 } // namespace base |
OLD | NEW |