From fc930ad16f70997bd7bb06e97ef3e74ca88153d7 Mon Sep 17 00:00:00 2001 From: Denis Palmeiro Date: Tue, 23 Oct 2018 20:46:31 +0000 Subject: [PATCH] Bug 1491909 - Add an AllocPolicy to mozilla::JSONWriter r=jwalden,mstange JSONWriter currently calls new and delete indirectly through mozilla::MakeUnique to allocate a buffer. Becuase of this, the methods of this class cannot be invoked within Spidermonkey due to https://searchfox.org/mozilla-central/source/config/check_vanilla_allocations.py#6-14. Therefore, JSONWriter needs an AllocPolicy template parameter so that the allocation and deallocation routines can be changed to match the JS AllocPolicy when invoked within SpiderMonkey. Differential Revision: https://phabricator.services.mozilla.com/D7279 --HG-- extra : moz-landing-system : lando --- dom/base/ContentBlockingLog.h | 2 +- dom/media/doctor/DDLogValue.cpp | 4 +-- dom/media/doctor/DDLogValue.h | 4 ++- dom/media/doctor/DDMediaLogs.cpp | 6 ++-- dom/notification/Notification.cpp | 2 +- ipc/mscom/RegistrationAnnotator.cpp | 30 +++++++++---------- js/public/AllocPolicy.h | 4 +++ memory/replace/dmd/DMD.cpp | 4 +-- mfbt/JSONWriter.h | 18 +++++++++-- mfbt/tests/TestJSONWriter.cpp | 12 ++++---- toolkit/components/places/Database.cpp | 4 +-- .../telemetry/core/TelemetryHistogram.cpp | 6 ++-- .../telemetry/core/TelemetryHistogram.h | 7 +++-- .../telemetry/core/TelemetryScalar.cpp | 6 ++-- .../telemetry/core/TelemetryScalar.h | 6 ++-- .../TelemetryGeckoViewPersistence.cpp | 2 +- tools/profiler/core/ProfileBufferEntry.cpp | 4 +-- tools/profiler/core/ProfileBufferEntry.h | 4 +-- tools/profiler/core/platform.cpp | 8 ++--- tools/profiler/core/platform.h | 5 +++- tools/profiler/gecko/nsProfiler.cpp | 2 +- tools/profiler/public/ProfileJSONWriter.h | 9 ++++-- xpcom/base/nsMacPreferencesReader.mm | 8 ++--- xpcom/base/nsMemoryInfoDumper.cpp | 6 ++-- 24 files changed, 97 insertions(+), 66 deletions(-) diff --git a/dom/base/ContentBlockingLog.h b/dom/base/ContentBlockingLog.h index 7ec955964ed9..cec6e0d7282c 100644 --- a/dom/base/ContentBlockingLog.h +++ b/dom/base/ContentBlockingLog.h @@ -85,7 +85,7 @@ public: { nsAutoString buffer; - JSONWriter w(MakeUnique(buffer)); + JSONWriter<> w(MakeUnique(buffer)); w.Start(); for (auto iter = mLog.Iter(); !iter.Done(); iter.Next()) { diff --git a/dom/media/doctor/DDLogValue.cpp b/dom/media/doctor/DDLogValue.cpp index bdc732b53ffd..0280a8a9e9cb 100644 --- a/dom/media/doctor/DDLogValue.cpp +++ b/dom/media/doctor/DDLogValue.cpp @@ -85,7 +85,7 @@ AppendToString(const DDLogValue& aValue, nsCString& aString) struct LogValueMatcherJson { - JSONWriter& mJW; + JSONWriter<>& mJW; const char* mPropertyName; void match(const DDNoValue&) const { mJW.NullProperty(mPropertyName); } @@ -137,7 +137,7 @@ struct LogValueMatcherJson void ToJSON(const DDLogValue& aValue, - JSONWriter& aJSONWriter, + JSONWriter<>& aJSONWriter, const char* aPropertyName) { aValue.match(LogValueMatcherJson{ aJSONWriter, aPropertyName }); diff --git a/dom/media/doctor/DDLogValue.h b/dom/media/doctor/DDLogValue.h index 4ee840fcf758..bc646eae19bb 100644 --- a/dom/media/doctor/DDLogValue.h +++ b/dom/media/doctor/DDLogValue.h @@ -54,10 +54,12 @@ using DDLogValue = Variant class JSONWriter; +class MallocAllocPolicy; void ToJSON(const DDLogValue& aValue, - JSONWriter& aJSONWriter, + JSONWriter& aJSONWriter, const char* aPropertyName); } // namespace mozilla diff --git a/dom/media/doctor/DDMediaLogs.cpp b/dom/media/doctor/DDMediaLogs.cpp index bfa36041d167..4e116bac7b96 100644 --- a/dom/media/doctor/DDMediaLogs.cpp +++ b/dom/media/doctor/DDMediaLogs.cpp @@ -462,11 +462,11 @@ DDMediaLogs::FulfillPromises() } nsCString json; - JSONWriter jw{ MakeUnique(json) }; + JSONWriter<> jw{ MakeUnique(json) }; jw.Start(); jw.StartArrayProperty("messages"); for (const DDLogMessage& message : log->mMessages) { - jw.StartObjectElement(JSONWriter::SingleLineStyle); + jw.StartObjectElement(JSONWriter<>::SingleLineStyle); jw.IntProperty("i", message.mIndex.Value()); jw.DoubleProperty("ts", ToSeconds(message.mTimeStamp)); DDLifetime* lifetime = @@ -505,7 +505,7 @@ DDMediaLogs::FulfillPromises() mediaElement, [&](const DDLifetime& lifetime) { jw.StartObjectProperty(nsPrintfCString("%" PRIi32, lifetime.mTag).get(), - JSONWriter::SingleLineStyle); + JSONWriter<>::SingleLineStyle); jw.IntProperty("tag", lifetime.mTag); jw.StringProperty("cls", lifetime.mObject.TypeName()); jw.StringProperty( diff --git a/dom/notification/Notification.cpp b/dom/notification/Notification.cpp index dd24bd1dc537..209107b94281 100644 --- a/dom/notification/Notification.cpp +++ b/dom/notification/Notification.cpp @@ -1742,7 +1742,7 @@ Notification::ShowInternal() if (isPersistent) { nsAutoString persistentData; - JSONWriter w(MakeUnique(persistentData)); + JSONWriter<> w(MakeUnique(persistentData)); w.Start(); nsAutoString origin; diff --git a/ipc/mscom/RegistrationAnnotator.cpp b/ipc/mscom/RegistrationAnnotator.cpp index 31c25604751c..0bca63ced8eb 100644 --- a/ipc/mscom/RegistrationAnnotator.cpp +++ b/ipc/mscom/RegistrationAnnotator.cpp @@ -141,9 +141,9 @@ GetLoadedPath(nsAString& aPath) } static void -AnnotateClsidRegistrationForHive(JSONWriter& aJson, HKEY aHive, +AnnotateClsidRegistrationForHive(JSONWriter<>& aJson, HKEY aHive, const nsAString& aClsid, - const JSONWriter::CollectionStyle aStyle) + const JSONWriter<>::CollectionStyle aStyle) { nsAutoString clsidSubkey; clsidSubkey.AppendLiteral(kSoftwareClasses); @@ -193,7 +193,7 @@ AnnotateClsidRegistrationForHive(JSONWriter& aJson, HKEY aHive, } static void -CheckTlbPath(JSONWriter& aJson, const nsAString& aTypelibPath) +CheckTlbPath(JSONWriter<>& aJson, const nsAString& aTypelibPath) { const nsString& flatPath = PromiseFlatString(aTypelibPath); DWORD bufCharLen = ExpandEnvironmentStrings(flatPath.get(), nullptr, 0); @@ -214,10 +214,10 @@ CheckTlbPath(JSONWriter& aJson, const nsAString& aTypelibPath) template static void -AnnotateTypelibPlatform(JSONWriter& aJson, HKEY aBaseKey, +AnnotateTypelibPlatform(JSONWriter<>& aJson, HKEY aBaseKey, const nsAString& aLcidSubkey, const char16_t (&aPlatform)[N], - const JSONWriter::CollectionStyle aStyle) + const JSONWriter<>::CollectionStyle aStyle) { nsLiteralString platform(aPlatform); @@ -235,10 +235,10 @@ AnnotateTypelibPlatform(JSONWriter& aJson, HKEY aBaseKey, } static void -AnnotateTypelibRegistrationForHive(JSONWriter& aJson, HKEY aHive, +AnnotateTypelibRegistrationForHive(JSONWriter<>& aJson, HKEY aHive, const nsAString& aTypelibId, const nsAString& aTypelibVersion, - const JSONWriter::CollectionStyle aStyle) + const JSONWriter<>::CollectionStyle aStyle) { nsAutoString typelibSubKey; typelibSubKey.AppendLiteral(kSoftwareClasses); @@ -293,8 +293,8 @@ AnnotateTypelibRegistrationForHive(JSONWriter& aJson, HKEY aHive, } static void -AnnotateInterfaceRegistrationForHive(JSONWriter& aJson, HKEY aHive, REFIID aIid, - const JSONWriter::CollectionStyle aStyle) +AnnotateInterfaceRegistrationForHive(JSONWriter<>& aJson, HKEY aHive, REFIID aIid, + const JSONWriter<>::CollectionStyle aStyle) { nsAutoString interfaceSubKey; interfaceSubKey.AppendLiteral(kSoftwareClasses); @@ -357,12 +357,12 @@ void AnnotateInterfaceRegistration(REFIID aIid) { #if defined(DEBUG) - const JSONWriter::CollectionStyle style = JSONWriter::MultiLineStyle; + const JSONWriter<>::CollectionStyle style = JSONWriter<>::MultiLineStyle; #else - const JSONWriter::CollectionStyle style = JSONWriter::SingleLineStyle; + const JSONWriter<>::CollectionStyle style = JSONWriter<>::SingleLineStyle; #endif - JSONWriter json(MakeUnique()); + JSONWriter<> json(MakeUnique()); json.Start(style); @@ -391,15 +391,15 @@ void AnnotateClassRegistration(REFCLSID aClsid) { #if defined(DEBUG) - const JSONWriter::CollectionStyle style = JSONWriter::MultiLineStyle; + const JSONWriter<>::CollectionStyle style = JSONWriter<>::MultiLineStyle; #else - const JSONWriter::CollectionStyle style = JSONWriter::SingleLineStyle; + const JSONWriter<>::CollectionStyle style = JSONWriter<>::SingleLineStyle; #endif nsAutoString strClsid; GUIDToString(aClsid, strClsid); - JSONWriter json(MakeUnique()); + JSONWriter<> json(MakeUnique()); json.Start(style); diff --git a/js/public/AllocPolicy.h b/js/public/AllocPolicy.h index 7811ac69f2a0..6a851775907c 100644 --- a/js/public/AllocPolicy.h +++ b/js/public/AllocPolicy.h @@ -134,4 +134,8 @@ class TempAllocPolicy : public AllocPolicyBase } /* namespace js */ +namespace JS { +using SystemAllocPolicy = js::SystemAllocPolicy; +} /* namespace JS */ + #endif /* js_AllocPolicy_h */ diff --git a/memory/replace/dmd/DMD.cpp b/memory/replace/dmd/DMD.cpp index 28927f373d7b..3d9da52445c8 100644 --- a/memory/replace/dmd/DMD.cpp +++ b/memory/replace/dmd/DMD.cpp @@ -1789,7 +1789,7 @@ private: }; static void -WriteBlockContents(JSONWriter& aWriter, const LiveBlock& aBlock) +WriteBlockContents(JSONWriter<>& aWriter, const LiveBlock& aBlock) { size_t numWords = aBlock.ReqSize() / sizeof(uintptr_t*); if (numWords == 0) { @@ -1817,7 +1817,7 @@ AnalyzeImpl(UniquePtr aWriter) // Therefore, this declaration must precede the AutoBlockIntercepts // declaration, to ensure that |write| is destroyed *after* intercepts are // unblocked. - JSONWriter writer(std::move(aWriter)); + JSONWriter<> writer(std::move(aWriter)); AutoBlockIntercepts block(Thread::Fetch()); AutoLockState lock; diff --git a/mfbt/JSONWriter.h b/mfbt/JSONWriter.h index a2426c312c4b..b74708ad7de8 100644 --- a/mfbt/JSONWriter.h +++ b/mfbt/JSONWriter.h @@ -120,6 +120,7 @@ namespace detail { extern MFBT_DATA const char gTwoCharEscapes[256]; } // namespace detail +template class JSONWriter { // From http://www.ietf.org/rfc/rfc4627.txt: @@ -142,7 +143,15 @@ class JSONWriter // wouldn't work with UniquePtr. bool mIsOwned; const char* mUnownedStr; - UniquePtr mOwnedStr; + + struct FreePolicy { + AllocPolicy mAllocPolicy; + void operator() (void* p) { + mAllocPolicy.free_(p); + } + }; + + UniquePtr mOwnedStr; void SanityCheck() const { @@ -156,6 +165,10 @@ class JSONWriter return u < 10 ? '0' + u : 'a' + (u - 10); } + AllocPolicy& allocPolicy() { + return mOwnedStr.get_deleter().mAllocPolicy; + } + public: explicit EscapedString(const char* aStr) : mUnownedStr(nullptr) @@ -189,7 +202,8 @@ class JSONWriter // Escapes are needed. We'll create a new string. mIsOwned = true; size_t len = (p - aStr) + nExtra; - mOwnedStr = MakeUnique(len + 1); + UniquePtr buffer(allocPolicy().template pod_malloc(len + 1)); + mOwnedStr = std::move(buffer); p = aStr; size_t i = 0; diff --git a/mfbt/tests/TestJSONWriter.cpp b/mfbt/tests/TestJSONWriter.cpp index cdf9920808bd..662bc58b15cb 100644 --- a/mfbt/tests/TestJSONWriter.cpp +++ b/mfbt/tests/TestJSONWriter.cpp @@ -101,7 +101,7 @@ void TestBasicProperties() }\n\ "; - JSONWriter w(MakeUnique()); + JSONWriter<> w(MakeUnique()); w.Start(); { @@ -260,7 +260,7 @@ void TestBasicElements() }\n\ "; - JSONWriter w(MakeUnique()); + JSONWriter<> w(MakeUnique()); w.Start(); w.StartArrayProperty("array"); @@ -376,7 +376,7 @@ void TestOneLineObject() {\"i\": 1, \"array\": [null, [{}], {\"o\": {}}, \"s\"], \"d\": 3.33}\n\ "; - JSONWriter w(MakeUnique()); + JSONWriter<> w(MakeUnique()); w.Start(w.SingleLineStyle); @@ -430,7 +430,7 @@ void TestStringEscaping() }\n\ "; - JSONWriter w(MakeUnique()); + JSONWriter<> w(MakeUnique()); // Test the string escaping behaviour. w.Start(); @@ -508,7 +508,7 @@ void TestDeepNesting() }\n\ "; - JSONWriter w(MakeUnique()); + JSONWriter<> w(MakeUnique()); w.Start(); { @@ -533,7 +533,7 @@ void TestEscapedPropertyNames() {\"i\\t\": 1, \"array\\t\": [null, [{}], {\"o\\t\": {}}, \"s\"], \"d\\t\": 3.33}\n\ "; - JSONWriter w(MakeUnique()); + JSONWriter<> w(MakeUnique()); w.Start(w.SingleLineStyle); diff --git a/toolkit/components/places/Database.cpp b/toolkit/components/places/Database.cpp index 4d35ed0df1eb..0cf031a23701 100644 --- a/toolkit/components/places/Database.cpp +++ b/toolkit/components/places/Database.cpp @@ -2584,8 +2584,8 @@ Database::MigrateV51Up() NS_ENSURE_SUCCESS(rv, rv); nsAutoCString json; - JSONWriter jw{ MakeUnique(json) }; - jw.StartArrayProperty(nullptr, JSONWriter::SingleLineStyle); + JSONWriter<> jw{ MakeUnique(json) }; + jw.StartArrayProperty(nullptr, JSONWriter<>::SingleLineStyle); bool hasAtLeastOne = false; bool hasMore = false; diff --git a/toolkit/components/telemetry/core/TelemetryHistogram.cpp b/toolkit/components/telemetry/core/TelemetryHistogram.cpp index 309701ef948a..7217b803a48e 100644 --- a/toolkit/components/telemetry/core/TelemetryHistogram.cpp +++ b/toolkit/components/telemetry/core/TelemetryHistogram.cpp @@ -2638,7 +2638,7 @@ namespace { */ void internal_ReflectHistogramToJSON(const HistogramSnapshotData& aSnapshot, - mozilla::JSONWriter& aWriter) + mozilla::JSONWriter<>& aWriter) { aWriter.IntProperty("sum", aSnapshot.mSampleSum); @@ -2765,7 +2765,7 @@ internal_ParseHistogramData(JSContext* aCx, JS::HandleId aEntryId, // PUBLIC: GeckoView serialization/deserialization functions. nsresult -TelemetryHistogram::SerializeHistograms(mozilla::JSONWriter& aWriter) +TelemetryHistogram::SerializeHistograms(mozilla::JSONWriter<>& aWriter) { MOZ_ASSERT(XRE_IsParentProcess(), "Only save histograms in the parent process"); if (!XRE_IsParentProcess()) { @@ -2812,7 +2812,7 @@ TelemetryHistogram::SerializeHistograms(mozilla::JSONWriter& aWriter) } nsresult -TelemetryHistogram::SerializeKeyedHistograms(mozilla::JSONWriter& aWriter) +TelemetryHistogram::SerializeKeyedHistograms(mozilla::JSONWriter<>& aWriter) { MOZ_ASSERT(XRE_IsParentProcess(), "Only save keyed histograms in the parent process"); if (!XRE_IsParentProcess()) { diff --git a/toolkit/components/telemetry/core/TelemetryHistogram.h b/toolkit/components/telemetry/core/TelemetryHistogram.h index f8f140348858..4050084405f5 100644 --- a/toolkit/components/telemetry/core/TelemetryHistogram.h +++ b/toolkit/components/telemetry/core/TelemetryHistogram.h @@ -13,9 +13,10 @@ namespace mozilla{ // This is only used for the GeckoView persistence. +template class JSONWriter; +class MallocAllocPolicy; } - // This module is internal to Telemetry. It encapsulates Telemetry's // histogram accumulation and storage logic. It should only be used by // Telemetry.cpp. These functions should not be used anywhere else. @@ -81,8 +82,8 @@ GetHistogramSizesOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf); // These functions are only meant to be used for GeckoView persistence. // They are responsible for updating in-memory probes with the data persisted // on the disk and vice-versa. -nsresult SerializeHistograms(mozilla::JSONWriter &aWriter); -nsresult SerializeKeyedHistograms(mozilla::JSONWriter &aWriter); +nsresult SerializeHistograms(mozilla::JSONWriter& aWriter); +nsresult SerializeKeyedHistograms(mozilla::JSONWriter& aWriter); nsresult DeserializeHistograms(JSContext* aCx, JS::HandleValue aData); nsresult DeserializeKeyedHistograms(JSContext* aCx, JS::HandleValue aData); diff --git a/toolkit/components/telemetry/core/TelemetryScalar.cpp b/toolkit/components/telemetry/core/TelemetryScalar.cpp index ce01fe5ce0d8..d1aa07957a82 100644 --- a/toolkit/components/telemetry/core/TelemetryScalar.cpp +++ b/toolkit/components/telemetry/core/TelemetryScalar.cpp @@ -272,7 +272,7 @@ GetVariantFromIVariant(nsIVariant* aInput, uint32_t aScalarKind, */ nsresult WriteVariantToJSONWriter(uint32_t aScalarType, nsIVariant* aInputValue, - const char* aPropertyName, mozilla::JSONWriter& aWriter) + const char* aPropertyName, mozilla::JSONWriter<>& aWriter) { MOZ_ASSERT(aInputValue); @@ -3480,7 +3480,7 @@ TelemetryScalar::AddDynamicScalarDefinitions( * @returns NS_OK or a failure value explaining why persistence failed. */ nsresult -TelemetryScalar::SerializeScalars(mozilla::JSONWriter& aWriter) +TelemetryScalar::SerializeScalars(mozilla::JSONWriter<>& aWriter) { // Get a copy of the data, without clearing. ScalarSnapshotTable scalarsToReflect; @@ -3531,7 +3531,7 @@ TelemetryScalar::SerializeScalars(mozilla::JSONWriter& aWriter) * @returns NS_OK or a failure value explaining why persistence failed. */ nsresult -TelemetryScalar::SerializeKeyedScalars(mozilla::JSONWriter& aWriter) +TelemetryScalar::SerializeKeyedScalars(mozilla::JSONWriter<>& aWriter) { // Get a copy of the data, without clearing. KeyedScalarSnapshotTable keyedScalarsToReflect; diff --git a/toolkit/components/telemetry/core/TelemetryScalar.h b/toolkit/components/telemetry/core/TelemetryScalar.h index 283c2a2a703e..4bb18161c3f3 100644 --- a/toolkit/components/telemetry/core/TelemetryScalar.h +++ b/toolkit/components/telemetry/core/TelemetryScalar.h @@ -18,7 +18,9 @@ namespace mozilla { // This is only used for the GeckoView persistence. +template class JSONWriter; +class MallocAllocPolicy; namespace Telemetry { struct ScalarAction; struct KeyedScalarAction; @@ -94,8 +96,8 @@ void AddDynamicScalarDefinitions(const nsTArray& aWriter); +nsresult SerializeKeyedScalars(mozilla::JSONWriter& aWriter); nsresult DeserializePersistedScalars(JSContext* aCx, JS::HandleValue aData); nsresult DeserializePersistedKeyedScalars(JSContext* aCx, JS::HandleValue aData); // Mark deserialization as in progress. diff --git a/toolkit/components/telemetry/geckoview/TelemetryGeckoViewPersistence.cpp b/toolkit/components/telemetry/geckoview/TelemetryGeckoViewPersistence.cpp index 8acfe239110c..18b59219858d 100644 --- a/toolkit/components/telemetry/geckoview/TelemetryGeckoViewPersistence.cpp +++ b/toolkit/components/telemetry/geckoview/TelemetryGeckoViewPersistence.cpp @@ -382,7 +382,7 @@ PersistenceThreadPersist() } // Build the JSON structure: give up the ownership of jsonWriter. - mozilla::JSONWriter w(std::move(jsonWriter)); + mozilla::JSONWriter<> w(std::move(jsonWriter)); w.Start(); w.StartObjectProperty("scalars"); diff --git a/tools/profiler/core/ProfileBufferEntry.cpp b/tools/profiler/core/ProfileBufferEntry.cpp index 6b1249fa04ed..ad775c5a70d5 100644 --- a/tools/profiler/core/ProfileBufferEntry.cpp +++ b/tools/profiler/core/ProfileBufferEntry.cpp @@ -85,11 +85,11 @@ ProfileBufferEntry::ProfileBufferEntry(Kind aKind, uint64_t aUint64) class JSONSchemaWriter { - JSONWriter& mWriter; + ProfilerJSONWriter& mWriter; uint32_t mIndex; public: - explicit JSONSchemaWriter(JSONWriter& aWriter) + explicit JSONSchemaWriter(ProfilerJSONWriter& aWriter) : mWriter(aWriter) , mIndex(0) { diff --git a/tools/profiler/core/ProfileBufferEntry.h b/tools/profiler/core/ProfileBufferEntry.h index 3bfef91c8799..05ea9de3098c 100644 --- a/tools/profiler/core/ProfileBufferEntry.h +++ b/tools/profiler/core/ProfileBufferEntry.h @@ -140,11 +140,11 @@ public: aWriter.TakeAndSplice(mStringTableWriter.WriteFunc()); } - void WriteProperty(mozilla::JSONWriter& aWriter, const char* aName, const char* aStr) { + void WriteProperty(ProfilerJSONWriter& aWriter, const char* aName, const char* aStr) { aWriter.IntProperty(aName, GetOrAddIndex(aStr)); } - void WriteElement(mozilla::JSONWriter& aWriter, const char* aStr) { + void WriteElement(ProfilerJSONWriter& aWriter, const char* aStr) { aWriter.IntElement(GetOrAddIndex(aStr)); } diff --git a/tools/profiler/core/platform.cpp b/tools/profiler/core/platform.cpp index e61eded9e72c..caca29cc1de7 100644 --- a/tools/profiler/core/platform.cpp +++ b/tools/profiler/core/platform.cpp @@ -1567,7 +1567,7 @@ SafeJSInteger(uint64_t aValue) { } static void -AddSharedLibraryInfoToStream(JSONWriter& aWriter, const SharedLibrary& aLib) +AddSharedLibraryInfoToStream(ProfilerJSONWriter& aWriter, const SharedLibrary& aLib) { aWriter.StartObjectElement(); aWriter.IntProperty("start", SafeJSInteger(aLib.GetStart())); @@ -1583,7 +1583,7 @@ AddSharedLibraryInfoToStream(JSONWriter& aWriter, const SharedLibrary& aLib) } void -AppendSharedLibraries(JSONWriter& aWriter) +AppendSharedLibraries(ProfilerJSONWriter& aWriter) { SharedLibraryInfo info = SharedLibraryInfo::GetInfoForSelf(); info.SortByAddress(); @@ -1594,7 +1594,7 @@ AppendSharedLibraries(JSONWriter& aWriter) #ifdef MOZ_TASK_TRACER static void -StreamNameAndThreadId(JSONWriter& aWriter, const char* aName, int aThreadId) +StreamNameAndThreadId(ProfilerJSONWriter& aWriter, const char* aName, int aThreadId) { aWriter.StartObjectElement(); { @@ -1823,7 +1823,7 @@ StreamMetaJSCustomObject(PSLockRef aLock, SpliceableJSONWriter& aWriter, ExtensionPolicyService::GetSingleton().GetAll(exts); for (auto& ext : exts) { - aWriter.StartArrayElement(JSONWriter::SingleLineStyle); + aWriter.StartArrayElement(ProfilerJSONWriter::SingleLineStyle); nsAutoString id; ext->GetId(id); diff --git a/tools/profiler/core/platform.h b/tools/profiler/core/platform.h index 2bb89c16a41d..187339364c28 100644 --- a/tools/profiler/core/platform.h +++ b/tools/profiler/core/platform.h @@ -40,6 +40,7 @@ #include "mozilla/UniquePtr.h" #include "mozilla/Unused.h" #include "PlatformMacros.h" +#include "ProfileJSONWriter.h" #include // We need a definition of gettid(), but glibc doesn't provide a @@ -116,9 +117,11 @@ typedef mozilla::UniquePtr UniquePlatformData AllocPlatformData(int aThreadId); namespace mozilla { +template class JSONWriter; +class MallocAllocPolicy; } -void AppendSharedLibraries(mozilla::JSONWriter& aWriter); +void AppendSharedLibraries(ProfilerJSONWriter& aWriter); // Convert the array of strings to a bitfield. uint32_t ParseFeaturesFromStringArray(const char** aFeatures, diff --git a/tools/profiler/gecko/nsProfiler.cpp b/tools/profiler/gecko/nsProfiler.cpp index 371d6a0bc0de..2805887ac7c1 100644 --- a/tools/profiler/gecko/nsProfiler.cpp +++ b/tools/profiler/gecko/nsProfiler.cpp @@ -193,7 +193,7 @@ nsProfiler::GetSharedLibraries(JSContext* aCx, JS::RootedValue val(aCx); { nsString buffer; - JSONWriter w(MakeUnique(buffer)); + ProfilerJSONWriter w(MakeUnique(buffer)); w.StartArrayElement(); AppendSharedLibraries(w); w.EndArray(); diff --git a/tools/profiler/public/ProfileJSONWriter.h b/tools/profiler/public/ProfileJSONWriter.h index 814e7316a570..936b67c53696 100644 --- a/tools/profiler/public/ProfileJSONWriter.h +++ b/tools/profiler/public/ProfileJSONWriter.h @@ -10,9 +10,14 @@ #include #include +#include "js/AllocPolicy.h" #include "mozilla/JSONWriter.h" #include "mozilla/UniquePtr.h" +// JS::SystemAllocPolicy is used here instead of the default so that the JSONWriter +// object can also be passed into SpiderMonkey and used there. +using ProfilerJSONWriter = mozilla::JSONWriter; + class SpliceableChunkedJSONWriter; // On average, profile JSONs are large enough such that we want to avoid @@ -83,11 +88,11 @@ struct OStreamJSONWriteFunc : public mozilla::JSONWriteFunc std::ostream& mStream; }; -class SpliceableJSONWriter : public mozilla::JSONWriter +class SpliceableJSONWriter : public ProfilerJSONWriter { public: explicit SpliceableJSONWriter(mozilla::UniquePtr aWriter) - : JSONWriter(std::move(aWriter)) + : ProfilerJSONWriter(std::move(aWriter)) { } void StartBareList(CollectionStyle aStyle = MultiLineStyle) { diff --git a/xpcom/base/nsMacPreferencesReader.mm b/xpcom/base/nsMacPreferencesReader.mm index e116797d3f57..ab31545e526b 100644 --- a/xpcom/base/nsMacPreferencesReader.mm +++ b/xpcom/base/nsMacPreferencesReader.mm @@ -25,10 +25,10 @@ struct StringWriteFunc : public JSONWriteFunc }; static void -EvaluateDict(JSONWriter* aWriter, NSDictionary* aDict); +EvaluateDict(JSONWriter<>* aWriter, NSDictionary* aDict); static void -EvaluateArray(JSONWriter* aWriter, NSArray* aArray) +EvaluateArray(JSONWriter<>* aWriter, NSArray* aArray) { for (id elem in aArray) { if ([elem isKindOfClass:[NSString class]]) { @@ -48,7 +48,7 @@ EvaluateArray(JSONWriter* aWriter, NSArray* aArray) } static void -EvaluateDict(JSONWriter* aWriter, NSDictionary* aDict) +EvaluateDict(JSONWriter<>* aWriter, NSDictionary* aDict) { for (NSString* key in aDict) { id value = aDict[key]; @@ -83,7 +83,7 @@ nsMacPreferencesReader::ReadPreferences(JSContext* aCx, JS::MutableHandle aResult) { nsAutoString jsonStr; - JSONWriter w(MakeUnique(jsonStr)); + JSONWriter<> w(MakeUnique(jsonStr)); w.Start(); EvaluateDict(&w, [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]); diff --git a/xpcom/base/nsMemoryInfoDumper.cpp b/xpcom/base/nsMemoryInfoDumper.cpp index c95550cc98e1..72b2bfa7725d 100644 --- a/xpcom/base/nsMemoryInfoDumper.cpp +++ b/xpcom/base/nsMemoryInfoDumper.cpp @@ -469,7 +469,7 @@ class HandleReportAndFinishReportingCallbacks final public: NS_DECL_ISUPPORTS - HandleReportAndFinishReportingCallbacks(UniquePtr aWriter, + HandleReportAndFinishReportingCallbacks(UniquePtr> aWriter, nsIFinishDumpingCallback* aFinishDumping, nsISupports* aFinishDumpingData) : mWriter(std::move(aWriter)) @@ -545,7 +545,7 @@ public: private: ~HandleReportAndFinishReportingCallbacks() {} - UniquePtr mWriter; + UniquePtr> mWriter; nsCOMPtr mFinishDumping; nsCOMPtr mFinishDumpingData; }; @@ -649,7 +649,7 @@ DumpMemoryInfoToFile( return rv; } auto jsonWriter = - MakeUnique(MakeUnique(gzWriter)); + MakeUnique>(MakeUnique(gzWriter)); nsCOMPtr mgr = do_GetService("@mozilla.org/memory-reporter-manager;1");