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 "nsXPCOM.h"
|
|
|
|
#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"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsISupportsArray.h"
|
|
|
|
|
|
|
|
#include "prmem.h"
|
|
|
|
#include "prcvar.h"
|
|
|
|
#include "pratom.h"
|
|
|
|
|
|
|
|
#include "nsAlgorithm.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.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
|
|
|
|
|
|
|
static nsMemoryImpl sGlobalMemory;
|
|
|
|
|
|
|
|
NS_IMPL_QUERY_INTERFACE1(nsMemoryImpl, nsIMemory)
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void*)
|
|
|
|
nsMemoryImpl::Alloc(PRSize size)
|
|
|
|
{
|
|
|
|
return NS_Alloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void*)
|
|
|
|
nsMemoryImpl::Realloc(void* ptr, PRSize size)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-28 12:58:49 -07:00
|
|
|
PRInt32 lastVal = PR_ATOMIC_SET(&sIsFlushing, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsMemoryImpl::sIsFlushing = 0;
|
|
|
|
|
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*)
|
|
|
|
NS_Alloc(PRSize size)
|
|
|
|
{
|
2011-11-09 08:42:00 -08:00
|
|
|
return moz_xmalloc(size);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
XPCOM_API(void*)
|
|
|
|
NS_Realloc(void* ptr, PRSize size)
|
|
|
|
{
|
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);
|
|
|
|
}
|