Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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 // MemoryPressure provides static APIs for handling memory pressure on | |
| 6 // platforms that have such signals, such as Android. | |
| 7 // The app will try to discard buffers that aren't deemed essential (individual | |
| 8 // modules will implement their own policy). | |
| 9 // | |
| 10 // Note that the platform would potentially kill the app in order to free memory | |
| 11 // that other apps may be requesting. | |
| 12 // The trade-off here is in then terms of how long does it take to free such | |
| 13 // memory, and later on potentially re-create it, versus being killed and | |
| 14 // restarted from cold. | |
|
Peter Kasting
2013/06/07 01:00:43
This paragraph has grammar problems and is confusi
bulach
2013/06/07 11:06:58
Done.
| |
| 15 | |
| 16 #ifndef BASE_MEMORY_PRESSURE_H_ | |
| 17 #define BASE_MEMORY_PRESSURE_H_ | |
| 18 | |
| 19 #include "base/base_export.h" | |
| 20 #include "base/basictypes.h" | |
| 21 #include "base/memory/ref_counted.h" | |
| 22 #include "base/memory/singleton.h" | |
| 23 #include "base/observer_list_threadsafe.h" | |
| 24 | |
| 25 namespace base { | |
| 26 | |
| 27 // To start listening, create a new instance, passing a callback to a | |
| 28 // function that takes an MemoryPressureLevel parameter. To stop listening, | |
|
Peter Kasting
2013/06/07 01:00:43
Nit: an -> a
bulach
2013/06/07 11:06:58
Done.
| |
| 29 // simply delete the listener object. The implementation guarantees | |
| 30 // that the callback will always be called on the thread that created | |
| 31 // the listener. | |
| 32 // If this is the same thread as the system is broadcasting the memory pressure | |
| 33 // event on, then it is guaranteed you're called synchronously within that | |
| 34 // broadcast and hence you should not do long-running garbage collection work. | |
| 35 // But conversely, if there's something that needs to be released before | |
| 36 // it returns control back to system code, this is the place to do it. | |
|
Peter Kasting
2013/06/07 01:00:43
Nit: it returns control back -> control is returne
bulach
2013/06/07 11:06:58
Done.
| |
| 37 // Please see notes on memory_pressure_level_list.h: some levels are absolutely | |
| 38 // critical, and if not enough memory is returned to the system, it'll | |
| 39 // potentially kill the app, and then later it'll have to be restarted from | |
|
Peter Kasting
2013/06/07 01:00:43
Nit: it'll have to be restarted from cold -> Chrom
bulach
2013/06/07 11:06:58
Done.
| |
| 40 // cold. | |
| 41 // | |
| 42 // | |
| 43 // Example: | |
| 44 // | |
| 45 // void OnMemoryPressure(MemoryPressureLevel memory_pressure_level) { | |
| 46 // ... | |
| 47 // } | |
| 48 // | |
| 49 // // Start listening. | |
| 50 // MemoryPressure::Listener* my_listener = | |
| 51 // new MemoryPressure::Listener(base::Bind(&OnMemoryPressure)); | |
| 52 // | |
| 53 // ... | |
| 54 // | |
| 55 // // Stop listening. | |
| 56 // delete my_listener | |
|
Peter Kasting
2013/06/07 01:00:43
Nit: semicolon
bulach
2013/06/07 11:06:58
Done.
| |
| 57 // | |
| 58 class BASE_EXPORT MemoryPressure { | |
|
Peter Kasting
2013/06/07 01:00:43
Nit: I'd call this class "MemoryPressureHandler",
bulach
2013/06/07 11:06:58
please see joth's nit, it was a handler before:
ht
| |
| 59 public: | |
| 60 enum MemoryPressureLevel { | |
| 61 #define DEFINE_MEMORY_PRESSURE_LEVEL(name, value) name = value, | |
| 62 #include "base/memory/memory_pressure_level_list.h" | |
| 63 }; | |
| 64 | |
| 65 typedef base::Callback<void(MemoryPressureLevel)> MemoryPressureCallback; | |
| 66 | |
| 67 class Listener { | |
| 68 public: | |
| 69 explicit Listener(const MemoryPressureCallback& memory_pressure_callback); | |
| 70 ~Listener(); | |
| 71 | |
| 72 private: | |
| 73 friend class MemoryPressure; | |
|
Peter Kasting
2013/06/07 01:00:43
I see no reason to make Notify() private and make
bulach
2013/06/07 11:06:58
Done.
| |
| 74 | |
| 75 void Notify(MemoryPressureLevel memory_pressure_level); | |
| 76 | |
| 77 MemoryPressureCallback callback_; | |
| 78 | |
| 79 DISALLOW_COPY_AND_ASSIGN(Listener); | |
| 80 }; | |
| 81 | |
| 82 static MemoryPressure* GetInstance(); | |
| 83 | |
| 84 // Internal use only: called when the platform receives a memory pressure | |
| 85 // signal. | |
| 86 void NotifyMemoryPressure(MemoryPressureLevel memory_pressure_level); | |
| 87 | |
| 88 private: | |
| 89 friend struct DefaultSingletonTraits<MemoryPressure>; | |
| 90 | |
| 91 MemoryPressure(); | |
| 92 ~MemoryPressure(); | |
| 93 | |
| 94 void RegisterListener(Listener* listener); | |
| 95 void UnregisterListener(Listener* listener); | |
| 96 | |
| 97 scoped_refptr<ObserverListThreadSafe<Listener> > observers_; | |
| 98 | |
| 99 DISALLOW_COPY_AND_ASSIGN(MemoryPressure); | |
| 100 }; | |
| 101 | |
| 102 } // namespace base | |
| 103 | |
| 104 #endif // BASE_MEMORY_PRESSURE_H_ | |
| OLD | NEW |