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 namespace { | 11 namespace { |
12 | 12 |
13 base::LazyInstance<internal::DiscardableMemoryManager>::Leaky g_manager = | 13 // This is admittedly pretty magical. It's approximately enough memory for four |
14 LAZY_INSTANCE_INITIALIZER; | 14 // 2560x1600 images. |
| 15 const size_t kEmulatedMemoryLimit = 64 * 1024 * 1024; |
| 16 const size_t kEmulatedBytesToKeepUnderModeratePressure = |
| 17 kEmulatedMemoryLimit / 4; |
| 18 |
| 19 struct SharedState { |
| 20 SharedState() |
| 21 : manager(kEmulatedMemoryLimit, |
| 22 kEmulatedBytesToKeepUnderModeratePressure) {} |
| 23 |
| 24 internal::DiscardableMemoryManager manager; |
| 25 }; |
| 26 LazyInstance<SharedState>::Leaky g_shared_state = LAZY_INSTANCE_INITIALIZER; |
15 | 27 |
16 } // namespace | 28 } // namespace |
17 | 29 |
18 namespace internal { | 30 namespace internal { |
19 | 31 |
20 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes) | 32 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes) |
21 : bytes_(bytes), | 33 : bytes_(bytes), |
22 is_locked_(false) { | 34 is_locked_(false) { |
23 g_manager.Pointer()->Register(this, bytes); | 35 g_shared_state.Pointer()->manager.Register(this, bytes); |
24 } | 36 } |
25 | 37 |
26 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() { | 38 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() { |
27 if (is_locked_) | 39 if (is_locked_) |
28 Unlock(); | 40 Unlock(); |
29 g_manager.Pointer()->Unregister(this); | 41 g_shared_state.Pointer()->manager.Unregister(this); |
30 } | 42 } |
31 | 43 |
32 // static | 44 // static |
33 void DiscardableMemoryEmulated::RegisterMemoryPressureListeners() { | 45 void DiscardableMemoryEmulated::RegisterMemoryPressureListeners() { |
34 g_manager.Pointer()->RegisterMemoryPressureListener(); | 46 g_shared_state.Pointer()->manager.RegisterMemoryPressureListener(); |
35 } | 47 } |
36 | 48 |
37 // static | 49 // static |
38 void DiscardableMemoryEmulated::UnregisterMemoryPressureListeners() { | 50 void DiscardableMemoryEmulated::UnregisterMemoryPressureListeners() { |
39 g_manager.Pointer()->UnregisterMemoryPressureListener(); | 51 g_shared_state.Pointer()->manager.UnregisterMemoryPressureListener(); |
40 } | 52 } |
41 | 53 |
42 // static | 54 // static |
43 void DiscardableMemoryEmulated::PurgeForTesting() { | 55 void DiscardableMemoryEmulated::PurgeForTesting() { |
44 g_manager.Pointer()->PurgeAll(); | 56 g_shared_state.Pointer()->manager.PurgeAll(); |
45 } | 57 } |
46 | 58 |
47 bool DiscardableMemoryEmulated::Initialize() { | 59 bool DiscardableMemoryEmulated::Initialize() { |
48 return Lock() == DISCARDABLE_MEMORY_LOCK_STATUS_PURGED; | 60 return Lock() == DISCARDABLE_MEMORY_LOCK_STATUS_PURGED; |
49 } | 61 } |
50 | 62 |
51 DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() { | 63 DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() { |
52 DCHECK(!is_locked_); | 64 DCHECK(!is_locked_); |
53 | 65 |
54 bool purged = false; | 66 bool purged = false; |
55 if (!g_manager.Pointer()->AcquireLock(this, &purged)) | 67 if (!g_shared_state.Pointer()->manager.AcquireLock(this, &purged)) |
56 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; | 68 return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; |
57 | 69 |
58 is_locked_ = true; | 70 is_locked_ = true; |
59 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED | 71 return purged ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED |
60 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; | 72 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; |
61 } | 73 } |
62 | 74 |
63 void DiscardableMemoryEmulated::Unlock() { | 75 void DiscardableMemoryEmulated::Unlock() { |
64 DCHECK(is_locked_); | 76 DCHECK(is_locked_); |
65 g_manager.Pointer()->ReleaseLock(this); | 77 g_shared_state.Pointer()->manager.ReleaseLock(this); |
66 is_locked_ = false; | 78 is_locked_ = false; |
67 } | 79 } |
68 | 80 |
69 void* DiscardableMemoryEmulated::Memory() const { | 81 void* DiscardableMemoryEmulated::Memory() const { |
70 DCHECK(is_locked_); | 82 DCHECK(is_locked_); |
71 DCHECK(memory_); | 83 DCHECK(memory_); |
72 return memory_.get(); | 84 return memory_.get(); |
73 } | 85 } |
74 | 86 |
75 bool DiscardableMemoryEmulated::AllocateAndAcquireLock() { | 87 bool DiscardableMemoryEmulated::AllocateAndAcquireLock() { |
76 if (memory_) | 88 if (memory_) |
77 return true; | 89 return true; |
78 | 90 |
79 memory_.reset(new uint8[bytes_]); | 91 memory_.reset(new uint8[bytes_]); |
80 return false; | 92 return false; |
81 } | 93 } |
82 | 94 |
83 void DiscardableMemoryEmulated::Purge() { | 95 void DiscardableMemoryEmulated::Purge() { |
84 memory_.reset(); | 96 memory_.reset(); |
85 } | 97 } |
86 | 98 |
87 } // namespace internal | 99 } // namespace internal |
88 } // namespace base | 100 } // namespace base |
OLD | NEW |