LCOV - app.info - xpcom/base/nsMemoryImpl.cpp

LCOV - code coverage report
| Current view: | directory - xpcom/base - nsMemoryImpl.cpp (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 67 | 20 | 29.9 %
| Date: | 2012-04-21 | Functions: | 20 | 10 | 50.0 %

           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
           4                 :  *
           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/
           9                 :  *
          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.
          14                 :  *
          15                 :  * The Original Code is mozilla.org code.
          16                 :  *
          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.
          21                 :  *
          22                 :  * Contributor(s):
          23                 :  *
          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.
          35                 :  *
          36                 :  * ***** END LICENSE BLOCK ***** */
          37                 :
          38                 : #include "nsXPCOM.h"
          39                 : #include "nsMemoryImpl.h"
          40                 : #include "nsThreadUtils.h"
          41                 :
          42                 : #include "nsIObserver.h"
          43                 : #include "nsIObserverService.h"
          44                 : #include "nsIServiceManager.h"
          45                 : #include "nsISupportsArray.h"
          46                 :
          47                 : #include "prmem.h"
          48                 : #include "prcvar.h"
          49                 : #include "pratom.h"
          50                 :
          51                 : #include "nsAlgorithm.h"
          52                 : #include "nsCOMPtr.h"
          53                 : #include "nsString.h"
          54                 : #include "mozilla/Services.h"
          55                 :
          56            1396 : static nsMemoryImpl sGlobalMemory;
          57                 :
          58           26489 : NS_IMPL_QUERY_INTERFACE1(nsMemoryImpl, nsIMemory)
          59                 :
          60                 : NS_IMETHODIMP_(void*)
          61         3679014 : nsMemoryImpl::Alloc(PRSize size)
          62                 : {
          63         3679014 :     return NS_Alloc(size);
          64                 : }
          65                 :
          66                 : NS_IMETHODIMP_(void*)
          67            8073 : nsMemoryImpl::Realloc(void* ptr, PRSize size)
          68                 : {
          69            8073 :     return NS_Realloc(ptr, size);
          70                 : }
          71                 :
          72                 : NS_IMETHODIMP_(void)
          73         3679012 : nsMemoryImpl::Free(void* ptr)
          74                 : {
          75         3679012 :     NS_Free(ptr);
          76         3679012 : }
          77                 :
          78                 : NS_IMETHODIMP
          79               0 : nsMemoryImpl::HeapMinimize(bool aImmediate)
          80                 : {
          81               0 :     return FlushMemory(NS_LITERAL_STRING("heap-minimize").get(), aImmediate);
          82                 : }
          83                 :
          84                 : NS_IMETHODIMP
          85               0 : nsMemoryImpl::IsLowMemory(bool *result)
          86                 : {
          87               0 :     NS_ERROR("IsLowMemory is deprecated.  See bug 592308.");
          88               0 :     *result = false;
          89               0 :     return NS_OK;
          90                 : }
          91                 :
          92                 : /*static*/ nsresult
          93               0 : nsMemoryImpl::Create(nsISupports* outer, const nsIID& aIID, void **aResult)
          94                 : {
          95               0 :     NS_ENSURE_NO_AGGREGATION(outer);
          96               0 :     return sGlobalMemory.QueryInterface(aIID, aResult);
          97                 : }
          98                 :
          99                 : nsresult
         100               0 : nsMemoryImpl::FlushMemory(const PRUnichar* aReason, bool aImmediate)
         101                 : {
         102               0 :     nsresult rv = NS_OK;
         103                 :
         104               0 :     if (aImmediate) {
         105                 :         // They've asked us to run the flusher *immediately*. We've
         106                 :         // got to be on the UI main thread for us to be able to do
         107                 :         // that...are we?
         108               0 :         if (!NS_IsMainThread()) {
         109               0 :             NS_ERROR("can't synchronously flush memory: not on UI thread");
         110               0 :             return NS_ERROR_FAILURE;
         111                 :         }
         112                 :     }
         113                 :
         114               0 :     PRInt32 lastVal = PR_ATOMIC_SET(&sIsFlushing, 1);
         115               0 :     if (lastVal)
         116               0 :         return NS_OK;
         117                 :
         118               0 :     PRIntervalTime now = PR_IntervalNow();
         119                 :
         120                 :     // Run the flushers immediately if we can; otherwise, proxy to the
         121                 :     // UI thread an run 'em asynchronously.
         122               0 :     if (aImmediate) {
         123               0 :         rv = RunFlushers(aReason);
         124                 :     }
         125                 :     else {
         126                 :         // Don't broadcast more than once every 1000ms to avoid being noisy
         127               0 :         if (PR_IntervalToMicroseconds(now - sLastFlushTime) > 1000) {
         128               0 :             sFlushEvent.mReason = aReason;
         129               0 :             rv = NS_DispatchToMainThread(&sFlushEvent, NS_DISPATCH_NORMAL);
         130                 :         }
         131                 :     }
         132                 :
         133               0 :     sLastFlushTime = now;
         134               0 :     return rv;
         135                 : }
         136                 :
         137                 : nsresult
         138               0 : nsMemoryImpl::RunFlushers(const PRUnichar* aReason)
         139                 : {
         140               0 :     nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
         141               0 :     if (os) {
         142                 :
         143                 :         // Instead of:
         144                 :         //  os->NotifyObservers(this, "memory-pressure", aReason);
         145                 :         // we are going to do this manually to see who/what is
         146                 :         // deallocating.
         147                 :
         148               0 :         nsCOMPtr<nsISimpleEnumerator> e;
         149               0 :         os->EnumerateObservers("memory-pressure", getter_AddRefs(e));
         150                 :
         151               0 :         if ( e ) {
         152               0 :           nsCOMPtr<nsIObserver> observer;
         153               0 :           bool loop = true;
         154                 :
         155               0 :           while (NS_SUCCEEDED(e->HasMoreElements(&loop)) && loop)
         156                 :           {
         157               0 :               e->GetNext(getter_AddRefs(observer));
         158                 :
         159               0 :               if (!observer)
         160               0 :                   continue;
         161                 :
         162               0 :               observer->Observe(observer, "memory-pressure", aReason);
         163                 :           }
         164                 :         }
         165                 :     }
         166                 :
         167               0 :     sIsFlushing = 0;
         168               0 :     return NS_OK;
         169                 : }
         170                 :
         171                 : // XXX need NS_IMPL_STATIC_ADDREF/RELEASE
         172               0 : NS_IMETHODIMP_(nsrefcnt) nsMemoryImpl::FlushEvent::AddRef() { return 2; }
         173               0 : NS_IMETHODIMP_(nsrefcnt) nsMemoryImpl::FlushEvent::Release() { return 1; }
         174               0 : NS_IMPL_QUERY_INTERFACE1(nsMemoryImpl::FlushEvent, nsIRunnable)
         175                 :
         176                 : NS_IMETHODIMP
         177               0 : nsMemoryImpl::FlushEvent::Run()
         178                 : {
         179               0 :     sGlobalMemory.RunFlushers(mReason);
         180               0 :     return NS_OK;
         181                 : }
         182                 :
         183                 : PRInt32
         184                 : nsMemoryImpl::sIsFlushing = 0;
         185                 :
         186                 : PRIntervalTime
         187                 : nsMemoryImpl::sLastFlushTime = 0;
         188                 :
         189                 : nsMemoryImpl::FlushEvent
         190            1396 : nsMemoryImpl::sFlushEvent;
         191                 :
         192                 : XPCOM_API(void*)
         193         9128427 : NS_Alloc(PRSize size)
         194                 : {
         195         9128427 :     return moz_xmalloc(size);
         196                 : }
         197                 :
         198                 : XPCOM_API(void*)
         199           32468 : NS_Realloc(void* ptr, PRSize size)
         200                 : {
         201           32468 :     return moz_xrealloc(ptr, size);
         202                 : }
         203                 :
         204                 : XPCOM_API(void)
         205         9549916 : NS_Free(void* ptr)
         206                 : {
         207         9549916 :     moz_free(ptr);
         208         9549916 : }
         209                 :
         210                 : nsresult
         211           26489 : NS_GetMemoryManager(nsIMemory* *result)
         212                 : {
         213           26489 :     return sGlobalMemory.QueryInterface(NS_GET_IID(nsIMemory), (void**) result);
         214            4188 : }

Generated by: LCOV version 1.7