Bug 575870 - Enable the firefox button on xp themed, classic, and aero basic. r=dao...
[mozilla-central.git] / xpcom / base / nsMemoryImpl.cpp
blob2bed5ec7d93206ae03756ab45347da5070551e41
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 #if defined(XP_WIN)
58 #include <windows.h>
59 #endif
61 #if (MOZ_PLATFORM_MAEMO == 5 || MOZ_PLATFORM_MAEMO == 4) && defined(__arm__)
62 #include <fcntl.h>
63 #include <unistd.h>
64 static const char kHighMark[] = "/sys/kernel/high_watermark";
65 #endif
67 // Some platforms notify you when system memory is low, others do not.
68 // In the case of those that do not, we want to post low memory
69 // notifications from IsLowMemory(). For those that can notify us, that
70 // code usually lives in toolkit.
71 #ifdef WINCE
72 #define NOTIFY_LOW_MEMORY
73 #endif
75 #ifdef WINCE_WINDOWS_MOBILE
76 #include "aygshell.h"
77 #endif
79 #include "nsITimer.h"
81 static nsMemoryImpl sGlobalMemory;
83 NS_IMPL_QUERY_INTERFACE1(nsMemoryImpl, nsIMemory)
85 NS_IMETHODIMP_(void*)
86 nsMemoryImpl::Alloc(PRSize size)
88 return NS_Alloc(size);
91 NS_IMETHODIMP_(void*)
92 nsMemoryImpl::Realloc(void* ptr, PRSize size)
94 return NS_Realloc(ptr, size);
97 NS_IMETHODIMP_(void)
98 nsMemoryImpl::Free(void* ptr)
100 NS_Free(ptr);
103 NS_IMETHODIMP
104 nsMemoryImpl::HeapMinimize(PRBool aImmediate)
106 return FlushMemory(NS_LITERAL_STRING("heap-minimize").get(), aImmediate);
109 /* this magic number is something greater than 40mb
110 * and after all, 40mb should be good enough for any web app
111 * unless it's part of an office suite.
113 static const int kRequiredMemory = 0x3000000;
115 NS_IMETHODIMP
116 nsMemoryImpl::IsLowMemory(PRBool *result)
118 #if defined(WINCE_WINDOWS_MOBILE)
119 MEMORYSTATUS stat;
120 GlobalMemoryStatus(&stat);
121 *result = (stat.dwMemoryLoad >= 98);
122 #elif defined(WINCE)
123 // Bug 525323 - GlobalMemoryStatus kills perf on WinCE.
124 *result = PR_FALSE;
125 #elif defined(XP_WIN)
126 MEMORYSTATUSEX stat;
127 stat.dwLength = sizeof stat;
128 GlobalMemoryStatusEx(&stat);
129 *result = (stat.ullAvailPageFile < kRequiredMemory) &&
130 ((float)stat.ullAvailPageFile / stat.ullTotalPageFile) < 0.1;
131 #elif (MOZ_PLATFORM_MAEMO == 5 || MOZ_PLATFORM_MAEMO == 4) && defined(__arm__)
132 static int osso_highmark_fd = -1;
133 if (osso_highmark_fd == -1) {
134 osso_highmark_fd = open (kHighMark, O_RDONLY);
136 if (osso_highmark_fd == -1) {
137 NS_ERROR("can't find the osso highmark file");
138 *result = PR_FALSE;
139 return NS_OK;
143 // be kind, rewind.
144 lseek(osso_highmark_fd, 0L, SEEK_SET);
146 int c = 0;
147 read (osso_highmark_fd, &c, 1);
149 *result = (c == '1');
150 #else
151 *result = PR_FALSE;
152 #endif
154 #ifdef NOTIFY_LOW_MEMORY
155 if (*result) {
156 sGlobalMemory.FlushMemory(NS_LITERAL_STRING("low-memory").get(), PR_FALSE);
158 #endif
159 return NS_OK;
162 /*static*/ nsresult
163 nsMemoryImpl::Create(nsISupports* outer, const nsIID& aIID, void **aResult)
165 NS_ENSURE_NO_AGGREGATION(outer);
166 return sGlobalMemory.QueryInterface(aIID, aResult);
169 nsresult
170 nsMemoryImpl::FlushMemory(const PRUnichar* aReason, PRBool aImmediate)
172 nsresult rv = NS_OK;
174 if (aImmediate) {
175 // They've asked us to run the flusher *immediately*. We've
176 // got to be on the UI main thread for us to be able to do
177 // that...are we?
178 if (!NS_IsMainThread()) {
179 NS_ERROR("can't synchronously flush memory: not on UI thread");
180 return NS_ERROR_FAILURE;
184 PRInt32 lastVal = PR_AtomicSet(&sIsFlushing, 1);
185 if (lastVal)
186 return NS_OK;
188 PRIntervalTime now = PR_IntervalNow();
190 // Run the flushers immediately if we can; otherwise, proxy to the
191 // UI thread an run 'em asynchronously.
192 if (aImmediate) {
193 rv = RunFlushers(aReason);
195 else {
196 // Don't broadcast more than once every 1000ms to avoid being noisy
197 if (PR_IntervalToMicroseconds(now - sLastFlushTime) > 1000) {
198 sFlushEvent.mReason = aReason;
199 rv = NS_DispatchToMainThread(&sFlushEvent, NS_DISPATCH_NORMAL);
203 sLastFlushTime = now;
204 return rv;
207 nsresult
208 nsMemoryImpl::RunFlushers(const PRUnichar* aReason)
210 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
211 if (os) {
213 // Instead of:
214 // os->NotifyObservers(this, "memory-pressure", aReason);
215 // we are going to do this manually to see who/what is
216 // deallocating.
218 nsCOMPtr<nsISimpleEnumerator> e;
219 os->EnumerateObservers("memory-pressure", getter_AddRefs(e));
221 if ( e ) {
222 nsCOMPtr<nsIObserver> observer;
223 PRBool loop = PR_TRUE;
225 while (NS_SUCCEEDED(e->HasMoreElements(&loop)) && loop)
227 e->GetNext(getter_AddRefs(observer));
229 if (!observer)
230 continue;
232 observer->Observe(observer, "memory-pressure", aReason);
237 // Run built-in system flushers
238 #ifdef WINCE_WINDOWS_MOBILE
240 // This function tries to free up memory for an application.
241 // If necessary, the shell closes down other applications by
242 // sending WM_CLOSE messages. We ask for 4MB.
244 SHCloseApps(1024 * 1024 * 4);
246 #endif
248 sIsFlushing = 0;
249 return NS_OK;
252 // XXX need NS_IMPL_STATIC_ADDREF/RELEASE
253 NS_IMETHODIMP_(nsrefcnt) nsMemoryImpl::FlushEvent::AddRef() { return 2; }
254 NS_IMETHODIMP_(nsrefcnt) nsMemoryImpl::FlushEvent::Release() { return 1; }
255 NS_IMPL_QUERY_INTERFACE1(nsMemoryImpl::FlushEvent, nsIRunnable)
257 NS_IMETHODIMP
258 nsMemoryImpl::FlushEvent::Run()
260 sGlobalMemory.RunFlushers(mReason);
261 return NS_OK;
264 PRInt32
265 nsMemoryImpl::sIsFlushing = 0;
267 PRIntervalTime
268 nsMemoryImpl::sLastFlushTime = 0;
270 nsMemoryImpl::FlushEvent
271 nsMemoryImpl::sFlushEvent;
273 XPCOM_API(void*)
274 NS_Alloc(PRSize size)
276 if (size > PR_INT32_MAX)
277 return nsnull;
279 void* result = moz_malloc(size);
280 if (! result) {
281 // Request an asynchronous flush
282 sGlobalMemory.FlushMemory(NS_LITERAL_STRING("alloc-failure").get(), PR_FALSE);
284 return result;
287 XPCOM_API(void*)
288 NS_Realloc(void* ptr, PRSize size)
290 if (size > PR_INT32_MAX)
291 return nsnull;
293 void* result = moz_realloc(ptr, size);
294 if (! result && size != 0) {
295 // Request an asynchronous flush
296 sGlobalMemory.FlushMemory(NS_LITERAL_STRING("alloc-failure").get(), PR_FALSE);
298 return result;
301 XPCOM_API(void)
302 NS_Free(void* ptr)
304 moz_free(ptr);
307 nsresult
308 NS_GetMemoryManager(nsIMemory* *result)
310 return sGlobalMemory.QueryInterface(NS_GET_IID(nsIMemory), (void**) result);