Bug 676057. Get rid of the malloc-happy AttributeSelectorEntry class. r=dbaron
authorBoris Zbarsky <bzbarsky@mit.edu>
Wed, 03 Aug 2011 14:32:32 -0400
changeset 73786 330937aaaebad4de473a5225d19b967562439d92
parent 73785 4dd0c8239b2b02411ceea7e1d4b9f9190befb549
child 73787 bb97ea5737a4a950ff2574745c8bd521e8516790
push id2
push userbsmedberg@mozilla.com
push dateFri, 19 Aug 2011 14:38:13 +0000
reviewersdbaron
bugs676057
milestone8.0a1
Bug 676057. Get rid of the malloc-happy AttributeSelectorEntry class. r=dbaron
layout/style/nsCSSRuleProcessor.cpp
--- a/layout/style/nsCSSRuleProcessor.cpp
+++ b/layout/style/nsCSSRuleProcessor.cpp
@@ -734,45 +734,17 @@ RuleHash::SizeOf() const
 
   n += mUniversalRules.SizeOf();
 
   return n;
 }
 
 //--------------------------------
 
-// Attribute selectors hash table.
-struct AttributeSelectorEntry : public PLDHashEntryHdr {
-  nsIAtom *mAttribute;
-  nsTArray<nsCSSSelector*> *mSelectors;
-};
-
-static void
-AttributeSelectorClearEntry(PLDHashTable *table, PLDHashEntryHdr *hdr)
-{
-  AttributeSelectorEntry *entry = static_cast<AttributeSelectorEntry*>(hdr);
-  delete entry->mSelectors;
-  memset(entry, 0, table->entrySize);
-}
-
-static const PLDHashTableOps AttributeSelectorOps = {
-  PL_DHashAllocTable,
-  PL_DHashFreeTable,
-  PL_DHashVoidPtrKeyStub,
-  PL_DHashMatchEntryStub,
-  PL_DHashMoveEntryStub,
-  AttributeSelectorClearEntry,
-  PL_DHashFinalizeStub,
-  NULL
-};
-
-
-//--------------------------------
-
-// Class selectors hash table.
+// A hash table mapping atoms to lists of selectors
 struct AtomSelectorEntry : public PLDHashEntryHdr {
   nsIAtom *mAtom;
   nsTArray<nsCSSSelector*> mSelectors;
 };
 
 static void
 AtomSelector_ClearEntry(PLDHashTable *table, PLDHashEntryHdr *hdr)
 {
@@ -832,18 +804,20 @@ struct RuleCascadeData {
   RuleCascadeData(nsIAtom *aMedium, PRBool aQuirksMode)
     : mRuleHash(aQuirksMode),
       mStateSelectors(),
       mSelectorDocumentStates(0),
       mCacheKey(aMedium),
       mNext(nsnull),
       mQuirksMode(aQuirksMode)
   {
-    PL_DHashTableInit(&mAttributeSelectors, &AttributeSelectorOps, nsnull,
-                      sizeof(AttributeSelectorEntry), 16);
+    // mAttributeSelectors is matching on the attribute _name_, not the value,
+    // and we case-fold names at parse-time, so this is a case-sensitive match.
+    PL_DHashTableInit(&mAttributeSelectors, &AtomSelector_CSOps.ops, nsnull,
+                      sizeof(AtomSelectorEntry), 16);
     PL_DHashTableInit(&mAnonBoxRules, &RuleHash_TagTable_Ops, nsnull,
                       sizeof(RuleHashTagTableEntry), 16);
     PL_DHashTableInit(&mIdSelectors,
                       aQuirksMode ? &AtomSelector_CIOps.ops :
                                     &AtomSelector_CSOps.ops,
                       nsnull, sizeof(AtomSelectorEntry), 16);
     PL_DHashTableInit(&mClassSelectors,
                       aQuirksMode ? &AtomSelector_CIOps.ops :
@@ -907,29 +881,16 @@ SelectorsSizeOfEnumerator(PLDHashTable* 
   PRInt64* n = (PRInt64*) aClosure;
   AtomSelectorEntry* entry = (AtomSelectorEntry*)aHdr;
 
   *n += entry->mSelectors.SizeOf();
 
   return PL_DHASH_NEXT;
 }
 
-static PLDHashOperator
-AttrSelectorsSizeOfEnumerator(PLDHashTable* aTable, PLDHashEntryHdr* aHdr,
-                              PRUint32 i, void* aClosure)
-{
-  PRInt64* n = (PRInt64*) aClosure;
-  AttributeSelectorEntry* entry = (AttributeSelectorEntry*)aHdr;
-
-  if (entry->mSelectors)
-    *n += entry->mSelectors->SizeOf() + sizeof(*entry->mSelectors);
-
-  return PL_DHASH_NEXT;
-}
-
 PRInt64
 RuleCascadeData::SizeOf() const
 {
   PRInt64 n = sizeof(*this);
 
   n += mRuleHash.SizeOf();
   for (PRUint32 i = 0; i < NS_ARRAY_LENGTH(mPseudoElementRuleHashes); ++i) {
     if (mPseudoElementRuleHashes[i])
@@ -943,19 +904,19 @@ RuleCascadeData::SizeOf() const
                          SelectorsSizeOfEnumerator, &n);
   n += PL_DHASH_TABLE_SIZE(&mClassSelectors) * sizeof(AtomSelectorEntry);
   PL_DHashTableEnumerate(const_cast<PLDHashTable*>(&mClassSelectors),
                          SelectorsSizeOfEnumerator, &n);
 
   n += mPossiblyNegatedClassSelectors.SizeOf();
   n += mPossiblyNegatedIDSelectors.SizeOf();
 
-  n += PL_DHASH_TABLE_SIZE(&mAttributeSelectors) * sizeof(AttributeSelectorEntry);
+  n += PL_DHASH_TABLE_SIZE(&mAttributeSelectors) * sizeof(AtomSelectorEntry);
   PL_DHashTableEnumerate(const_cast<PLDHashTable*>(&mAttributeSelectors),
-                         AttrSelectorsSizeOfEnumerator, &n);
+                         SelectorsSizeOfEnumerator, &n);
 
   n += PL_DHASH_TABLE_SIZE(&mAnonBoxRules) * sizeof(RuleHashTagTableEntry);
   PL_DHashTableEnumerate(const_cast<PLDHashTable*>(&mAnonBoxRules),
                          RuleHashTableSizeOfEnumerator, &n);
 
 #ifdef MOZ_XUL
   n += PL_DHASH_TABLE_SIZE(&mXULTreeRules) * sizeof(RuleHashTagTableEntry);
   PL_DHashTableEnumerate(const_cast<PLDHashTable*>(&mAnonBoxRules),
@@ -966,28 +927,23 @@ RuleCascadeData::SizeOf() const
   n += mKeyframesRules.SizeOf();
 
   return n;
 }
 
 nsTArray<nsCSSSelector*>*
 RuleCascadeData::AttributeListFor(nsIAtom* aAttribute)
 {
-  AttributeSelectorEntry *entry = static_cast<AttributeSelectorEntry*>
-                                             (PL_DHashTableOperate(&mAttributeSelectors, aAttribute, PL_DHASH_ADD));
+  AtomSelectorEntry *entry =
+    static_cast<AtomSelectorEntry*>
+               (PL_DHashTableOperate(&mAttributeSelectors, aAttribute,
+                                     PL_DHASH_ADD));
   if (!entry)
     return nsnull;
-  if (!entry->mSelectors) {
-    if (!(entry->mSelectors = new nsTArray<nsCSSSelector*>)) {
-      PL_DHashTableRawRemove(&mAttributeSelectors, entry);
-      return nsnull;
-    }
-    entry->mAttribute = aAttribute;
-  }
-  return entry->mSelectors;
+  return &entry->mSelectors;
 }
 
 class nsPrivateBrowsingObserver : nsIObserver,
                                   nsSupportsWeakReference
 {
 public:
   nsPrivateBrowsingObserver();
 
@@ -2530,22 +2486,23 @@ nsCSSRuleProcessor::HasAttributeDependen
       nsCSSSelector **iter = cascade->mPossiblyNegatedClassSelectors.Elements(),
                     **end = iter +
                               cascade->mPossiblyNegatedClassSelectors.Length();
       for (; iter != end; ++iter) {
         AttributeEnumFunc(*iter, &data);
       }
     }
 
-    AttributeSelectorEntry *entry = static_cast<AttributeSelectorEntry*>
-                                               (PL_DHashTableOperate(&cascade->mAttributeSelectors, aData->mAttribute,
-                             PL_DHASH_LOOKUP));
+    AtomSelectorEntry *entry =
+      static_cast<AtomSelectorEntry*>
+                 (PL_DHashTableOperate(&cascade->mAttributeSelectors,
+                                       aData->mAttribute, PL_DHASH_LOOKUP));
     if (PL_DHASH_ENTRY_IS_BUSY(entry)) {
-      nsCSSSelector **iter = entry->mSelectors->Elements(),
-                    **end = iter + entry->mSelectors->Length();
+      nsCSSSelector **iter = entry->mSelectors.Elements(),
+                    **end = iter + entry->mSelectors.Length();
       for(; iter != end; ++iter) {
         AttributeEnumFunc(*iter, &data);
       }
     }
   }
 
   return data.change;
 }