Bug 572417 - Release mouse capture in flash subclass after mouse events get delivered...
[mozilla-central.git] / xpcom / base / nsMemoryImpl.cpp
blobf6adb9cb790c7a04c913312a0d91f40292d0069a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsXPCOM.h"
39 #include "nsMemoryImpl.h"
40 #include "nsThreadUtils.h"
42 #include "nsIObserver.h"
43 #include "nsIObserverService.h"
44 #include "nsIServiceManager.h"
45 #include "nsISupportsArray.h"
47 #include "prmem.h"
48 #include "prcvar.h"
49 #include "pratom.h"
51 #include "nsAlgorithm.h"
52 #include "nsAutoLock.h"
53 #include "nsCOMPtr.h"
54 #include "nsString.h"
55 #include "mozilla/Services.h"
57 static nsMemoryImpl sGlobalMemory;
59 NS_IMPL_QUERY_INTERFACE1(nsMemoryImpl, nsIMemory)
61 NS_IMETHODIMP_(void*)
62 nsMemoryImpl::Alloc(PRSize size)
64 return NS_Alloc(size);
67 NS_IMETHODIMP_(void*)
68 nsMemoryImpl::Realloc(void* ptr, PRSize size)
70 return NS_Realloc(ptr, size);
73 NS_IMETHODIMP_(void)
74 nsMemoryImpl::Free(void* ptr)
76 NS_Free(ptr);
79 NS_IMETHODIMP
80 nsMemoryImpl::HeapMinimize(PRBool aImmediate)
82 return FlushMemory(NS_LITERAL_STRING("heap-minimize").get(), aImmediate);
85 NS_IMETHODIMP
86 nsMemoryImpl::IsLowMemory(PRBool *result)
88 NS_ERROR("IsLowMemory is deprecated. See bug 592308.");
89 *result = PR_FALSE;
90 return NS_OK;
93 /*static*/ nsresult
94 nsMemoryImpl::Create(nsISupports* outer, const nsIID& aIID, void **aResult)
96 NS_ENSURE_NO_AGGREGATION(outer);
97 return sGlobalMemory.QueryInterface(aIID, aResult);
100 nsresult
101 nsMemoryImpl::FlushMemory(const PRUnichar* aReason, PRBool aImmediate)
103 nsresult rv = NS_OK;
105 if (aImmediate) {
106 // They've asked us to run the flusher *immediately*. We've
107 // got to be on the UI main thread for us to be able to do
108 // that...are we?
109 if (!NS_IsMainThread()) {
110 NS_ERROR("can't synchronously flush memory: not on UI thread");
111 return NS_ERROR_FAILURE;
115 PRInt32 lastVal = PR_AtomicSet(&sIsFlushing, 1);
116 if (lastVal)
117 return NS_OK;
119 PRIntervalTime now = PR_IntervalNow();
121 // Run the flushers immediately if we can; otherwise, proxy to the
122 // UI thread an run 'em asynchronously.
123 if (aImmediate) {
124 rv = RunFlushers(aReason);
126 else {
127 // Don't broadcast more than once every 1000ms to avoid being noisy
128 if (PR_IntervalToMicroseconds(now - sLastFlushTime) > 1000) {
129 sFlushEvent.mReason = aReason;
130 rv = NS_DispatchToMainThread(&sFlushEvent, NS_DISPATCH_NORMAL);
134 sLastFlushTime = now;
135 return rv;
138 nsresult
139 nsMemoryImpl::RunFlushers(const PRUnichar* aReason)
141 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
142 if (os) {
144 // Instead of:
145 // os->NotifyObservers(this, "memory-pressure", aReason);
146 // we are going to do this manually to see who/what is
147 // deallocating.
149 nsCOMPtr<nsISimpleEnumerator> e;
150 os->EnumerateObservers("memory-pressure", getter_AddRefs(e));
152 if ( e ) {
153 nsCOMPtr<nsIObserver> observer;
154 PRBool loop = PR_TRUE;
156 while (NS_SUCCEEDED(e->HasMoreElements(&loop)) && loop)
158 e->GetNext(getter_AddRefs(observer));
160 if (!observer)
161 continue;
163 observer->Observe(observer, "memory-pressure", aReason);
168 // Run built-in system flushers
169 #ifdef WINCE_WINDOWS_MOBILE
171 // This function tries to free up memory for an application.
172 // If necessary, the shell closes down other applications by
173 // sending WM_CLOSE messages. We ask for 4MB.
175 SHCloseApps(1024 * 1024 * 4);
177 #endif
179 sIsFlushing = 0;
180 return NS_OK;
183 // XXX need NS_IMPL_STATIC_ADDREF/RELEASE
184 NS_IMETHODIMP_(nsrefcnt) nsMemoryImpl::FlushEvent::AddRef() { return 2; }
185 NS_IMETHODIMP_(nsrefcnt) nsMemoryImpl::FlushEvent::Release() { return 1; }
186 NS_IMPL_QUERY_INTERFACE1(nsMemoryImpl::FlushEvent, nsIRunnable)
188 NS_IMETHODIMP
189 nsMemoryImpl::FlushEvent::Run()
191 sGlobalMemory.RunFlushers(mReason);
192 return NS_OK;
195 PRInt32
196 nsMemoryImpl::sIsFlushing = 0;
198 PRIntervalTime
199 nsMemoryImpl::sLastFlushTime = 0;
201 nsMemoryImpl::FlushEvent
202 nsMemoryImpl::sFlushEvent;
204 XPCOM_API(void*)
205 NS_Alloc(PRSize size)
207 if (size > PR_INT32_MAX)
208 return nsnull;
210 void* result = moz_malloc(size);
211 if (! result) {
212 // Request an asynchronous flush
213 sGlobalMemory.FlushMemory(NS_LITERAL_STRING("alloc-failure").get(), PR_FALSE);
215 return result;
218 XPCOM_API(void*)
219 NS_Realloc(void* ptr, PRSize size)
221 if (size > PR_INT32_MAX)
222 return nsnull;
224 void* result = moz_realloc(ptr, size);
225 if (! result && size != 0) {
226 // Request an asynchronous flush
227 sGlobalMemory.FlushMemory(NS_LITERAL_STRING("alloc-failure").get(), PR_FALSE);
229 return result;
232 XPCOM_API(void)
233 NS_Free(void* ptr)
235 moz_free(ptr);
238 nsresult
239 NS_GetMemoryManager(nsIMemory* *result)
241 return sGlobalMemory.QueryInterface(NS_GET_IID(nsIMemory), (void**) result);