xpcom/base/nsMemoryImpl.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsMemoryImpl.h"
michael@0 7 #include "nsThreadUtils.h"
michael@0 8
michael@0 9 #include "nsIObserver.h"
michael@0 10 #include "nsIObserverService.h"
michael@0 11 #include "nsISimpleEnumerator.h"
michael@0 12
michael@0 13 #include "nsCOMPtr.h"
michael@0 14 #include "mozilla/Services.h"
michael@0 15
michael@0 16 #ifdef ANDROID
michael@0 17 #include <stdio.h>
michael@0 18
michael@0 19 // Minimum memory threshold for a device to be considered
michael@0 20 // a low memory platform. This value has be in sync with
michael@0 21 // Java's equivalent threshold, defined in
michael@0 22 // mobile/android/base/util/HardwareUtils.java
michael@0 23 #define LOW_MEMORY_THRESHOLD_KB (384 * 1024)
michael@0 24 #endif
michael@0 25
michael@0 26 static nsMemoryImpl sGlobalMemory;
michael@0 27
michael@0 28 NS_IMPL_QUERY_INTERFACE(nsMemoryImpl, nsIMemory)
michael@0 29
michael@0 30 NS_IMETHODIMP_(void*)
michael@0 31 nsMemoryImpl::Alloc(size_t size)
michael@0 32 {
michael@0 33 return NS_Alloc(size);
michael@0 34 }
michael@0 35
michael@0 36 NS_IMETHODIMP_(void*)
michael@0 37 nsMemoryImpl::Realloc(void* ptr, size_t size)
michael@0 38 {
michael@0 39 return NS_Realloc(ptr, size);
michael@0 40 }
michael@0 41
michael@0 42 NS_IMETHODIMP_(void)
michael@0 43 nsMemoryImpl::Free(void* ptr)
michael@0 44 {
michael@0 45 NS_Free(ptr);
michael@0 46 }
michael@0 47
michael@0 48 NS_IMETHODIMP
michael@0 49 nsMemoryImpl::HeapMinimize(bool aImmediate)
michael@0 50 {
michael@0 51 return FlushMemory(MOZ_UTF16("heap-minimize"), aImmediate);
michael@0 52 }
michael@0 53
michael@0 54 NS_IMETHODIMP
michael@0 55 nsMemoryImpl::IsLowMemory(bool *result)
michael@0 56 {
michael@0 57 NS_ERROR("IsLowMemory is deprecated. See bug 592308.");
michael@0 58 *result = false;
michael@0 59 return NS_OK;
michael@0 60 }
michael@0 61
michael@0 62 NS_IMETHODIMP
michael@0 63 nsMemoryImpl::IsLowMemoryPlatform(bool *result)
michael@0 64 {
michael@0 65 #ifdef ANDROID
michael@0 66 static int sLowMemory = -1; // initialize to unknown, lazily evaluate to 0 or 1
michael@0 67 if (sLowMemory == -1) {
michael@0 68 sLowMemory = 0; // assume "not low memory" in case file operations fail
michael@0 69 *result = false;
michael@0 70
michael@0 71 // check if MemTotal from /proc/meminfo is less than LOW_MEMORY_THRESHOLD_KB
michael@0 72 FILE* fd = fopen("/proc/meminfo", "r");
michael@0 73 if (!fd) {
michael@0 74 return NS_OK;
michael@0 75 }
michael@0 76 uint64_t mem = 0;
michael@0 77 int rv = fscanf(fd, "MemTotal: %llu kB", &mem);
michael@0 78 if (fclose(fd)) {
michael@0 79 return NS_OK;
michael@0 80 }
michael@0 81 if (rv != 1) {
michael@0 82 return NS_OK;
michael@0 83 }
michael@0 84 sLowMemory = (mem < LOW_MEMORY_THRESHOLD_KB) ? 1 : 0;
michael@0 85 }
michael@0 86 *result = (sLowMemory == 1);
michael@0 87 #else
michael@0 88 *result = false;
michael@0 89 #endif
michael@0 90 return NS_OK;
michael@0 91 }
michael@0 92
michael@0 93 /*static*/ nsresult
michael@0 94 nsMemoryImpl::Create(nsISupports* outer, const nsIID& aIID, void **aResult)
michael@0 95 {
michael@0 96 if (NS_WARN_IF(outer))
michael@0 97 return NS_ERROR_NO_AGGREGATION;
michael@0 98 return sGlobalMemory.QueryInterface(aIID, aResult);
michael@0 99 }
michael@0 100
michael@0 101 nsresult
michael@0 102 nsMemoryImpl::FlushMemory(const char16_t* aReason, bool aImmediate)
michael@0 103 {
michael@0 104 nsresult rv = NS_OK;
michael@0 105
michael@0 106 if (aImmediate) {
michael@0 107 // They've asked us to run the flusher *immediately*. We've
michael@0 108 // got to be on the UI main thread for us to be able to do
michael@0 109 // that...are we?
michael@0 110 if (!NS_IsMainThread()) {
michael@0 111 NS_ERROR("can't synchronously flush memory: not on UI thread");
michael@0 112 return NS_ERROR_FAILURE;
michael@0 113 }
michael@0 114 }
michael@0 115
michael@0 116 bool lastVal = sIsFlushing.exchange(true);
michael@0 117 if (lastVal)
michael@0 118 return NS_OK;
michael@0 119
michael@0 120 PRIntervalTime now = PR_IntervalNow();
michael@0 121
michael@0 122 // Run the flushers immediately if we can; otherwise, proxy to the
michael@0 123 // UI thread an run 'em asynchronously.
michael@0 124 if (aImmediate) {
michael@0 125 rv = RunFlushers(aReason);
michael@0 126 }
michael@0 127 else {
michael@0 128 // Don't broadcast more than once every 1000ms to avoid being noisy
michael@0 129 if (PR_IntervalToMicroseconds(now - sLastFlushTime) > 1000) {
michael@0 130 sFlushEvent.mReason = aReason;
michael@0 131 rv = NS_DispatchToMainThread(&sFlushEvent, NS_DISPATCH_NORMAL);
michael@0 132 }
michael@0 133 }
michael@0 134
michael@0 135 sLastFlushTime = now;
michael@0 136 return rv;
michael@0 137 }
michael@0 138
michael@0 139 nsresult
michael@0 140 nsMemoryImpl::RunFlushers(const char16_t* aReason)
michael@0 141 {
michael@0 142 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
michael@0 143 if (os) {
michael@0 144
michael@0 145 // Instead of:
michael@0 146 // os->NotifyObservers(this, "memory-pressure", aReason);
michael@0 147 // we are going to do this manually to see who/what is
michael@0 148 // deallocating.
michael@0 149
michael@0 150 nsCOMPtr<nsISimpleEnumerator> e;
michael@0 151 os->EnumerateObservers("memory-pressure", getter_AddRefs(e));
michael@0 152
michael@0 153 if ( e ) {
michael@0 154 nsCOMPtr<nsIObserver> observer;
michael@0 155 bool loop = true;
michael@0 156
michael@0 157 while (NS_SUCCEEDED(e->HasMoreElements(&loop)) && loop)
michael@0 158 {
michael@0 159 nsCOMPtr<nsISupports> supports;
michael@0 160 e->GetNext(getter_AddRefs(supports));
michael@0 161
michael@0 162 if (!supports)
michael@0 163 continue;
michael@0 164
michael@0 165 observer = do_QueryInterface(supports);
michael@0 166 observer->Observe(observer, "memory-pressure", aReason);
michael@0 167 }
michael@0 168 }
michael@0 169 }
michael@0 170
michael@0 171 sIsFlushing = false;
michael@0 172 return NS_OK;
michael@0 173 }
michael@0 174
michael@0 175 // XXX need NS_IMPL_STATIC_ADDREF/RELEASE
michael@0 176 NS_IMETHODIMP_(MozExternalRefCountType) nsMemoryImpl::FlushEvent::AddRef() { return 2; }
michael@0 177 NS_IMETHODIMP_(MozExternalRefCountType) nsMemoryImpl::FlushEvent::Release() { return 1; }
michael@0 178 NS_IMPL_QUERY_INTERFACE(nsMemoryImpl::FlushEvent, nsIRunnable)
michael@0 179
michael@0 180 NS_IMETHODIMP
michael@0 181 nsMemoryImpl::FlushEvent::Run()
michael@0 182 {
michael@0 183 sGlobalMemory.RunFlushers(mReason);
michael@0 184 return NS_OK;
michael@0 185 }
michael@0 186
michael@0 187 mozilla::Atomic<bool>
michael@0 188 nsMemoryImpl::sIsFlushing;
michael@0 189
michael@0 190 PRIntervalTime
michael@0 191 nsMemoryImpl::sLastFlushTime = 0;
michael@0 192
michael@0 193 nsMemoryImpl::FlushEvent
michael@0 194 nsMemoryImpl::sFlushEvent;
michael@0 195
michael@0 196 XPCOM_API(void*)
michael@0 197 NS_Alloc(size_t size)
michael@0 198 {
michael@0 199 return moz_xmalloc(size);
michael@0 200 }
michael@0 201
michael@0 202 XPCOM_API(void*)
michael@0 203 NS_Realloc(void* ptr, size_t size)
michael@0 204 {
michael@0 205 return moz_xrealloc(ptr, size);
michael@0 206 }
michael@0 207
michael@0 208 XPCOM_API(void)
michael@0 209 NS_Free(void* ptr)
michael@0 210 {
michael@0 211 moz_free(ptr);
michael@0 212 }
michael@0 213
michael@0 214 nsresult
michael@0 215 NS_GetMemoryManager(nsIMemory* *result)
michael@0 216 {
michael@0 217 return sGlobalMemory.QueryInterface(NS_GET_IID(nsIMemory), (void**) result);
michael@0 218 }

mercurial