Bug 936964 (part 1) - Make StartupCache ref-counted. r=aklotz.
☠☠ backed out by 190eedf8577a ☠ ☠
authorNicholas Nethercote <nnethercote@mozilla.com>
Mon, 25 Nov 2013 15:57:40 -0800
changeset 173470 fd1459e715850b049ed9e44e7a62073eb8051028
parent 173469 3e8a701d8bdc40469b5a9f7c2eb273e36e97ac0d
child 173471 34e9c313780441686e3c2beb273c7fa193f02fc6
push id445
push userffxbld
push dateMon, 10 Mar 2014 22:05:19 +0000
treeherdermozilla-release@dc38b741b04e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaklotz
bugs936964
milestone28.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 936964 (part 1) - Make StartupCache ref-counted. r=aklotz.
startupcache/StartupCache.cpp
startupcache/StartupCache.h
--- a/startupcache/StartupCache.cpp
+++ b/startupcache/StartupCache.cpp
@@ -105,39 +105,39 @@ StartupCache::GetSingleton()
   }
 
   return StartupCache::gStartupCache;
 }
 
 void
 StartupCache::DeleteSingleton()
 {
-  delete StartupCache::gStartupCache;
   StartupCache::gStartupCache = nullptr;
 }
 
 nsresult
 StartupCache::InitSingleton()
 {
   nsresult rv;
   StartupCache::gStartupCache = new StartupCache();
 
   rv = StartupCache::gStartupCache->Init();
   if (NS_FAILED(rv)) {
-    delete StartupCache::gStartupCache;
     StartupCache::gStartupCache = nullptr;
   }
   return rv;
 }
 
-StartupCache* StartupCache::gStartupCache;
+StaticRefPtr<StartupCache> StartupCache::gStartupCache;
 bool StartupCache::gShutdownInitiated;
 bool StartupCache::gIgnoreDiskCache;
 enum StartupCache::TelemetrifyAge StartupCache::gPostFlushAgeAction = StartupCache::IGNORE_AGE;
 
+NS_IMPL_ISUPPORTS1(StartupCache, nsISupports)
+
 StartupCache::StartupCache()
   : mArchive(nullptr), mStartupWriteInitiated(false), mWriteThread(nullptr)
 { }
 
 StartupCache::~StartupCache()
 {
   if (mTimer) {
     mTimer->Cancel();
@@ -151,17 +151,16 @@ StartupCache::~StartupCache()
   // If we shutdown quickly timer wont have fired. Instead of writing
   // it on the main thread and block the shutdown we simply wont update
   // the startup cache. Always do this if the file doesn't exist since
   // we use it part of the packge step.
   if (!mArchive) {
     WriteToDisk();
   }
 
-  gStartupCache = nullptr;
   NS_UnregisterMemoryReporter(mMappingReporter);
   NS_UnregisterMemoryReporter(mDataReporter);
 }
 
 nsresult
 StartupCache::Init()
 {
   // workaround for bug 653936
@@ -538,17 +537,17 @@ StartupCache::WaitOnWriteThread()
   NS_ASSERTION(NS_IsMainThread(), "Startup cache should only wait for io thread on main thread");
   if (!mWriteThread || mWriteThread == PR_GetCurrentThread())
     return;
 
   PR_JoinThread(mWriteThread);
   mWriteThread = nullptr;
 }
 
-void 
+void
 StartupCache::ThreadedWrite(void *aClosure)
 {
   PR_SetCurrentThreadName("StartupCache");
   gStartupCache->WriteToDisk();
 }
 
 /*
  * The write-thread is spawned on a timeout(which is reset with every write). This
--- a/startupcache/StartupCache.h
+++ b/startupcache/StartupCache.h
@@ -12,16 +12,17 @@
 #include "nsIStartupCache.h"
 #include "nsITimer.h"
 #include "nsIObserverService.h"
 #include "nsIObserver.h"
 #include "nsIOutputStream.h"
 #include "nsIFile.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/MemoryReporting.h"
+#include "mozilla/StaticPtr.h"
 
 class nsIMemoryReporter;
 
 /**
  * The StartupCache is a persistent cache of simple key-value pairs,
  * where the keys are null-terminated c-strings and the values are 
  * arbitrary data, passed as a (char*, size) tuple. 
  *
@@ -62,19 +63,20 @@ class nsIMemoryReporter;
  * Some utility functions are provided in StartupCacheUtils. These functions wrap the
  * buffers into object streams, which may be useful for serializing objects. Note
  * the above caution about multiply-referenced objects, though -- the streams are just
  * as 'dumb' as the underlying buffers about multiply-referenced objects. They just
  * provide some convenience in writing out data.
  */
 
 namespace mozilla {
+
 namespace scache {
 
-struct CacheEntry 
+struct CacheEntry
 {
   nsAutoArrayPtr<char> data;
   uint32_t size;
 
   CacheEntry() : data(nullptr), size(0) { }
 
   // Takes possession of buf
   CacheEntry(char* buf, uint32_t len) : data(buf), size(len) { }
@@ -91,27 +93,28 @@ struct CacheEntry
 // We don't want to refcount StartupCache, and ObserverService wants to
 // refcount its listeners, so we'll let it refcount this instead.
 class StartupCacheListener MOZ_FINAL : public nsIObserver
 {
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSIOBSERVER
 };
 
-class StartupCache
+class StartupCache : public nsISupports
 {
 
 friend class StartupCacheListener;
 friend class StartupCacheWrapper;
-                                
+
 public:
+  NS_DECL_ISUPPORTS
 
   // StartupCache methods. See above comments for a more detailed description.
 
-  // Returns a buffer that was previously stored, caller takes ownership. 
+  // Returns a buffer that was previously stored, caller takes ownership.
   nsresult GetBuffer(const char* id, char** outbuf, uint32_t* length);
 
   // Stores a buffer. Caller keeps ownership, we make a copy.
   nsresult PutBuffer(const char* id, const char* inbuf, uint32_t length);
 
   // Removes the cache file.
   void InvalidateCache();
 
@@ -131,17 +134,17 @@ public:
   // This measures all the heap memory used by the StartupCache, i.e. it
   // excludes the mapping.
   size_t HeapSizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf);
 
   size_t SizeOfMapping();
 
 private:
   StartupCache();
-  ~StartupCache();
+  virtual ~StartupCache();
 
   enum TelemetrifyAge {
     IGNORE_AGE = 0,
     RECORD_AGE = 1
   };
   static enum TelemetrifyAge gPostFlushAgeAction;
 
   nsresult LoadArchive(enum TelemetrifyAge flag);
@@ -157,24 +160,24 @@ private:
   static size_t SizeOfEntryExcludingThis(const nsACString& key,
                                          const nsAutoPtr<CacheEntry>& data,
                                          mozilla::MallocSizeOf mallocSizeOf,
                                          void *);
 
   nsClassHashtable<nsCStringHashKey, CacheEntry> mTable;
   nsRefPtr<nsZipArchive> mArchive;
   nsCOMPtr<nsIFile> mFile;
-  
+
   nsCOMPtr<nsIObserverService> mObserverService;
   nsRefPtr<StartupCacheListener> mListener;
   nsCOMPtr<nsITimer> mTimer;
 
   bool mStartupWriteInitiated;
 
-  static StartupCache *gStartupCache;
+  static StaticRefPtr<StartupCache> gStartupCache;
   static bool gShutdownInitiated;
   static bool gIgnoreDiskCache;
   PRThread *mWriteThread;
 #ifdef DEBUG
   nsTHashtable<nsISupportsHashKey> mWriteObjectMap;
 #endif
 
   nsCOMPtr<nsIMemoryReporter> mMappingReporter;