From 125cde630317132b4cbaa3b2cde8e6f9209475f5 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 12 Jul 2012 23:51:01 -0700 Subject: [PATCH] Bug 749010 (part 2) - Move code around to merge two anonymous namespaces; no functional changes. r=bent. --- dom/workers/WorkerPrivate.cpp | 326 +++++++++++++++++----------------- 1 file changed, 161 insertions(+), 165 deletions(-) diff --git a/dom/workers/WorkerPrivate.cpp b/dom/workers/WorkerPrivate.cpp index ce52787d3e41..1bf906327d85 100644 --- a/dom/workers/WorkerPrivate.cpp +++ b/dom/workers/WorkerPrivate.cpp @@ -74,6 +74,8 @@ USING_WORKERS_NAMESPACE using namespace mozilla::dom::workers::events; using namespace mozilla::dom; +NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(JsWorkerMallocSizeOf, "js-worker") + namespace { const char gErrorChars[] = "error"; @@ -123,171 +125,6 @@ SwapToISupportsArray(SmartPtr& aSrc, dest->swap(rawSupports); } -} /* anonymous namespace */ - -NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(JsWorkerMallocSizeOf, "js-worker") - -struct WorkerJSRuntimeStats : public JS::RuntimeStats -{ - WorkerJSRuntimeStats(nsACString &aRtPath) - : JS::RuntimeStats(JsWorkerMallocSizeOf), mRtPath(aRtPath) { } - - ~WorkerJSRuntimeStats() { - for (size_t i = 0; i != compartmentStatsVector.length(); i++) { - free(compartmentStatsVector[i].extra1); - // no need to free |extra2|, because it's a static string - } - } - - virtual void initExtraCompartmentStats(JSCompartment *c, - JS::CompartmentStats *cstats) MOZ_OVERRIDE - { - MOZ_ASSERT(!cstats->extra1); - MOZ_ASSERT(!cstats->extra2); - - // ReportJSRuntimeExplicitTreeStats expects that cstats->{extra1,extra2} - // are char pointers. - - // This is the |cJSPathPrefix|. Each worker has exactly two compartments: - // one for atoms, and one for everything else. - nsCString cJSPathPrefix(mRtPath); - cJSPathPrefix += js::IsAtomsCompartment(c) - ? NS_LITERAL_CSTRING("compartment(web-worker-atoms)/") - : NS_LITERAL_CSTRING("compartment(web-worker)/"); - cstats->extra1 = strdup(cJSPathPrefix.get()); - - // This is the |cDOMPathPrefix|, which should never be used when reporting - // with workers (hence the "?!"). - cstats->extra2 = (void *)"explicit/workers/?!/"; - } - -private: - nsCString mRtPath; -}; - -BEGIN_WORKERS_NAMESPACE - -class WorkerMemoryReporter MOZ_FINAL : public nsIMemoryMultiReporter -{ - WorkerPrivate* mWorkerPrivate; - nsCString mAddressString; - nsCString mRtPath; - -public: - NS_DECL_ISUPPORTS - - WorkerMemoryReporter(WorkerPrivate* aWorkerPrivate) - : mWorkerPrivate(aWorkerPrivate) - { - aWorkerPrivate->AssertIsOnWorkerThread(); - - nsCString escapedDomain(aWorkerPrivate->Domain()); - escapedDomain.ReplaceChar('/', '\\'); - - NS_ConvertUTF16toUTF8 escapedURL(aWorkerPrivate->ScriptURL()); - escapedURL.ReplaceChar('/', '\\'); - - { - // 64bit address plus '0x' plus null terminator. - char address[21]; - uint32_t addressSize = - JS_snprintf(address, sizeof(address), "%p", aWorkerPrivate); - if (addressSize != uint32_t(-1)) { - mAddressString.Assign(address, addressSize); - } - else { - NS_WARNING("JS_snprintf failed!"); - mAddressString.AssignLiteral(""); - } - } - - mRtPath = NS_LITERAL_CSTRING("explicit/workers/workers(") + - escapedDomain + NS_LITERAL_CSTRING(")/worker(") + - escapedURL + NS_LITERAL_CSTRING(", ") + mAddressString + - NS_LITERAL_CSTRING(")/"); - } - - nsresult - CollectForRuntime(bool aIsQuick, void* aData) - { - AssertIsOnMainThread(); - - if (!mWorkerPrivate) { -#ifdef DEBUG - nsCAutoString message("Unable to report memory for "); - if (mWorkerPrivate->IsChromeWorker()) { - message.AppendLiteral("Chrome"); - } - message += NS_LITERAL_CSTRING("Worker (") + mAddressString + - NS_LITERAL_CSTRING(")! It is either using ctypes or is in " - "the process of being destroyed"); - NS_WARNING(message.get()); -#endif - return NS_OK; - } - - if (!mWorkerPrivate->BlockAndCollectRuntimeStats(aIsQuick, aData)) { - return NS_ERROR_FAILURE; - } - - return NS_OK; - } - - NS_IMETHOD GetName(nsACString &aName) - { - aName.AssignLiteral("workers"); - return NS_OK; - } - - NS_IMETHOD - CollectReports(nsIMemoryMultiReporterCallback* aCallback, - nsISupports* aClosure) - { - AssertIsOnMainThread(); - - WorkerJSRuntimeStats rtStats(mRtPath); - nsresult rv = CollectForRuntime(/* isQuick = */false, &rtStats); - if (NS_FAILED(rv)) { - return rv; - } - - // Always report, even if we're disabled, so that we at least get an entry - // in about::memory. - return xpc::ReportJSRuntimeExplicitTreeStats(rtStats, mRtPath, - aCallback, aClosure); - } - - NS_IMETHOD - GetExplicitNonHeap(PRInt64 *aAmount) - { - AssertIsOnMainThread(); - - return CollectForRuntime(/* isQuick = */true, aAmount); - } - - void Disable() - { -#ifdef DEBUG - // Setting mWorkerPrivate to nsnull is safe only because we've locked the - // worker's mutex on the worker's thread, in the caller. So we check that. - // - // Also, we may have already disabled the reporter (and thus set - // mWorkerPrivate to nsnull) due to the use of CTypes (see - // ChromeWorkerScope.cpp). That's why the NULL check is necessary. - if (mWorkerPrivate) { - mWorkerPrivate->mMutex.AssertCurrentThreadOwns(); - } -#endif - mWorkerPrivate = nsnull; - } -}; - -END_WORKERS_NAMESPACE - -NS_IMPL_THREADSAFE_ISUPPORTS1(WorkerMemoryReporter, nsIMemoryMultiReporter) - -namespace { - struct WorkerStructuredCloneCallbacks { static JSObject* @@ -1629,6 +1466,165 @@ public: } /* anonymous namespace */ +struct WorkerJSRuntimeStats : public JS::RuntimeStats +{ + WorkerJSRuntimeStats(nsACString &aRtPath) + : JS::RuntimeStats(JsWorkerMallocSizeOf), mRtPath(aRtPath) { } + + ~WorkerJSRuntimeStats() { + for (size_t i = 0; i != compartmentStatsVector.length(); i++) { + free(compartmentStatsVector[i].extra1); + // no need to free |extra2|, because it's a static string + } + } + + virtual void initExtraCompartmentStats(JSCompartment *c, + JS::CompartmentStats *cstats) MOZ_OVERRIDE + { + MOZ_ASSERT(!cstats->extra1); + MOZ_ASSERT(!cstats->extra2); + + // ReportJSRuntimeExplicitTreeStats expects that cstats->{extra1,extra2} + // are char pointers. + + // This is the |cJSPathPrefix|. Each worker has exactly two compartments: + // one for atoms, and one for everything else. + nsCString cJSPathPrefix(mRtPath); + cJSPathPrefix += js::IsAtomsCompartment(c) + ? NS_LITERAL_CSTRING("compartment(web-worker-atoms)/") + : NS_LITERAL_CSTRING("compartment(web-worker)/"); + cstats->extra1 = strdup(cJSPathPrefix.get()); + + // This is the |cDOMPathPrefix|, which should never be used when reporting + // with workers (hence the "?!"). + cstats->extra2 = (void *)"explicit/workers/?!/"; + } + +private: + nsCString mRtPath; +}; + +BEGIN_WORKERS_NAMESPACE + +class WorkerMemoryReporter MOZ_FINAL : public nsIMemoryMultiReporter +{ + WorkerPrivate* mWorkerPrivate; + nsCString mAddressString; + nsCString mRtPath; + +public: + NS_DECL_ISUPPORTS + + WorkerMemoryReporter(WorkerPrivate* aWorkerPrivate) + : mWorkerPrivate(aWorkerPrivate) + { + aWorkerPrivate->AssertIsOnWorkerThread(); + + nsCString escapedDomain(aWorkerPrivate->Domain()); + escapedDomain.ReplaceChar('/', '\\'); + + NS_ConvertUTF16toUTF8 escapedURL(aWorkerPrivate->ScriptURL()); + escapedURL.ReplaceChar('/', '\\'); + + { + // 64bit address plus '0x' plus null terminator. + char address[21]; + uint32_t addressSize = + JS_snprintf(address, sizeof(address), "%p", aWorkerPrivate); + if (addressSize != uint32_t(-1)) { + mAddressString.Assign(address, addressSize); + } + else { + NS_WARNING("JS_snprintf failed!"); + mAddressString.AssignLiteral(""); + } + } + + mRtPath = NS_LITERAL_CSTRING("explicit/workers/workers(") + + escapedDomain + NS_LITERAL_CSTRING(")/worker(") + + escapedURL + NS_LITERAL_CSTRING(", ") + mAddressString + + NS_LITERAL_CSTRING(")/"); + } + + nsresult + CollectForRuntime(bool aIsQuick, void* aData) + { + AssertIsOnMainThread(); + + if (!mWorkerPrivate) { +#ifdef DEBUG + nsCAutoString message("Unable to report memory for "); + if (mWorkerPrivate->IsChromeWorker()) { + message.AppendLiteral("Chrome"); + } + message += NS_LITERAL_CSTRING("Worker (") + mAddressString + + NS_LITERAL_CSTRING(")! It is either using ctypes or is in " + "the process of being destroyed"); + NS_WARNING(message.get()); +#endif + return NS_OK; + } + + if (!mWorkerPrivate->BlockAndCollectRuntimeStats(aIsQuick, aData)) { + return NS_ERROR_FAILURE; + } + + return NS_OK; + } + + NS_IMETHOD GetName(nsACString &aName) + { + aName.AssignLiteral("workers"); + return NS_OK; + } + + NS_IMETHOD + CollectReports(nsIMemoryMultiReporterCallback* aCallback, + nsISupports* aClosure) + { + AssertIsOnMainThread(); + + WorkerJSRuntimeStats rtStats(mRtPath); + nsresult rv = CollectForRuntime(/* isQuick = */false, &rtStats); + if (NS_FAILED(rv)) { + return rv; + } + + // Always report, even if we're disabled, so that we at least get an entry + // in about::memory. + return xpc::ReportJSRuntimeExplicitTreeStats(rtStats, mRtPath, + aCallback, aClosure); + } + + NS_IMETHOD + GetExplicitNonHeap(PRInt64 *aAmount) + { + AssertIsOnMainThread(); + + return CollectForRuntime(/* isQuick = */true, aAmount); + } + + void Disable() + { +#ifdef DEBUG + // Setting mWorkerPrivate to nsnull is safe only because we've locked the + // worker's mutex on the worker's thread, in the caller. So we check that. + // + // Also, we may have already disabled the reporter (and thus set + // mWorkerPrivate to nsnull) due to the use of CTypes (see + // ChromeWorkerScope.cpp). That's why the NULL check is necessary. + if (mWorkerPrivate) { + mWorkerPrivate->mMutex.AssertCurrentThreadOwns(); + } +#endif + mWorkerPrivate = nsnull; + } +}; + +END_WORKERS_NAMESPACE + +NS_IMPL_THREADSAFE_ISUPPORTS1(WorkerMemoryReporter, nsIMemoryMultiReporter) + #ifdef DEBUG void mozilla::dom::workers::AssertIsOnMainThread()