Bug 1175771 (part 3) - Replace nsCacheEntryHashable::VisitEntries() with iterators. r=michal.

Note that FreeCacheEntries was unused, and so could be removed as well.
This commit is contained in:
Nicholas Nethercote 2015-06-17 22:31:08 -07:00
parent 7e7e83f233
commit 52c7443b67
5 changed files with 38 additions and 86 deletions

View File

@ -462,15 +462,17 @@ nsCacheEntryHashTable::RemoveEntry( nsCacheEntry *cacheEntry)
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

View File

@ -255,11 +255,11 @@ private:
/******************************************************************************
* nsCacheEntryHashTable
*******************************************************************************/
typedef struct {
PLDHashNumber keyHash;
nsCacheEntry *cacheEntry;
} nsCacheEntryHashTableEntry;
struct nsCacheEntryHashTableEntry : public PLDHashEntryHdr
{
nsCacheEntry *cacheEntry;
};
class nsCacheEntryHashTable
{
@ -274,7 +274,8 @@ public:
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
@ -292,17 +293,6 @@ private:
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;

View File

@ -2914,58 +2914,32 @@ nsCacheService::ClearDoomList()
}
}
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()
{
@ -2979,7 +2953,10 @@ nsCacheService::CloseAllStreams()
#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

View File

@ -310,17 +310,6 @@ private:
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,

View File

@ -529,14 +529,6 @@ nsMemoryCacheDevice::SetMaxEntrySize(int32_t maxSizeInKilobytes)
}
#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()
{
@ -553,8 +545,10 @@ nsMemoryCacheDevice::CheckEntryCount()
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