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 // This is admittedly pretty magical. | 13 // This is admittedly pretty magical. |
14 const size_t kEmulatedMemoryLimit = 512 * 1024 * 1024; | 14 const size_t kEmulatedMemoryLimit = 512 * 1024 * 1024; |
15 const size_t kEmulatedSoftMemoryLimit = 32 * 1024 * 1024; | 15 const size_t kEmulatedSoftMemoryLimit = 32 * 1024 * 1024; |
16 const size_t kEmulatedBytesToKeepUnderModeratePressure = 4 * 1024 * 1024; | |
17 const size_t kEmulatedHardMemoryLimitExpirationTimeMs = 1000; | 16 const size_t kEmulatedHardMemoryLimitExpirationTimeMs = 1000; |
18 | 17 |
19 struct SharedState { | 18 struct SharedState { |
20 SharedState() | 19 SharedState() |
21 : manager(kEmulatedMemoryLimit, | 20 : manager(kEmulatedMemoryLimit, |
22 kEmulatedSoftMemoryLimit, | 21 kEmulatedSoftMemoryLimit, |
23 kEmulatedBytesToKeepUnderModeratePressure, | |
24 TimeDelta::FromMilliseconds( | 22 TimeDelta::FromMilliseconds( |
25 kEmulatedHardMemoryLimitExpirationTimeMs)) {} | 23 kEmulatedHardMemoryLimitExpirationTimeMs)) {} |
26 | 24 |
27 internal::DiscardableMemoryManager manager; | 25 internal::DiscardableMemoryManager manager; |
28 }; | 26 }; |
29 LazyInstance<SharedState>::Leaky g_shared_state = LAZY_INSTANCE_INITIALIZER; | 27 LazyInstance<SharedState>::Leaky g_shared_state = LAZY_INSTANCE_INITIALIZER; |
30 | 28 |
31 } // namespace | 29 } // namespace |
32 | 30 |
33 namespace internal { | 31 namespace internal { |
34 | 32 |
35 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes) | 33 DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes) |
36 : bytes_(bytes), | 34 : bytes_(bytes), |
37 is_locked_(false) { | 35 is_locked_(false) { |
38 g_shared_state.Pointer()->manager.Register(this, bytes); | 36 g_shared_state.Pointer()->manager.Register(this, bytes); |
39 } | 37 } |
40 | 38 |
41 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() { | 39 DiscardableMemoryEmulated::~DiscardableMemoryEmulated() { |
42 if (is_locked_) | 40 if (is_locked_) |
43 Unlock(); | 41 Unlock(); |
44 g_shared_state.Pointer()->manager.Unregister(this); | 42 g_shared_state.Pointer()->manager.Unregister(this); |
45 } | 43 } |
46 | 44 |
47 // static | 45 // static |
48 void DiscardableMemoryEmulated::RegisterMemoryPressureListeners() { | |
49 g_shared_state.Pointer()->manager.RegisterMemoryPressureListener(); | |
50 } | |
51 | |
52 // static | |
53 void DiscardableMemoryEmulated::UnregisterMemoryPressureListeners() { | |
54 g_shared_state.Pointer()->manager.UnregisterMemoryPressureListener(); | |
55 } | |
56 | |
57 // static | |
58 bool DiscardableMemoryEmulated::ReduceMemoryUsage() { | 46 bool DiscardableMemoryEmulated::ReduceMemoryUsage() { |
59 return g_shared_state.Pointer()->manager.ReduceMemoryUsage(); | 47 return g_shared_state.Pointer()->manager.ReduceMemoryUsage(); |
60 } | 48 } |
61 | 49 |
62 // static | 50 // static |
| 51 void DiscardableMemoryEmulated::ReduceMemoryUsageUntilWithinLimit( |
| 52 size_t bytes) { |
| 53 g_shared_state.Pointer()->manager.ReduceMemoryUsageUntilWithinLimit(bytes); |
| 54 } |
| 55 |
| 56 // static |
63 void DiscardableMemoryEmulated::PurgeForTesting() { | 57 void DiscardableMemoryEmulated::PurgeForTesting() { |
64 g_shared_state.Pointer()->manager.PurgeAll(); | 58 g_shared_state.Pointer()->manager.PurgeAll(); |
65 } | 59 } |
66 | 60 |
67 bool DiscardableMemoryEmulated::Initialize() { | 61 bool DiscardableMemoryEmulated::Initialize() { |
68 return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; | 62 return Lock() != DISCARDABLE_MEMORY_LOCK_STATUS_FAILED; |
69 } | 63 } |
70 | 64 |
71 DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() { | 65 DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() { |
72 DCHECK(!is_locked_); | 66 DCHECK(!is_locked_); |
(...skipping 26 matching lines...) Expand all Loading... |
99 memory_.reset(new uint8[bytes_]); | 93 memory_.reset(new uint8[bytes_]); |
100 return false; | 94 return false; |
101 } | 95 } |
102 | 96 |
103 void DiscardableMemoryEmulated::Purge() { | 97 void DiscardableMemoryEmulated::Purge() { |
104 memory_.reset(); | 98 memory_.reset(); |
105 } | 99 } |
106 | 100 |
107 } // namespace internal | 101 } // namespace internal |
108 } // namespace base | 102 } // namespace base |
OLD | NEW |