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
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.
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 ***** */
39 #include "nsMemoryImpl.h"
40 #include "nsThreadUtils.h"
42 #include "nsIObserver.h"
43 #include "nsIObserverService.h"
44 #include "nsIServiceManager.h"
45 #include "nsISupportsArray.h"
51 #include "nsAlgorithm.h"
52 #include "nsAutoLock.h"
55 #include "mozilla/Services.h"
61 #if (MOZ_PLATFORM_MAEMO == 5 || MOZ_PLATFORM_MAEMO == 4) && defined(__arm__)
64 static const char kHighMark
[] = "/sys/kernel/high_watermark";
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.
72 #define NOTIFY_LOW_MEMORY
75 #ifdef WINCE_WINDOWS_MOBILE
81 static nsMemoryImpl sGlobalMemory
;
83 NS_IMPL_QUERY_INTERFACE1(nsMemoryImpl
, nsIMemory
)
86 nsMemoryImpl::Alloc(PRSize size
)
88 return NS_Alloc(size
);
92 nsMemoryImpl::Realloc(void* ptr
, PRSize size
)
94 return NS_Realloc(ptr
, size
);
98 nsMemoryImpl::Free(void* ptr
)
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;
116 nsMemoryImpl::IsLowMemory(PRBool
*result
)
118 #if defined(WINCE_WINDOWS_MOBILE)
120 GlobalMemoryStatus(&stat
);
121 *result
= (stat
.dwMemoryLoad
>= 98);
123 // Bug 525323 - GlobalMemoryStatus kills perf on WinCE.
125 #elif defined(XP_WIN)
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");
144 lseek(osso_highmark_fd
, 0L, SEEK_SET
);
147 read (osso_highmark_fd
, &c
, 1);
149 *result
= (c
== '1');
154 #ifdef NOTIFY_LOW_MEMORY
156 sGlobalMemory
.FlushMemory(NS_LITERAL_STRING("low-memory").get(), PR_FALSE
);
163 nsMemoryImpl::Create(nsISupports
* outer
, const nsIID
& aIID
, void **aResult
)
165 NS_ENSURE_NO_AGGREGATION(outer
);
166 return sGlobalMemory
.QueryInterface(aIID
, aResult
);
170 nsMemoryImpl::FlushMemory(const PRUnichar
* aReason
, PRBool 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
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);
188 PRIntervalTime now
= PR_IntervalNow();
190 // Run the flushers immediately if we can; otherwise, proxy to the
191 // UI thread an run 'em asynchronously.
193 rv
= RunFlushers(aReason
);
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
;
208 nsMemoryImpl::RunFlushers(const PRUnichar
* aReason
)
210 nsCOMPtr
<nsIObserverService
> os
= mozilla::services::GetObserverService();
214 // os->NotifyObservers(this, "memory-pressure", aReason);
215 // we are going to do this manually to see who/what is
218 nsCOMPtr
<nsISimpleEnumerator
> e
;
219 os
->EnumerateObservers("memory-pressure", getter_AddRefs(e
));
222 nsCOMPtr
<nsIObserver
> observer
;
223 PRBool loop
= PR_TRUE
;
225 while (NS_SUCCEEDED(e
->HasMoreElements(&loop
)) && loop
)
227 e
->GetNext(getter_AddRefs(observer
));
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);
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
)
258 nsMemoryImpl::FlushEvent::Run()
260 sGlobalMemory
.RunFlushers(mReason
);
265 nsMemoryImpl::sIsFlushing
= 0;
268 nsMemoryImpl::sLastFlushTime
= 0;
270 nsMemoryImpl::FlushEvent
271 nsMemoryImpl::sFlushEvent
;
274 NS_Alloc(PRSize size
)
276 if (size
> PR_INT32_MAX
)
279 void* result
= moz_malloc(size
);
281 // Request an asynchronous flush
282 sGlobalMemory
.FlushMemory(NS_LITERAL_STRING("alloc-failure").get(), PR_FALSE
);
288 NS_Realloc(void* ptr
, PRSize size
)
290 if (size
> PR_INT32_MAX
)
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
);
308 NS_GetMemoryManager(nsIMemory
* *result
)
310 return sGlobalMemory
.QueryInterface(NS_GET_IID(nsIMemory
), (void**) result
);