Bug 1322863 - part 2 - make ThreadInfo manage mProfile with UniquePtr; r=mstange
authorNathan Froyd <froydnj@mozilla.com>
Fri, 06 Jan 2017 09:21:01 -0500
changeset 328209 07f764d100786fc146ef73d60fc937f6b6a1f4db
parent 328208 41e0e99837f998dcc39e3234908afb464c4eb563
child 328210 e1a7c28606f131cba5058f74661c716a7441a55f
push id85407
push usernfroyd@mozilla.com
push dateFri, 06 Jan 2017 14:21:10 +0000
treeherdermozilla-inbound@faffd8c889bd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmstange
bugs1322863
milestone53.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1322863 - part 2 - make ThreadInfo manage mProfile with UniquePtr; r=mstange Smart pointers are better than raw pointers. This change also has the benefit of removing the manual memory management in ~GeckoSampler and locating all the memory management in ThreadInfo, where it belongs.
tools/profiler/core/GeckoSampler.cpp
tools/profiler/core/GeckoSampler.h
tools/profiler/core/ThreadInfo.cpp
tools/profiler/core/ThreadInfo.h
--- a/tools/profiler/core/GeckoSampler.cpp
+++ b/tools/profiler/core/GeckoSampler.cpp
@@ -261,21 +261,16 @@ GeckoSampler::~GeckoSampler()
   SetActiveSampler(nullptr);
 
   // Destroy ThreadProfile for all threads
   {
     ::MutexAutoLock lock(*sRegisteredThreadsMutex);
 
     for (uint32_t i = 0; i < sRegisteredThreads->size(); i++) {
       ThreadInfo* info = sRegisteredThreads->at(i);
-      ThreadProfile* profile = info->Profile();
-      if (profile) {
-        delete profile;
-        info->SetProfile(nullptr);
-      }
       // We've stopped profiling. We no longer need to retain
       // information for an old thread.
       if (info->IsPendingDelete()) {
         delete info;
         sRegisteredThreads->erase(sRegisteredThreads->begin() + i);
         i--;
       }
     }
--- a/tools/profiler/core/GeckoSampler.h
+++ b/tools/profiler/core/GeckoSampler.h
@@ -3,16 +3,17 @@
  * 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/. */
 
 #ifndef GeckoSampler_h
 #define GeckoSampler_h
 
 #include "platform.h"
 #include "ProfileEntry.h"
+#include "mozilla/UniquePtr.h"
 #include "mozilla/Vector.h"
 #include "ThreadProfile.h"
 #include "ThreadInfo.h"
 #ifndef SPS_STANDALONE
 #include "IntelPowerGadget.h"
 #endif
 #ifdef MOZ_TASK_TRACER
 #include "GeckoTaskTracer.h"
@@ -64,18 +65,17 @@ class GeckoSampler: public Sampler {
     if (!aInfo->IsMainThread() && !mProfileThreads) {
       return;
     }
 
     if (!threadSelected(aInfo, mThreadNameFilters)) {
       return;
     }
 
-    ThreadProfile* profile = new ThreadProfile(aInfo, mBuffer);
-    aInfo->SetProfile(profile);
+    aInfo->SetProfile(mozilla::MakeUnique<ThreadProfile>(aInfo, mBuffer));
   }
 
   // Called within a signal. This function must be reentrant
   virtual void Tick(TickSample* sample) override;
 
   // Immediately captures the calling thread's call stack and returns it.
   virtual SyncProfile* GetBacktrace() override;
 
--- a/tools/profiler/core/ThreadInfo.cpp
+++ b/tools/profiler/core/ThreadInfo.cpp
@@ -12,17 +12,16 @@
 ThreadInfo::ThreadInfo(const char* aName, int aThreadId,
                        bool aIsMainThread, PseudoStack* aPseudoStack,
                        void* aStackTop)
   : mName(strdup(aName))
   , mThreadId(aThreadId)
   , mIsMainThread(aIsMainThread)
   , mPseudoStack(aPseudoStack)
   , mPlatformData(Sampler::AllocPlatformData(aThreadId))
-  , mProfile(nullptr)
   , mStackTop(aStackTop)
   , mPendingDelete(false)
 {
   MOZ_COUNT_CTOR(ThreadInfo);
 #ifndef SPS_STANDALONE
   mThread = NS_GetCurrentThread();
 #endif
 
@@ -31,19 +30,16 @@ ThreadInfo::ThreadInfo(const char* aName
   pthread_t self = pthread_self();
   mStackTop = pthread_get_stackaddr_np(self);
 #endif
 }
 
 ThreadInfo::~ThreadInfo() {
   MOZ_COUNT_DTOR(ThreadInfo);
 
-  if (mProfile)
-    delete mProfile;
-
   Sampler::FreePlatformData(mPlatformData);
 }
 
 void
 ThreadInfo::SetPendingDelete()
 {
   mPendingDelete = true;
   // We don't own the pseudostack so disconnect it.
--- a/tools/profiler/core/ThreadInfo.h
+++ b/tools/profiler/core/ThreadInfo.h
@@ -18,18 +18,21 @@ class ThreadInfo {
   virtual ~ThreadInfo();
 
   const char* Name() const { return mName.get(); }
   int ThreadId() const { return mThreadId; }
 
   bool IsMainThread() const { return mIsMainThread; }
   PseudoStack* Stack() const { return mPseudoStack; }
 
-  void SetProfile(ThreadProfile* aProfile) { mProfile = aProfile; }
-  ThreadProfile* Profile() const { return mProfile; }
+  void SetProfile(mozilla::UniquePtr<ThreadProfile> aProfile)
+  {
+    mProfile = mozilla::Move(aProfile);
+  }
+  ThreadProfile* Profile() const { return mProfile.get(); }
 
   PlatformData* GetPlatformData() const { return mPlatformData; }
   void* StackTop() const { return mStackTop; }
 
   virtual void SetPendingDelete();
   bool IsPendingDelete() const { return mPendingDelete; }
 
 #ifndef SPS_STANDALONE
@@ -43,17 +46,17 @@ class ThreadInfo {
   bool CanInvokeJS() const;
 
  private:
   mozilla::UniqueFreePtr<char> mName;
   int mThreadId;
   const bool mIsMainThread;
   PseudoStack* mPseudoStack;
   PlatformData* mPlatformData;
-  ThreadProfile* mProfile;
+  mozilla::UniquePtr<ThreadProfile> mProfile;
   void* mStackTop;
 #ifndef SPS_STANDALONE
   nsCOMPtr<nsIThread> mThread;
 #endif
   bool mPendingDelete;
 };
 
 // Just like ThreadInfo, but owns a reference to the PseudoStack.