From bf4d4d181789c6918dd84cf586cd55e485df7d96 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 28 Jul 2015 21:14:39 -0700 Subject: [PATCH] Bug 1182961 (part 1, attempt 2) - Use nsTHashtable::Iterator in CacheIndex. r=michal. --- netwerk/cache2/CacheIndex.cpp | 139 +++++++++++++++------------------- netwerk/cache2/CacheIndex.h | 6 -- 2 files changed, 61 insertions(+), 84 deletions(-) diff --git a/netwerk/cache2/CacheIndex.cpp b/netwerk/cache2/CacheIndex.cpp index 9e69792819bb..8ee779c7c15f 100644 --- a/netwerk/cache2/CacheIndex.cpp +++ b/netwerk/cache2/CacheIndex.cpp @@ -1488,64 +1488,58 @@ CacheIndex::ProcessPendingOperations() AssertOwnsLock(); - mPendingUpdates.EnumerateEntries(&CacheIndex::UpdateEntryInIndex, this); + for (auto iter = mPendingUpdates.Iter(); !iter.Done(); iter.Next()) { + CacheIndexEntryUpdate* update = iter.Get(); + + LOG(("CacheIndex::ProcessPendingOperations() [hash=%08x%08x%08x%08x%08x]", + LOGSHA1(update->Hash()))); + + MOZ_ASSERT(update->IsFresh()); + + CacheIndexEntry* entry = mIndex.GetEntry(*update->Hash()); + + { + CacheIndexEntryAutoManage emng(update->Hash(), this); + emng.DoNotSearchInUpdates(); + + if (update->IsRemoved()) { + if (entry) { + if (entry->IsRemoved()) { + MOZ_ASSERT(entry->IsFresh()); + MOZ_ASSERT(entry->IsDirty()); + } else if (!entry->IsDirty() && entry->IsFileEmpty()) { + // Entries with empty file are not stored in index on disk. Just + // remove the entry, but only in case the entry is not dirty, i.e. + // the entry file was empty when we wrote the index. + mIndex.RemoveEntry(*update->Hash()); + entry = nullptr; + } else { + entry->MarkRemoved(); + entry->MarkDirty(); + entry->MarkFresh(); + } + } + } else if (entry) { + // Some information in mIndex can be newer than in mPendingUpdates (see + // bug 1074832). This will copy just those values that were really + // updated. + update->ApplyUpdate(entry); + } else { + // There is no entry in mIndex, copy all information from + // mPendingUpdates to mIndex. + entry = mIndex.PutEntry(*update->Hash()); + *entry = *update; + } + } + + iter.Remove(); + } MOZ_ASSERT(mPendingUpdates.Count() == 0); EnsureCorrectStats(); } -// static -PLDHashOperator -CacheIndex::UpdateEntryInIndex(CacheIndexEntryUpdate *aEntry, void* aClosure) -{ - CacheIndex *index = static_cast(aClosure); - - LOG(("CacheFile::UpdateEntryInIndex() [hash=%08x%08x%08x%08x%08x]", - LOGSHA1(aEntry->Hash()))); - - MOZ_ASSERT(aEntry->IsFresh()); - - CacheIndexEntry *entry = index->mIndex.GetEntry(*aEntry->Hash()); - - CacheIndexEntryAutoManage emng(aEntry->Hash(), index); - emng.DoNotSearchInUpdates(); - - if (aEntry->IsRemoved()) { - if (entry) { - if (entry->IsRemoved()) { - MOZ_ASSERT(entry->IsFresh()); - MOZ_ASSERT(entry->IsDirty()); - } else if (!entry->IsDirty() && entry->IsFileEmpty()) { - // Entries with empty file are not stored in index on disk. Just remove - // the entry, but only in case the entry is not dirty, i.e. the entry - // file was empty when we wrote the index. - index->mIndex.RemoveEntry(*aEntry->Hash()); - entry = nullptr; - } else { - entry->MarkRemoved(); - entry->MarkDirty(); - entry->MarkFresh(); - } - } - - return PL_DHASH_REMOVE; - } - - if (entry) { - // Some information in mIndex can be newer than in mPendingUpdates (see bug - // 1074832). This will copy just those values that were really updated. - aEntry->ApplyUpdate(entry); - } else { - // There is no entry in mIndex, copy all information from mPendingUpdates - // to mIndex. - entry = index->mIndex.PutEntry(*aEntry->Hash()); - *entry = *aEntry; - } - - return PL_DHASH_REMOVE; -} - bool CacheIndex::WriteIndexToDiskIfNeeded() { @@ -1969,7 +1963,13 @@ CacheIndex::WriteLogToDisk() NS_ENSURE_SUCCESS(rv, rv); WriteLogHelper wlh(fd); - mIndex.EnumerateEntries(&CacheIndex::WriteEntryToLog, &wlh); + for (auto iter = mIndex.Iter(); !iter.Done(); iter.Next()) { + CacheIndexEntry* entry = iter.Get(); + if (entry->IsRemoved() || entry->IsDirty()) { + wlh.AddEntry(entry); + } + iter.Remove(); + } rv = wlh.Finish(); PR_Close(fd); @@ -2002,19 +2002,6 @@ CacheIndex::WriteLogToDisk() return NS_OK; } -// static -PLDHashOperator -CacheIndex::WriteEntryToLog(CacheIndexEntry *aEntry, void* aClosure) -{ - WriteLogHelper *wlh = static_cast(aClosure); - - if (aEntry->IsRemoved() || aEntry->IsDirty()) { - wlh->AddEntry(aEntry); - } - - return PL_DHASH_REMOVE; -} - void CacheIndex::ReadIndexFromDisk() { @@ -2392,7 +2379,10 @@ CacheIndex::EnsureNoFreshEntry() #ifdef DEBUG_STATS CacheIndexStats debugStats; debugStats.DisableLogging(); - mIndex.EnumerateEntries(&CacheIndex::SumIndexStats, &debugStats); + for (auto iter = mIndex.Iter(); !iter.Done(); iter.Next()) { + debugStats.BeforeChange(nullptr); + debugStats.AfterChange(iter.Get()); + } MOZ_ASSERT(debugStats.Fresh() == 0); #endif } @@ -2404,21 +2394,14 @@ CacheIndex::EnsureCorrectStats() MOZ_ASSERT(mPendingUpdates.Count() == 0); CacheIndexStats debugStats; debugStats.DisableLogging(); - mIndex.EnumerateEntries(&CacheIndex::SumIndexStats, &debugStats); + for (auto iter = mIndex.Iter(); !iter.Done(); iter.Next()) { + debugStats.BeforeChange(nullptr); + debugStats.AfterChange(iter.Get()); + } MOZ_ASSERT(debugStats == mIndexStats); #endif } -// static -PLDHashOperator -CacheIndex::SumIndexStats(CacheIndexEntry *aEntry, void* aClosure) -{ - CacheIndexStats *stats = static_cast(aClosure); - stats->BeforeChange(nullptr); - stats->AfterChange(aEntry); - return PL_DHASH_NEXT; -} - void CacheIndex::FinishRead(bool aSucceeded) { diff --git a/netwerk/cache2/CacheIndex.h b/netwerk/cache2/CacheIndex.h index 58ed8ea8db84..6e2797ae39d9 100644 --- a/netwerk/cache2/CacheIndex.h +++ b/netwerk/cache2/CacheIndex.h @@ -723,8 +723,6 @@ private: // Merge all pending operations from mPendingUpdates into mIndex. void ProcessPendingOperations(); - static PLDHashOperator UpdateEntryInIndex(CacheIndexEntryUpdate *aEntry, - void* aClosure); // Following methods perform writing of the index file. // @@ -761,9 +759,6 @@ private: // Writes journal to the disk and clears dirty flag in index header. nsresult WriteLogToDisk(); - static PLDHashOperator WriteEntryToLog(CacheIndexEntry *aEntry, - void* aClosure); - // Following methods perform reading of the index from the disk. // // Index is read at startup just after initializing the CacheIndex. There are @@ -818,7 +813,6 @@ private: // In debug build this method is called after processing pending operations // to make sure mIndexStats contains correct information. void EnsureCorrectStats(); - static PLDHashOperator SumIndexStats(CacheIndexEntry *aEntry, void* aClosure); // Finalizes reading process. void FinishRead(bool aSucceeded);