Chromium Code Reviews| Index: base/memory/memory_pressure.h |
| diff --git a/base/memory/memory_pressure.h b/base/memory/memory_pressure.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..87a6bb75dfb14f1f6564f233fc8d91a88d452d09 |
| --- /dev/null |
| +++ b/base/memory/memory_pressure.h |
| @@ -0,0 +1,104 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// MemoryPressure provides static APIs for handling memory pressure on |
| +// platforms that have such signals, such as Android. |
| +// The app will try to discard buffers that aren't deemed essential (individual |
| +// modules will implement their own policy). |
| +// |
| +// Note that the platform would potentially kill the app in order to free memory |
| +// that other apps may be requesting. |
| +// The trade-off here is in then terms of how long does it take to free such |
| +// memory, and later on potentially re-create it, versus being killed and |
| +// 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.
|
| + |
| +#ifndef BASE_MEMORY_PRESSURE_H_ |
| +#define BASE_MEMORY_PRESSURE_H_ |
| + |
| +#include "base/base_export.h" |
| +#include "base/basictypes.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/singleton.h" |
| +#include "base/observer_list_threadsafe.h" |
| + |
| +namespace base { |
| + |
| +// To start listening, create a new instance, passing a callback to a |
| +// 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.
|
| +// simply delete the listener object. The implementation guarantees |
| +// that the callback will always be called on the thread that created |
| +// the listener. |
| +// If this is the same thread as the system is broadcasting the memory pressure |
| +// event on, then it is guaranteed you're called synchronously within that |
| +// broadcast and hence you should not do long-running garbage collection work. |
| +// But conversely, if there's something that needs to be released before |
| +// 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.
|
| +// Please see notes on memory_pressure_level_list.h: some levels are absolutely |
| +// critical, and if not enough memory is returned to the system, it'll |
| +// 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.
|
| +// cold. |
| +// |
| +// |
| +// Example: |
| +// |
| +// void OnMemoryPressure(MemoryPressureLevel memory_pressure_level) { |
| +// ... |
| +// } |
| +// |
| +// // Start listening. |
| +// MemoryPressure::Listener* my_listener = |
| +// new MemoryPressure::Listener(base::Bind(&OnMemoryPressure)); |
| +// |
| +// ... |
| +// |
| +// // Stop listening. |
| +// delete my_listener |
|
Peter Kasting
2013/06/07 01:00:43
Nit: semicolon
bulach
2013/06/07 11:06:58
Done.
|
| +// |
| +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
|
| + public: |
| + enum MemoryPressureLevel { |
| +#define DEFINE_MEMORY_PRESSURE_LEVEL(name, value) name = value, |
| +#include "base/memory/memory_pressure_level_list.h" |
| + }; |
| + |
| + typedef base::Callback<void(MemoryPressureLevel)> MemoryPressureCallback; |
| + |
| + class Listener { |
| + public: |
| + explicit Listener(const MemoryPressureCallback& memory_pressure_callback); |
| + ~Listener(); |
| + |
| + private: |
| + 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.
|
| + |
| + void Notify(MemoryPressureLevel memory_pressure_level); |
| + |
| + MemoryPressureCallback callback_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(Listener); |
| + }; |
| + |
| + static MemoryPressure* GetInstance(); |
| + |
| + // Internal use only: called when the platform receives a memory pressure |
| + // signal. |
| + void NotifyMemoryPressure(MemoryPressureLevel memory_pressure_level); |
| + |
| + private: |
| + friend struct DefaultSingletonTraits<MemoryPressure>; |
| + |
| + MemoryPressure(); |
| + ~MemoryPressure(); |
| + |
| + void RegisterListener(Listener* listener); |
| + void UnregisterListener(Listener* listener); |
| + |
| + scoped_refptr<ObserverListThreadSafe<Listener> > observers_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MemoryPressure); |
| +}; |
| + |
| +} // namespace base |
| + |
| +#endif // BASE_MEMORY_PRESSURE_H_ |