Bug 1175771 (part 3) - Replace nsCacheEntryHashable::VisitEntries() with iterators. r=michal.
authorNicholas Nethercote <nnethercote@mozilla.com>
Wed, 17 Jun 2015 22:31:08 -0700
changeset 250675 4e1a2bf4694e812196d56842ff1f80d47084a378
parent 250674 a7992bd02bd07dbd27a1f189404a3fb9694e9ed3
child 250676 f29afc5cc5a06cc21ba7f017189cd67ae81ef79a
push idunknown
push userunknown
push dateunknown
reviewersmichal
bugs1175771
milestone42.0a1
Bug 1175771 (part 3) - Replace nsCacheEntryHashable::VisitEntries() with iterators. r=michal. Note that FreeCacheEntries was unused, and so could be removed as well.
netwerk/cache/nsCacheEntry.cpp
netwerk/cache/nsCacheEntry.h
netwerk/cache/nsCacheService.cpp
netwerk/cache/nsCacheService.h
netwerk/cache/nsMemoryCacheDevice.cpp
--- a/netwerk/cache/nsCacheEntry.cpp
+++ b/netwerk/cache/nsCacheEntry.cpp
@@ -457,25 +457,27 @@ nsCacheEntryHashTable::RemoveEntry( nsCa
 #if DEBUG
     // XXX debug code to make sure we have the entry we're trying to remove
     nsCacheEntry *check = GetEntry(&(cacheEntry->mKey));
     NS_ASSERTION(check == cacheEntry, "### Attempting to remove unknown cache entry!!!");
 #endif
     PL_DHashTableRemove(&table, &(cacheEntry->mKey));
 }
 
-
-void
-nsCacheEntryHashTable::VisitEntries( PLDHashEnumerator etor, void *arg)
+PLDHashTable::Iterator
+nsCacheEntryHashTable::Iter() const
 {
-    NS_ASSERTION(initialized, "nsCacheEntryHashTable not initialized");
-    if (!initialized)  return; // NS_ERROR_NOT_INITIALIZED
-    PL_DHashTableEnumerate(&table, etor, arg);
+    return PLDHashTable::Iterator(&table);
 }
 
+PLDHashTable::RemovingIterator
+nsCacheEntryHashTable::RemovingIter()
+{
+    return PLDHashTable::RemovingIterator(&table);
+}
 
 /**
  *  hash table operation callback functions
  */
 
 PLDHashNumber
 nsCacheEntryHashTable::HashKey( PLDHashTable *table, const void *key)
 {
--- a/netwerk/cache/nsCacheEntry.h
+++ b/netwerk/cache/nsCacheEntry.h
@@ -250,36 +250,37 @@ private:
 
     virtual ~nsCacheEntryInfo() {}
 };
 
 
 /******************************************************************************
 * nsCacheEntryHashTable
 *******************************************************************************/
-typedef struct {
-    PLDHashNumber  keyHash;
+
+struct nsCacheEntryHashTableEntry : public PLDHashEntryHdr
+{
     nsCacheEntry  *cacheEntry;
-} nsCacheEntryHashTableEntry;
-
+};
 
 class nsCacheEntryHashTable
 {
 public:
     nsCacheEntryHashTable();
     ~nsCacheEntryHashTable();
 
     void          Init();
     void          Shutdown();
 
     nsCacheEntry *GetEntry( const nsCString * key);
     nsresult      AddEntry( nsCacheEntry *entry);
     void          RemoveEntry( nsCacheEntry *entry);
 
-    void          VisitEntries( PLDHashEnumerator etor, void *arg);
+    PLDHashTable::Iterator Iter() const;
+    PLDHashTable::RemovingIterator RemovingIter();
 
 private:
     // PLDHashTable operation callbacks
     static PLDHashNumber  HashKey( PLDHashTable *table, const void *key);
 
     static bool           MatchEntry( PLDHashTable *           table,
                                       const PLDHashEntryHdr *  entry,
                                       const void *             key);
@@ -287,27 +288,16 @@ private:
     static void           MoveEntry( PLDHashTable *table,
                                      const PLDHashEntryHdr *from,
                                      PLDHashEntryHdr       *to);
 
     static void           ClearEntry( PLDHashTable *table, PLDHashEntryHdr *entry);
 
     static void           Finalize( PLDHashTable *table);
 
-    static
-    PLDHashOperator       FreeCacheEntries(PLDHashTable *    table,
-                                           PLDHashEntryHdr * hdr,
-                                           uint32_t          number,
-                                           void *            arg);
-    static
-    PLDHashOperator       VisitEntry(PLDHashTable *         table,
-                                     PLDHashEntryHdr *      hdr,
-                                     uint32_t               number,
-                                     void *                 arg);
-
     // member variables
     static const PLDHashTableOps ops;
     PLDHashTable                 table;
     bool                         initialized;
 
     static const uint32_t kInitialTableLength = 256;
 };
 
--- a/netwerk/cache/nsCacheService.cpp
+++ b/netwerk/cache/nsCacheService.cpp
@@ -2909,82 +2909,59 @@ nsCacheService::ClearDoomList()
         nsCacheEntry * next = (nsCacheEntry *)PR_NEXT_LINK(entry);
 
         entry->DetachDescriptors();
         DeactivateEntry(entry);
         entry = next;
     }
 }
 
-PLDHashOperator
-nsCacheService::GetActiveEntries(PLDHashTable *    table,
-                                 PLDHashEntryHdr * hdr,
-                                 uint32_t          number,
-                                 void *            arg)
-{
-    static_cast<nsTArray<nsCacheEntry*>*>(arg)->AppendElement(
-        ((nsCacheEntryHashTableEntry *)hdr)->cacheEntry);
-    return PL_DHASH_NEXT;
-}
-
-struct ActiveEntryArgs
-{
-    nsTArray<nsCacheEntry*>* mActiveArray;
-    nsCacheService::DoomCheckFn mCheckFn;
-};
-
 void
 nsCacheService::DoomActiveEntries(DoomCheckFn check)
 {
     nsAutoTArray<nsCacheEntry*, 8> array;
-    ActiveEntryArgs args = { &array, check };
-
-    mActiveEntries.VisitEntries(RemoveActiveEntry, &args);
+
+    for (auto iter = mActiveEntries.RemovingIter(); !iter.Done(); iter.Next()) {
+        nsCacheEntry* entry =
+            static_cast<nsCacheEntryHashTableEntry*>(iter.Get())->cacheEntry;
+
+        if (check && !check(entry)) {
+            continue;
+        }
+
+        array.AppendElement(entry);
+
+        // entry is being removed from the active entry list
+        entry->MarkInactive();
+        iter.Remove();
+    }
 
     uint32_t count = array.Length();
-    for (uint32_t i=0; i < count; ++i)
+    for (uint32_t i = 0; i < count; ++i) {
         DoomEntry_Internal(array[i], true);
+    }
 }
 
-PLDHashOperator
-nsCacheService::RemoveActiveEntry(PLDHashTable *    table,
-                                  PLDHashEntryHdr * hdr,
-                                  uint32_t          number,
-                                  void *            arg)
-{
-    nsCacheEntry * entry = ((nsCacheEntryHashTableEntry *)hdr)->cacheEntry;
-    NS_ASSERTION(entry, "### active entry = nullptr!");
-
-    ActiveEntryArgs* args = static_cast<ActiveEntryArgs*>(arg);
-    if (args->mCheckFn && !args->mCheckFn(entry))
-        return PL_DHASH_NEXT;
-
-    NS_ASSERTION(args->mActiveArray, "### array = nullptr!");
-    args->mActiveArray->AppendElement(entry);
-
-    // entry is being removed from the active entry list
-    entry->MarkInactive();
-    return PL_DHASH_REMOVE; // and continue enumerating
-}
-
-
 void
 nsCacheService::CloseAllStreams()
 {
     nsTArray<nsRefPtr<nsCacheEntryDescriptor::nsInputStreamWrapper> > inputs;
     nsTArray<nsRefPtr<nsCacheEntryDescriptor::nsOutputStreamWrapper> > outputs;
 
     {
         nsCacheServiceAutoLock lock(LOCK_TELEM(NSCACHESERVICE_CLOSEALLSTREAMS));
 
         nsTArray<nsCacheEntry*> entries;
 
 #if DEBUG
         // make sure there is no active entry
-        mActiveEntries.VisitEntries(GetActiveEntries, &entries);
+        for (auto iter = mActiveEntries.Iter(); !iter.Done(); iter.Next()) {
+            auto entry = static_cast<nsCacheEntryHashTableEntry*>(iter.Get());
+            entries.AppendElement(entry->cacheEntry);
+        }
         NS_ASSERTION(entries.IsEmpty(), "Bad state");
 #endif
 
         // Get doomed entries
         nsCacheEntry * entry = (nsCacheEntry *)PR_LIST_HEAD(&mDoomedEntries);
         while (entry != &mDoomedEntries) {
             nsCacheEntry * next = (nsCacheEntry *)PR_NEXT_LINK(entry);
             entries.AppendElement(entry);
--- a/netwerk/cache/nsCacheService.h
+++ b/netwerk/cache/nsCacheService.h
@@ -306,27 +306,16 @@ private:
     nsresult         ProcessPendingRequests(nsCacheEntry * entry);
 
     void             ClearDoomList(void);
     void             DoomActiveEntries(DoomCheckFn check);
     void             CloseAllStreams();
     void             FireClearNetworkCacheStoredAnywhereNotification();
 
     static
-    PLDHashOperator  GetActiveEntries(PLDHashTable *    table,
-                                      PLDHashEntryHdr * hdr,
-                                      uint32_t          number,
-                                      void *            arg);
-    static
-    PLDHashOperator  RemoveActiveEntry(PLDHashTable *    table,
-                                       PLDHashEntryHdr * hdr,
-                                       uint32_t          number,
-                                       void *            arg);
-
-    static
     PLDHashOperator  ShutdownCustomCacheDeviceEnum(const nsAString& aProfileDir,
                                                    nsRefPtr<nsOfflineCacheDevice>& aDevice,
                                                    void* aUserArg);
     void LogCacheStatistics();
 
     nsresult         SetDiskSmartSize_Locked();
 
     /**
--- a/netwerk/cache/nsMemoryCacheDevice.cpp
+++ b/netwerk/cache/nsMemoryCacheDevice.cpp
@@ -524,42 +524,36 @@ nsMemoryCacheDevice::SetMaxEntrySize(int
     // change and has no consequences for existing cache-entries
     if (maxSizeInKilobytes >= 0)
         mMaxEntrySize = maxSizeInKilobytes * 1024;
     else
         mMaxEntrySize = -1;
 }
 
 #ifdef DEBUG
-static PLDHashOperator
-CountEntry(PLDHashTable * table, PLDHashEntryHdr * hdr, uint32_t number, void * arg)
-{
-    int32_t *entryCount = (int32_t *)arg;
-    ++(*entryCount);
-    return PL_DHASH_NEXT;
-}
-
 void
 nsMemoryCacheDevice::CheckEntryCount()
 {
     if (!mInitialized)  return;
 
     int32_t evictionListCount = 0;
     for (int i=0; i<kQueueCount; ++i) {
         PRCList * elem = PR_LIST_HEAD(&mEvictionList[i]);
         while (elem != &mEvictionList[i]) {
             elem = PR_NEXT_LINK(elem);
             ++evictionListCount;
         }
     }
     NS_ASSERTION(mEntryCount == evictionListCount, "### mem cache badness");
 
     int32_t entryCount = 0;
-    mMemCacheEntries.VisitEntries(CountEntry, &entryCount);
-    NS_ASSERTION(mEntryCount == entryCount, "### mem cache badness");    
+    for (auto iter = mMemCacheEntries.Iter(); !iter.Done(); iter.Next()) {
+        ++entryCount;
+    }
+    NS_ASSERTION(mEntryCount == entryCount, "### mem cache badness");
 }
 #endif
 
 /******************************************************************************
  * nsMemoryCacheDeviceInfo - for implementing about:cache
  *****************************************************************************/