1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef nsMemoryPressure_h__
8 #define nsMemoryPressure_h__
9 
10 #include "nscore.h"
11 
12 /*
13  * These pre-defined strings are the topic to pass to the observer
14  * service to declare the memory-pressure or lift the memory-pressure.
15  *
16  * 1. Notify kTopicMemoryPressure with kSubTopicLowMemoryNew
17  * New memory pressure deteced
18  * On a new memory pressure, we stop everything to start cleaning
19  * aggresively the memory used, in order to free as much memory as
20  * possible.
21  *
22  * 2. Notify kTopicMemoryPressure with kSubTopicLowMemoryOngoing
23  * Repeated memory pressure.
24  * A repeated memory pressure implies to clean softly recent allocations.
25  * It is supposed to happen after a new memory pressure which already
26  * cleaned aggressivley.  So there is no need to damage the reactivity of
27  * Gecko by stopping the world again.
28  *
29  * In case of conflict with an new memory pressue, the new memory pressure
30  * takes precedence over an ongoing memory pressure.  The reason being
31  * that if no events are processed between 2 notifications (new followed
32  * by ongoing, or ongoing followed by a new) we want to be as aggresive as
33  * possible on the clean-up of the memory.  After all, we are trying to
34  * keep Gecko alive as long as possible.
35  *
36  * 3. Notify kTopicMemoryPressureStop with nullptr
37  * Memory pressure stopped.
38  * We're no longer under acute memory pressure, so we might want to have a
39  * chance of (cautiously) re-enabling some things we previously turned off.
40  * As above, an already enqueued new memory pressure event takes precedence.
41  * The priority ordering between concurrent attempts to queue both stopped
42  * and ongoing memory pressure is currently not defined.
43  */
44 extern const char* const kTopicMemoryPressure;
45 extern const char* const kTopicMemoryPressureStop;
46 extern const char16_t* const kSubTopicLowMemoryNew;
47 extern const char16_t* const kSubTopicLowMemoryOngoing;
48 
49 enum class MemoryPressureState : uint32_t {
50   None,  // For internal use.  Don't use this value.
51   LowMemory,
52   NoPressure,
53 };
54 
55 /**
56  * This function causes the main thread to fire a memory pressure event
57  * before processing the next event, but if there are no events pending in
58  * the main thread's event queue, the memory pressure event would not be
59  * dispatched until one is enqueued. It is infallible and does not allocate
60  * any memory.
61  *
62  * You may call this function from any thread.
63  */
64 void NS_NotifyOfEventualMemoryPressure(MemoryPressureState aState);
65 
66 /**
67  * This function causes the main thread to fire a memory pressure event
68  * before processing the next event. We wake up the main thread by adding a
69  * dummy event to its event loop, so, unlike with
70  * NS_NotifyOfEventualMemoryPressure, this memory-pressure event is always
71  * fired relatively quickly, even if the event loop is otherwise empty.
72  *
73  * You may call this function from any thread.
74  */
75 nsresult NS_NotifyOfMemoryPressure(MemoryPressureState aState);
76 
77 #endif  // nsMemoryPressure_h__
78