Bug 1124973 (part 1) - Always use inheritance rather than composition for PLDHashTable entries. r=froydnj.

Because (a) this is how it's usually done, (b) it allows static_cast<> instead
of reinterpret_cast<>, and (c) it will make subsequent patches easier.

--HG--
extra : rebase_source : 76e67d4b6ec0e5dc898a8214b6a6b562f9e08380
This commit is contained in:
Nicholas Nethercote 2015-01-22 21:05:52 -08:00
parent 05c25dce64
commit 8f48f04d55
5 changed files with 52 additions and 52 deletions

View File

@ -32,11 +32,12 @@ public:
if (!hdr)
return NS_ERROR_OUT_OF_MEMORY;
Entry* entry = reinterpret_cast<Entry*>(hdr);
Entry* entry = static_cast<Entry*>(hdr);
NS_ASSERTION(entry->mMatch == nullptr, "over-writing entry");
entry->mContent = aElement;
entry->mMatch = aMatch;
return NS_OK; }
return NS_OK;
}
bool Get(nsIContent* aElement, nsTemplateMatch** aMatch) {
if (!mMap.IsInitialized())
@ -46,9 +47,10 @@ public:
if (PL_DHASH_ENTRY_IS_FREE(hdr))
return false;
Entry* entry = reinterpret_cast<Entry*>(hdr);
Entry* entry = static_cast<Entry*>(hdr);
*aMatch = entry->mMatch;
return true; }
return true;
}
nsresult Remove(nsIContent* aElement);
@ -60,8 +62,7 @@ protected:
void Init();
void Finish();
struct Entry {
PLDHashEntryHdr mHdr;
struct Entry : public PLDHashEntryHdr {
nsIContent* mContent;
nsTemplateMatch* mMatch;
};

View File

@ -11,8 +11,7 @@
class nsTemplateMap {
protected:
struct Entry {
PLDHashEntryHdr mHdr;
struct Entry : public PLDHashEntryHdr {
nsIContent* mContent;
nsIContent* mTemplate;
};
@ -38,8 +37,7 @@ public:
NS_ASSERTION(PL_DHASH_ENTRY_IS_FREE(PL_DHashTableLookup(&mTable, aContent)),
"aContent already in map");
Entry* entry =
reinterpret_cast<Entry*>(PL_DHashTableAdd(&mTable, aContent));
Entry* entry = static_cast<Entry*>(PL_DHashTableAdd(&mTable, aContent));
if (entry) {
entry->mContent = aContent;
@ -62,9 +60,9 @@ public:
void
GetTemplateFor(nsIContent* aContent, nsIContent** aResult) {
Entry* entry =
reinterpret_cast<Entry*>(PL_DHashTableLookup(&mTable, aContent));
static_cast<Entry*>(PL_DHashTableLookup(&mTable, aContent));
if (PL_DHASH_ENTRY_IS_BUSY(&entry->mHdr))
if (PL_DHASH_ENTRY_IS_BUSY(entry))
NS_IF_ADDREF(*aResult = entry->mTemplate);
else
*aResult = nullptr;

View File

@ -146,8 +146,7 @@ public:
};
struct Entry {
PLDHashEntryHdr mHdr;
struct Entry : PLDHashEntryHdr {
nsIRDFNode* mNode;
Assertion* mAssertions;
};
@ -217,7 +216,7 @@ PLDHashOperator
Assertion::DeletePropertyHashEntry(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
uint32_t aNumber, void* aArg)
{
Entry* entry = reinterpret_cast<Entry*>(aHdr);
Entry* entry = static_cast<Entry*>(aHdr);
Assertion* as = entry->mAssertions;
while (as) {
@ -322,20 +321,22 @@ public:
GetForwardArcs(nsIRDFResource* u) {
PLDHashEntryHdr* hdr = PL_DHashTableLookup(&mForwardArcs, u);
return PL_DHASH_ENTRY_IS_BUSY(hdr)
? reinterpret_cast<Entry*>(hdr)->mAssertions
: nullptr; }
? static_cast<Entry*>(hdr)->mAssertions
: nullptr;
}
Assertion*
GetReverseArcs(nsIRDFNode* v) {
PLDHashEntryHdr* hdr = PL_DHashTableLookup(&mReverseArcs, v);
return PL_DHASH_ENTRY_IS_BUSY(hdr)
? reinterpret_cast<Entry*>(hdr)->mAssertions
: nullptr; }
? static_cast<Entry*>(hdr)->mAssertions
: nullptr;
}
void
SetForwardArcs(nsIRDFResource* u, Assertion* as) {
if (as) {
Entry* entry = reinterpret_cast<Entry*>(PL_DHashTableAdd(&mForwardArcs, u));
Entry* entry = static_cast<Entry*>(PL_DHashTableAdd(&mForwardArcs, u));
if (entry) {
entry->mNode = u;
entry->mAssertions = as;
@ -349,7 +350,7 @@ public:
void
SetReverseArcs(nsIRDFNode* v, Assertion* as) {
if (as) {
Entry* entry = reinterpret_cast<Entry*>(PL_DHashTableAdd(&mReverseArcs, v));
Entry* entry = static_cast<Entry*>(PL_DHashTableAdd(&mReverseArcs, v));
if (entry) {
entry->mNode = v;
entry->mAssertions = as;
@ -447,7 +448,7 @@ InMemoryAssertionEnumeratorImpl::InMemoryAssertionEnumeratorImpl(
PLDHashEntryHdr* hdr = PL_DHashTableLookup(mNextAssertion->u.hash.mPropertyHash,
aProperty);
mNextAssertion = PL_DHASH_ENTRY_IS_BUSY(hdr)
? reinterpret_cast<Entry*>(hdr)->mAssertions
? static_cast<Entry*>(hdr)->mAssertions
: nullptr;
}
}
@ -593,7 +594,7 @@ InMemoryArcsEnumeratorImpl::ArcEnumerator(PLDHashTable* aTable,
PLDHashEntryHdr* aHdr,
uint32_t aNumber, void* aArg)
{
Entry* entry = reinterpret_cast<Entry*>(aHdr);
Entry* entry = static_cast<Entry*>(aHdr);
nsISupportsArray* resources = static_cast<nsISupportsArray*>(aArg);
resources->AppendElement(entry->mNode);
@ -828,7 +829,7 @@ PLDHashOperator
InMemoryDataSource::DeleteForwardArcsEntry(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
uint32_t aNumber, void* aArg)
{
Entry* entry = reinterpret_cast<Entry*>(aHdr);
Entry* entry = static_cast<Entry*>(aHdr);
Assertion* as = entry->mAssertions;
while (as) {
@ -981,7 +982,7 @@ InMemoryDataSource::GetTarget(nsIRDFResource* source,
if (as && as->mHashEntry) {
PLDHashEntryHdr* hdr = PL_DHashTableLookup(as->u.hash.mPropertyHash, property);
Assertion* val = PL_DHASH_ENTRY_IS_BUSY(hdr)
? reinterpret_cast<Entry*>(hdr)->mAssertions
? static_cast<Entry*>(hdr)->mAssertions
: nullptr;
while (val) {
if (tv == val->u.as.mTruthValue) {
@ -1027,7 +1028,7 @@ InMemoryDataSource::HasAssertion(nsIRDFResource* source,
if (as && as->mHashEntry) {
PLDHashEntryHdr* hdr = PL_DHashTableLookup(as->u.hash.mPropertyHash, property);
Assertion* val = PL_DHASH_ENTRY_IS_BUSY(hdr)
? reinterpret_cast<Entry*>(hdr)->mAssertions
? static_cast<Entry*>(hdr)->mAssertions
: nullptr;
while (val) {
if ((val->u.as.mTarget == target) && (tv == (val->u.as.mTruthValue))) {
@ -1140,7 +1141,7 @@ InMemoryDataSource::LockedAssert(nsIRDFResource* aSource,
if (haveHash) {
PLDHashEntryHdr* hdr = PL_DHashTableLookup(next->u.hash.mPropertyHash, aProperty);
Assertion* val = PL_DHASH_ENTRY_IS_BUSY(hdr)
? reinterpret_cast<Entry*>(hdr)->mAssertions
? static_cast<Entry*>(hdr)->mAssertions
: nullptr;
while (val) {
if (val->u.as.mTarget == aTarget) {
@ -1182,7 +1183,7 @@ InMemoryDataSource::LockedAssert(nsIRDFResource* aSource,
PLDHashEntryHdr* hdr = PL_DHashTableLookup(next->u.hash.mPropertyHash,
aProperty);
Assertion *asRef = PL_DHASH_ENTRY_IS_BUSY(hdr)
? reinterpret_cast<Entry*>(hdr)->mAssertions
? static_cast<Entry*>(hdr)->mAssertions
: nullptr;
if (asRef)
{
@ -1194,7 +1195,7 @@ InMemoryDataSource::LockedAssert(nsIRDFResource* aSource,
hdr = PL_DHashTableAdd(next->u.hash.mPropertyHash, aProperty);
if (hdr)
{
Entry* entry = reinterpret_cast<Entry*>(hdr);
Entry* entry = static_cast<Entry*>(hdr);
entry->mNode = aProperty;
entry->mAssertions = as;
}
@ -1277,13 +1278,13 @@ InMemoryDataSource::LockedUnassert(nsIRDFResource* aSource,
Assertion* prev = next;
Assertion* root = next;
Assertion* as = nullptr;
bool haveHash = (next) ? next->mHashEntry : false;
if (haveHash) {
PLDHashEntryHdr* hdr = PL_DHashTableLookup(next->u.hash.mPropertyHash,
aProperty);
prev = next = PL_DHASH_ENTRY_IS_BUSY(hdr)
? reinterpret_cast<Entry*>(hdr)->mAssertions
? static_cast<Entry*>(hdr)->mAssertions
: nullptr;
bool first = true;
while (next) {
@ -1307,7 +1308,7 @@ InMemoryDataSource::LockedUnassert(nsIRDFResource* aSource,
PLDHashEntryHdr* hdr = PL_DHashTableAdd(root->u.hash.mPropertyHash,
aProperty);
if (hdr) {
Entry* entry = reinterpret_cast<Entry*>(hdr);
Entry* entry = static_cast<Entry*>(hdr);
entry->mNode = aProperty;
entry->mAssertions = next->mNext;
}
@ -1578,7 +1579,7 @@ InMemoryDataSource::HasArcIn(nsIRDFNode *aNode, nsIRDFResource *aArc, bool *resu
return NS_OK;
}
NS_IMETHODIMP
NS_IMETHODIMP
InMemoryDataSource::HasArcOut(nsIRDFResource *aSource, nsIRDFResource *aArc, bool *result)
{
Assertion* ass = GetForwardArcs(aSource);
@ -1586,7 +1587,7 @@ InMemoryDataSource::HasArcOut(nsIRDFResource *aSource, nsIRDFResource *aArc, boo
PLDHashEntryHdr* hdr = PL_DHashTableLookup(ass->u.hash.mPropertyHash,
aArc);
Assertion* val = PL_DHASH_ENTRY_IS_BUSY(hdr)
? reinterpret_cast<Entry*>(hdr)->mAssertions
? static_cast<Entry*>(hdr)->mAssertions
: nullptr;
if (val) {
*result = true;
@ -1649,7 +1650,7 @@ InMemoryDataSource::ResourceEnumerator(PLDHashTable* aTable,
PLDHashEntryHdr* aHdr,
uint32_t aNumber, void* aArg)
{
Entry* entry = reinterpret_cast<Entry*>(aHdr);
Entry* entry = static_cast<Entry*>(aHdr);
static_cast<nsCOMArray<nsIRDFNode>*>(aArg)->AppendObject(entry->mNode);
return PL_DHASH_NEXT;
}
@ -1747,7 +1748,7 @@ InMemoryDataSource::EnsureFastContainment(nsIRDFResource* aSource)
PLDHashEntryHdr* hdr = PL_DHashTableLookup(table,
prop);
Assertion* val = PL_DHASH_ENTRY_IS_BUSY(hdr)
? reinterpret_cast<Entry*>(hdr)->mAssertions
? static_cast<Entry*>(hdr)->mAssertions
: nullptr;
if (val) {
first->mNext = val->mNext;
@ -1756,7 +1757,7 @@ InMemoryDataSource::EnsureFastContainment(nsIRDFResource* aSource)
else {
PLDHashEntryHdr* hdr = PL_DHashTableAdd(table, prop);
if (hdr) {
Entry* entry = reinterpret_cast<Entry*>(hdr);
Entry* entry = static_cast<Entry*>(hdr);
entry->mNode = prop;
entry->mAssertions = first;
first->mNext = nullptr;
@ -1812,7 +1813,7 @@ InMemoryDataSource::Mark(nsIRDFResource* aSource,
PLDHashEntryHdr* hdr = PL_DHashTableLookup(as->u.hash.mPropertyHash,
aProperty);
Assertion* val = PL_DHASH_ENTRY_IS_BUSY(hdr)
? reinterpret_cast<Entry*>(hdr)->mAssertions
? static_cast<Entry*>(hdr)->mAssertions
: nullptr;
while (val) {
if ((val->u.as.mTarget == aTarget) &&
@ -1906,7 +1907,7 @@ InMemoryDataSource::SweepForwardArcsEntries(PLDHashTable* aTable,
uint32_t aNumber, void* aArg)
{
PLDHashOperator result = PL_DHASH_NEXT;
Entry* entry = reinterpret_cast<Entry*>(aHdr);
Entry* entry = static_cast<Entry*>(aHdr);
SweepInfo* info = static_cast<SweepInfo*>(aArg);
Assertion* as = entry->mAssertions;
@ -1948,7 +1949,7 @@ InMemoryDataSource::SweepForwardArcsEntries(PLDHashTable* aTable,
PL_DHashTableLookup(info->mReverseArcs, as->u.as.mTarget);
NS_ASSERTION(PL_DHASH_ENTRY_IS_BUSY(hdr), "no assertion in reverse arcs");
Entry* rentry = reinterpret_cast<Entry*>(hdr);
Entry* rentry = static_cast<Entry*>(hdr);
Assertion* ras = rentry->mAssertions;
Assertion* rprev = nullptr;
while (ras) {
@ -2006,7 +2007,7 @@ public:
PLDHashOperator
SubjectEnumerator(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
uint32_t aNumber, void* aArg) {
Entry* entry = reinterpret_cast<Entry*>(aHdr);
Entry* entry = static_cast<Entry*>(aHdr);
VisitorClosure* closure = static_cast<VisitorClosure*>(aArg);
nsresult rv;
@ -2048,7 +2049,7 @@ public:
PLDHashOperator
TriplesInnerEnumerator(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
uint32_t aNumber, void* aArg) {
Entry* entry = reinterpret_cast<Entry*>(aHdr);
Entry* entry = static_cast<Entry*>(aHdr);
Assertion* assertion = entry->mAssertions;
TriplesInnerClosure* closure =
static_cast<TriplesInnerClosure*>(aArg);
@ -2069,7 +2070,7 @@ TriplesInnerEnumerator(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
PLDHashOperator
TriplesEnumerator(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
uint32_t aNumber, void* aArg) {
Entry* entry = reinterpret_cast<Entry*>(aHdr);
Entry* entry = static_cast<Entry*>(aHdr);
VisitorClosure* closure = static_cast<VisitorClosure*>(aArg);
nsresult rv;

View File

@ -129,7 +129,7 @@ public:
NS_ASSERTION(mTable.IsInitialized(),
"nsTHashtable was not initialized properly.");
EntryType* entry = reinterpret_cast<EntryType*>(
EntryType* entry = static_cast<EntryType*>(
PL_DHashTableLookup(const_cast<PLDHashTable*>(&mTable),
EntryType::KeyToPointer(aKey)));
return PL_DHASH_ENTRY_IS_BUSY(entry) ? entry : nullptr;
@ -483,7 +483,7 @@ void
nsTHashtable<EntryType>::s_ClearEntry(PLDHashTable* aTable,
PLDHashEntryHdr* aEntry)
{
reinterpret_cast<EntryType*>(aEntry)->~EntryType();
static_cast<EntryType*>(aEntry)->~EntryType();
}
template<class EntryType>
@ -505,7 +505,7 @@ nsTHashtable<EntryType>::s_EnumStub(PLDHashTable* aTable,
{
// dereferences the function-pointer to the user's enumeration function
return (*reinterpret_cast<s_EnumArgs*>(aArg)->userFunc)(
reinterpret_cast<EntryType*>(aEntry),
static_cast<EntryType*>(aEntry),
reinterpret_cast<s_EnumArgs*>(aArg)->userArg);
}
@ -517,7 +517,7 @@ nsTHashtable<EntryType>::s_SizeOfStub(PLDHashEntryHdr* aEntry,
{
// dereferences the function-pointer to the user's enumeration function
return (*reinterpret_cast<s_SizeOfArgs*>(aArg)->userFunc)(
reinterpret_cast<EntryType*>(aEntry),
static_cast<EntryType*>(aEntry),
aMallocSizeOf,
reinterpret_cast<s_SizeOfArgs*>(aArg)->userArg);
}

View File

@ -70,11 +70,11 @@ struct PLDHashTableOps;
* Callback types are defined below and grouped into the PLDHashTableOps
* structure, for single static initialization per hash table sub-type.
*
* Each hash table sub-type should nest the PLDHashEntryHdr structure at the
* front of its particular entry type. The keyHash member contains the result
* of multiplying the hash code returned from the hashKey callback (see below)
* by PL_DHASH_GOLDEN_RATIO, then constraining the result to avoid the magic 0
* and 1 values. The stored keyHash value is table size invariant, and it is
* Each hash table sub-type should make its entry type a subclass of
* PLDHashEntryHdr. The keyHash member contains the result of multiplying the
* hash code returned from the hashKey callback (see below) by
* PL_DHASH_GOLDEN_RATIO, then constraining the result to avoid the magic 0 and
* 1 values. The stored keyHash value is table size invariant, and it is
* maintained automatically -- users should never set it, and its only uses
* should be via the entry macros below.
*