OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_emulated.h" | 5 #include "base/memory/discardable_memory_emulated.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/memory/discardable_memory_manager.h" | 8 #include "base/memory/discardable_memory_manager.h" |
9 | 9 |
10 namespace base { | 10 namespace base { |
11 | |
12 namespace { | 11 namespace { |
13 | 12 |
14 base::LazyInstance<internal::DiscardableMemoryManager>::Leaky g_manager = | 13 base::LazyInstance<internal::DiscardableMemoryManager>::Leaky g_manager = |
15 LAZY_INSTANCE_INITIALIZER; | 14 LAZY_INSTANCE_INITIALIZER; |
16 | 15 |
17 } // namespace | 16 } // namespace |
18 | 17 |
19 namespace internal { | 18 namespace internal { |
20 | 19 |
21 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t size) | 20 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t size) |
(...skipping 23 matching lines...) Expand all Loading... |
45 } | 44 } |
46 | 45 |
47 bool DiscardableMemoryEmulated::Initialize() { | 46 bool DiscardableMemoryEmulated::Initialize() { |
48 return Lock() == DISCARDABLE_MEMORY_LOCK_STATUS_PURGED; | 47 return Lock() == DISCARDABLE_MEMORY_LOCK_STATUS_PURGED; |
49 } | 48 } |
50 | 49 |
51 DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() { | 50 DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() { |
52 DCHECK(!is_locked_); | 51 DCHECK(!is_locked_); |
53 | 52 |
54 bool purged = false; | 53 bool purged = false; |
55 memory_ = g_manager.Pointer()->Acquire(this, &purged); | 54 if (!g_manager.Pointer()->AcquireLock(this, &purged)) |
56 if (!memory_) | |
57 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; | 55 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; |
58 | 56 |
59 is_locked_ = true; | 57 is_locked_ = true; |
60 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED | 58 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED |
61 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; | 59 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; |
62 } | 60 } |
63 | 61 |
64 void DiscardableMemoryEmulated::Unlock() { | 62 void DiscardableMemoryEmulated::Unlock() { |
65 DCHECK(is_locked_); | 63 DCHECK(is_locked_); |
66 g_manager.Pointer()->Release(this, memory_.Pass()); | 64 g_manager.Pointer()->ReleaseLock(this); |
67 is_locked_ = false; | 65 is_locked_ = false; |
68 } | 66 } |
69 | 67 |
70 void* DiscardableMemoryEmulated::Memory() const { | 68 void* DiscardableMemoryEmulated::Memory() const { |
| 69 DCHECK(is_locked_); |
71 DCHECK(memory_); | 70 DCHECK(memory_); |
72 return memory_.get(); | 71 return memory_.get(); |
73 } | 72 } |
74 | 73 |
| 74 bool DiscardableMemoryEmulated::AllocateAndAcquireLock(size_t bytes) { |
| 75 if (memory_) |
| 76 return true; |
| 77 |
| 78 memory_.reset(new uint8[bytes]); |
| 79 return false; |
| 80 } |
| 81 |
| 82 void DiscardableMemoryEmulated::Purge() { |
| 83 memory_.reset(); |
| 84 } |
| 85 |
75 } // namespace internal | 86 } // namespace internal |
76 } // namespace base | 87 } // namespace base |
OLD | NEW |