2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsMemoryImpl.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
2009-09-14 20:11:30 -07:00
|
|
|
#include "nsIObserver.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIObserverService.h"
|
2013-04-08 11:35:31 -07:00
|
|
|
#include "nsISimpleEnumerator.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 09:59:13 -07:00
|
|
|
#include "mozilla/Services.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-10-31 11:59:55 -07:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <stdio.h>
|
2013-10-01 06:22:15 -07:00
|
|
|
|
|
|
|
// Minimum memory threshold for a device to be considered
|
|
|
|
// a low memory platform. This value has be in sync with
|
|
|
|
// Java's equivalent threshold, defined in
|
|
|
|
// mobile/android/base/util/HardwareUtils.java
|
2013-01-09 08:12:36 -08:00
|
|
|
#define LOW_MEMORY_THRESHOLD_KB (384 * 1024)
|
2012-10-31 11:59:55 -07:00
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static nsMemoryImpl sGlobalMemory;
|
|
|
|
|
|
|
|
NS_IMPL_QUERY_INTERFACE1(nsMemoryImpl, nsIMemory)
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void*)
|
2012-08-09 00:10:11 -07:00
|
|
|
nsMemoryImpl::Alloc(size_t size)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_Alloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void*)
|
2012-08-09 00:10:11 -07:00
|
|
|
nsMemoryImpl::Realloc(void* ptr, size_t size)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_Realloc(ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
nsMemoryImpl::Free(void* ptr)
|
|
|
|
{
|
|
|
|
NS_Free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsMemoryImpl::HeapMinimize(bool aImmediate)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return FlushMemory(NS_LITERAL_STRING("heap-minimize").get(), aImmediate);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsMemoryImpl::IsLowMemory(bool *result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-09-02 15:02:06 -07:00
|
|
|
NS_ERROR("IsLowMemory is deprecated. See bug 592308.");
|
2011-10-17 07:59:28 -07:00
|
|
|
*result = false;
|
2009-09-14 20:11:30 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-10-31 11:59:55 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMemoryImpl::IsLowMemoryPlatform(bool *result)
|
|
|
|
{
|
|
|
|
#ifdef ANDROID
|
|
|
|
static int sLowMemory = -1; // initialize to unknown, lazily evaluate to 0 or 1
|
|
|
|
if (sLowMemory == -1) {
|
|
|
|
sLowMemory = 0; // assume "not low memory" in case file operations fail
|
|
|
|
*result = false;
|
|
|
|
|
2012-11-06 07:05:18 -08:00
|
|
|
// check if MemTotal from /proc/meminfo is less than LOW_MEMORY_THRESHOLD_KB
|
2012-10-31 11:59:55 -07:00
|
|
|
FILE* fd = fopen("/proc/meminfo", "r");
|
|
|
|
if (!fd) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
uint64_t mem = 0;
|
|
|
|
int rv = fscanf(fd, "MemTotal: %lu kB", &mem);
|
|
|
|
if (fclose(fd)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (rv != 1) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-11-06 07:05:18 -08:00
|
|
|
sLowMemory = (mem < LOW_MEMORY_THRESHOLD_KB) ? 1 : 0;
|
2012-10-31 11:59:55 -07:00
|
|
|
}
|
|
|
|
*result = (sLowMemory == 1);
|
|
|
|
#else
|
|
|
|
*result = false;
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*static*/ nsresult
|
|
|
|
nsMemoryImpl::Create(nsISupports* outer, const nsIID& aIID, void **aResult)
|
|
|
|
{
|
|
|
|
NS_ENSURE_NO_AGGREGATION(outer);
|
|
|
|
return sGlobalMemory.QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
nsMemoryImpl::FlushMemory(const PRUnichar* aReason, bool aImmediate)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-03-11 22:50:11 -08:00
|
|
|
nsresult rv = NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (aImmediate) {
|
|
|
|
// They've asked us to run the flusher *immediately*. We've
|
|
|
|
// got to be on the UI main thread for us to be able to do
|
|
|
|
// that...are we?
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NS_ERROR("can't synchronously flush memory: not on UI thread");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-22 08:14:42 -07:00
|
|
|
int32_t lastVal = sIsFlushing.exchange(1);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (lastVal)
|
|
|
|
return NS_OK;
|
|
|
|
|
2009-09-14 20:11:30 -07:00
|
|
|
PRIntervalTime now = PR_IntervalNow();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Run the flushers immediately if we can; otherwise, proxy to the
|
|
|
|
// UI thread an run 'em asynchronously.
|
|
|
|
if (aImmediate) {
|
|
|
|
rv = RunFlushers(aReason);
|
|
|
|
}
|
|
|
|
else {
|
2009-09-15 11:33:53 -07:00
|
|
|
// Don't broadcast more than once every 1000ms to avoid being noisy
|
|
|
|
if (PR_IntervalToMicroseconds(now - sLastFlushTime) > 1000) {
|
2009-09-14 20:11:30 -07:00
|
|
|
sFlushEvent.mReason = aReason;
|
|
|
|
rv = NS_DispatchToMainThread(&sFlushEvent, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-09-14 20:11:30 -07:00
|
|
|
sLastFlushTime = now;
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsMemoryImpl::RunFlushers(const PRUnichar* aReason)
|
|
|
|
{
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 09:59:13 -07:00
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (os) {
|
2009-09-14 20:11:30 -07:00
|
|
|
|
|
|
|
// Instead of:
|
|
|
|
// os->NotifyObservers(this, "memory-pressure", aReason);
|
|
|
|
// we are going to do this manually to see who/what is
|
|
|
|
// deallocating.
|
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> e;
|
|
|
|
os->EnumerateObservers("memory-pressure", getter_AddRefs(e));
|
|
|
|
|
|
|
|
if ( e ) {
|
|
|
|
nsCOMPtr<nsIObserver> observer;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool loop = true;
|
2009-09-14 20:11:30 -07:00
|
|
|
|
|
|
|
while (NS_SUCCEEDED(e->HasMoreElements(&loop)) && loop)
|
|
|
|
{
|
|
|
|
e->GetNext(getter_AddRefs(observer));
|
|
|
|
|
|
|
|
if (!observer)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
observer->Observe(observer, "memory-pressure", aReason);
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
sIsFlushing = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX need NS_IMPL_STATIC_ADDREF/RELEASE
|
|
|
|
NS_IMETHODIMP_(nsrefcnt) nsMemoryImpl::FlushEvent::AddRef() { return 2; }
|
|
|
|
NS_IMETHODIMP_(nsrefcnt) nsMemoryImpl::FlushEvent::Release() { return 1; }
|
|
|
|
NS_IMPL_QUERY_INTERFACE1(nsMemoryImpl::FlushEvent, nsIRunnable)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMemoryImpl::FlushEvent::Run()
|
|
|
|
{
|
|
|
|
sGlobalMemory.RunFlushers(mReason);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-08-22 08:14:42 -07:00
|
|
|
mozilla::Atomic<int32_t>
|
|
|
|
nsMemoryImpl::sIsFlushing;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-14 20:11:30 -07:00
|
|
|
PRIntervalTime
|
|
|
|
nsMemoryImpl::sLastFlushTime = 0;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsMemoryImpl::FlushEvent
|
|
|
|
nsMemoryImpl::sFlushEvent;
|
|
|
|
|
|
|
|
XPCOM_API(void*)
|
2012-08-09 00:10:11 -07:00
|
|
|
NS_Alloc(size_t size)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-11-09 08:42:00 -08:00
|
|
|
return moz_xmalloc(size);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
XPCOM_API(void*)
|
2012-08-09 00:10:11 -07:00
|
|
|
NS_Realloc(void* ptr, size_t size)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-11-09 08:42:00 -08:00
|
|
|
return moz_xrealloc(ptr, size);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
XPCOM_API(void)
|
|
|
|
NS_Free(void* ptr)
|
|
|
|
{
|
2010-03-03 21:02:58 -08:00
|
|
|
moz_free(ptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_GetMemoryManager(nsIMemory* *result)
|
|
|
|
{
|
|
|
|
return sGlobalMemory.QueryInterface(NS_GET_IID(nsIMemory), (void**) result);
|
|
|
|
}
|