Bug 1176163 - Remove remaining uses of PL_DHashTableEnumerate() from xpcom/. r=froydnj.

This commit is contained in:
Nicholas Nethercote 2015-06-18 18:09:37 -07:00
parent a606cf71b0
commit 5f3d1cb2da
4 changed files with 44 additions and 129 deletions

View File

@ -167,10 +167,17 @@ public:
*/
uint32_t EnumerateRead(EnumReadFunction aEnumFunc, void* aUserArg) const
{
s_EnumReadArgs enumData = { aEnumFunc, aUserArg };
return PL_DHashTableEnumerate(const_cast<PLDHashTable*>(&this->mTable),
s_EnumReadStub,
&enumData);
uint32_t n = 0;
for (auto iter = this->mTable.Iter(); !iter.Done(); iter.Next()) {
auto entry = static_cast<EntryType*>(iter.Get());
PLDHashOperator op = aEnumFunc(entry->GetKey(), entry->mData, aUserArg);
n++;
MOZ_ASSERT(!(op & PL_DHASH_REMOVE));
if (op & PL_DHASH_STOP) {
break;
}
}
return n;
}
/**
@ -196,10 +203,19 @@ public:
*/
uint32_t Enumerate(EnumFunction aEnumFunc, void* aUserArg)
{
s_EnumArgs enumData = { aEnumFunc, aUserArg };
return PL_DHashTableEnumerate(&this->mTable,
s_EnumStub,
&enumData);
uint32_t n = 0;
for (auto iter = this->mTable.RemovingIter(); !iter.Done(); iter.Next()) {
auto entry = static_cast<EntryType*>(iter.Get());
PLDHashOperator op = aEnumFunc(entry->GetKey(), entry->mData, aUserArg);
n++;
if (op & PL_DHASH_REMOVE) {
iter.Remove();
}
if (op & PL_DHASH_STOP) {
break;
}
}
return n;
}
/**
@ -273,33 +289,6 @@ public:
#endif
protected:
/**
* used internally during EnumerateRead. Allocated on the stack.
* @param func the enumerator passed to EnumerateRead
* @param userArg the userArg passed to EnumerateRead
*/
struct s_EnumReadArgs
{
EnumReadFunction func;
void* userArg;
};
static PLDHashOperator s_EnumReadStub(PLDHashTable* aTable,
PLDHashEntryHdr* aHdr,
uint32_t aNumber,
void* aArg);
struct s_EnumArgs
{
EnumFunction func;
void* userArg;
};
static PLDHashOperator s_EnumStub(PLDHashTable* aTable,
PLDHashEntryHdr* aHdr,
uint32_t aNumber,
void* aArg);
struct s_SizeOfArgs
{
SizeOfEntryExcludingThisFun func;
@ -375,37 +364,6 @@ nsBaseHashtableET<KeyClass, DataType>::~nsBaseHashtableET()
// nsBaseHashtable definitions
//
template<class KeyClass, class DataType, class UserDataType>
PLDHashOperator
nsBaseHashtable<KeyClass, DataType, UserDataType>::s_EnumReadStub(
PLDHashTable* aTable, PLDHashEntryHdr* aHdr, uint32_t aNumber, void* aArg)
{
EntryType* ent = static_cast<EntryType*>(aHdr);
s_EnumReadArgs* eargs = (s_EnumReadArgs*)aArg;
PLDHashOperator res = (eargs->func)(ent->GetKey(), ent->mData, eargs->userArg);
NS_ASSERTION(!(res & PL_DHASH_REMOVE),
"PL_DHASH_REMOVE return during const enumeration; ignoring.");
if (res & PL_DHASH_STOP) {
return PL_DHASH_STOP;
}
return PL_DHASH_NEXT;
}
template<class KeyClass, class DataType, class UserDataType>
PLDHashOperator
nsBaseHashtable<KeyClass, DataType, UserDataType>::s_EnumStub(
PLDHashTable* aTable, PLDHashEntryHdr* aHdr, uint32_t aNumber, void* aArg)
{
EntryType* ent = static_cast<EntryType*>(aHdr);
s_EnumArgs* eargs = (s_EnumArgs*)aArg;
return (eargs->func)(ent->GetKey(), ent->mData, eargs->userArg);
}
template<class KeyClass, class DataType, class UserDataType>
size_t
nsBaseHashtable<KeyClass, DataType, UserDataType>::s_SizeOfStub(

View File

@ -199,8 +199,19 @@ public:
*/
uint32_t EnumerateEntries(Enumerator aEnumFunc, void* aUserArg)
{
s_EnumArgs args = { aEnumFunc, aUserArg };
return PL_DHashTableEnumerate(&mTable, s_EnumStub, &args);
uint32_t n = 0;
for (auto iter = mTable.RemovingIter(); !iter.Done(); iter.Next()) {
auto entry = static_cast<EntryType*>(iter.Get());
PLDHashOperator op = aEnumFunc(entry, aUserArg);
n++;
if (op & PL_DHASH_REMOVE) {
iter.Remove();
}
if (op & PL_DHASH_STOP) {
break;
}
}
return n;
}
/**
@ -318,23 +329,6 @@ protected:
static void s_InitEntry(PLDHashEntryHdr* aEntry, const void* aKey);
/**
* passed internally during enumeration. Allocated on the stack.
*
* @param userFunc the Enumerator function passed to
* EnumerateEntries by the client
* @param userArg the userArg passed unaltered
*/
struct s_EnumArgs
{
Enumerator userFunc;
void* userArg;
};
static PLDHashOperator s_EnumStub(PLDHashTable* aTable,
PLDHashEntryHdr* aEntry,
uint32_t aNumber, void* aArg);
/**
* passed internally during sizeOf counting. Allocated on the stack.
*
@ -467,19 +461,6 @@ nsTHashtable<EntryType>::s_InitEntry(PLDHashEntryHdr* aEntry,
new (aEntry) EntryType(reinterpret_cast<KeyTypePointer>(aKey));
}
template<class EntryType>
PLDHashOperator
nsTHashtable<EntryType>::s_EnumStub(PLDHashTable* aTable,
PLDHashEntryHdr* aEntry,
uint32_t aNumber,
void* aArg)
{
// dereferences the function-pointer to the user's enumeration function
return (*reinterpret_cast<s_EnumArgs*>(aArg)->userFunc)(
static_cast<EntryType*>(aEntry),
reinterpret_cast<s_EnumArgs*>(aArg)->userArg);
}
template<class EntryType>
size_t
nsTHashtable<EntryType>::s_SizeOfStub(PLDHashEntryHdr* aEntry,

View File

@ -845,23 +845,6 @@ PL_DHashTableEnumerate(PLDHashTable* aTable, PLDHashEnumerator aEtor,
return aTable->Enumerate(aEtor, aArg);
}
struct SizeOfEntryExcludingThisArg
{
size_t total;
PLDHashSizeOfEntryExcludingThisFun sizeOfEntryExcludingThis;
MallocSizeOf mallocSizeOf;
void* arg; // the arg passed by the user
};
static PLDHashOperator
SizeOfEntryExcludingThisEnumerator(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
uint32_t aNumber, void* aArg)
{
SizeOfEntryExcludingThisArg* e = (SizeOfEntryExcludingThisArg*)aArg;
e->total += e->sizeOfEntryExcludingThis(aHdr, e->mallocSizeOf, e->arg);
return PL_DHASH_NEXT;
}
MOZ_ALWAYS_INLINE size_t
PLDHashTable::SizeOfExcludingThis(
PLDHashSizeOfEntryExcludingThisFun aSizeOfEntryExcludingThis,
@ -871,15 +854,11 @@ PLDHashTable::SizeOfExcludingThis(
return 0;
}
size_t n = 0;
n += aMallocSizeOf(mEntryStore);
size_t n = aMallocSizeOf(mEntryStore);
if (aSizeOfEntryExcludingThis) {
SizeOfEntryExcludingThisArg arg2 = {
0, aSizeOfEntryExcludingThis, aMallocSizeOf, aArg
};
PL_DHashTableEnumerate(const_cast<PLDHashTable*>(this),
SizeOfEntryExcludingThisEnumerator, &arg2);
n += arg2.total;
for (auto iter = Iter(); !iter.Done(); iter.Next()) {
n += aSizeOfEntryExcludingThis(iter.Get(), aMallocSizeOf, aArg);
}
}
return n;
}

View File

@ -64,14 +64,11 @@ static bool test_pldhash_lazy_storage()
// No result to check here, but call it to make sure it doesn't crash.
PL_DHashTableRemove(&t, (const void*)2);
// Using a null |enumerator| should be fine because it shouldn't be called
// for an empty table.
PLDHashEnumerator enumerator = nullptr;
if (PL_DHashTableEnumerate(&t, enumerator, nullptr) != 0) {
return false; // enumeration count is non-zero?
for (auto iter = t.Iter(); !iter.Done(); iter.Next()) {
return false; // shouldn't hit this on an empty table
}
for (auto iter = t.Iter(); !iter.Done(); iter.Get()) {
for (auto iter = t.RemovingIter(); !iter.Done(); iter.Next()) {
return false; // shouldn't hit this on an empty table
}