Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * Storage of the children and attributes of a DOM node; storage for |
michael@0 | 8 | * the two is unified to minimize footprint. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #ifndef nsAttrAndChildArray_h___ |
michael@0 | 12 | #define nsAttrAndChildArray_h___ |
michael@0 | 13 | |
michael@0 | 14 | #include "mozilla/Attributes.h" |
michael@0 | 15 | #include "mozilla/MemoryReporting.h" |
michael@0 | 16 | |
michael@0 | 17 | #include "nscore.h" |
michael@0 | 18 | #include "nsAttrName.h" |
michael@0 | 19 | #include "nsAttrValue.h" |
michael@0 | 20 | #include "nsCaseTreatment.h" |
michael@0 | 21 | |
michael@0 | 22 | class nsINode; |
michael@0 | 23 | class nsIContent; |
michael@0 | 24 | class nsMappedAttributes; |
michael@0 | 25 | class nsHTMLStyleSheet; |
michael@0 | 26 | class nsRuleWalker; |
michael@0 | 27 | class nsMappedAttributeElement; |
michael@0 | 28 | |
michael@0 | 29 | #define ATTRCHILD_ARRAY_GROWSIZE 8 |
michael@0 | 30 | #define ATTRCHILD_ARRAY_LINEAR_THRESHOLD 32 |
michael@0 | 31 | |
michael@0 | 32 | #define ATTRCHILD_ARRAY_ATTR_SLOTS_BITS 10 |
michael@0 | 33 | |
michael@0 | 34 | #define ATTRCHILD_ARRAY_MAX_ATTR_COUNT \ |
michael@0 | 35 | ((1 << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) - 1) |
michael@0 | 36 | |
michael@0 | 37 | #define ATTRCHILD_ARRAY_MAX_CHILD_COUNT \ |
michael@0 | 38 | (~uint32_t(0) >> ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) |
michael@0 | 39 | |
michael@0 | 40 | #define ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK \ |
michael@0 | 41 | ((1 << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) - 1) |
michael@0 | 42 | |
michael@0 | 43 | |
michael@0 | 44 | #define ATTRSIZE (sizeof(InternalAttr) / sizeof(void*)) |
michael@0 | 45 | |
michael@0 | 46 | class nsAttrAndChildArray |
michael@0 | 47 | { |
michael@0 | 48 | public: |
michael@0 | 49 | nsAttrAndChildArray(); |
michael@0 | 50 | ~nsAttrAndChildArray(); |
michael@0 | 51 | |
michael@0 | 52 | uint32_t ChildCount() const |
michael@0 | 53 | { |
michael@0 | 54 | return mImpl ? (mImpl->mAttrAndChildCount >> ATTRCHILD_ARRAY_ATTR_SLOTS_BITS) : 0; |
michael@0 | 55 | } |
michael@0 | 56 | nsIContent* ChildAt(uint32_t aPos) const |
michael@0 | 57 | { |
michael@0 | 58 | NS_ASSERTION(aPos < ChildCount(), "out-of-bounds access in nsAttrAndChildArray"); |
michael@0 | 59 | return reinterpret_cast<nsIContent*>(mImpl->mBuffer[AttrSlotsSize() + aPos]); |
michael@0 | 60 | } |
michael@0 | 61 | nsIContent* GetSafeChildAt(uint32_t aPos) const; |
michael@0 | 62 | nsIContent * const * GetChildArray(uint32_t* aChildCount) const; |
michael@0 | 63 | nsresult AppendChild(nsIContent* aChild) |
michael@0 | 64 | { |
michael@0 | 65 | return InsertChildAt(aChild, ChildCount()); |
michael@0 | 66 | } |
michael@0 | 67 | nsresult InsertChildAt(nsIContent* aChild, uint32_t aPos); |
michael@0 | 68 | void RemoveChildAt(uint32_t aPos); |
michael@0 | 69 | // Like RemoveChildAt but hands the reference to the child being |
michael@0 | 70 | // removed back to the caller instead of just releasing it. |
michael@0 | 71 | already_AddRefed<nsIContent> TakeChildAt(uint32_t aPos); |
michael@0 | 72 | int32_t IndexOfChild(const nsINode* aPossibleChild) const; |
michael@0 | 73 | |
michael@0 | 74 | bool HasAttrs() const |
michael@0 | 75 | { |
michael@0 | 76 | return MappedAttrCount() || (AttrSlotCount() && AttrSlotIsTaken(0)); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | uint32_t AttrCount() const; |
michael@0 | 80 | const nsAttrValue* GetAttr(nsIAtom* aLocalName, |
michael@0 | 81 | int32_t aNamespaceID = kNameSpaceID_None) const; |
michael@0 | 82 | // As above but using a string attr name and always using |
michael@0 | 83 | // kNameSpaceID_None. This is always case-sensitive. |
michael@0 | 84 | const nsAttrValue* GetAttr(const nsAString& aName) const; |
michael@0 | 85 | // Get an nsAttrValue by qualified name. Can optionally do |
michael@0 | 86 | // ASCII-case-insensitive name matching. |
michael@0 | 87 | const nsAttrValue* GetAttr(const nsAString& aName, |
michael@0 | 88 | nsCaseTreatment aCaseSensitive) const; |
michael@0 | 89 | const nsAttrValue* AttrAt(uint32_t aPos) const; |
michael@0 | 90 | nsresult SetAndTakeAttr(nsIAtom* aLocalName, nsAttrValue& aValue); |
michael@0 | 91 | nsresult SetAndTakeAttr(nsINodeInfo* aName, nsAttrValue& aValue); |
michael@0 | 92 | |
michael@0 | 93 | // Remove the attr at position aPos. The value of the attr is placed in |
michael@0 | 94 | // aValue; any value that was already in aValue is destroyed. |
michael@0 | 95 | nsresult RemoveAttrAt(uint32_t aPos, nsAttrValue& aValue); |
michael@0 | 96 | |
michael@0 | 97 | // Returns attribute name at given position, *not* out-of-bounds safe |
michael@0 | 98 | const nsAttrName* AttrNameAt(uint32_t aPos) const; |
michael@0 | 99 | |
michael@0 | 100 | // Returns attribute name at given position or null if aPos is out-of-bounds |
michael@0 | 101 | const nsAttrName* GetSafeAttrNameAt(uint32_t aPos) const; |
michael@0 | 102 | |
michael@0 | 103 | const nsAttrName* GetExistingAttrNameFromQName(const nsAString& aName) const; |
michael@0 | 104 | int32_t IndexOfAttr(nsIAtom* aLocalName, int32_t aNamespaceID = kNameSpaceID_None) const; |
michael@0 | 105 | |
michael@0 | 106 | nsresult SetAndTakeMappedAttr(nsIAtom* aLocalName, nsAttrValue& aValue, |
michael@0 | 107 | nsMappedAttributeElement* aContent, |
michael@0 | 108 | nsHTMLStyleSheet* aSheet); |
michael@0 | 109 | nsresult SetMappedAttrStyleSheet(nsHTMLStyleSheet* aSheet) { |
michael@0 | 110 | if (!mImpl || !mImpl->mMappedAttrs) { |
michael@0 | 111 | return NS_OK; |
michael@0 | 112 | } |
michael@0 | 113 | return DoSetMappedAttrStyleSheet(aSheet); |
michael@0 | 114 | } |
michael@0 | 115 | void WalkMappedAttributeStyleRules(nsRuleWalker* aRuleWalker); |
michael@0 | 116 | |
michael@0 | 117 | void Compact(); |
michael@0 | 118 | |
michael@0 | 119 | bool CanFitMoreAttrs() const |
michael@0 | 120 | { |
michael@0 | 121 | return AttrSlotCount() < ATTRCHILD_ARRAY_MAX_ATTR_COUNT || |
michael@0 | 122 | !AttrSlotIsTaken(ATTRCHILD_ARRAY_MAX_ATTR_COUNT - 1); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; |
michael@0 | 126 | bool HasMappedAttrs() const |
michael@0 | 127 | { |
michael@0 | 128 | return MappedAttrCount(); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | private: |
michael@0 | 132 | nsAttrAndChildArray(const nsAttrAndChildArray& aOther) MOZ_DELETE; |
michael@0 | 133 | nsAttrAndChildArray& operator=(const nsAttrAndChildArray& aOther) MOZ_DELETE; |
michael@0 | 134 | |
michael@0 | 135 | void Clear(); |
michael@0 | 136 | |
michael@0 | 137 | uint32_t NonMappedAttrCount() const; |
michael@0 | 138 | uint32_t MappedAttrCount() const; |
michael@0 | 139 | |
michael@0 | 140 | // Returns a non-null zero-refcount object. |
michael@0 | 141 | nsMappedAttributes* |
michael@0 | 142 | GetModifiableMapped(nsMappedAttributeElement* aContent, |
michael@0 | 143 | nsHTMLStyleSheet* aSheet, |
michael@0 | 144 | bool aWillAddAttr); |
michael@0 | 145 | nsresult MakeMappedUnique(nsMappedAttributes* aAttributes); |
michael@0 | 146 | |
michael@0 | 147 | uint32_t AttrSlotsSize() const |
michael@0 | 148 | { |
michael@0 | 149 | return AttrSlotCount() * ATTRSIZE; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | uint32_t AttrSlotCount() const |
michael@0 | 153 | { |
michael@0 | 154 | return mImpl ? mImpl->mAttrAndChildCount & ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK : 0; |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | bool AttrSlotIsTaken(uint32_t aSlot) const |
michael@0 | 158 | { |
michael@0 | 159 | NS_PRECONDITION(aSlot < AttrSlotCount(), "out-of-bounds"); |
michael@0 | 160 | return mImpl->mBuffer[aSlot * ATTRSIZE]; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | void SetChildCount(uint32_t aCount) |
michael@0 | 164 | { |
michael@0 | 165 | mImpl->mAttrAndChildCount = |
michael@0 | 166 | (mImpl->mAttrAndChildCount & ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK) | |
michael@0 | 167 | (aCount << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | void SetAttrSlotCount(uint32_t aCount) |
michael@0 | 171 | { |
michael@0 | 172 | mImpl->mAttrAndChildCount = |
michael@0 | 173 | (mImpl->mAttrAndChildCount & ~ATTRCHILD_ARRAY_ATTR_SLOTS_COUNT_MASK) | |
michael@0 | 174 | aCount; |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | void SetAttrSlotAndChildCount(uint32_t aSlotCount, uint32_t aChildCount) |
michael@0 | 178 | { |
michael@0 | 179 | mImpl->mAttrAndChildCount = aSlotCount | |
michael@0 | 180 | (aChildCount << ATTRCHILD_ARRAY_ATTR_SLOTS_BITS); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | bool GrowBy(uint32_t aGrowSize); |
michael@0 | 184 | bool AddAttrSlot(); |
michael@0 | 185 | |
michael@0 | 186 | /** |
michael@0 | 187 | * Set *aPos to aChild and update sibling pointers as needed. aIndex is the |
michael@0 | 188 | * index at which aChild is actually being inserted. aChildCount is the |
michael@0 | 189 | * number of kids we had before the insertion. |
michael@0 | 190 | */ |
michael@0 | 191 | inline void SetChildAtPos(void** aPos, nsIContent* aChild, uint32_t aIndex, |
michael@0 | 192 | uint32_t aChildCount); |
michael@0 | 193 | |
michael@0 | 194 | /** |
michael@0 | 195 | * Guts of SetMappedAttrStyleSheet for the rare case when we have mapped attrs |
michael@0 | 196 | */ |
michael@0 | 197 | nsresult DoSetMappedAttrStyleSheet(nsHTMLStyleSheet* aSheet); |
michael@0 | 198 | |
michael@0 | 199 | struct InternalAttr |
michael@0 | 200 | { |
michael@0 | 201 | nsAttrName mName; |
michael@0 | 202 | nsAttrValue mValue; |
michael@0 | 203 | }; |
michael@0 | 204 | |
michael@0 | 205 | struct Impl { |
michael@0 | 206 | uint32_t mAttrAndChildCount; |
michael@0 | 207 | uint32_t mBufferSize; |
michael@0 | 208 | nsMappedAttributes* mMappedAttrs; |
michael@0 | 209 | void* mBuffer[1]; |
michael@0 | 210 | }; |
michael@0 | 211 | |
michael@0 | 212 | Impl* mImpl; |
michael@0 | 213 | }; |
michael@0 | 214 | |
michael@0 | 215 | #endif |