| File: | root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h |
| Warning: | line 1205, column 27 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* This Source Code Form is subject to the terms of the Mozilla Public | ||||
| 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this | ||||
| 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||||
| 4 | |||||
| 5 | #include "mozilla/a11y/PdfStructTreeBuilder.h" | ||||
| 6 | |||||
| 7 | #include "mozilla/ClearOnShutdown.h" | ||||
| 8 | #include "mozilla/StaticPrefs_accessibility.h" | ||||
| 9 | #include "mozilla/StaticPtr.h" | ||||
| 10 | #include "mozilla/a11y/DocAccessible.h" | ||||
| 11 | #include "mozilla/a11y/DocAccessibleParent.h" | ||||
| 12 | #include "mozilla/a11y/TableCellAccessible.h" | ||||
| 13 | #include "mozilla/dom/BrowserParent.h" | ||||
| 14 | #include "mozilla/dom/CanonicalBrowsingContext.h" | ||||
| 15 | #include "mozilla/dom/Document.h" | ||||
| 16 | #include "mozilla/dom/WindowGlobalParent.h" | ||||
| 17 | #include "skia/include/docs/SkPDFDocument.h" | ||||
| 18 | |||||
| 19 | namespace mozilla::a11y { | ||||
| 20 | |||||
| 21 | static void AccNameToPdfAlt(Accessible* aAcc, | ||||
| 22 | SkPDF::StructureElementNode& aPdf) { | ||||
| 23 | nsAutoString name; | ||||
| 24 | aAcc->Name(name); | ||||
| 25 | if (!name.IsEmpty()) { | ||||
| 26 | aPdf.fAlt = SkString(NS_ConvertUTF16toUTF8(name).get()); | ||||
| 27 | } | ||||
| 28 | } | ||||
| 29 | |||||
| 30 | // We use an array rather than a map for two reasons: | ||||
| 31 | // 1. There aren't likely to be many of these alive at once. | ||||
| 32 | // 2. Some functions need to find a builder associated with a descendant | ||||
| 33 | // WindowContext, not just the root. | ||||
| 34 | // If this turns out to be a problem, we could include ids for the root | ||||
| 35 | // WindowContext and any descendant WindowContexts in the map. | ||||
| 36 | static StaticAutoPtr<nsTArray<PdfStructTreeBuilder>> sBuilders; | ||||
| 37 | |||||
| 38 | /* static */ | ||||
| 39 | void PdfStructTreeBuilder::Init(dom::WindowContext* aWindowContext) { | ||||
| 40 | if (!StaticPrefs::accessibility_tagged_pdf_output_enabled()) { | ||||
| 41 | return; | ||||
| 42 | } | ||||
| 43 | if (!sBuilders) { | ||||
| 44 | sBuilders = new nsTArray<PdfStructTreeBuilder>(); | ||||
| 45 | if (NS_IsMainThread()) { | ||||
| 46 | ClearOnShutdown(&sBuilders); | ||||
| 47 | } | ||||
| 48 | } | ||||
| 49 | for (PdfStructTreeBuilder& builder : *sBuilders) { | ||||
| 50 | for (dom::WindowContext* ancestor = | ||||
| 51 | aWindowContext->GetParentWindowContext(); | ||||
| 52 | ancestor; ancestor = ancestor->GetParentWindowContext()) { | ||||
| 53 | if (ancestor->InnerWindowId() == builder.mRootInnerWindowId) { | ||||
| 54 | // This is an OOP iframe associated with an existing builder. | ||||
| 55 | builder.InitInternal(aWindowContext); | ||||
| 56 | return; | ||||
| 57 | } | ||||
| 58 | } | ||||
| 59 | } | ||||
| 60 | // This is a new document being printed. | ||||
| 61 | auto builder = sBuilders->EmplaceBack(aWindowContext->InnerWindowId()); | ||||
| 62 | builder->InitInternal(aWindowContext); | ||||
| 63 | } | ||||
| 64 | |||||
| 65 | /* static */ | ||||
| 66 | PdfStructTreeBuilder* PdfStructTreeBuilder::Get(uint64_t aInnerWindowId) { | ||||
| 67 | if (!sBuilders) { | ||||
| 68 | return nullptr; | ||||
| 69 | } | ||||
| 70 | for (PdfStructTreeBuilder& builder : *sBuilders) { | ||||
| 71 | if (builder.mRootInnerWindowId == aInnerWindowId) { | ||||
| 72 | return &builder; | ||||
| 73 | } | ||||
| 74 | } | ||||
| 75 | return nullptr; | ||||
| 76 | } | ||||
| 77 | |||||
| 78 | /* static */ | ||||
| 79 | void PdfStructTreeBuilder::Done(uint64_t aInnerWindowId) { | ||||
| 80 | if (!sBuilders) { | ||||
| 81 | return; | ||||
| 82 | } | ||||
| 83 | for (size_t i = 0; i < sBuilders->Length(); ++i) { | ||||
| 84 | if ((*sBuilders)[i].mRootInnerWindowId == aInnerWindowId) { | ||||
| 85 | sBuilders->RemoveElementAt(i); | ||||
| 86 | break; | ||||
| 87 | } | ||||
| 88 | } | ||||
| 89 | } | ||||
| 90 | |||||
| 91 | /* static */ | ||||
| 92 | int PdfStructTreeBuilder::GetPdfId(uint64_t aInnerWindowId, uint64_t aAccId) { | ||||
| 93 | if (NS_WARN_IF(!NS_IsMainThread())NS_warn_if_impl(!NS_IsMainThread(), "!NS_IsMainThread()", "/root/firefox-clang/accessible/pdf/PdfStructTreeBuilder.cpp" , 93)) { | ||||
| 94 | return SkPDF::NodeID::Nothing; | ||||
| 95 | } | ||||
| 96 | if (!sBuilders || sBuilders->IsEmpty()) { | ||||
| 97 | return SkPDF::NodeID::Nothing; | ||||
| 98 | } | ||||
| 99 | if (aInnerWindowId == 0) { | ||||
| 100 | // This indicates that the following drawing instructions are not associated | ||||
| 101 | // with anything in the struct tree; e.g. page headers and footers. | ||||
| 102 | switch (aAccId) { | ||||
| 103 | case SpecialId::Nothing: | ||||
| 104 | return SkPDF::NodeID::Nothing; | ||||
| 105 | case SpecialId::PageHeader: | ||||
| 106 | return SkPDF::NodeID::PaginationHeaderArtifact; | ||||
| 107 | case SpecialId::PageFooter: | ||||
| 108 | return SkPDF::NodeID::PaginationFooterArtifact; | ||||
| 109 | case SpecialId::OtherArtifact: | ||||
| 110 | return SkPDF::NodeID::OtherArtifact; | ||||
| 111 | default: | ||||
| 112 | MOZ_ASSERT_UNREACHABLE("Invalid special acc id")do { static_assert( mozilla::detail::AssertionConditionType< decltype(false)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while ( false); MOZ_ReportAssertionFailure("false" " (" "MOZ_ASSERT_UNREACHABLE: " "Invalid special acc id" ")", "/root/firefox-clang/accessible/pdf/PdfStructTreeBuilder.cpp" , 112); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") (" "MOZ_ASSERT_UNREACHABLE: " "Invalid special acc id" ")"); do { MOZ_CrashSequence(__null, 112); __attribute__((nomerge)) :: abort(); } while (false); } } while (false); | ||||
| 113 | } | ||||
| 114 | return SkPDF::NodeID::Nothing; | ||||
| 115 | } | ||||
| 116 | // aInnerWindowId might be a descendant WindowContext. Rather than walking the | ||||
| 117 | // WindowContext ancestry for each builder, we just ask each builder whether | ||||
| 118 | // it contains this id, since there won't be many builders. | ||||
| 119 | for (const PdfStructTreeBuilder& builder : *sBuilders) { | ||||
| 120 | if (int pdfId = builder.GetPdfIdInternal(aInnerWindowId, aAccId)) { | ||||
| 121 | return pdfId; | ||||
| 122 | } | ||||
| 123 | } | ||||
| 124 | return 0; | ||||
| 125 | } | ||||
| 126 | |||||
| 127 | /* static */ | ||||
| 128 | PdfStructTreeBuilder::GlobalAccessibleId PdfStructTreeBuilder::GetAccId( | ||||
| 129 | nsIFrame* aFrame) { | ||||
| 130 | if (!StaticPrefs::accessibility_tagged_pdf_output_enabled()) { | ||||
| 131 | return {}; | ||||
| 132 | } | ||||
| 133 | nsIContent* content = aFrame->GetContent(); | ||||
| 134 | if (!content) { | ||||
| 135 | return {}; | ||||
| 136 | } | ||||
| 137 | if (content->IsText()) { | ||||
| 138 | nsINode* parent = content->GetParent(); | ||||
| 139 | if (parent->IsMathMLElement()) { | ||||
| 140 | // Inside MathML, we can't represent a TextLeafAccessible as a separate | ||||
| 141 | // PDF structure element, as NonStruct isn't valid in a PDF MathML | ||||
| 142 | // subtree. Therefore, map any content to the parent MathML element. | ||||
| 143 | content = parent->AsContent(); | ||||
| 144 | } | ||||
| 145 | } | ||||
| 146 | dom::Document* doc = content->OwnerDoc(); | ||||
| 147 | // This should only ever be called for a document being printed and those are | ||||
| 148 | // always static documents. | ||||
| 149 | MOZ_ASSERT(doc->IsStaticDocument())do { static_assert( mozilla::detail::AssertionConditionType< decltype(doc->IsStaticDocument())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(doc->IsStaticDocument())) ), 0))) { do { } while (false); MOZ_ReportAssertionFailure("doc->IsStaticDocument()" , "/root/firefox-clang/accessible/pdf/PdfStructTreeBuilder.cpp" , 149); AnnotateMozCrashReason("MOZ_ASSERT" "(" "doc->IsStaticDocument()" ")"); do { MOZ_CrashSequence(__null, 149); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||
| 150 | DocAccessible* docAcc = GetExistingDocAccessible(doc); | ||||
| 151 | if (!docAcc) { | ||||
| 152 | return {}; | ||||
| 153 | } | ||||
| 154 | Accessible* acc = docAcc->GetAccessible(content); | ||||
| 155 | if (!acc) { | ||||
| 156 | return {}; | ||||
| 157 | } | ||||
| 158 | uint64_t innerWindowId = doc->InnerWindowID(); | ||||
| 159 | if (!innerWindowId) { | ||||
| 160 | return {}; | ||||
| 161 | } | ||||
| 162 | return {innerWindowId, acc->ID()}; | ||||
| 163 | } | ||||
| 164 | |||||
| 165 | PdfStructTreeBuilder::PdfStructTreeBuilder(uint64_t aInnerWindowId) | ||||
| 166 | : mRootInnerWindowId(aInnerWindowId) { | ||||
| 167 | mReadyPromise = new ReadyPromise::Private(__func__); | ||||
| 168 | } | ||||
| 169 | |||||
| 170 | void PdfStructTreeBuilder::InitInternal(dom::WindowContext* aWindowContext) { | ||||
| 171 | if (aWindowContext->InnerWindowId() != mRootInnerWindowId) { | ||||
| 172 | // We've just received the document for an out-of-process iframe. | ||||
| 173 | MOZ_ASSERT(mPendingOopIframes > 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mPendingOopIframes > 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(mPendingOopIframes > 0))) , 0))) { do { } while (false); MOZ_ReportAssertionFailure("mPendingOopIframes > 0" , "/root/firefox-clang/accessible/pdf/PdfStructTreeBuilder.cpp" , 173); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mPendingOopIframes > 0" ")"); do { MOZ_CrashSequence(__null, 173); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||
| 174 | --mPendingOopIframes; | ||||
| 175 | } | ||||
| 176 | if (dom::BrowserParent* bp = | ||||
| 177 | aWindowContext->Canonical()->GetBrowserParent()) { | ||||
| 178 | // Request the accessibility tree for each descendant out-of-process | ||||
| 179 | // iframe. While all of the direct children should be reachable, some of the | ||||
| 180 | // deeper descendants might not be yet, so we also traverse the descendants | ||||
| 181 | // for each OOP iframe when InitInternal is called for it , skipping any | ||||
| 182 | // that have already been requested. We could instead walk only the direct | ||||
| 183 | // children, but walking descendants means we benefit from greater | ||||
| 184 | // parallelism in the case that deeper descendants *are* already reachable. | ||||
| 185 | bp->VisitAllDescendants([this](dom::BrowserParent* descBp) { | ||||
| 186 | if (mRequestedBrowserParentIds.EnsureInserted(descBp->GetTabId())) { | ||||
| 187 | MOZ_ASSERT(!descBp->GetTopLevelDocAccessible())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!descBp->GetTopLevelDocAccessible())>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(!descBp->GetTopLevelDocAccessible()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!descBp->GetTopLevelDocAccessible()" , "/root/firefox-clang/accessible/pdf/PdfStructTreeBuilder.cpp" , 187); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!descBp->GetTopLevelDocAccessible()" ")"); do { MOZ_CrashSequence(__null, 187); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||
| 188 | (void)descBp->SendRequestDocAccessibleForPrint(); | ||||
| 189 | ++mPendingOopIframes; | ||||
| 190 | } | ||||
| 191 | }); | ||||
| 192 | } | ||||
| 193 | // XXX support out-of-process iframes inside a parent process document. | ||||
| 194 | if (mPendingOopIframes == 0) { | ||||
| 195 | // Once we've received all pending out-of-process iframes, we are ready to | ||||
| 196 | // build the PDF struct tree. | ||||
| 197 | mReadyPromise->Resolve(mozilla::Ok(), __func__); | ||||
| 198 | } | ||||
| 199 | } | ||||
| 200 | |||||
| 201 | bool PdfStructTreeBuilder::BuildStructTree(SkPDF::StructureElementNode& aRoot) { | ||||
| 202 | MOZ_ASSERT(!mBuilt, "Shouldn't try to build the struct tree more than once")do { static_assert( mozilla::detail::AssertionConditionType< decltype(!mBuilt)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!mBuilt))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!mBuilt" " (" "Shouldn't try to build the struct tree more than once" ")", "/root/firefox-clang/accessible/pdf/PdfStructTreeBuilder.cpp" , 202); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mBuilt" ") (" "Shouldn't try to build the struct tree more than once" ")") ; do { MOZ_CrashSequence(__null, 202); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||
| |||||
| 203 | RefPtr wgp = dom::WindowGlobalParent::GetByInnerWindowId(mRootInnerWindowId); | ||||
| 204 | if (!wgp) { | ||||
| 205 | return false; | ||||
| 206 | } | ||||
| 207 | Accessible* rootAcc = nullptr; | ||||
| 208 | if (wgp->IsInProcess()) { | ||||
| 209 | if (dom::Document* doc = wgp->GetDocument()) { | ||||
| 210 | rootAcc = GetExistingDocAccessible(doc); | ||||
| 211 | } | ||||
| 212 | } else { | ||||
| 213 | rootAcc = DocAccessibleParent::GetFrom(wgp); | ||||
| 214 | } | ||||
| 215 | if (!rootAcc) { | ||||
| 216 | return false; | ||||
| 217 | } | ||||
| 218 | BuildStructSubtree(rootAcc, aRoot); | ||||
| 219 | mBuilt = true; | ||||
| 220 | return true; | ||||
| 221 | } | ||||
| 222 | |||||
| 223 | // Returns the inner window id of the document containing aAcc. This must match | ||||
| 224 | // the id computed by GetAccId in the process rendering that document. | ||||
| 225 | static uint64_t InnerWindowIdFor(Accessible* aAcc) { | ||||
| 226 | if (RemoteAccessible* remoteAcc = aAcc->AsRemote()) { | ||||
| 227 | return remoteAcc->Document()->Manager()->InnerWindowId(); | ||||
| 228 | } | ||||
| 229 | return aAcc->AsLocal()->Document()->DocumentNode()->InnerWindowID(); | ||||
| 230 | } | ||||
| 231 | |||||
| 232 | int PdfStructTreeBuilder::GeneratePdfId(Accessible* aAcc) { | ||||
| 233 | // This can be called more than once for the same Accessible; e.g. when | ||||
| 234 | // referencing table cell headers. It should always return the same id for the | ||||
| 235 | // same Accessible. | ||||
| 236 | GlobalAccessibleId key = {InnerWindowIdFor(aAcc), aAcc->ID()}; | ||||
| 237 | auto entry = mAccToPdf.lookupForAdd(key); | ||||
| 238 | if (!entry) { | ||||
| 239 | // We haven't seen this Accessible before. Generate a new PDF id. | ||||
| 240 | MOZ_ALWAYS_TRUE(mAccToPdf.add(entry, key, ++mLastPdfId))do { if ((__builtin_expect(!!(mAccToPdf.add(entry, key, ++mLastPdfId )), 1))) { } else { do { do { } while (false); MOZ_ReportCrash ("" "mAccToPdf.add(entry, key, ++mLastPdfId)", "/root/firefox-clang/accessible/pdf/PdfStructTreeBuilder.cpp" , 240); AnnotateMozCrashReason("MOZ_CRASH(" "mAccToPdf.add(entry, key, ++mLastPdfId)" ")"); do { MOZ_CrashSequence(__null, 240); __attribute__((nomerge )) ::abort(); } while (false); } while (false); } } while (false ); | ||||
| 241 | } | ||||
| 242 | return entry->value(); | ||||
| 243 | } | ||||
| 244 | |||||
| 245 | void PdfStructTreeBuilder::BuildStructSubtree(Accessible* aAcc, | ||||
| 246 | SkPDF::StructureElementNode& aPdf, | ||||
| 247 | bool aInMath) { | ||||
| 248 | role accRole = aAcc->Role(); | ||||
| 249 | if (!aInMath
| ||||
| 250 | // MathML must be contained inside a Formula node. That doesn't exist in the | ||||
| 251 | // Gecko accessibility tree, so manufacture this wrapper here. | ||||
| 252 | aPdf.fNodeId = ++mLastPdfId; | ||||
| 253 | aPdf.fTypeString = "Formula"; | ||||
| 254 | aPdf.fChildVector.resize(1); | ||||
| 255 | aPdf.fChildVector[0] = std::make_unique<SkPDF::StructureElementNode>(); | ||||
| 256 | BuildStructSubtree(aAcc, *aPdf.fChildVector[0], true); | ||||
| 257 | return; | ||||
| 258 | } | ||||
| 259 | aPdf.fNodeId = GeneratePdfId(aAcc); | ||||
| 260 | switch (accRole) { | ||||
| 261 | case roles::ARTICLE: | ||||
| 262 | aPdf.fTypeString = "Art"; | ||||
| 263 | break; | ||||
| 264 | case roles::BLOCKQUOTE: | ||||
| 265 | aPdf.fTypeString = "BlockQuote"; | ||||
| 266 | break; | ||||
| 267 | case roles::CAPTION: | ||||
| 268 | aPdf.fTypeString = "Caption"; | ||||
| 269 | break; | ||||
| 270 | case roles::CELL: | ||||
| 271 | case roles::GRID_CELL: { | ||||
| 272 | aPdf.fTypeString = "TD"; | ||||
| 273 | TableCellAccessible* cell = aAcc->AsTableCell(); | ||||
| 274 | if (!cell) { | ||||
| 275 | break; | ||||
| 276 | } | ||||
| 277 | // Query each axis separately so one doesn't suppress the other's implicit | ||||
| 278 | // headers. | ||||
| 279 | nsTArray<Accessible*> accHeaders; | ||||
| 280 | cell->ColHeaderCells(&accHeaders); | ||||
| 281 | nsTArray<Accessible*> accRowHeaders; | ||||
| 282 | cell->RowHeaderCells(&accRowHeaders); | ||||
| 283 | accHeaders.AppendElements(std::move(accRowHeaders)); | ||||
| 284 | std::vector<int> pdfHeaders; | ||||
| 285 | pdfHeaders.reserve(accHeaders.Length()); | ||||
| 286 | for (Accessible* accHeader : accHeaders) { | ||||
| 287 | pdfHeaders.push_back(GeneratePdfId(accHeader)); | ||||
| 288 | } | ||||
| 289 | aPdf.fAttributes.appendNodeIdArray("Table", "Headers", pdfHeaders); | ||||
| 290 | break; | ||||
| 291 | } | ||||
| 292 | case roles::CODE: | ||||
| 293 | aPdf.fTypeString = "Code"; | ||||
| 294 | break; | ||||
| 295 | case roles::COLUMNHEADER: | ||||
| 296 | aPdf.fTypeString = "TH"; | ||||
| 297 | aPdf.fAttributes.appendName("Table", "Scope", "Column"); | ||||
| 298 | break; | ||||
| 299 | case roles::DOCUMENT: | ||||
| 300 | aPdf.fTypeString = "Document"; | ||||
| 301 | break; | ||||
| 302 | case roles::EMPHASIS: | ||||
| 303 | aPdf.fTypeString = "Em"; | ||||
| 304 | break; | ||||
| 305 | case roles::GRID: | ||||
| 306 | case roles::TABLE: | ||||
| 307 | case roles::TREE_TABLE: | ||||
| 308 | aPdf.fTypeString = "Table"; | ||||
| 309 | break; | ||||
| 310 | case roles::GROUPING: | ||||
| 311 | aPdf.fTypeString = "Div"; | ||||
| 312 | break; | ||||
| 313 | case roles::GRAPHIC: | ||||
| 314 | aPdf.fTypeString = "Figure"; | ||||
| 315 | AccNameToPdfAlt(aAcc, aPdf); | ||||
| 316 | // XXX We should ideally expose a BBox attribute, but how do we calculate | ||||
| 317 | // this? | ||||
| 318 | break; | ||||
| 319 | case roles::HEADING: { | ||||
| 320 | // For the PDF outline, SkPDF accumulates the title text itself from | ||||
| 321 | // the glyph runs drawn within the heading. However, if the accessible | ||||
| 322 | // name doesn't come from the heading's own content (e.g. aria-label), | ||||
| 323 | // that text never appears in the rendered glyphs, so provide it | ||||
| 324 | // explicitly as alt text in that case. | ||||
| 325 | nsAutoString name; | ||||
| 326 | if (aAcc->Name(name) != eNameFromSubtree && !name.IsEmpty()) { | ||||
| 327 | aPdf.fAlt = SkString(NS_ConvertUTF16toUTF8(name).get()); | ||||
| 328 | } | ||||
| 329 | int32_t level = aAcc->GroupPosition().level; | ||||
| 330 | // PDF has H1 through H6. | ||||
| 331 | if (1 <= level && level <= 6) { | ||||
| 332 | nsAutoCString type; | ||||
| 333 | type.AppendPrintf("H%d", level); | ||||
| 334 | aPdf.fTypeString = SkString(type.get()); | ||||
| 335 | break; | ||||
| 336 | } | ||||
| 337 | // Otherwise, use the generic H. | ||||
| 338 | aPdf.fTypeString = "H"; | ||||
| 339 | break; | ||||
| 340 | } | ||||
| 341 | case roles::LANDMARK: | ||||
| 342 | if (aAcc->LandmarkRole() == nsGkAtoms::complementary) { | ||||
| 343 | aPdf.fTypeString = "Aside"; | ||||
| 344 | } | ||||
| 345 | break; | ||||
| 346 | case roles::LINK: | ||||
| 347 | aPdf.fTypeString = "Link"; | ||||
| 348 | break; | ||||
| 349 | case roles::LIST: | ||||
| 350 | aPdf.fTypeString = "L"; | ||||
| 351 | break; | ||||
| 352 | case roles::LISTITEM: | ||||
| 353 | aPdf.fTypeString = "LI"; | ||||
| 354 | break; | ||||
| 355 | case roles::LISTITEM_MARKER: | ||||
| 356 | aPdf.fTypeString = "Lbl"; | ||||
| 357 | break; | ||||
| 358 | case roles::MATHML_CELL: | ||||
| 359 | aPdf.fTypeString = "mtd"; | ||||
| 360 | break; | ||||
| 361 | case roles::MATHML_EMPTY_GROUP: | ||||
| 362 | case roles::MATHML_ROW: | ||||
| 363 | case roles::MATHML_TEXT: { | ||||
| 364 | // These MathML roles are shared by several different elements which don't | ||||
| 365 | // have a common PDF tag, so we use the actual tag name instead. | ||||
| 366 | nsAutoString tagName; | ||||
| 367 | aAcc->TagName()->ToString(tagName); | ||||
| 368 | aPdf.fTypeString = SkString(NS_ConvertUTF16toUTF8(tagName).get()); | ||||
| 369 | break; | ||||
| 370 | } | ||||
| 371 | case roles::MATHML_ENCLOSED: | ||||
| 372 | aPdf.fTypeString = "menclose"; | ||||
| 373 | // XXX Expose attributes: notation | ||||
| 374 | break; | ||||
| 375 | case roles::MATHML_ERROR: | ||||
| 376 | aPdf.fTypeString = "merror"; | ||||
| 377 | break; | ||||
| 378 | case roles::MATHML_FRACTION: | ||||
| 379 | aPdf.fTypeString = "mfrac"; | ||||
| 380 | // XXX Expose attributes: bevelled, linethickness | ||||
| 381 | break; | ||||
| 382 | case roles::MATHML_GLYPH: | ||||
| 383 | aPdf.fTypeString = "mglyph"; | ||||
| 384 | break; | ||||
| 385 | case roles::MATHML_IDENTIFIER: | ||||
| 386 | aPdf.fTypeString = "mi"; | ||||
| 387 | break; | ||||
| 388 | case roles::MATHML_LABELED_ROW: | ||||
| 389 | aPdf.fTypeString = "mlabeledtr"; | ||||
| 390 | break; | ||||
| 391 | case roles::MATHML_LINK: | ||||
| 392 | aPdf.fTypeString = "a"; | ||||
| 393 | break; | ||||
| 394 | case roles::MATHML_LONG_DIVISION: | ||||
| 395 | aPdf.fTypeString = "mlongdiv"; | ||||
| 396 | // XXX Expose attributes: longdivstyle | ||||
| 397 | break; | ||||
| 398 | case roles::MATHML_MATH: | ||||
| 399 | aPdf.fTypeString = "math"; | ||||
| 400 | // We only specify the namespace on the math element, since an unspecified | ||||
| 401 | // namespace inherits from the parent. | ||||
| 402 | aPdf.fNamespace = "http://www.w3.org/1998/Math/MathML"; | ||||
| 403 | break; | ||||
| 404 | case roles::MATHML_MULTISCRIPTS: | ||||
| 405 | aPdf.fTypeString = "mmultiscripts"; | ||||
| 406 | break; | ||||
| 407 | case roles::MATHML_NUMBER: | ||||
| 408 | aPdf.fTypeString = "mn"; | ||||
| 409 | break; | ||||
| 410 | case roles::MATHML_OPERATOR: | ||||
| 411 | aPdf.fTypeString = "mo"; | ||||
| 412 | // XXX Expose attributes: accent, fence, separator, largeop | ||||
| 413 | break; | ||||
| 414 | case roles::MATHML_OVER: | ||||
| 415 | aPdf.fTypeString = "mover"; | ||||
| 416 | // XXX Expose attributes: accent, align | ||||
| 417 | break; | ||||
| 418 | case roles::MATHML_ROOT: | ||||
| 419 | aPdf.fTypeString = "mroot"; | ||||
| 420 | break; | ||||
| 421 | case roles::MATHML_SQUARE_ROOT: | ||||
| 422 | aPdf.fTypeString = "msqrt"; | ||||
| 423 | break; | ||||
| 424 | case roles::MATHML_STACK: | ||||
| 425 | aPdf.fTypeString = "mstack"; | ||||
| 426 | // XXX Expose attributes: align, position | ||||
| 427 | break; | ||||
| 428 | case roles::MATHML_STACK_CARRIES: | ||||
| 429 | aPdf.fTypeString = "mscarries"; | ||||
| 430 | // XXX Expose attributes: location, position | ||||
| 431 | break; | ||||
| 432 | case roles::MATHML_STACK_CARRY: | ||||
| 433 | aPdf.fTypeString = "mscarry"; | ||||
| 434 | // XXX Expose attributes: crossout | ||||
| 435 | break; | ||||
| 436 | case roles::MATHML_STACK_GROUP: | ||||
| 437 | aPdf.fTypeString = "msgroup"; | ||||
| 438 | // XXX Expose attributes: position, shift | ||||
| 439 | break; | ||||
| 440 | case roles::MATHML_STACK_LINE: | ||||
| 441 | aPdf.fTypeString = "msline"; | ||||
| 442 | // XXX Expose attributes: position | ||||
| 443 | break; | ||||
| 444 | case roles::MATHML_STACK_ROW: | ||||
| 445 | aPdf.fTypeString = "msrow"; | ||||
| 446 | // XXX Expose attributes: position | ||||
| 447 | break; | ||||
| 448 | case roles::MATHML_STYLE: | ||||
| 449 | aPdf.fTypeString = "mstyle"; | ||||
| 450 | break; | ||||
| 451 | case roles::MATHML_SUB: | ||||
| 452 | aPdf.fTypeString = "msub"; | ||||
| 453 | break; | ||||
| 454 | case roles::MATHML_SUB_SUP: | ||||
| 455 | aPdf.fTypeString = "msubsup"; | ||||
| 456 | break; | ||||
| 457 | case roles::MATHML_SUP: | ||||
| 458 | aPdf.fTypeString = "msup"; | ||||
| 459 | break; | ||||
| 460 | case roles::MATHML_TABLE: | ||||
| 461 | aPdf.fTypeString = "mtable"; | ||||
| 462 | // XXX Expose attributes: align, columnlines, rowlines | ||||
| 463 | break; | ||||
| 464 | case roles::MATHML_TABLE_ROW: | ||||
| 465 | aPdf.fTypeString = "mtr"; | ||||
| 466 | break; | ||||
| 467 | case roles::MATHML_UNDER: | ||||
| 468 | aPdf.fTypeString = "munder"; | ||||
| 469 | // XXX Expose attributes: accentunder, align | ||||
| 470 | break; | ||||
| 471 | case roles::MATHML_UNDER_OVER: | ||||
| 472 | aPdf.fTypeString = "munderover"; | ||||
| 473 | // XXX Expose attributes: accent, accentunder, align | ||||
| 474 | break; | ||||
| 475 | case roles::PARAGRAPH: | ||||
| 476 | aPdf.fTypeString = "P"; | ||||
| 477 | break; | ||||
| 478 | case roles::ROW: | ||||
| 479 | aPdf.fTypeString = "TR"; | ||||
| 480 | break; | ||||
| 481 | case roles::ROWHEADER: | ||||
| 482 | aPdf.fTypeString = "TH"; | ||||
| 483 | aPdf.fAttributes.appendName("Table", "Scope", "Row"); | ||||
| 484 | break; | ||||
| 485 | case roles::STRONG: | ||||
| 486 | aPdf.fTypeString = "Strong"; | ||||
| 487 | break; | ||||
| 488 | default: | ||||
| 489 | aPdf.fTypeString = "NonStruct"; | ||||
| 490 | } | ||||
| 491 | if (TableCellAccessible* cell = aAcc->AsTableCell()) { | ||||
| 492 | uint32_t rowSpan = cell->RowExtent(); | ||||
| 493 | if (rowSpan > 1) { | ||||
| 494 | aPdf.fAttributes.appendInt("Table", "RowSpan", static_cast<int>(rowSpan)); | ||||
| 495 | } | ||||
| 496 | uint32_t colSpan = cell->ColExtent(); | ||||
| 497 | if (colSpan > 1) { | ||||
| 498 | aPdf.fAttributes.appendInt("Table", "ColSpan", static_cast<int>(colSpan)); | ||||
| 499 | } | ||||
| 500 | } | ||||
| 501 | |||||
| 502 | uint32_t count = aAcc->ChildCount(); | ||||
| 503 | aPdf.fChildVector.resize(count); | ||||
| 504 | for (uint32_t c = 0; c < count; ++c) { | ||||
| 505 | aPdf.fChildVector[c] = std::make_unique<SkPDF::StructureElementNode>(); | ||||
| 506 | Accessible* child = aAcc->ChildAt(c); | ||||
| 507 | if (aInMath && child->IsTextLeaf()) { | ||||
| 508 | // For MathML, we map a TextLeafAccessible to the parent MathML element. | ||||
| 509 | // See GetAccId(). | ||||
| 510 | continue; | ||||
| 511 | } | ||||
| 512 | BuildStructSubtree(child, *aPdf.fChildVector[c], aInMath); | ||||
| 513 | } | ||||
| 514 | } | ||||
| 515 | |||||
| 516 | int PdfStructTreeBuilder::GetPdfIdInternal(uint64_t aInnerWindowId, | ||||
| 517 | uint64_t aAccId) const { | ||||
| 518 | if (auto entry = mAccToPdf.lookup({aInnerWindowId, aAccId})) { | ||||
| 519 | return entry->value(); | ||||
| 520 | } | ||||
| 521 | MOZ_ASSERT(!mBuilt,do { static_assert( mozilla::detail::AssertionConditionType< decltype(!mBuilt)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!mBuilt))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!mBuilt" " (" "Display list contains Accessible id which isn't in the map!" ")", "/root/firefox-clang/accessible/pdf/PdfStructTreeBuilder.cpp" , 522); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mBuilt" ") (" "Display list contains Accessible id which isn't in the map!" ")"); do { MOZ_CrashSequence(__null, 522); __attribute__((nomerge )) ::abort(); } while (false); } } while (false) | ||||
| 522 | "Display list contains Accessible id which isn't in the map!")do { static_assert( mozilla::detail::AssertionConditionType< decltype(!mBuilt)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!mBuilt))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!mBuilt" " (" "Display list contains Accessible id which isn't in the map!" ")", "/root/firefox-clang/accessible/pdf/PdfStructTreeBuilder.cpp" , 522); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mBuilt" ") (" "Display list contains Accessible id which isn't in the map!" ")"); do { MOZ_CrashSequence(__null, 522); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||
| 523 | return 0; | ||||
| 524 | } | ||||
| 525 | |||||
| 526 | } // namespace mozilla::a11y |
| 1 | /* This Source Code Form is subject to the terms of the Mozilla Public | |||
| 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this | |||
| 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |||
| 4 | ||||
| 5 | //--------------------------------------------------------------------------- | |||
| 6 | // Overview | |||
| 7 | //--------------------------------------------------------------------------- | |||
| 8 | // | |||
| 9 | // This file defines HashMap<Key, Value> and HashSet<T>, hash tables that are | |||
| 10 | // fast and have a nice API. | |||
| 11 | // | |||
| 12 | // Both hash tables have two optional template parameters. | |||
| 13 | // | |||
| 14 | // - HashPolicy. This defines the operations for hashing and matching keys. The | |||
| 15 | // default HashPolicy is appropriate when both of the following two | |||
| 16 | // conditions are true. | |||
| 17 | // | |||
| 18 | // - The key type stored in the table (|Key| for |HashMap<Key, Value>|, |T| | |||
| 19 | // for |HashSet<T>|) is an integer, pointer, UniquePtr, float, or double. | |||
| 20 | // | |||
| 21 | // - The type used for lookups (|Lookup|) is the same as the key type. This | |||
| 22 | // is usually the case, but not always. | |||
| 23 | // | |||
| 24 | // There is also a |CStringHasher| policy for |char*| keys. If your keys | |||
| 25 | // don't match any of the above cases, you must provide your own hash policy; | |||
| 26 | // see the "Hash Policy" section below. | |||
| 27 | // | |||
| 28 | // - AllocPolicy. This defines how allocations are done by the table. | |||
| 29 | // | |||
| 30 | // - |MallocAllocPolicy| is the default and is usually appropriate; note that | |||
| 31 | // operations (such as insertions) that might cause allocations are | |||
| 32 | // fallible and must be checked for OOM. These checks are enforced by the | |||
| 33 | // use of [[nodiscard]]. | |||
| 34 | // | |||
| 35 | // - |InfallibleAllocPolicy| is another possibility; it allows the | |||
| 36 | // abovementioned OOM checks to be done with MOZ_ALWAYS_TRUE(). | |||
| 37 | // | |||
| 38 | // Note that entry storage allocation is lazy, and not done until the first | |||
| 39 | // lookupForAdd(), put(), or putNew() is performed. | |||
| 40 | // | |||
| 41 | // See AllocPolicy.h for more details. | |||
| 42 | // | |||
| 43 | // Documentation on how to use HashMap and HashSet, including examples, is | |||
| 44 | // present within those classes. Search for "class HashMap" and "class | |||
| 45 | // HashSet". | |||
| 46 | // | |||
| 47 | // Both HashMap and HashSet are implemented on top of a third class, HashTable. | |||
| 48 | // You only need to look at HashTable if you want to understand the | |||
| 49 | // implementation. | |||
| 50 | // | |||
| 51 | // How does mozilla::HashTable (this file) compare with PLDHashTable (and its | |||
| 52 | // subclasses, such as nsTHashtable)? | |||
| 53 | // | |||
| 54 | // - mozilla::HashTable is a lot faster, largely because it uses templates | |||
| 55 | // throughout *and* inlines everything. PLDHashTable inlines operations much | |||
| 56 | // less aggressively, and also uses "virtual ops" for operations like hashing | |||
| 57 | // and matching entries that require function calls. | |||
| 58 | // | |||
| 59 | // - Correspondingly, mozilla::HashTable use is likely to increase executable | |||
| 60 | // size much more than PLDHashTable. | |||
| 61 | // | |||
| 62 | // - mozilla::HashTable has a nicer API, with a proper HashSet vs. HashMap | |||
| 63 | // distinction. | |||
| 64 | // | |||
| 65 | // - mozilla::HashTable requires more explicit OOM checking. As mentioned | |||
| 66 | // above, the use of |InfallibleAllocPolicy| can simplify things. | |||
| 67 | // | |||
| 68 | // - mozilla::HashTable has a default capacity on creation of 32 and a minimum | |||
| 69 | // capacity of 4. PLDHashTable has a default capacity on creation of 8 and a | |||
| 70 | // minimum capacity of 8. | |||
| 71 | ||||
| 72 | #ifndef mozilla_HashTable_h | |||
| 73 | #define mozilla_HashTable_h | |||
| 74 | ||||
| 75 | #include <bit> | |||
| 76 | #include <type_traits> | |||
| 77 | #include <utility> | |||
| 78 | ||||
| 79 | #include "mozilla/AllocPolicy.h" | |||
| 80 | #include "mozilla/Assertions.h" | |||
| 81 | #include "mozilla/Attributes.h" | |||
| 82 | #include "mozilla/Casting.h" | |||
| 83 | #include "mozilla/HashFunctions.h" | |||
| 84 | #include "mozilla/MathAlgorithms.h" | |||
| 85 | #include "mozilla/Maybe.h" | |||
| 86 | #include "mozilla/MemoryChecking.h" | |||
| 87 | #include "mozilla/MemoryReporting.h" | |||
| 88 | #include "mozilla/Opaque.h" | |||
| 89 | #include "mozilla/OperatorNewExtensions.h" | |||
| 90 | #include "mozilla/ReentrancyGuard.h" | |||
| 91 | #include "mozilla/UniquePtr.h" | |||
| 92 | #include "mozilla/WrappingOperations.h" | |||
| 93 | ||||
| 94 | namespace mozilla { | |||
| 95 | ||||
| 96 | template <class, class = void> | |||
| 97 | struct DefaultHasher; | |||
| 98 | ||||
| 99 | template <class, class> | |||
| 100 | class HashMapEntry; | |||
| 101 | ||||
| 102 | namespace detail { | |||
| 103 | ||||
| 104 | template <typename T> | |||
| 105 | class HashTableEntry; | |||
| 106 | ||||
| 107 | template <class T, class HashPolicy, class AllocPolicy> | |||
| 108 | class HashTable; | |||
| 109 | ||||
| 110 | } // namespace detail | |||
| 111 | ||||
| 112 | // The "generation" of a hash table is an opaque value indicating the state of | |||
| 113 | // modification of the hash table through its lifetime. If the generation of | |||
| 114 | // a hash table compares equal at times T1 and T2, then lookups in the hash | |||
| 115 | // table, pointers to (or into) hash table entries, etc. at time T1 are valid | |||
| 116 | // at time T2. If the generation compares unequal, these computations are all | |||
| 117 | // invalid and must be performed again to be used. | |||
| 118 | // | |||
| 119 | // Generations are meaningfully comparable only with respect to a single hash | |||
| 120 | // table. It's always nonsensical to compare the generation of distinct hash | |||
| 121 | // tables H1 and H2. | |||
| 122 | using Generation = Opaque<uint64_t>; | |||
| 123 | ||||
| 124 | //--------------------------------------------------------------------------- | |||
| 125 | // HashMap | |||
| 126 | //--------------------------------------------------------------------------- | |||
| 127 | ||||
| 128 | // HashMap is a fast hash-based map from keys to values. | |||
| 129 | // | |||
| 130 | // Template parameter requirements: | |||
| 131 | // - Key/Value: movable, destructible, assignable. | |||
| 132 | // - HashPolicy: see the "Hash Policy" section below. | |||
| 133 | // - AllocPolicy: see AllocPolicy.h. | |||
| 134 | // | |||
| 135 | // Note: | |||
| 136 | // - HashMap is not reentrant: Key/Value/HashPolicy/AllocPolicy members | |||
| 137 | // called by HashMap must not call back into the same HashMap object. | |||
| 138 | // | |||
| 139 | template <class Key, class Value, class HashPolicy = DefaultHasher<Key>, | |||
| 140 | class AllocPolicy = MallocAllocPolicy> | |||
| 141 | class MOZ_STANDALONE_DEBUG[[clang::standalone_debug]] HashMap { | |||
| 142 | // -- Implementation details ----------------------------------------------- | |||
| 143 | ||||
| 144 | using TableEntry = HashMapEntry<Key, Value>; | |||
| 145 | ||||
| 146 | struct MapHashPolicy : HashPolicy { | |||
| 147 | using Base = HashPolicy; | |||
| 148 | using KeyType = Key; | |||
| 149 | ||||
| 150 | static const Key& getKey(TableEntry& aEntry) { return aEntry.key(); } | |||
| 151 | ||||
| 152 | template <typename KeyInput> | |||
| 153 | static void setKey(TableEntry& aEntry, KeyInput&& aKey) { | |||
| 154 | HashPolicy::rekey(aEntry.mutableKey(), std::forward<KeyInput>(aKey)); | |||
| 155 | } | |||
| 156 | }; | |||
| 157 | ||||
| 158 | using Impl = detail::HashTable<TableEntry, MapHashPolicy, AllocPolicy>; | |||
| 159 | Impl mImpl; | |||
| 160 | ||||
| 161 | public: | |||
| 162 | using Lookup = typename HashPolicy::Lookup; | |||
| 163 | using Entry = TableEntry; | |||
| 164 | ||||
| 165 | // -- Initialization ------------------------------------------------------- | |||
| 166 | ||||
| 167 | constexpr explicit HashMap(AllocPolicy aAllocPolicy = AllocPolicy(), | |||
| 168 | uint32_t aLen = Impl::sDefaultLen) | |||
| 169 | : mImpl(std::move(aAllocPolicy), aLen) {} | |||
| 170 | ||||
| 171 | explicit HashMap(uint32_t aLen) : mImpl(AllocPolicy(), aLen) {} | |||
| 172 | ||||
| 173 | // HashMap is movable. | |||
| 174 | HashMap(HashMap&& aRhs) = default; | |||
| 175 | HashMap& operator=(HashMap&& aRhs) = default; | |||
| 176 | ||||
| 177 | // Swap the contents of this hash map with another. | |||
| 178 | void swap(HashMap& aOther) { mImpl.swap(aOther.mImpl); } | |||
| 179 | ||||
| 180 | // -- Status and sizing ---------------------------------------------------- | |||
| 181 | ||||
| 182 | // The map's current generation. | |||
| 183 | Generation generation() const { return mImpl.generation(); } | |||
| 184 | ||||
| 185 | // Is the map empty? | |||
| 186 | bool empty() const { return mImpl.empty(); } | |||
| 187 | ||||
| 188 | // Number of keys/values in the map. | |||
| 189 | uint32_t count() const { return mImpl.count(); } | |||
| 190 | ||||
| 191 | // Number of key/value slots in the map. Note: resize will happen well before | |||
| 192 | // count() == capacity(). | |||
| 193 | uint32_t capacity() const { return mImpl.capacity(); } | |||
| 194 | ||||
| 195 | // The size of the map's entry storage, in bytes. If the keys/values contain | |||
| 196 | // pointers to other heap blocks, you must iterate over the map and measure | |||
| 197 | // them separately; hence the "shallow" prefix. | |||
| 198 | size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { | |||
| 199 | return mImpl.shallowSizeOfExcludingThis(aMallocSizeOf); | |||
| 200 | } | |||
| 201 | size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { | |||
| 202 | return aMallocSizeOf(this) + | |||
| 203 | mImpl.shallowSizeOfExcludingThis(aMallocSizeOf); | |||
| 204 | } | |||
| 205 | ||||
| 206 | // Attempt to minimize the capacity(). If the table is empty, this will free | |||
| 207 | // the empty storage and upon regrowth it will be given the minimum capacity. | |||
| 208 | void compact() { mImpl.compact(); } | |||
| 209 | ||||
| 210 | // Attempt to reserve enough space to fit at least |aLen| elements. This is | |||
| 211 | // total capacity, including elements already present. Does nothing if the | |||
| 212 | // map already has sufficient capacity. | |||
| 213 | [[nodiscard]] bool reserve(uint32_t aLen) { return mImpl.reserve(aLen); } | |||
| 214 | ||||
| 215 | // HashMap is not copyable or assignable. | |||
| 216 | HashMap(const HashMap& hm) = delete; | |||
| 217 | HashMap& operator=(const HashMap& hm) = delete; | |||
| 218 | ||||
| 219 | // -- Lookups -------------------------------------------------------------- | |||
| 220 | ||||
| 221 | // Does the map contain a key/value matching |aLookup|? | |||
| 222 | bool has(const Lookup& aLookup) const { | |||
| 223 | return mImpl.lookup(aLookup).found(); | |||
| 224 | } | |||
| 225 | ||||
| 226 | // Return a Ptr indicating whether a key/value matching |aLookup| is | |||
| 227 | // present in the map. E.g.: | |||
| 228 | // | |||
| 229 | // using HM = HashMap<int,char>; | |||
| 230 | // HM h; | |||
| 231 | // if (HM::Ptr p = h.lookup(3)) { | |||
| 232 | // assert(p->key() == 3); | |||
| 233 | // char val = p->value(); | |||
| 234 | // } | |||
| 235 | // | |||
| 236 | using Ptr = typename Impl::Ptr; | |||
| 237 | MOZ_ALWAYS_INLINEinline Ptr lookup(const Lookup& aLookup) const { | |||
| 238 | return mImpl.lookup(aLookup); | |||
| 239 | } | |||
| 240 | ||||
| 241 | // Like lookup(), but does not assert if two threads call it at the same | |||
| 242 | // time. Only use this method when none of the threads will modify the map. | |||
| 243 | MOZ_ALWAYS_INLINEinline Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const { | |||
| 244 | return mImpl.readonlyThreadsafeLookup(aLookup); | |||
| 245 | } | |||
| 246 | ||||
| 247 | // -- Insertions ----------------------------------------------------------- | |||
| 248 | ||||
| 249 | // Overwrite existing value with |aValue|, or add it if not present. Returns | |||
| 250 | // false on OOM. | |||
| 251 | template <typename KeyInput, typename ValueInput> | |||
| 252 | [[nodiscard]] bool put(KeyInput&& aKey, ValueInput&& aValue) { | |||
| 253 | return put(aKey, std::forward<KeyInput>(aKey), | |||
| 254 | std::forward<ValueInput>(aValue)); | |||
| 255 | } | |||
| 256 | ||||
| 257 | template <typename KeyInput, typename ValueInput> | |||
| 258 | [[nodiscard]] bool put(const Lookup& aLookup, KeyInput&& aKey, | |||
| 259 | ValueInput&& aValue) { | |||
| 260 | AddPtr p = lookupForAdd(aLookup); | |||
| 261 | if (p) { | |||
| 262 | p->value() = std::forward<ValueInput>(aValue); | |||
| 263 | return true; | |||
| 264 | } | |||
| 265 | return add(p, std::forward<KeyInput>(aKey), | |||
| 266 | std::forward<ValueInput>(aValue)); | |||
| 267 | } | |||
| 268 | ||||
| 269 | // Like put(), but slightly faster. Must only be used when the given key is | |||
| 270 | // not already present. (In debug builds, assertions check this.) | |||
| 271 | template <typename KeyInput, typename ValueInput> | |||
| 272 | [[nodiscard]] bool putNew(KeyInput&& aKey, ValueInput&& aValue) { | |||
| 273 | return mImpl.putNew(aKey, std::forward<KeyInput>(aKey), | |||
| 274 | std::forward<ValueInput>(aValue)); | |||
| 275 | } | |||
| 276 | ||||
| 277 | template <typename KeyInput, typename ValueInput> | |||
| 278 | [[nodiscard]] bool putNew(const Lookup& aLookup, KeyInput&& aKey, | |||
| 279 | ValueInput&& aValue) { | |||
| 280 | return mImpl.putNew(aLookup, std::forward<KeyInput>(aKey), | |||
| 281 | std::forward<ValueInput>(aValue)); | |||
| 282 | } | |||
| 283 | ||||
| 284 | // Like putNew(), but should be only used when the table is known to be big | |||
| 285 | // enough for the insertion, and hashing cannot fail. Typically this is used | |||
| 286 | // to populate an empty map with known-unique keys after reserving space with | |||
| 287 | // reserve(), e.g. | |||
| 288 | // | |||
| 289 | // using HM = HashMap<int,char>; | |||
| 290 | // HM h; | |||
| 291 | // if (!h.reserve(3)) { | |||
| 292 | // MOZ_CRASH("OOM"); | |||
| 293 | // } | |||
| 294 | // h.putNewInfallible(1, 'a'); // unique key | |||
| 295 | // h.putNewInfallible(2, 'b'); // unique key | |||
| 296 | // h.putNewInfallible(3, 'c'); // unique key | |||
| 297 | // | |||
| 298 | template <typename KeyInput, typename ValueInput> | |||
| 299 | void putNewInfallible(KeyInput&& aKey, ValueInput&& aValue) { | |||
| 300 | mImpl.putNewInfallible(aKey, std::forward<KeyInput>(aKey), | |||
| 301 | std::forward<ValueInput>(aValue)); | |||
| 302 | } | |||
| 303 | ||||
| 304 | // Like |lookup(l)|, but on miss, |p = lookupForAdd(l)| allows efficient | |||
| 305 | // insertion of Key |k| (where |HashPolicy::match(k,l) == true|) using | |||
| 306 | // |add(p,k,v)|. After |add(p,k,v)|, |p| points to the new key/value. E.g.: | |||
| 307 | // | |||
| 308 | // using HM = HashMap<int,char>; | |||
| 309 | // HM h; | |||
| 310 | // HM::AddPtr p = h.lookupForAdd(3); | |||
| 311 | // if (!p) { | |||
| 312 | // if (!h.add(p, 3, 'a')) { | |||
| 313 | // return false; | |||
| 314 | // } | |||
| 315 | // } | |||
| 316 | // assert(p->key() == 3); | |||
| 317 | // char val = p->value(); | |||
| 318 | // | |||
| 319 | // N.B. The caller must ensure that no mutating hash table operations occur | |||
| 320 | // between a pair of lookupForAdd() and add() calls. To avoid looking up the | |||
| 321 | // key a second time, the caller may use the more efficient relookupOrAdd() | |||
| 322 | // method. This method reuses part of the hashing computation to more | |||
| 323 | // efficiently insert the key if it has not been added. For example, a | |||
| 324 | // mutation-handling version of the previous example: | |||
| 325 | // | |||
| 326 | // HM::AddPtr p = h.lookupForAdd(3); | |||
| 327 | // if (!p) { | |||
| 328 | // call_that_may_mutate_h(); | |||
| 329 | // if (!h.relookupOrAdd(p, 3, 'a')) { | |||
| 330 | // return false; | |||
| 331 | // } | |||
| 332 | // } | |||
| 333 | // assert(p->key() == 3); | |||
| 334 | // char val = p->value(); | |||
| 335 | // | |||
| 336 | using AddPtr = typename Impl::AddPtr; | |||
| 337 | MOZ_ALWAYS_INLINEinline AddPtr lookupForAdd(const Lookup& aLookup) { | |||
| 338 | return mImpl.lookupForAdd(aLookup); | |||
| 339 | } | |||
| 340 | ||||
| 341 | // Add a key/value. Returns false on OOM. | |||
| 342 | template <typename KeyInput, typename ValueInput> | |||
| 343 | [[nodiscard]] bool add(AddPtr& aPtr, KeyInput&& aKey, ValueInput&& aValue) { | |||
| 344 | return mImpl.add(aPtr, std::forward<KeyInput>(aKey), | |||
| 345 | std::forward<ValueInput>(aValue)); | |||
| 346 | } | |||
| 347 | ||||
| 348 | // See the comment above lookupForAdd() for details. | |||
| 349 | template <typename KeyInput, typename ValueInput> | |||
| 350 | [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, KeyInput&& aKey, | |||
| 351 | ValueInput&& aValue) { | |||
| 352 | return mImpl.relookupOrAdd(aPtr, aKey, std::forward<KeyInput>(aKey), | |||
| 353 | std::forward<ValueInput>(aValue)); | |||
| 354 | } | |||
| 355 | ||||
| 356 | // -- Removal -------------------------------------------------------------- | |||
| 357 | ||||
| 358 | // Lookup and remove the key/value matching |aLookup|, if present. | |||
| 359 | void remove(const Lookup& aLookup) { | |||
| 360 | if (Ptr p = lookup(aLookup)) { | |||
| 361 | remove(p); | |||
| 362 | } | |||
| 363 | } | |||
| 364 | ||||
| 365 | // Remove a previously found key/value (assuming aPtr.found()). The map must | |||
| 366 | // not have been mutated in the interim. | |||
| 367 | void remove(Ptr aPtr) { mImpl.remove(aPtr); } | |||
| 368 | ||||
| 369 | // Remove all keys/values without changing the capacity. | |||
| 370 | void clear() { mImpl.clear(); } | |||
| 371 | ||||
| 372 | // Like clear() followed by compact(). | |||
| 373 | void clearAndCompact() { mImpl.clearAndCompact(); } | |||
| 374 | ||||
| 375 | // -- Rekeying ------------------------------------------------------------- | |||
| 376 | ||||
| 377 | // Infallibly rekey one entry, if necessary. Requires Key to be rekeyable (via | |||
| 378 | // to HashPolicy::rekey) from Lookup. | |||
| 379 | void rekeyIfMoved(const Lookup& aOldKey, const Lookup& aNewKeyInput) { | |||
| 380 | if (aOldKey != aNewKeyInput) { | |||
| 381 | rekeyAs(aOldKey, aNewKeyInput, aNewKeyInput); | |||
| 382 | } | |||
| 383 | } | |||
| 384 | ||||
| 385 | // Infallibly rekey one entry if present, and return whether that happened. | |||
| 386 | template <typename KeyInput> | |||
| 387 | bool rekeyAs(const Lookup& aOldLookup, const Lookup& aNewLookup, | |||
| 388 | KeyInput&& aNewKey) { | |||
| 389 | if (Ptr p = lookup(aOldLookup)) { | |||
| 390 | mImpl.rekeyAndMaybeRehash(p, aNewLookup, std::forward<KeyInput>(aNewKey)); | |||
| 391 | return true; | |||
| 392 | } | |||
| 393 | return false; | |||
| 394 | } | |||
| 395 | ||||
| 396 | // -- Iteration ------------------------------------------------------------ | |||
| 397 | ||||
| 398 | // |iter()| returns an Iterator: | |||
| 399 | // | |||
| 400 | // HashMap<int, char> h; | |||
| 401 | // for (auto iter = h.iter(); !iter.done(); iter.next()) { | |||
| 402 | // char c = iter.get().value(); | |||
| 403 | // } | |||
| 404 | // | |||
| 405 | using Iterator = typename Impl::Iterator; | |||
| 406 | Iterator iter() const { return mImpl.iter(); } | |||
| 407 | ||||
| 408 | // |modIter()| returns a ModIterator: | |||
| 409 | // | |||
| 410 | // HashMap<int, char> h; | |||
| 411 | // for (auto iter = h.modIter(); !iter.done(); iter.next()) { | |||
| 412 | // if (iter.get().value() == 'l') { | |||
| 413 | // iter.remove(); | |||
| 414 | // } | |||
| 415 | // } | |||
| 416 | // | |||
| 417 | // Table resize may occur in ModIterator's destructor. | |||
| 418 | using ModIterator = typename Impl::ModIterator; | |||
| 419 | ModIterator modIter() { return mImpl.modIter(); } | |||
| 420 | ||||
| 421 | // -- Alloc policy --------------------------------------------------------- | |||
| 422 | ||||
| 423 | // Get the alloc policy. | |||
| 424 | const AllocPolicy& allocPolicy() const { return mImpl.allocPolicy(); } | |||
| 425 | AllocPolicy& allocPolicy() { return mImpl.allocPolicy(); } | |||
| 426 | ||||
| 427 | // For internal use by allocation policies that provide garbage collected | |||
| 428 | // memory. | |||
| 429 | // | |||
| 430 | // Trace any allocations owned by this object that were made with AllocPolicy. | |||
| 431 | // Call the supplied closure |aTraceFunc| for each of them, passing a double | |||
| 432 | // pointer to the memory held (e.g. a void** pointer). | |||
| 433 | template <typename F> | |||
| 434 | void traceOwnedAllocs(F&& aTraceFunc) { | |||
| 435 | mImpl.traceOwnedAllocs(std::forward<F>(aTraceFunc)); | |||
| 436 | } | |||
| 437 | ||||
| 438 | // -- Layout information for JIT access ------------------------------------ | |||
| 439 | ||||
| 440 | static size_t offsetOfHashShift() { | |||
| 441 | return offsetof(HashMap, mImpl)__builtin_offsetof(HashMap, mImpl) + Impl::offsetOfHashShift(); | |||
| 442 | } | |||
| 443 | static size_t offsetOfTable() { | |||
| 444 | return offsetof(HashMap, mImpl)__builtin_offsetof(HashMap, mImpl) + Impl::offsetOfTable(); | |||
| 445 | } | |||
| 446 | static size_t offsetOfEntryCount() { | |||
| 447 | return offsetof(HashMap, mImpl)__builtin_offsetof(HashMap, mImpl) + Impl::offsetOfEntryCount(); | |||
| 448 | } | |||
| 449 | }; | |||
| 450 | ||||
| 451 | //--------------------------------------------------------------------------- | |||
| 452 | // HashSet | |||
| 453 | //--------------------------------------------------------------------------- | |||
| 454 | ||||
| 455 | // HashSet is a fast hash-based set of values. | |||
| 456 | // | |||
| 457 | // Template parameter requirements: | |||
| 458 | // - T: movable, destructible, assignable. | |||
| 459 | // - HashPolicy: see the "Hash Policy" section below. | |||
| 460 | // - AllocPolicy: see AllocPolicy.h | |||
| 461 | // | |||
| 462 | // Note: | |||
| 463 | // - HashSet is not reentrant: T/HashPolicy/AllocPolicy members called by | |||
| 464 | // HashSet must not call back into the same HashSet object. | |||
| 465 | // | |||
| 466 | template <class T, class HashPolicy = DefaultHasher<T>, | |||
| 467 | class AllocPolicy = MallocAllocPolicy> | |||
| 468 | class HashSet { | |||
| 469 | // -- Implementation details ----------------------------------------------- | |||
| 470 | ||||
| 471 | struct SetHashPolicy : HashPolicy { | |||
| 472 | using Base = HashPolicy; | |||
| 473 | using KeyType = T; | |||
| 474 | ||||
| 475 | static const KeyType& getKey(const T& aT) { return aT; } | |||
| 476 | ||||
| 477 | template <typename KeyInput> | |||
| 478 | static void setKey(T& aT, KeyInput&& aKey) { | |||
| 479 | HashPolicy::rekey(aT, std::forward<KeyInput>(aKey)); | |||
| 480 | } | |||
| 481 | }; | |||
| 482 | ||||
| 483 | using Impl = detail::HashTable<const T, SetHashPolicy, AllocPolicy>; | |||
| 484 | Impl mImpl; | |||
| 485 | ||||
| 486 | public: | |||
| 487 | using Lookup = typename HashPolicy::Lookup; | |||
| 488 | using Entry = T; | |||
| 489 | ||||
| 490 | // -- Initialization ------------------------------------------------------- | |||
| 491 | ||||
| 492 | explicit HashSet(AllocPolicy aAllocPolicy = AllocPolicy(), | |||
| 493 | uint32_t aLen = Impl::sDefaultLen) | |||
| 494 | : mImpl(std::move(aAllocPolicy), aLen) {} | |||
| 495 | ||||
| 496 | explicit HashSet(uint32_t aLen) : mImpl(AllocPolicy(), aLen) {} | |||
| 497 | ||||
| 498 | // HashSet is movable. | |||
| 499 | HashSet(HashSet&& aRhs) = default; | |||
| 500 | HashSet& operator=(HashSet&& aRhs) = default; | |||
| 501 | ||||
| 502 | // HashSet is not copyable or assignable. | |||
| 503 | HashSet(const HashSet& hs) = delete; | |||
| 504 | HashSet& operator=(const HashSet& hs) = delete; | |||
| 505 | ||||
| 506 | // Swap the contents of this hash set with another. | |||
| 507 | void swap(HashSet& aOther) { mImpl.swap(aOther.mImpl); } | |||
| 508 | ||||
| 509 | // -- Status and sizing ---------------------------------------------------- | |||
| 510 | ||||
| 511 | // The set's current generation. | |||
| 512 | Generation generation() const { return mImpl.generation(); } | |||
| 513 | ||||
| 514 | // Is the set empty? | |||
| 515 | bool empty() const { return mImpl.empty(); } | |||
| 516 | ||||
| 517 | // Number of elements in the set. | |||
| 518 | uint32_t count() const { return mImpl.count(); } | |||
| 519 | ||||
| 520 | // Number of element slots in the set. Note: resize will happen well before | |||
| 521 | // count() == capacity(). | |||
| 522 | uint32_t capacity() const { return mImpl.capacity(); } | |||
| 523 | ||||
| 524 | // The size of the set's entry storage, in bytes. If the elements contain | |||
| 525 | // pointers to other heap blocks, you must iterate over the set and measure | |||
| 526 | // them separately; hence the "shallow" prefix. | |||
| 527 | size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { | |||
| 528 | return mImpl.shallowSizeOfExcludingThis(aMallocSizeOf); | |||
| 529 | } | |||
| 530 | size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { | |||
| 531 | return aMallocSizeOf(this) + | |||
| 532 | mImpl.shallowSizeOfExcludingThis(aMallocSizeOf); | |||
| 533 | } | |||
| 534 | ||||
| 535 | // Attempt to minimize the capacity(). If the table is empty, this will free | |||
| 536 | // the empty storage and upon regrowth it will be given the minimum capacity. | |||
| 537 | void compact() { mImpl.compact(); } | |||
| 538 | ||||
| 539 | // Attempt to reserve enough space to fit at least |aLen| elements. This is | |||
| 540 | // total capacity, including elements already present. Does nothing if the | |||
| 541 | // map already has sufficient capacity. | |||
| 542 | [[nodiscard]] bool reserve(uint32_t aLen) { return mImpl.reserve(aLen); } | |||
| 543 | ||||
| 544 | // -- Lookups -------------------------------------------------------------- | |||
| 545 | ||||
| 546 | // Does the set contain an element matching |aLookup|? | |||
| 547 | bool has(const Lookup& aLookup) const { | |||
| 548 | return mImpl.lookup(aLookup).found(); | |||
| 549 | } | |||
| 550 | ||||
| 551 | // Return a Ptr indicating whether an element matching |aLookup| is present | |||
| 552 | // in the set. E.g.: | |||
| 553 | // | |||
| 554 | // using HS = HashSet<int>; | |||
| 555 | // HS h; | |||
| 556 | // if (HS::Ptr p = h.lookup(3)) { | |||
| 557 | // assert(*p == 3); // p acts like a pointer to int | |||
| 558 | // } | |||
| 559 | // | |||
| 560 | using Ptr = typename Impl::Ptr; | |||
| 561 | MOZ_ALWAYS_INLINEinline Ptr lookup(const Lookup& aLookup) const { | |||
| 562 | return mImpl.lookup(aLookup); | |||
| 563 | } | |||
| 564 | ||||
| 565 | // Like lookup(), but does not assert if two threads call it at the same | |||
| 566 | // time. Only use this method when none of the threads will modify the set. | |||
| 567 | MOZ_ALWAYS_INLINEinline Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const { | |||
| 568 | return mImpl.readonlyThreadsafeLookup(aLookup); | |||
| 569 | } | |||
| 570 | ||||
| 571 | // -- Insertions ----------------------------------------------------------- | |||
| 572 | ||||
| 573 | // Add |aU| if it is not present already. Returns false on OOM. | |||
| 574 | template <typename U> | |||
| 575 | [[nodiscard]] bool put(U&& aU) { | |||
| 576 | AddPtr p = lookupForAdd(aU); | |||
| 577 | return p ? true : add(p, std::forward<U>(aU)); | |||
| 578 | } | |||
| 579 | ||||
| 580 | // Like put(), but slightly faster. Must only be used when the given element | |||
| 581 | // is not already present. (In debug builds, assertions check this.) | |||
| 582 | template <typename U> | |||
| 583 | [[nodiscard]] bool putNew(U&& aU) { | |||
| 584 | return mImpl.putNew(aU, std::forward<U>(aU)); | |||
| 585 | } | |||
| 586 | ||||
| 587 | // Like the other putNew(), but for when |Lookup| is different to |T|. | |||
| 588 | template <typename U> | |||
| 589 | [[nodiscard]] bool putNew(const Lookup& aLookup, U&& aU) { | |||
| 590 | return mImpl.putNew(aLookup, std::forward<U>(aU)); | |||
| 591 | } | |||
| 592 | ||||
| 593 | // Like putNew(), but should be only used when the table is known to be big | |||
| 594 | // enough for the insertion, and hashing cannot fail. Typically this is used | |||
| 595 | // to populate an empty set with known-unique elements after reserving space | |||
| 596 | // with reserve(), e.g. | |||
| 597 | // | |||
| 598 | // using HS = HashMap<int>; | |||
| 599 | // HS h; | |||
| 600 | // if (!h.reserve(3)) { | |||
| 601 | // MOZ_CRASH("OOM"); | |||
| 602 | // } | |||
| 603 | // h.putNewInfallible(1); // unique element | |||
| 604 | // h.putNewInfallible(2); // unique element | |||
| 605 | // h.putNewInfallible(3); // unique element | |||
| 606 | // | |||
| 607 | template <typename U> | |||
| 608 | void putNewInfallible(const Lookup& aLookup, U&& aU) { | |||
| 609 | mImpl.putNewInfallible(aLookup, std::forward<U>(aU)); | |||
| 610 | } | |||
| 611 | ||||
| 612 | // Like |lookup(l)|, but on miss, |p = lookupForAdd(l)| allows efficient | |||
| 613 | // insertion of T value |t| (where |HashPolicy::match(t,l) == true|) using | |||
| 614 | // |add(p,t)|. After |add(p,t)|, |p| points to the new element. E.g.: | |||
| 615 | // | |||
| 616 | // using HS = HashSet<int>; | |||
| 617 | // HS h; | |||
| 618 | // HS::AddPtr p = h.lookupForAdd(3); | |||
| 619 | // if (!p) { | |||
| 620 | // if (!h.add(p, 3)) { | |||
| 621 | // return false; | |||
| 622 | // } | |||
| 623 | // } | |||
| 624 | // assert(*p == 3); // p acts like a pointer to int | |||
| 625 | // | |||
| 626 | // N.B. The caller must ensure that no mutating hash table operations occur | |||
| 627 | // between a pair of lookupForAdd() and add() calls. To avoid looking up the | |||
| 628 | // key a second time, the caller may use the more efficient relookupOrAdd() | |||
| 629 | // method. This method reuses part of the hashing computation to more | |||
| 630 | // efficiently insert the key if it has not been added. For example, a | |||
| 631 | // mutation-handling version of the previous example: | |||
| 632 | // | |||
| 633 | // HS::AddPtr p = h.lookupForAdd(3); | |||
| 634 | // if (!p) { | |||
| 635 | // call_that_may_mutate_h(); | |||
| 636 | // if (!h.relookupOrAdd(p, 3, 3)) { | |||
| 637 | // return false; | |||
| 638 | // } | |||
| 639 | // } | |||
| 640 | // assert(*p == 3); | |||
| 641 | // | |||
| 642 | // Note that relookupOrAdd(p,l,t) performs Lookup using |l| and adds the | |||
| 643 | // entry |t|, where the caller ensures match(l,t). | |||
| 644 | using AddPtr = typename Impl::AddPtr; | |||
| 645 | MOZ_ALWAYS_INLINEinline AddPtr lookupForAdd(const Lookup& aLookup) { | |||
| 646 | return mImpl.lookupForAdd(aLookup); | |||
| 647 | } | |||
| 648 | ||||
| 649 | // Add an element. Returns false on OOM. | |||
| 650 | template <typename U> | |||
| 651 | [[nodiscard]] bool add(AddPtr& aPtr, U&& aU) { | |||
| 652 | return mImpl.add(aPtr, std::forward<U>(aU)); | |||
| 653 | } | |||
| 654 | ||||
| 655 | // See the comment above lookupForAdd() for details. | |||
| 656 | template <typename U> | |||
| 657 | [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, const Lookup& aLookup, | |||
| 658 | U&& aU) { | |||
| 659 | return mImpl.relookupOrAdd(aPtr, aLookup, std::forward<U>(aU)); | |||
| 660 | } | |||
| 661 | ||||
| 662 | // -- Removal -------------------------------------------------------------- | |||
| 663 | ||||
| 664 | // Lookup and remove the element matching |aLookup|, if present. | |||
| 665 | void remove(const Lookup& aLookup) { | |||
| 666 | if (Ptr p = lookup(aLookup)) { | |||
| 667 | remove(p); | |||
| 668 | } | |||
| 669 | } | |||
| 670 | ||||
| 671 | // Remove a previously found element (assuming aPtr.found()). The set must | |||
| 672 | // not have been mutated in the interim. | |||
| 673 | void remove(Ptr aPtr) { mImpl.remove(aPtr); } | |||
| 674 | ||||
| 675 | // Remove all keys/values without changing the capacity. | |||
| 676 | void clear() { mImpl.clear(); } | |||
| 677 | ||||
| 678 | // Like clear() followed by compact(). | |||
| 679 | void clearAndCompact() { mImpl.clearAndCompact(); } | |||
| 680 | ||||
| 681 | // -- Rekeying ------------------------------------------------------------- | |||
| 682 | ||||
| 683 | // Infallibly rekey one entry, if necessary. Requires Key be rekeyable (via | |||
| 684 | // to HashPolicy::rekey) from Lookup. | |||
| 685 | void rekeyIfMoved(const Lookup& aOldValue, const Lookup& aNewValue) { | |||
| 686 | if (aOldValue != aNewValue) { | |||
| 687 | rekeyAs(aOldValue, aNewValue, aNewValue); | |||
| 688 | } | |||
| 689 | } | |||
| 690 | ||||
| 691 | // Infallibly rekey one entry if present, and return whether that happened. | |||
| 692 | template <typename U> | |||
| 693 | bool rekeyAs(const Lookup& aOldLookup, const Lookup& aNewLookup, | |||
| 694 | U&& aNewValue) { | |||
| 695 | if (Ptr p = lookup(aOldLookup)) { | |||
| 696 | mImpl.rekeyAndMaybeRehash(p, aNewLookup, std::forward<U>(aNewValue)); | |||
| 697 | return true; | |||
| 698 | } | |||
| 699 | return false; | |||
| 700 | } | |||
| 701 | ||||
| 702 | // Infallibly replace the current key at |aPtr| with an equivalent key. | |||
| 703 | // Specifically, both HashPolicy::hash and HashPolicy::match must return | |||
| 704 | // identical results for the new and old key when applied against all | |||
| 705 | // possible matching values. | |||
| 706 | template <typename U> | |||
| 707 | void replaceKey(Ptr aPtr, const Lookup& aLookup, U&& aNewValue) { | |||
| 708 | MOZ_ASSERT(aPtr.found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(aPtr.found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aPtr.found()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("aPtr.found()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 708); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.found()" ")"); do { MOZ_CrashSequence(__null, 708); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 709 | MOZ_ASSERT(HashPolicy::match(*aPtr, aLookup))do { static_assert( mozilla::detail::AssertionConditionType< decltype(HashPolicy::match(*aPtr, aLookup))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(HashPolicy::match(*aPtr, aLookup )))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("HashPolicy::match(*aPtr, aLookup)", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 709); AnnotateMozCrashReason("MOZ_ASSERT" "(" "HashPolicy::match(*aPtr, aLookup)" ")"); do { MOZ_CrashSequence(__null, 709); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 710 | MOZ_ASSERT(*aPtr != aNewValue)do { static_assert( mozilla::detail::AssertionConditionType< decltype(*aPtr != aNewValue)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(*aPtr != aNewValue))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("*aPtr != aNewValue" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 710); AnnotateMozCrashReason("MOZ_ASSERT" "(" "*aPtr != aNewValue" ")"); do { MOZ_CrashSequence(__null, 710); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 711 | const_cast<T&>(*aPtr) = std::forward<U>(aNewValue); | |||
| 712 | MOZ_ASSERT(*lookup(aLookup) == aNewValue)do { static_assert( mozilla::detail::AssertionConditionType< decltype(*lookup(aLookup) == aNewValue)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(*lookup(aLookup) == aNewValue ))), 0))) { do { } while (false); MOZ_ReportAssertionFailure( "*lookup(aLookup) == aNewValue", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 712); AnnotateMozCrashReason("MOZ_ASSERT" "(" "*lookup(aLookup) == aNewValue" ")"); do { MOZ_CrashSequence(__null, 712); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 713 | } | |||
| 714 | void replaceKey(Ptr aPtr, const T& aNewValue) { | |||
| 715 | replaceKey(aPtr, aNewValue, aNewValue); | |||
| 716 | } | |||
| 717 | ||||
| 718 | // -- Iteration ------------------------------------------------------------ | |||
| 719 | ||||
| 720 | // |iter()| returns an Iterator: | |||
| 721 | // | |||
| 722 | // HashSet<int> h; | |||
| 723 | // for (auto iter = h.iter(); !iter.done(); iter.next()) { | |||
| 724 | // int i = iter.get(); | |||
| 725 | // } | |||
| 726 | // | |||
| 727 | using Iterator = typename Impl::Iterator; | |||
| 728 | Iterator iter() const { return mImpl.iter(); } | |||
| 729 | ||||
| 730 | // |modIter()| returns a ModIterator: | |||
| 731 | // | |||
| 732 | // HashSet<int> h; | |||
| 733 | // for (auto iter = h.modIter(); !iter.done(); iter.next()) { | |||
| 734 | // if (iter.get() == 42) { | |||
| 735 | // iter.remove(); | |||
| 736 | // } | |||
| 737 | // } | |||
| 738 | // | |||
| 739 | // Table resize may occur in ModIterator's destructor. | |||
| 740 | using ModIterator = typename Impl::ModIterator; | |||
| 741 | ModIterator modIter() { return mImpl.modIter(); } | |||
| 742 | ||||
| 743 | // -- Alloc policy --------------------------------------------------------- | |||
| 744 | ||||
| 745 | // Get the alloc policy. | |||
| 746 | const AllocPolicy& allocPolicy() const { return mImpl.allocPolicy(); } | |||
| 747 | AllocPolicy& allocPolicy() { return mImpl.allocPolicy(); } | |||
| 748 | ||||
| 749 | // For internal use by allocation policies that provide garbage collected | |||
| 750 | // memory. | |||
| 751 | // | |||
| 752 | // Trace any allocations owned by this object that were made with AllocPolicy. | |||
| 753 | // Call the supplied closure |aTraceFunc| for each of them, passing a double | |||
| 754 | // pointer to the memory held (e.g. a void** pointer). | |||
| 755 | template <typename F> | |||
| 756 | void traceOwnedAllocs(F&& aTraceFunc) { | |||
| 757 | mImpl.traceOwnedAllocs(std::forward<F>(aTraceFunc)); | |||
| 758 | } | |||
| 759 | }; | |||
| 760 | ||||
| 761 | //--------------------------------------------------------------------------- | |||
| 762 | // Hash Policy | |||
| 763 | //--------------------------------------------------------------------------- | |||
| 764 | ||||
| 765 | // A hash policy |HP| for a hash table with key-type |Key| must provide: | |||
| 766 | // | |||
| 767 | // - a type |HP::Lookup| to use to lookup table entries; | |||
| 768 | // | |||
| 769 | // - a static member function |HP::hash| that hashes lookup values: | |||
| 770 | // | |||
| 771 | // static mozilla::HashNumber hash(const Lookup&); | |||
| 772 | // | |||
| 773 | // - a static member function |HP::match| that tests equality of key and | |||
| 774 | // lookup values: | |||
| 775 | // | |||
| 776 | // static bool match(const Key& aKey, const Lookup& aLookup); | |||
| 777 | // | |||
| 778 | // |aKey| and |aLookup| can have different hash numbers, only when a | |||
| 779 | // collision happens with |prepareHash| operation, which is less frequent. | |||
| 780 | // Thus, |HP::match| shouldn't assume the hash equality in the comparison, | |||
| 781 | // even if the hash numbers are almost always same between them. | |||
| 782 | // | |||
| 783 | // Normally, Lookup = Key. In general, though, different values and types of | |||
| 784 | // values can be used to lookup and store. If a Lookup value |l| is not equal | |||
| 785 | // to the added Key value |k|, the user must ensure that |HP::match(k,l)| is | |||
| 786 | // true. E.g.: | |||
| 787 | // | |||
| 788 | // mozilla::HashSet<Key, HP>::AddPtr p = h.lookup(l); | |||
| 789 | // if (!p) { | |||
| 790 | // assert(HP::match(k, l)); // must hold | |||
| 791 | // h.add(p, k); | |||
| 792 | // } | |||
| 793 | ||||
| 794 | // A pointer hashing policy that uses HashGeneric() to create good hashes for | |||
| 795 | // pointers. Note that we don't shift out the lowest k bits because we don't | |||
| 796 | // want to assume anything about the alignment of the pointers. | |||
| 797 | template <typename Key> | |||
| 798 | struct PointerHasher { | |||
| 799 | static_assert(std::is_pointer_v<Key>); | |||
| 800 | ||||
| 801 | using Lookup = Key; | |||
| 802 | ||||
| 803 | static HashNumber hash(const Lookup& aLookup) { return HashGeneric(aLookup); } | |||
| 804 | ||||
| 805 | static bool match(const Key& aKey, const Lookup& aLookup) { | |||
| 806 | return aKey == aLookup; | |||
| 807 | } | |||
| 808 | ||||
| 809 | static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; } | |||
| 810 | }; | |||
| 811 | ||||
| 812 | // The default hash policy, which only works with integers. | |||
| 813 | template <class Key, typename> | |||
| 814 | struct DefaultHasher { | |||
| 815 | using Lookup = Key; | |||
| 816 | ||||
| 817 | static HashNumber hash(const Lookup& aLookup) { | |||
| 818 | // Just convert the integer to a HashNumber and use that as is. (This | |||
| 819 | // discards the high 32-bits of 64-bit integers!) ScrambleHashCode() is | |||
| 820 | // subsequently called on the value to improve the distribution. | |||
| 821 | return aLookup; | |||
| 822 | } | |||
| 823 | ||||
| 824 | static bool match(const Key& aKey, const Lookup& aLookup) { | |||
| 825 | // Use builtin or overloaded operator==. | |||
| 826 | return aKey == aLookup; | |||
| 827 | } | |||
| 828 | ||||
| 829 | static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; } | |||
| 830 | }; | |||
| 831 | ||||
| 832 | // A DefaultHasher specialization for enums. | |||
| 833 | template <class T> | |||
| 834 | struct DefaultHasher<T, std::enable_if_t<std::is_enum_v<T>>> { | |||
| 835 | using Key = T; | |||
| 836 | using Lookup = Key; | |||
| 837 | ||||
| 838 | static HashNumber hash(const Lookup& aLookup) { return HashGeneric(aLookup); } | |||
| 839 | ||||
| 840 | static bool match(const Key& aKey, const Lookup& aLookup) { | |||
| 841 | // Use builtin or overloaded operator==. | |||
| 842 | return aKey == static_cast<Key>(aLookup); | |||
| 843 | } | |||
| 844 | ||||
| 845 | static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; } | |||
| 846 | }; | |||
| 847 | ||||
| 848 | // A DefaultHasher specialization for pointers. | |||
| 849 | template <class T> | |||
| 850 | struct DefaultHasher<T*> : PointerHasher<T*> {}; | |||
| 851 | ||||
| 852 | // A DefaultHasher specialization for mozilla::UniquePtr. | |||
| 853 | template <class T, class D> | |||
| 854 | struct DefaultHasher<UniquePtr<T, D>> { | |||
| 855 | using Key = UniquePtr<T, D>; | |||
| 856 | using Lookup = Key; | |||
| 857 | using PtrHasher = PointerHasher<T*>; | |||
| 858 | ||||
| 859 | static HashNumber hash(const Lookup& aLookup) { | |||
| 860 | return PtrHasher::hash(aLookup.get()); | |||
| 861 | } | |||
| 862 | ||||
| 863 | static bool match(const Key& aKey, const Lookup& aLookup) { | |||
| 864 | return PtrHasher::match(aKey.get(), aLookup.get()); | |||
| 865 | } | |||
| 866 | ||||
| 867 | static void rekey(Key& aKey, Key&& aNewKey) { aKey = std::move(aNewKey); } | |||
| 868 | }; | |||
| 869 | ||||
| 870 | // A DefaultHasher specialization for doubles. | |||
| 871 | template <> | |||
| 872 | struct DefaultHasher<double> { | |||
| 873 | using Key = double; | |||
| 874 | using Lookup = Key; | |||
| 875 | ||||
| 876 | static HashNumber hash(const Lookup& aLookup) { | |||
| 877 | // Just xor the high bits with the low bits, and then treat the bits of the | |||
| 878 | // result as a uint32_t. | |||
| 879 | static_assert(sizeof(HashNumber) == 4, | |||
| 880 | "subsequent code assumes a four-byte hash"); | |||
| 881 | uint64_t u = BitwiseCast<uint64_t>(aLookup); | |||
| 882 | return HashNumber(u ^ (u >> 32)); | |||
| 883 | } | |||
| 884 | ||||
| 885 | static bool match(const Key& aKey, const Lookup& aLookup) { | |||
| 886 | return BitwiseCast<uint64_t>(aKey) == BitwiseCast<uint64_t>(aLookup); | |||
| 887 | } | |||
| 888 | }; | |||
| 889 | ||||
| 890 | // A DefaultHasher specialization for floats. | |||
| 891 | template <> | |||
| 892 | struct DefaultHasher<float> { | |||
| 893 | using Key = float; | |||
| 894 | using Lookup = Key; | |||
| 895 | ||||
| 896 | static HashNumber hash(const Lookup& aLookup) { | |||
| 897 | // Just use the value as if its bits form an integer. ScrambleHashCode() is | |||
| 898 | // subsequently called on the value to improve the distribution. | |||
| 899 | static_assert(sizeof(HashNumber) == 4, | |||
| 900 | "subsequent code assumes a four-byte hash"); | |||
| 901 | return HashNumber(BitwiseCast<uint32_t>(aLookup)); | |||
| 902 | } | |||
| 903 | ||||
| 904 | static bool match(const Key& aKey, const Lookup& aLookup) { | |||
| 905 | return BitwiseCast<uint32_t>(aKey) == BitwiseCast<uint32_t>(aLookup); | |||
| 906 | } | |||
| 907 | }; | |||
| 908 | ||||
| 909 | // A hash policy for C strings. | |||
| 910 | struct CStringHasher { | |||
| 911 | using Key = const char*; | |||
| 912 | using Lookup = const char*; | |||
| 913 | ||||
| 914 | static HashNumber hash(const Lookup& aLookup) { | |||
| 915 | return HashString(aLookup, strlen(aLookup)); | |||
| 916 | } | |||
| 917 | ||||
| 918 | static bool match(const Key& aKey, const Lookup& aLookup) { | |||
| 919 | return strcmp(aKey, aLookup) == 0; | |||
| 920 | } | |||
| 921 | }; | |||
| 922 | ||||
| 923 | //--------------------------------------------------------------------------- | |||
| 924 | // Fallible Hashing Interface | |||
| 925 | //--------------------------------------------------------------------------- | |||
| 926 | ||||
| 927 | // Most of the time generating a hash code is infallible, but sometimes it is | |||
| 928 | // necessary to generate hash codes on demand in a way that can fail. Specialize | |||
| 929 | // this class for your own hash policy to provide fallible hashing. | |||
| 930 | // | |||
| 931 | // This is used by MovableCellHasher to handle the fact that generating a unique | |||
| 932 | // ID for cell pointer may fail due to OOM. | |||
| 933 | // | |||
| 934 | // The default implementations of these methods delegate to the usual HashPolicy | |||
| 935 | // implementation and always succeed. | |||
| 936 | template <typename HashPolicy> | |||
| 937 | struct FallibleHashMethods { | |||
| 938 | // Return true if a hashcode is already available for its argument, and | |||
| 939 | // sets |aHashOut|. Once this succeeds for a specific argument it | |||
| 940 | // must continue to do so. | |||
| 941 | // | |||
| 942 | // Return false if a hashcode is not already available. This implies that any | |||
| 943 | // lookup must fail, as the hash code would have to have been successfully | |||
| 944 | // created on insertion. | |||
| 945 | template <typename Lookup> | |||
| 946 | static bool maybeGetHash(Lookup&& aLookup, HashNumber* aHashOut) { | |||
| 947 | *aHashOut = HashPolicy::hash(aLookup); | |||
| 948 | return true; | |||
| 949 | } | |||
| 950 | ||||
| 951 | // Fallible method to ensure a hashcode exists for its argument and create one | |||
| 952 | // if not. Sets |aHashOut| to the hashcode and retuns true on success. Returns | |||
| 953 | // false on error, e.g. out of memory. | |||
| 954 | template <typename Lookup> | |||
| 955 | static bool ensureHash(Lookup&& aLookup, HashNumber* aHashOut) { | |||
| 956 | *aHashOut = HashPolicy::hash(aLookup); | |||
| 957 | return true; | |||
| 958 | } | |||
| 959 | }; | |||
| 960 | ||||
| 961 | template <typename HashPolicy, typename Lookup> | |||
| 962 | bool MaybeGetHash(Lookup&& aLookup, HashNumber* aHashOut) { | |||
| 963 | return FallibleHashMethods<typename HashPolicy::Base>::maybeGetHash( | |||
| 964 | std::forward<Lookup>(aLookup), aHashOut); | |||
| 965 | } | |||
| 966 | ||||
| 967 | template <typename HashPolicy, typename Lookup> | |||
| 968 | bool EnsureHash(Lookup&& aLookup, HashNumber* aHashOut) { | |||
| 969 | return FallibleHashMethods<typename HashPolicy::Base>::ensureHash( | |||
| 970 | std::forward<Lookup>(aLookup), aHashOut); | |||
| 971 | } | |||
| 972 | ||||
| 973 | //--------------------------------------------------------------------------- | |||
| 974 | // Implementation Details (HashMapEntry, HashTableEntry, HashTable) | |||
| 975 | //--------------------------------------------------------------------------- | |||
| 976 | ||||
| 977 | // Both HashMap and HashSet are implemented by a single HashTable that is even | |||
| 978 | // more heavily parameterized than the other two. This leaves HashTable gnarly | |||
| 979 | // and extremely coupled to HashMap and HashSet; thus code should not use | |||
| 980 | // HashTable directly. | |||
| 981 | ||||
| 982 | template <class Key, class Value> | |||
| 983 | class HashMapEntry { | |||
| 984 | Key key_; | |||
| 985 | Value value_; | |||
| 986 | ||||
| 987 | template <class, class, class> | |||
| 988 | friend class detail::HashTable; | |||
| 989 | template <class> | |||
| 990 | friend class detail::HashTableEntry; | |||
| 991 | template <class, class, class, class> | |||
| 992 | friend class HashMap; | |||
| 993 | ||||
| 994 | public: | |||
| 995 | template <typename KeyInput, typename ValueInput> | |||
| 996 | HashMapEntry(KeyInput&& aKey, ValueInput&& aValue) | |||
| 997 | : key_(std::forward<KeyInput>(aKey)), | |||
| 998 | value_(std::forward<ValueInput>(aValue)) {} | |||
| 999 | ||||
| 1000 | HashMapEntry(HashMapEntry&& aRhs) = default; | |||
| 1001 | HashMapEntry& operator=(HashMapEntry&& aRhs) = default; | |||
| 1002 | ||||
| 1003 | using KeyType = Key; | |||
| 1004 | using ValueType = Value; | |||
| 1005 | ||||
| 1006 | const Key& key() const { return key_; } | |||
| 1007 | ||||
| 1008 | // Use this method with caution! If the key is changed such that its hash | |||
| 1009 | // value also changes, the map will be left in an invalid state. | |||
| 1010 | Key& mutableKey() { return key_; } | |||
| 1011 | ||||
| 1012 | const Value& value() const { return value_; } | |||
| 1013 | Value& value() { return value_; } | |||
| 1014 | ||||
| 1015 | static size_t offsetOfKey() { return offsetof(HashMapEntry, key_)__builtin_offsetof(HashMapEntry, key_); } | |||
| 1016 | static size_t offsetOfValue() { return offsetof(HashMapEntry, value_)__builtin_offsetof(HashMapEntry, value_); } | |||
| 1017 | ||||
| 1018 | HashMapEntry(const HashMapEntry&) = delete; | |||
| 1019 | void operator=(const HashMapEntry&) = delete; | |||
| 1020 | }; | |||
| 1021 | ||||
| 1022 | namespace detail { | |||
| 1023 | ||||
| 1024 | static const HashNumber kHashTableFreeKey = 0; | |||
| 1025 | static const HashNumber kHashTableRemovedKey = 1; | |||
| 1026 | static const HashNumber kHashTableCollisionBit = 1; | |||
| 1027 | ||||
| 1028 | template <class T, class HashPolicy, class AllocPolicy> | |||
| 1029 | class HashTable; | |||
| 1030 | ||||
| 1031 | template <typename T> | |||
| 1032 | class EntrySlot; | |||
| 1033 | ||||
| 1034 | template <typename T> | |||
| 1035 | class HashTableEntry { | |||
| 1036 | private: | |||
| 1037 | using NonConstT = std::remove_const_t<T>; | |||
| 1038 | ||||
| 1039 | // Instead of having a hash table entry store that looks like this: | |||
| 1040 | // | |||
| 1041 | // +--------+--------+--------+--------+ | |||
| 1042 | // | entry0 | entry1 | .... | entryN | | |||
| 1043 | // +--------+--------+--------+--------+ | |||
| 1044 | // | |||
| 1045 | // where the entries contained their cached hash code, we're going to lay out | |||
| 1046 | // the entry store thusly: | |||
| 1047 | // | |||
| 1048 | // +-------+-------+-------+-------+--------+--------+--------+--------+ | |||
| 1049 | // | hash0 | hash1 | ... | hashN | entry0 | entry1 | .... | entryN | | |||
| 1050 | // +-------+-------+-------+-------+--------+--------+--------+--------+ | |||
| 1051 | // | |||
| 1052 | // with all the cached hashes prior to the actual entries themselves. | |||
| 1053 | // | |||
| 1054 | // We do this because implementing the first strategy requires us to make | |||
| 1055 | // HashTableEntry look roughly like: | |||
| 1056 | // | |||
| 1057 | // template <typename T> | |||
| 1058 | // class HashTableEntry { | |||
| 1059 | // HashNumber mKeyHash; | |||
| 1060 | // T mValue; | |||
| 1061 | // }; | |||
| 1062 | // | |||
| 1063 | // The problem with this setup is that, depending on the layout of `T`, there | |||
| 1064 | // may be platform ABI-mandated padding between `mKeyHash` and the first | |||
| 1065 | // member of `T`. This ABI-mandated padding is wasted space, and can be | |||
| 1066 | // surprisingly common, e.g. when `T` is a single pointer on 64-bit platforms. | |||
| 1067 | // In such cases, we're throwing away a quarter of our entry store on padding, | |||
| 1068 | // which is undesirable. | |||
| 1069 | // | |||
| 1070 | // The second layout above, namely: | |||
| 1071 | // | |||
| 1072 | // +-------+-------+-------+-------+--------+--------+--------+--------+ | |||
| 1073 | // | hash0 | hash1 | ... | hashN | entry0 | entry1 | .... | entryN | | |||
| 1074 | // +-------+-------+-------+-------+--------+--------+--------+--------+ | |||
| 1075 | // | |||
| 1076 | // means there is no wasted space between the hashes themselves, and no wasted | |||
| 1077 | // space between the entries themselves. However, we would also like there to | |||
| 1078 | // be no gap between the last hash and the first entry. The memory allocator | |||
| 1079 | // guarantees the alignment of the start of the hashes. The use of a | |||
| 1080 | // power-of-two capacity of at least 4 guarantees that the alignment of the | |||
| 1081 | // *end* of the hash array is no less than the alignment of the start. | |||
| 1082 | // Finally, the static_asserts here guarantee that the entries themselves | |||
| 1083 | // don't need to be any more aligned than the alignment of the entry store | |||
| 1084 | // itself. | |||
| 1085 | // | |||
| 1086 | // This assertion is safe for 32-bit builds because on both Windows and Linux | |||
| 1087 | // (including Android), the minimum alignment for allocations larger than 8 | |||
| 1088 | // bytes is 8 bytes, and the actual data for entries in our entry store is | |||
| 1089 | // guaranteed to have that alignment as well, thanks to the power-of-two | |||
| 1090 | // number of cached hash values stored prior to the entry data. | |||
| 1091 | ||||
| 1092 | // The allocation policy must allocate a table with at least this much | |||
| 1093 | // alignment. | |||
| 1094 | static constexpr size_t kMinimumAlignment = 8; | |||
| 1095 | ||||
| 1096 | static_assert(alignof(HashNumber) <= kMinimumAlignment, | |||
| 1097 | "[N*2 hashes, N*2 T values] allocation's alignment must be " | |||
| 1098 | "enough to align each hash"); | |||
| 1099 | static_assert(alignof(NonConstT) <= 2 * sizeof(HashNumber), | |||
| 1100 | "subsequent N*2 T values must not require more than an even " | |||
| 1101 | "number of HashNumbers provides"); | |||
| 1102 | ||||
| 1103 | static const HashNumber sFreeKey = kHashTableFreeKey; | |||
| 1104 | static const HashNumber sRemovedKey = kHashTableRemovedKey; | |||
| 1105 | static const HashNumber sCollisionBit = kHashTableCollisionBit; | |||
| 1106 | ||||
| 1107 | alignas(NonConstT) unsigned char mValueData[sizeof(NonConstT)]; | |||
| 1108 | ||||
| 1109 | private: | |||
| 1110 | template <class, class, class> | |||
| 1111 | friend class HashTable; | |||
| 1112 | template <typename> | |||
| 1113 | friend class EntrySlot; | |||
| 1114 | ||||
| 1115 | // Some versions of GCC treat it as a -Wstrict-aliasing violation (ergo a | |||
| 1116 | // -Werror compile error) to reinterpret_cast<> |mValueData| to |T*|, even | |||
| 1117 | // through |void*|. Placing the latter cast in these separate functions | |||
| 1118 | // breaks the chain such that affected GCC versions no longer warn/error. | |||
| 1119 | void* rawValuePtr() { return mValueData; } | |||
| 1120 | ||||
| 1121 | static bool isLiveHash(HashNumber hash) { return hash > sRemovedKey; } | |||
| 1122 | ||||
| 1123 | NonConstT* valuePtr() { return reinterpret_cast<NonConstT*>(rawValuePtr()); } | |||
| 1124 | ||||
| 1125 | void destroyStoredT() { | |||
| 1126 | NonConstT* ptr = valuePtr(); | |||
| 1127 | ptr->~T(); | |||
| 1128 | MOZ_MAKE_MEM_UNDEFINED(ptr, sizeof(*ptr))do { } while (0); | |||
| 1129 | } | |||
| 1130 | ||||
| 1131 | public: | |||
| 1132 | HashTableEntry() = default; | |||
| 1133 | ||||
| 1134 | ~HashTableEntry() { MOZ_MAKE_MEM_UNDEFINED(this, sizeof(*this))do { } while (0); } | |||
| 1135 | ||||
| 1136 | HashTableEntry(const HashTableEntry&) = delete; | |||
| 1137 | void operator=(const HashTableEntry&) = delete; | |||
| 1138 | ||||
| 1139 | void destroy() { destroyStoredT(); } | |||
| 1140 | ||||
| 1141 | void swap(HashTableEntry* aOther, bool aOtherIsLive) { | |||
| 1142 | // This allows types to use Argument-Dependent-Lookup, and thus use a custom | |||
| 1143 | // std::swap, which is needed by types like JS::Heap and such. | |||
| 1144 | using std::swap; | |||
| 1145 | ||||
| 1146 | if (this == aOther) { | |||
| 1147 | return; | |||
| 1148 | } | |||
| 1149 | if (aOtherIsLive) { | |||
| 1150 | swap(*valuePtr(), *aOther->valuePtr()); | |||
| 1151 | } else { | |||
| 1152 | new (KnownNotNull, aOther->valuePtr()) NonConstT(std::move(*valuePtr())); | |||
| 1153 | destroy(); | |||
| 1154 | } | |||
| 1155 | } | |||
| 1156 | ||||
| 1157 | T& get() { return *valuePtr(); } | |||
| 1158 | ||||
| 1159 | NonConstT& getMutable() { return *valuePtr(); } | |||
| 1160 | }; | |||
| 1161 | ||||
| 1162 | // A slot represents a cached hash value and its associated entry stored | |||
| 1163 | // in the hash table. These two things are not stored in contiguous memory. | |||
| 1164 | template <class T> | |||
| 1165 | class EntrySlot { | |||
| 1166 | using NonConstT = std::remove_const_t<T>; | |||
| 1167 | ||||
| 1168 | using Entry = HashTableEntry<T>; | |||
| 1169 | ||||
| 1170 | Entry* mEntry; | |||
| 1171 | HashNumber* mKeyHash; | |||
| 1172 | ||||
| 1173 | template <class, class, class> | |||
| 1174 | friend class HashTable; | |||
| 1175 | ||||
| 1176 | EntrySlot(Entry* aEntry, HashNumber* aKeyHash) | |||
| 1177 | : mEntry(aEntry), mKeyHash(aKeyHash) {} | |||
| 1178 | ||||
| 1179 | public: | |||
| 1180 | static bool isLiveHash(HashNumber hash) { return hash > Entry::sRemovedKey; } | |||
| 1181 | ||||
| 1182 | EntrySlot(const EntrySlot&) = default; | |||
| 1183 | EntrySlot(EntrySlot&& aOther) = default; | |||
| 1184 | ||||
| 1185 | EntrySlot& operator=(const EntrySlot&) = default; | |||
| 1186 | EntrySlot& operator=(EntrySlot&&) = default; | |||
| 1187 | ||||
| 1188 | bool operator==(const EntrySlot& aRhs) const { return mEntry == aRhs.mEntry; } | |||
| 1189 | ||||
| 1190 | bool operator<(const EntrySlot& aRhs) const { return mEntry < aRhs.mEntry; } | |||
| 1191 | ||||
| 1192 | EntrySlot& operator++() { | |||
| 1193 | ++mEntry; | |||
| 1194 | ++mKeyHash; | |||
| 1195 | return *this; | |||
| 1196 | } | |||
| 1197 | ||||
| 1198 | void destroy() { mEntry->destroy(); } | |||
| 1199 | ||||
| 1200 | void swap(EntrySlot& aOther) { | |||
| 1201 | mEntry->swap(aOther.mEntry, aOther.isLive()); | |||
| 1202 | std::swap(*mKeyHash, *aOther.mKeyHash); | |||
| 1203 | } | |||
| 1204 | ||||
| 1205 | T& get() const { return mEntry->get(); } | |||
| ||||
| 1206 | ||||
| 1207 | NonConstT& getMutable() { return mEntry->getMutable(); } | |||
| 1208 | ||||
| 1209 | bool isFree() const { return *mKeyHash == Entry::sFreeKey; } | |||
| 1210 | ||||
| 1211 | void clearLive() { | |||
| 1212 | MOZ_ASSERT(isLive())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isLive())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isLive()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isLive()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1212); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { MOZ_CrashSequence(__null, 1212); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1213 | *mKeyHash = Entry::sFreeKey; | |||
| 1214 | mEntry->destroyStoredT(); | |||
| 1215 | } | |||
| 1216 | ||||
| 1217 | void clear() { | |||
| 1218 | if (isLive()) { | |||
| 1219 | mEntry->destroyStoredT(); | |||
| 1220 | } | |||
| 1221 | MOZ_MAKE_MEM_UNDEFINED(mEntry, sizeof(*mEntry))do { } while (0); | |||
| 1222 | *mKeyHash = Entry::sFreeKey; | |||
| 1223 | } | |||
| 1224 | ||||
| 1225 | bool isRemoved() const { return *mKeyHash == Entry::sRemovedKey; } | |||
| 1226 | ||||
| 1227 | void removeLive() { | |||
| 1228 | MOZ_ASSERT(isLive())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isLive())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isLive()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isLive()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1228); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { MOZ_CrashSequence(__null, 1228); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1229 | *mKeyHash = Entry::sRemovedKey; | |||
| 1230 | mEntry->destroyStoredT(); | |||
| 1231 | } | |||
| 1232 | ||||
| 1233 | bool isLive() const { return isLiveHash(*mKeyHash); } | |||
| 1234 | ||||
| 1235 | void setCollision() { | |||
| 1236 | MOZ_ASSERT(isLive())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isLive())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isLive()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isLive()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1236); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { MOZ_CrashSequence(__null, 1236); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1237 | *mKeyHash |= Entry::sCollisionBit; | |||
| 1238 | } | |||
| 1239 | void unsetCollision() { *mKeyHash &= ~Entry::sCollisionBit; } | |||
| 1240 | bool hasCollision() const { return *mKeyHash & Entry::sCollisionBit; } | |||
| 1241 | bool matchHash(HashNumber hn) { | |||
| 1242 | return (*mKeyHash & ~Entry::sCollisionBit) == hn; | |||
| 1243 | } | |||
| 1244 | HashNumber getKeyHash() const { return *mKeyHash & ~Entry::sCollisionBit; } | |||
| 1245 | ||||
| 1246 | template <typename... Args> | |||
| 1247 | void setLive(HashNumber aHashNumber, Args&&... aArgs) { | |||
| 1248 | MOZ_ASSERT(!isLive())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!isLive())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!isLive()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!isLive()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1248); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!isLive()" ")" ); do { MOZ_CrashSequence(__null, 1248); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1249 | *mKeyHash = aHashNumber; | |||
| 1250 | new (KnownNotNull, mEntry->valuePtr()) T(std::forward<Args>(aArgs)...); | |||
| 1251 | MOZ_ASSERT(isLive())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isLive())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isLive()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isLive()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1251); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { MOZ_CrashSequence(__null, 1251); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1252 | } | |||
| 1253 | ||||
| 1254 | Entry* toEntry() const { return mEntry; } | |||
| 1255 | }; | |||
| 1256 | ||||
| 1257 | template <class T, class HashPolicy, class AllocPolicy> | |||
| 1258 | class MOZ_STANDALONE_DEBUG[[clang::standalone_debug]] HashTable : private AllocPolicy { | |||
| 1259 | friend class mozilla::ReentrancyGuard; | |||
| 1260 | ||||
| 1261 | using NonConstT = std::remove_const_t<T>; | |||
| 1262 | using Key = typename HashPolicy::KeyType; | |||
| 1263 | using Lookup = typename HashPolicy::Lookup; | |||
| 1264 | ||||
| 1265 | public: | |||
| 1266 | using Entry = HashTableEntry<T>; | |||
| 1267 | using Slot = EntrySlot<T>; | |||
| 1268 | ||||
| 1269 | template <typename F> | |||
| 1270 | static void forEachSlot(char* aTable, uint32_t aCapacity, F&& f) { | |||
| 1271 | auto hashes = reinterpret_cast<HashNumber*>(aTable); | |||
| 1272 | auto entries = reinterpret_cast<Entry*>(&hashes[aCapacity]); | |||
| 1273 | Slot slot(entries, hashes); | |||
| 1274 | for (size_t i = 0; i < size_t(aCapacity); ++i) { | |||
| 1275 | f(slot); | |||
| 1276 | ++slot; | |||
| 1277 | } | |||
| 1278 | } | |||
| 1279 | ||||
| 1280 | // A nullable pointer to a hash table element. A Ptr |p| can be tested | |||
| 1281 | // either explicitly |if (p.found()) p->...| or using boolean conversion | |||
| 1282 | // |if (p) p->...|. Ptr objects must not be used after any mutating hash | |||
| 1283 | // table operations unless |generation()| is tested. | |||
| 1284 | class Ptr { | |||
| 1285 | friend class HashTable; | |||
| 1286 | ||||
| 1287 | Slot mSlot; | |||
| 1288 | #ifdef DEBUG1 | |||
| 1289 | const HashTable* mTable; | |||
| 1290 | Generation mGeneration; | |||
| 1291 | #endif | |||
| 1292 | ||||
| 1293 | protected: | |||
| 1294 | Ptr(Slot aSlot, const HashTable& aTable) | |||
| 1295 | : mSlot(aSlot) | |||
| 1296 | #ifdef DEBUG1 | |||
| 1297 | , | |||
| 1298 | mTable(&aTable), | |||
| 1299 | mGeneration(aTable.generation()) | |||
| 1300 | #endif | |||
| 1301 | { | |||
| 1302 | } | |||
| 1303 | ||||
| 1304 | // This constructor is used only by AddPtr() within lookupForAdd(). | |||
| 1305 | explicit Ptr(const HashTable& aTable) | |||
| 1306 | : mSlot(nullptr, nullptr) | |||
| 1307 | #ifdef DEBUG1 | |||
| 1308 | , | |||
| 1309 | mTable(&aTable), | |||
| 1310 | mGeneration(aTable.generation()) | |||
| 1311 | #endif | |||
| 1312 | { | |||
| 1313 | } | |||
| 1314 | ||||
| 1315 | bool isValid() const { return !!mSlot.toEntry(); } | |||
| 1316 | ||||
| 1317 | public: | |||
| 1318 | Ptr() | |||
| 1319 | : mSlot(nullptr, nullptr) | |||
| 1320 | #ifdef DEBUG1 | |||
| 1321 | , | |||
| 1322 | mTable(nullptr), | |||
| 1323 | mGeneration(0) | |||
| 1324 | #endif | |||
| 1325 | { | |||
| 1326 | } | |||
| 1327 | ||||
| 1328 | bool found() const { | |||
| 1329 | if (!isValid()) { | |||
| 1330 | return false; | |||
| 1331 | } | |||
| 1332 | #ifdef DEBUG1 | |||
| 1333 | MOZ_ASSERT(mGeneration == mTable->generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(mGeneration == mTable->generation())>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(mGeneration == mTable->generation()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mGeneration == mTable->generation()" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1333); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()" ")"); do { MOZ_CrashSequence(__null, 1333); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1334 | #endif | |||
| 1335 | return mSlot.isLive(); | |||
| 1336 | } | |||
| 1337 | ||||
| 1338 | explicit operator bool() const { return found(); } | |||
| 1339 | ||||
| 1340 | bool operator==(const Ptr& aRhs) const { | |||
| 1341 | MOZ_ASSERT(found() && aRhs.found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(found() && aRhs.found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(found() && aRhs.found ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("found() && aRhs.found()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1341); AnnotateMozCrashReason("MOZ_ASSERT" "(" "found() && aRhs.found()" ")"); do { MOZ_CrashSequence(__null, 1341); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1342 | return mSlot == aRhs.mSlot; | |||
| 1343 | } | |||
| 1344 | ||||
| 1345 | bool operator!=(const Ptr& aRhs) const { | |||
| 1346 | #ifdef DEBUG1 | |||
| 1347 | MOZ_ASSERT(mGeneration == mTable->generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(mGeneration == mTable->generation())>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(mGeneration == mTable->generation()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mGeneration == mTable->generation()" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1347); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()" ")"); do { MOZ_CrashSequence(__null, 1347); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1348 | #endif | |||
| 1349 | return !(*this == aRhs); | |||
| 1350 | } | |||
| 1351 | ||||
| 1352 | T& operator*() const { | |||
| 1353 | #ifdef DEBUG1 | |||
| 1354 | MOZ_ASSERT(found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(found()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("found()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1354); AnnotateMozCrashReason("MOZ_ASSERT" "(" "found()" ")" ); do { MOZ_CrashSequence(__null, 1354); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1355 | MOZ_ASSERT(mGeneration == mTable->generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(mGeneration == mTable->generation())>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(mGeneration == mTable->generation()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mGeneration == mTable->generation()" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1355); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()" ")"); do { MOZ_CrashSequence(__null, 1355); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1356 | #endif | |||
| 1357 | return mSlot.get(); | |||
| 1358 | } | |||
| 1359 | ||||
| 1360 | T* operator->() const { | |||
| 1361 | #ifdef DEBUG1 | |||
| 1362 | MOZ_ASSERT(found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(found()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("found()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1362); AnnotateMozCrashReason("MOZ_ASSERT" "(" "found()" ")" ); do { MOZ_CrashSequence(__null, 1362); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1363 | MOZ_ASSERT(mGeneration == mTable->generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(mGeneration == mTable->generation())>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(mGeneration == mTable->generation()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mGeneration == mTable->generation()" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1363); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()" ")"); do { MOZ_CrashSequence(__null, 1363); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1364 | #endif | |||
| 1365 | return &mSlot.get(); | |||
| 1366 | } | |||
| 1367 | }; | |||
| 1368 | ||||
| 1369 | // A Ptr that can be used to add a key after a failed lookup. | |||
| 1370 | class AddPtr : public Ptr { | |||
| 1371 | friend class HashTable; | |||
| 1372 | ||||
| 1373 | HashNumber mKeyHash; | |||
| 1374 | #ifdef DEBUG1 | |||
| 1375 | uint64_t mMutationCount; | |||
| 1376 | #endif | |||
| 1377 | ||||
| 1378 | AddPtr(Slot aSlot, const HashTable& aTable, HashNumber aHashNumber) | |||
| 1379 | : Ptr(aSlot, aTable), | |||
| 1380 | mKeyHash(aHashNumber) | |||
| 1381 | #ifdef DEBUG1 | |||
| 1382 | , | |||
| 1383 | mMutationCount(aTable.mMutationCount) | |||
| 1384 | #endif | |||
| 1385 | { | |||
| 1386 | } | |||
| 1387 | ||||
| 1388 | // This constructor is used when lookupForAdd() is performed on a table | |||
| 1389 | // lacking entry storage; it leaves mSlot null but initializes everything | |||
| 1390 | // else. | |||
| 1391 | AddPtr(const HashTable& aTable, HashNumber aHashNumber) | |||
| 1392 | : Ptr(aTable), | |||
| 1393 | mKeyHash(aHashNumber) | |||
| 1394 | #ifdef DEBUG1 | |||
| 1395 | , | |||
| 1396 | mMutationCount(aTable.mMutationCount) | |||
| 1397 | #endif | |||
| 1398 | { | |||
| 1399 | MOZ_ASSERT(isLive())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isLive())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isLive()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isLive()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1399); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { MOZ_CrashSequence(__null, 1399); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1400 | } | |||
| 1401 | ||||
| 1402 | bool isLive() const { return isLiveHash(mKeyHash); } | |||
| 1403 | ||||
| 1404 | public: | |||
| 1405 | AddPtr() : mKeyHash(0) {} | |||
| 1406 | }; | |||
| 1407 | ||||
| 1408 | // A hash table iterator that (mostly) doesn't allow table modifications. | |||
| 1409 | // As with Ptr/AddPtr, Iterator objects must not be used after any mutating | |||
| 1410 | // hash table operation unless the |generation()| is tested. | |||
| 1411 | class Iterator { | |||
| 1412 | void moveToNextLiveEntry() { | |||
| 1413 | while (++mCur < mEnd && !mCur.isLive()) { | |||
| 1414 | continue; | |||
| 1415 | } | |||
| 1416 | } | |||
| 1417 | ||||
| 1418 | protected: | |||
| 1419 | friend class HashTable; | |||
| 1420 | ||||
| 1421 | explicit Iterator(const HashTable& aTable) | |||
| 1422 | : mCur(aTable.slotForIndex(0)), | |||
| 1423 | mEnd(aTable.slotForIndex(aTable.capacity())) | |||
| 1424 | #ifdef DEBUG1 | |||
| 1425 | , | |||
| 1426 | mTable(aTable), | |||
| 1427 | mMutationCount(aTable.mMutationCount), | |||
| 1428 | mGeneration(aTable.generation()), | |||
| 1429 | mValidEntry(true) | |||
| 1430 | #endif | |||
| 1431 | { | |||
| 1432 | if (!done() && !mCur.isLive()) { | |||
| 1433 | moveToNextLiveEntry(); | |||
| 1434 | } | |||
| 1435 | } | |||
| 1436 | ||||
| 1437 | Slot mCur; | |||
| 1438 | Slot mEnd; | |||
| 1439 | #ifdef DEBUG1 | |||
| 1440 | const HashTable& mTable; | |||
| 1441 | uint64_t mMutationCount; | |||
| 1442 | Generation mGeneration; | |||
| 1443 | bool mValidEntry; | |||
| 1444 | #endif | |||
| 1445 | ||||
| 1446 | public: | |||
| 1447 | bool done() const { | |||
| 1448 | MOZ_ASSERT(mGeneration == mTable.generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(mGeneration == mTable.generation())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(mGeneration == mTable.generation ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("mGeneration == mTable.generation()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1448); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable.generation()" ")"); do { MOZ_CrashSequence(__null, 1448); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1449 | MOZ_ASSERT(mMutationCount == mTable.mMutationCount)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mMutationCount == mTable.mMutationCount)>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(mMutationCount == mTable.mMutationCount))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mMutationCount == mTable.mMutationCount" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1449); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mMutationCount == mTable.mMutationCount" ")"); do { MOZ_CrashSequence(__null, 1449); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1450 | return mCur == mEnd; | |||
| 1451 | } | |||
| 1452 | ||||
| 1453 | T& get() const { | |||
| 1454 | MOZ_ASSERT(!done())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!done())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!done()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!done()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1454); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!done()" ")" ); do { MOZ_CrashSequence(__null, 1454); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1455 | MOZ_ASSERT(mValidEntry)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mValidEntry)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(mValidEntry))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mValidEntry", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1455); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mValidEntry" ")"); do { MOZ_CrashSequence(__null, 1455); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1456 | MOZ_ASSERT(mGeneration == mTable.generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(mGeneration == mTable.generation())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(mGeneration == mTable.generation ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("mGeneration == mTable.generation()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1456); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable.generation()" ")"); do { MOZ_CrashSequence(__null, 1456); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1457 | MOZ_ASSERT(mMutationCount == mTable.mMutationCount)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mMutationCount == mTable.mMutationCount)>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(mMutationCount == mTable.mMutationCount))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mMutationCount == mTable.mMutationCount" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1457); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mMutationCount == mTable.mMutationCount" ")"); do { MOZ_CrashSequence(__null, 1457); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1458 | return mCur.get(); | |||
| 1459 | } | |||
| 1460 | ||||
| 1461 | void next() { | |||
| 1462 | MOZ_ASSERT(!done())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!done())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!done()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!done()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1462); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!done()" ")" ); do { MOZ_CrashSequence(__null, 1462); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1463 | MOZ_ASSERT(mGeneration == mTable.generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(mGeneration == mTable.generation())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(mGeneration == mTable.generation ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("mGeneration == mTable.generation()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1463); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable.generation()" ")"); do { MOZ_CrashSequence(__null, 1463); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1464 | MOZ_ASSERT(mMutationCount == mTable.mMutationCount)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mMutationCount == mTable.mMutationCount)>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(mMutationCount == mTable.mMutationCount))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mMutationCount == mTable.mMutationCount" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1464); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mMutationCount == mTable.mMutationCount" ")"); do { MOZ_CrashSequence(__null, 1464); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1465 | moveToNextLiveEntry(); | |||
| 1466 | #ifdef DEBUG1 | |||
| 1467 | mValidEntry = true; | |||
| 1468 | #endif | |||
| 1469 | } | |||
| 1470 | }; | |||
| 1471 | ||||
| 1472 | // A hash table iterator that permits modification, removal and rekeying. | |||
| 1473 | // Since rehashing when elements were removed during iteration would be | |||
| 1474 | // bad, it is postponed until the ModIterator is destructed. Since the | |||
| 1475 | // ModIterator's destructor touches the hash table, the user must ensure | |||
| 1476 | // that the hash table is still alive when the destructor runs. | |||
| 1477 | class ModIterator : public Iterator { | |||
| 1478 | friend class HashTable; | |||
| 1479 | ||||
| 1480 | HashTable& mTable; | |||
| 1481 | bool mRekeyed; | |||
| 1482 | bool mRemoved; | |||
| 1483 | ||||
| 1484 | protected: | |||
| 1485 | explicit ModIterator(HashTable& aTable) | |||
| 1486 | : Iterator(aTable), mTable(aTable), mRekeyed(false), mRemoved(false) {} | |||
| 1487 | ||||
| 1488 | public: | |||
| 1489 | MOZ_IMPLICIT ModIterator(ModIterator&& aOther) | |||
| 1490 | : Iterator(aOther), | |||
| 1491 | mTable(aOther.mTable), | |||
| 1492 | mRekeyed(aOther.mRekeyed), | |||
| 1493 | mRemoved(aOther.mRemoved) { | |||
| 1494 | aOther.mRekeyed = false; | |||
| 1495 | aOther.mRemoved = false; | |||
| 1496 | } | |||
| 1497 | ||||
| 1498 | // ModIterator is movable but not copyable. | |||
| 1499 | ModIterator(const ModIterator&) = delete; | |||
| 1500 | void operator=(const ModIterator&) = delete; | |||
| 1501 | ||||
| 1502 | // Removes the current element from the table, leaving |get()| invalid until | |||
| 1503 | // the next call to |next()|. | |||
| 1504 | // | |||
| 1505 | // See the comments on ~ModIterator about table resizing after removing | |||
| 1506 | // entries. | |||
| 1507 | void remove() { | |||
| 1508 | mTable.remove(this->mCur); | |||
| 1509 | mRemoved = true; | |||
| 1510 | #ifdef DEBUG1 | |||
| 1511 | this->mValidEntry = false; | |||
| 1512 | this->mMutationCount = mTable.mMutationCount; | |||
| 1513 | #endif | |||
| 1514 | } | |||
| 1515 | ||||
| 1516 | NonConstT& getMutable() { | |||
| 1517 | MOZ_ASSERT(!this->done())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!this->done())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!this->done()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!this->done()" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1517); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!this->done()" ")"); do { MOZ_CrashSequence(__null, 1517); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1518 | MOZ_ASSERT(this->mValidEntry)do { static_assert( mozilla::detail::AssertionConditionType< decltype(this->mValidEntry)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(this->mValidEntry))), 0)) ) { do { } while (false); MOZ_ReportAssertionFailure("this->mValidEntry" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1518); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this->mValidEntry" ")"); do { MOZ_CrashSequence(__null, 1518); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1519 | MOZ_ASSERT(this->mGeneration == this->Iterator::mTable.generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(this->mGeneration == this->Iterator::mTable.generation ())>::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(this->mGeneration == this->Iterator::mTable.generation ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("this->mGeneration == this->Iterator::mTable.generation()" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1519); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this->mGeneration == this->Iterator::mTable.generation()" ")"); do { MOZ_CrashSequence(__null, 1519); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1520 | MOZ_ASSERT(this->mMutationCount == this->Iterator::mTable.mMutationCount)do { static_assert( mozilla::detail::AssertionConditionType< decltype(this->mMutationCount == this->Iterator::mTable .mMutationCount)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(this->mMutationCount == this ->Iterator::mTable.mMutationCount))), 0))) { do { } while ( false); MOZ_ReportAssertionFailure("this->mMutationCount == this->Iterator::mTable.mMutationCount" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1520); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this->mMutationCount == this->Iterator::mTable.mMutationCount" ")"); do { MOZ_CrashSequence(__null, 1520); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1521 | return this->mCur.getMutable(); | |||
| 1522 | } | |||
| 1523 | ||||
| 1524 | // Removes the current element and re-inserts it into the table with | |||
| 1525 | // a new key at the new Lookup position. |get()| is invalid after | |||
| 1526 | // this operation until the next call to |next()|. | |||
| 1527 | template <typename KeyInput> | |||
| 1528 | void rekey(const Lookup& l, KeyInput&& k) { | |||
| 1529 | MOZ_ASSERT(do { static_assert( mozilla::detail::AssertionConditionType< decltype(static_cast<const void*>(&k) != static_cast <const void*>(&HashPolicy::getKey(this->mCur.get ())))>::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(static_cast<const void*>(&k) != static_cast <const void*>(&HashPolicy::getKey(this->mCur.get ()))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("static_cast<const void*>(&k) != static_cast<const void*>(&HashPolicy::getKey(this->mCur.get()))" " (" "Don't pass a reference into the table here" ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1532); AnnotateMozCrashReason("MOZ_ASSERT" "(" "static_cast<const void*>(&k) != static_cast<const void*>(&HashPolicy::getKey(this->mCur.get()))" ") (" "Don't pass a reference into the table here" ")"); do { MOZ_CrashSequence(__null, 1532); __attribute__((nomerge)) :: abort(); } while (false); } } while (false) | |||
| 1530 | static_cast<const void*>(&k) !=do { static_assert( mozilla::detail::AssertionConditionType< decltype(static_cast<const void*>(&k) != static_cast <const void*>(&HashPolicy::getKey(this->mCur.get ())))>::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(static_cast<const void*>(&k) != static_cast <const void*>(&HashPolicy::getKey(this->mCur.get ()))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("static_cast<const void*>(&k) != static_cast<const void*>(&HashPolicy::getKey(this->mCur.get()))" " (" "Don't pass a reference into the table here" ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1532); AnnotateMozCrashReason("MOZ_ASSERT" "(" "static_cast<const void*>(&k) != static_cast<const void*>(&HashPolicy::getKey(this->mCur.get()))" ") (" "Don't pass a reference into the table here" ")"); do { MOZ_CrashSequence(__null, 1532); __attribute__((nomerge)) :: abort(); } while (false); } } while (false) | |||
| 1531 | static_cast<const void*>(&HashPolicy::getKey(this->mCur.get())),do { static_assert( mozilla::detail::AssertionConditionType< decltype(static_cast<const void*>(&k) != static_cast <const void*>(&HashPolicy::getKey(this->mCur.get ())))>::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(static_cast<const void*>(&k) != static_cast <const void*>(&HashPolicy::getKey(this->mCur.get ()))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("static_cast<const void*>(&k) != static_cast<const void*>(&HashPolicy::getKey(this->mCur.get()))" " (" "Don't pass a reference into the table here" ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1532); AnnotateMozCrashReason("MOZ_ASSERT" "(" "static_cast<const void*>(&k) != static_cast<const void*>(&HashPolicy::getKey(this->mCur.get()))" ") (" "Don't pass a reference into the table here" ")"); do { MOZ_CrashSequence(__null, 1532); __attribute__((nomerge)) :: abort(); } while (false); } } while (false) | |||
| 1532 | "Don't pass a reference into the table here")do { static_assert( mozilla::detail::AssertionConditionType< decltype(static_cast<const void*>(&k) != static_cast <const void*>(&HashPolicy::getKey(this->mCur.get ())))>::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(static_cast<const void*>(&k) != static_cast <const void*>(&HashPolicy::getKey(this->mCur.get ()))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("static_cast<const void*>(&k) != static_cast<const void*>(&HashPolicy::getKey(this->mCur.get()))" " (" "Don't pass a reference into the table here" ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1532); AnnotateMozCrashReason("MOZ_ASSERT" "(" "static_cast<const void*>(&k) != static_cast<const void*>(&HashPolicy::getKey(this->mCur.get()))" ") (" "Don't pass a reference into the table here" ")"); do { MOZ_CrashSequence(__null, 1532); __attribute__((nomerge)) :: abort(); } while (false); } } while (false); | |||
| 1533 | Ptr p(this->mCur, mTable); | |||
| 1534 | mTable.rekeyWithoutRehash(p, l, std::forward<KeyInput>(k)); | |||
| 1535 | mRekeyed = true; | |||
| 1536 | #ifdef DEBUG1 | |||
| 1537 | this->mValidEntry = false; | |||
| 1538 | this->mMutationCount = mTable.mMutationCount; | |||
| 1539 | #endif | |||
| 1540 | } | |||
| 1541 | ||||
| 1542 | void rekey(const Lookup& l) { rekey(l, l); } | |||
| 1543 | ||||
| 1544 | // This can rehash the table or resize it if entries were removed. | |||
| 1545 | // | |||
| 1546 | // This does not go as far as freeing the table if it is now empty, as that | |||
| 1547 | // can lead to repeatedly allocating and freeing the table when a small | |||
| 1548 | // number of entries are repeatedly added and removed. If callers require | |||
| 1549 | // memory to be minimised after removing entries they should call compact(). | |||
| 1550 | ~ModIterator() { | |||
| 1551 | if (mRekeyed) { | |||
| 1552 | mTable.incrementGeneration(); | |||
| 1553 | mTable.infallibleRehashIfOverloaded(); | |||
| 1554 | } | |||
| 1555 | ||||
| 1556 | if (mRemoved) { | |||
| 1557 | mTable.shrinkToBestCapacity(); | |||
| 1558 | } | |||
| 1559 | } | |||
| 1560 | }; | |||
| 1561 | ||||
| 1562 | // HashTable is movable | |||
| 1563 | HashTable(HashTable&& aRhs) : AllocPolicy(std::move(aRhs)) { moveFrom(aRhs); } | |||
| 1564 | HashTable& operator=(HashTable&& aRhs) { | |||
| 1565 | MOZ_ASSERT(this != &aRhs, "self-move assignment is prohibited")do { static_assert( mozilla::detail::AssertionConditionType< decltype(this != &aRhs)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(this != &aRhs))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("this != &aRhs" " (" "self-move assignment is prohibited" ")", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1565); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this != &aRhs" ") (" "self-move assignment is prohibited" ")"); do { MOZ_CrashSequence (__null, 1565); __attribute__((nomerge)) ::abort(); } while ( false); } } while (false); | |||
| 1566 | if (mTable) { | |||
| 1567 | destroyTable(*this, mTable, capacity()); | |||
| 1568 | } | |||
| 1569 | AllocPolicy::operator=(std::move(aRhs)); | |||
| 1570 | moveFrom(aRhs); | |||
| 1571 | return *this; | |||
| 1572 | } | |||
| 1573 | ||||
| 1574 | void swap(HashTable& aOther) { | |||
| 1575 | ReentrancyGuard g1(*this); | |||
| 1576 | ReentrancyGuard g2(aOther); | |||
| 1577 | ||||
| 1578 | std::swap(mGenAndHashShift, aOther.mGenAndHashShift); | |||
| 1579 | std::swap(mTable, aOther.mTable); | |||
| 1580 | std::swap(mEntryCount, aOther.mEntryCount); | |||
| 1581 | std::swap(mRemovedCount, aOther.mRemovedCount); | |||
| 1582 | #ifdef DEBUG1 | |||
| 1583 | std::swap(mMutationCount, aOther.mMutationCount); | |||
| 1584 | std::swap(mEntered, aOther.mEntered); | |||
| 1585 | #endif | |||
| 1586 | } | |||
| 1587 | ||||
| 1588 | AllocPolicy& allocPolicy() { return *this; } | |||
| 1589 | const AllocPolicy& allocPolicy() const { return *this; } | |||
| 1590 | ||||
| 1591 | template <typename F> | |||
| 1592 | void traceOwnedAllocs(F&& aTraceFunc) { | |||
| 1593 | if (mTable) { | |||
| 1594 | aTraceFunc(&mTable); | |||
| 1595 | } | |||
| 1596 | } | |||
| 1597 | ||||
| 1598 | private: | |||
| 1599 | void moveFrom(HashTable& aRhs) { | |||
| 1600 | mGenAndHashShift = aRhs.mGenAndHashShift; | |||
| 1601 | mTable = aRhs.mTable; | |||
| 1602 | mEntryCount = aRhs.mEntryCount; | |||
| 1603 | mRemovedCount = aRhs.mRemovedCount; | |||
| 1604 | #ifdef DEBUG1 | |||
| 1605 | mMutationCount = aRhs.mMutationCount; | |||
| 1606 | mEntered = aRhs.mEntered; | |||
| 1607 | #endif | |||
| 1608 | aRhs.mTable = nullptr; | |||
| 1609 | aRhs.clearAndCompact(); | |||
| 1610 | } | |||
| 1611 | ||||
| 1612 | static const uint32_t CAP_BITS = 30; | |||
| 1613 | ||||
| 1614 | public: | |||
| 1615 | uint64_t mGenAndHashShift; // entry storage generation number (56 bits) | |||
| 1616 | // and multiplicative hash shift (8 bits) | |||
| 1617 | char* mTable; // entry storage | |||
| 1618 | uint32_t mEntryCount; // number of entries in mTable | |||
| 1619 | uint32_t mRemovedCount; // removed entry sentinels in mTable | |||
| 1620 | ||||
| 1621 | #ifdef DEBUG1 | |||
| 1622 | uint64_t mMutationCount; | |||
| 1623 | mutable bool mEntered; | |||
| 1624 | #endif | |||
| 1625 | ||||
| 1626 | // The default initial capacity is 32 (enough to hold 16 elements), but it | |||
| 1627 | // can be as low as 4. | |||
| 1628 | static const uint32_t sDefaultLen = 16; | |||
| 1629 | static const uint32_t sMinCapacity = 4; | |||
| 1630 | // See the comments in HashTableEntry about this value. | |||
| 1631 | static_assert(sMinCapacity >= 4, "too-small sMinCapacity breaks assumptions"); | |||
| 1632 | static const uint32_t sMaxInit = 1u << (CAP_BITS - 1); | |||
| 1633 | static const uint32_t sMaxCapacity = 1u << CAP_BITS; | |||
| 1634 | ||||
| 1635 | // Hash-table alpha is conceptually a fraction, but to avoid floating-point | |||
| 1636 | // math we implement it as a ratio of integers. | |||
| 1637 | static const uint8_t sAlphaDenominator = 4; | |||
| 1638 | static const uint8_t sMinAlphaNumerator = 1; // min alpha: 1/4 | |||
| 1639 | static const uint8_t sMaxAlphaNumerator = 3; // max alpha: 3/4 | |||
| 1640 | ||||
| 1641 | static const HashNumber sFreeKey = Entry::sFreeKey; | |||
| 1642 | static const HashNumber sRemovedKey = Entry::sRemovedKey; | |||
| 1643 | static const HashNumber sCollisionBit = Entry::sCollisionBit; | |||
| 1644 | ||||
| 1645 | static const uint64_t sHashShiftBits = 8; | |||
| 1646 | static const uint64_t sHashShiftMask = (1 << sHashShiftBits) - 1; | |||
| 1647 | static const uint64_t sGenerationShift = sHashShiftBits; | |||
| 1648 | ||||
| 1649 | MOZ_ALWAYS_INLINEinline uint8_t hashShift() const { | |||
| 1650 | return uint8_t(mGenAndHashShift & sHashShiftMask); | |||
| 1651 | } | |||
| 1652 | MOZ_ALWAYS_INLINEinline uint64_t gen() const { | |||
| 1653 | return mGenAndHashShift >> sGenerationShift; | |||
| 1654 | } | |||
| 1655 | ||||
| 1656 | // HashTable is not copyable or assignable | |||
| 1657 | HashTable(const HashTable&) = delete; | |||
| 1658 | void operator=(const HashTable&) = delete; | |||
| 1659 | ||||
| 1660 | private: | |||
| 1661 | void setGenAndHashShift(uint64_t aGeneration, uint8_t aHashShift) { | |||
| 1662 | mGenAndHashShift = aGeneration << sGenerationShift | aHashShift; | |||
| 1663 | } | |||
| 1664 | ||||
| 1665 | public: | |||
| 1666 | void incrementGeneration() { setGenAndHashShift(gen() + 1, hashShift()); } | |||
| 1667 | void setHashShift(uint32_t aHashShift) { | |||
| 1668 | MOZ_ASSERT((aHashShift & sHashShiftMask) == aHashShift)do { static_assert( mozilla::detail::AssertionConditionType< decltype((aHashShift & sHashShiftMask) == aHashShift)> ::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!((aHashShift & sHashShiftMask) == aHashShift))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("(aHashShift & sHashShiftMask) == aHashShift" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1668); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(aHashShift & sHashShiftMask) == aHashShift" ")"); do { MOZ_CrashSequence(__null, 1668); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1669 | mGenAndHashShift = (mGenAndHashShift & ~sHashShiftMask) | aHashShift; | |||
| 1670 | } | |||
| 1671 | ||||
| 1672 | constexpr static uint32_t bestCapacity(uint32_t aLen) { | |||
| 1673 | static_assert( | |||
| 1674 | (sMaxInit * sAlphaDenominator) / sAlphaDenominator == sMaxInit, | |||
| 1675 | "multiplication in numerator below could overflow"); | |||
| 1676 | static_assert( | |||
| 1677 | sMaxInit * sAlphaDenominator <= UINT32_MAX(4294967295U) - sMaxAlphaNumerator, | |||
| 1678 | "numerator calculation below could potentially overflow"); | |||
| 1679 | ||||
| 1680 | // Callers should ensure this is true. | |||
| 1681 | MOZ_ASSERT(aLen <= sMaxInit)do { static_assert( mozilla::detail::AssertionConditionType< decltype(aLen <= sMaxInit)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aLen <= sMaxInit))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("aLen <= sMaxInit" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1681); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aLen <= sMaxInit" ")"); do { MOZ_CrashSequence(__null, 1681); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1682 | ||||
| 1683 | // Compute the smallest capacity allowing |aLen| elements to be | |||
| 1684 | // inserted without rehashing: ceil(aLen / max-alpha). (Ceiling | |||
| 1685 | // integral division: <http://stackoverflow.com/a/2745086>.) | |||
| 1686 | uint32_t capacity = (aLen * sAlphaDenominator + sMaxAlphaNumerator - 1) / | |||
| 1687 | sMaxAlphaNumerator; | |||
| 1688 | capacity = (capacity < sMinCapacity) ? sMinCapacity : RoundUpPow2(capacity); | |||
| 1689 | ||||
| 1690 | MOZ_ASSERT(capacity >= aLen)do { static_assert( mozilla::detail::AssertionConditionType< decltype(capacity >= aLen)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(capacity >= aLen))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("capacity >= aLen" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1690); AnnotateMozCrashReason("MOZ_ASSERT" "(" "capacity >= aLen" ")"); do { MOZ_CrashSequence(__null, 1690); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1691 | MOZ_ASSERT(capacity <= sMaxCapacity)do { static_assert( mozilla::detail::AssertionConditionType< decltype(capacity <= sMaxCapacity)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(capacity <= sMaxCapacity) )), 0))) { do { } while (false); MOZ_ReportAssertionFailure("capacity <= sMaxCapacity" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1691); AnnotateMozCrashReason("MOZ_ASSERT" "(" "capacity <= sMaxCapacity" ")"); do { MOZ_CrashSequence(__null, 1691); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1692 | ||||
| 1693 | return capacity; | |||
| 1694 | } | |||
| 1695 | ||||
| 1696 | constexpr static uint32_t hashShiftForLength(uint32_t aLen) { | |||
| 1697 | // Reject all lengths whose initial computed capacity would exceed | |||
| 1698 | // sMaxCapacity. Round that maximum aLen down to the nearest power of two | |||
| 1699 | // for speedier code. | |||
| 1700 | if (MOZ_UNLIKELY(aLen > sMaxInit)(__builtin_expect(!!(aLen > sMaxInit), 0))) { | |||
| 1701 | MOZ_CRASH("initial length is too large")do { do { } while (false); MOZ_ReportCrash("" "initial length is too large" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1701); AnnotateMozCrashReason("MOZ_CRASH(" "initial length is too large" ")"); do { MOZ_CrashSequence(__null, 1701); __attribute__((nomerge )) ::abort(); } while (false); } while (false); | |||
| 1702 | } | |||
| 1703 | ||||
| 1704 | return kHashNumberBits - mozilla::CeilingLog2(bestCapacity(aLen)); | |||
| 1705 | } | |||
| 1706 | ||||
| 1707 | static bool isLiveHash(HashNumber aHash) { return Entry::isLiveHash(aHash); } | |||
| 1708 | ||||
| 1709 | static HashNumber prepareHash(HashNumber aInputHash) { | |||
| 1710 | HashNumber keyHash = ScrambleHashCode(aInputHash); | |||
| 1711 | ||||
| 1712 | // Avoid reserved hash codes. | |||
| 1713 | if (!isLiveHash(keyHash)) { | |||
| 1714 | keyHash -= (sRemovedKey + 1); | |||
| 1715 | } | |||
| 1716 | return keyHash & ~sCollisionBit; | |||
| 1717 | } | |||
| 1718 | ||||
| 1719 | enum FailureBehavior { DontReportFailure = false, ReportFailure = true }; | |||
| 1720 | ||||
| 1721 | // Fake a struct that we're going to alloc. See the comments in | |||
| 1722 | // HashTableEntry about how the table is laid out, and why it's safe. | |||
| 1723 | struct FakeSlot { | |||
| 1724 | unsigned char c[sizeof(HashNumber) + sizeof(typename Entry::NonConstT)]; | |||
| 1725 | }; | |||
| 1726 | ||||
| 1727 | static char* createTable(AllocPolicy& aAllocPolicy, uint32_t aCapacity, | |||
| 1728 | FailureBehavior aReportFailure = ReportFailure) { | |||
| 1729 | FakeSlot* fake = | |||
| 1730 | aReportFailure | |||
| 1731 | ? aAllocPolicy.template pod_malloc<FakeSlot>(aCapacity) | |||
| 1732 | : aAllocPolicy.template maybe_pod_malloc<FakeSlot>(aCapacity); | |||
| 1733 | ||||
| 1734 | MOZ_ASSERT((reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) ==do { static_assert( mozilla::detail::AssertionConditionType< decltype((reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment ) == 0)>::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!((reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment ) == 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("(reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) == 0" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1735); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) == 0" ")"); do { MOZ_CrashSequence(__null, 1735); __attribute__((nomerge )) ::abort(); } while (false); } } while (false) | |||
| 1735 | 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype((reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment ) == 0)>::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!((reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment ) == 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("(reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) == 0" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1735); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) == 0" ")"); do { MOZ_CrashSequence(__null, 1735); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1736 | ||||
| 1737 | char* table = reinterpret_cast<char*>(fake); | |||
| 1738 | if (table) { | |||
| 1739 | forEachSlot(table, aCapacity, [&](Slot& slot) { | |||
| 1740 | *slot.mKeyHash = sFreeKey; | |||
| 1741 | new (KnownNotNull, slot.toEntry()) Entry(); | |||
| 1742 | }); | |||
| 1743 | } | |||
| 1744 | return table; | |||
| 1745 | } | |||
| 1746 | ||||
| 1747 | static void destroyTable(AllocPolicy& aAllocPolicy, char* aOldTable, | |||
| 1748 | uint32_t aCapacity) { | |||
| 1749 | forEachSlot(aOldTable, aCapacity, [&](const Slot& slot) { | |||
| 1750 | if (slot.isLive()) { | |||
| 1751 | slot.toEntry()->destroyStoredT(); | |||
| 1752 | } | |||
| 1753 | }); | |||
| 1754 | freeTable(aAllocPolicy, aOldTable, aCapacity); | |||
| 1755 | } | |||
| 1756 | ||||
| 1757 | static void freeTable(AllocPolicy& aAllocPolicy, char* aOldTable, | |||
| 1758 | uint32_t aCapacity) { | |||
| 1759 | FakeSlot* fake = reinterpret_cast<FakeSlot*>(aOldTable); | |||
| 1760 | aAllocPolicy.free_(fake, aCapacity); | |||
| 1761 | } | |||
| 1762 | ||||
| 1763 | public: | |||
| 1764 | constexpr HashTable(AllocPolicy aAllocPolicy, uint32_t aLen) | |||
| 1765 | : AllocPolicy(std::move(aAllocPolicy)), | |||
| 1766 | mGenAndHashShift(hashShiftForLength(aLen)), | |||
| 1767 | mTable(nullptr), | |||
| 1768 | mEntryCount(0), | |||
| 1769 | mRemovedCount(0) | |||
| 1770 | #ifdef DEBUG1 | |||
| 1771 | , | |||
| 1772 | mMutationCount(0), | |||
| 1773 | mEntered(false) | |||
| 1774 | #endif | |||
| 1775 | { | |||
| 1776 | } | |||
| 1777 | ||||
| 1778 | explicit HashTable(AllocPolicy aAllocPolicy) | |||
| 1779 | : HashTable(aAllocPolicy, sDefaultLen) {} | |||
| 1780 | ||||
| 1781 | ~HashTable() { | |||
| 1782 | if (mTable) { | |||
| 1783 | destroyTable(*this, mTable, capacity()); | |||
| 1784 | } | |||
| 1785 | } | |||
| 1786 | ||||
| 1787 | private: | |||
| 1788 | HashNumber hash1(HashNumber aHash0) const { return aHash0 >> hashShift(); } | |||
| 1789 | ||||
| 1790 | struct DoubleHash { | |||
| 1791 | HashNumber mHash2; | |||
| 1792 | HashNumber mSizeMask; | |||
| 1793 | }; | |||
| 1794 | ||||
| 1795 | DoubleHash hash2(HashNumber aCurKeyHash) const { | |||
| 1796 | uint32_t sizeLog2 = kHashNumberBits - hashShift(); | |||
| 1797 | DoubleHash dh = {((aCurKeyHash << sizeLog2) >> hashShift()) | 1, | |||
| 1798 | (HashNumber(1) << sizeLog2) - 1}; | |||
| 1799 | return dh; | |||
| 1800 | } | |||
| 1801 | ||||
| 1802 | static HashNumber applyDoubleHash(HashNumber aHash1, | |||
| 1803 | const DoubleHash& aDoubleHash) { | |||
| 1804 | return WrappingSubtract(aHash1, aDoubleHash.mHash2) & aDoubleHash.mSizeMask; | |||
| 1805 | } | |||
| 1806 | ||||
| 1807 | static MOZ_ALWAYS_INLINEinline bool match(T& aEntry, const Lookup& aLookup) { | |||
| 1808 | return HashPolicy::match(HashPolicy::getKey(aEntry), aLookup); | |||
| 1809 | } | |||
| 1810 | ||||
| 1811 | enum LookupReason { ForNonAdd, ForAdd }; | |||
| 1812 | ||||
| 1813 | Slot slotForIndex(HashNumber aIndex) const { | |||
| 1814 | auto hashes = reinterpret_cast<HashNumber*>(mTable); | |||
| 1815 | auto entries = reinterpret_cast<Entry*>(&hashes[capacity()]); | |||
| 1816 | return Slot(&entries[aIndex], &hashes[aIndex]); | |||
| 1817 | } | |||
| 1818 | ||||
| 1819 | // Warning: in order for readonlyThreadsafeLookup() to be safe this | |||
| 1820 | // function must not modify the table in any way when Reason==ForNonAdd. | |||
| 1821 | template <LookupReason Reason> | |||
| 1822 | MOZ_ALWAYS_INLINEinline Slot lookup(const Lookup& aLookup, | |||
| 1823 | HashNumber aKeyHash) const { | |||
| 1824 | MOZ_ASSERT(isLiveHash(aKeyHash))do { static_assert( mozilla::detail::AssertionConditionType< decltype(isLiveHash(aKeyHash))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isLiveHash(aKeyHash)))), 0)) ) { do { } while (false); MOZ_ReportAssertionFailure("isLiveHash(aKeyHash)" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1824); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLiveHash(aKeyHash)" ")"); do { MOZ_CrashSequence(__null, 1824); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1825 | MOZ_ASSERT(!(aKeyHash & sCollisionBit))do { static_assert( mozilla::detail::AssertionConditionType< decltype(!(aKeyHash & sCollisionBit))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!(aKeyHash & sCollisionBit )))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("!(aKeyHash & sCollisionBit)", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1825); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!(aKeyHash & sCollisionBit)" ")"); do { MOZ_CrashSequence(__null, 1825); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1826 | MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mTable)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mTable", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1826); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { MOZ_CrashSequence(__null, 1826); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1827 | ||||
| 1828 | // Compute the primary hash address. | |||
| 1829 | HashNumber h1 = hash1(aKeyHash); | |||
| 1830 | Slot slot = slotForIndex(h1); | |||
| 1831 | ||||
| 1832 | // Miss: return space for a new entry. | |||
| 1833 | if (slot.isFree()) { | |||
| 1834 | return slot; | |||
| 1835 | } | |||
| 1836 | ||||
| 1837 | // Hit: return entry. | |||
| 1838 | if (slot.matchHash(aKeyHash) && match(slot.get(), aLookup)) { | |||
| 1839 | return slot; | |||
| 1840 | } | |||
| 1841 | ||||
| 1842 | // Collision: double hash. | |||
| 1843 | DoubleHash dh = hash2(aKeyHash); | |||
| 1844 | ||||
| 1845 | // Save the first removed entry pointer so we can recycle later. | |||
| 1846 | Maybe<Slot> firstRemoved; | |||
| 1847 | ||||
| 1848 | while (true) { | |||
| 1849 | if (Reason == ForAdd && !firstRemoved) { | |||
| 1850 | if (MOZ_UNLIKELY(slot.isRemoved())(__builtin_expect(!!(slot.isRemoved()), 0))) { | |||
| 1851 | firstRemoved.emplace(slot); | |||
| 1852 | } else { | |||
| 1853 | slot.setCollision(); | |||
| 1854 | } | |||
| 1855 | } | |||
| 1856 | ||||
| 1857 | h1 = applyDoubleHash(h1, dh); | |||
| 1858 | ||||
| 1859 | slot = slotForIndex(h1); | |||
| 1860 | if (slot.isFree()) { | |||
| 1861 | return firstRemoved.refOr(slot); | |||
| 1862 | } | |||
| 1863 | ||||
| 1864 | if (slot.matchHash(aKeyHash) && match(slot.get(), aLookup)) { | |||
| 1865 | return slot; | |||
| 1866 | } | |||
| 1867 | } | |||
| 1868 | } | |||
| 1869 | ||||
| 1870 | // This is a copy of lookup() hardcoded to the assumptions: | |||
| 1871 | // 1. the lookup is for an add; | |||
| 1872 | // 2. the key, whose |keyHash| has been passed, is not in the table. | |||
| 1873 | Slot findNonLiveSlot(HashNumber aKeyHash) { | |||
| 1874 | MOZ_ASSERT(!(aKeyHash & sCollisionBit))do { static_assert( mozilla::detail::AssertionConditionType< decltype(!(aKeyHash & sCollisionBit))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!(aKeyHash & sCollisionBit )))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("!(aKeyHash & sCollisionBit)", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1874); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!(aKeyHash & sCollisionBit)" ")"); do { MOZ_CrashSequence(__null, 1874); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1875 | MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mTable)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mTable", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1875); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { MOZ_CrashSequence(__null, 1875); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1876 | ||||
| 1877 | // We assume 'aKeyHash' has already been distributed. | |||
| 1878 | ||||
| 1879 | // Compute the primary hash address. | |||
| 1880 | HashNumber h1 = hash1(aKeyHash); | |||
| 1881 | Slot slot = slotForIndex(h1); | |||
| 1882 | ||||
| 1883 | // Miss: return space for a new entry. | |||
| 1884 | if (!slot.isLive()) { | |||
| 1885 | return slot; | |||
| 1886 | } | |||
| 1887 | ||||
| 1888 | // Collision: double hash. | |||
| 1889 | DoubleHash dh = hash2(aKeyHash); | |||
| 1890 | ||||
| 1891 | while (true) { | |||
| 1892 | slot.setCollision(); | |||
| 1893 | ||||
| 1894 | h1 = applyDoubleHash(h1, dh); | |||
| 1895 | ||||
| 1896 | slot = slotForIndex(h1); | |||
| 1897 | if (!slot.isLive()) { | |||
| 1898 | return slot; | |||
| 1899 | } | |||
| 1900 | } | |||
| 1901 | } | |||
| 1902 | ||||
| 1903 | enum RebuildStatus { NotOverloaded, Rehashed, RehashFailed }; | |||
| 1904 | ||||
| 1905 | RebuildStatus changeTableSize( | |||
| 1906 | uint32_t newCapacity, FailureBehavior aReportFailure = ReportFailure) { | |||
| 1907 | MOZ_ASSERT(std::has_single_bit(newCapacity))do { static_assert( mozilla::detail::AssertionConditionType< decltype(std::has_single_bit(newCapacity))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(std::has_single_bit(newCapacity )))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("std::has_single_bit(newCapacity)", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1907); AnnotateMozCrashReason("MOZ_ASSERT" "(" "std::has_single_bit(newCapacity)" ")"); do { MOZ_CrashSequence(__null, 1907); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1908 | MOZ_ASSERT(!!mTable == !!capacity())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!!mTable == !!capacity())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!!mTable == !!capacity()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!!mTable == !!capacity()" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1908); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!!mTable == !!capacity()" ")"); do { MOZ_CrashSequence(__null, 1908); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1909 | ||||
| 1910 | // Look, but don't touch, until we succeed in getting new entry store. | |||
| 1911 | char* oldTable = mTable; | |||
| 1912 | uint32_t oldCapacity = capacity(); | |||
| 1913 | uint32_t newLog2 = mozilla::CeilingLog2(newCapacity); | |||
| 1914 | ||||
| 1915 | if (MOZ_UNLIKELY(newCapacity > sMaxCapacity)(__builtin_expect(!!(newCapacity > sMaxCapacity), 0))) { | |||
| 1916 | if (aReportFailure) { | |||
| 1917 | this->reportAllocOverflow(); | |||
| 1918 | } | |||
| 1919 | return RehashFailed; | |||
| 1920 | } | |||
| 1921 | ||||
| 1922 | char* newTable = createTable(*this, newCapacity, aReportFailure); | |||
| 1923 | if (!newTable) { | |||
| 1924 | return RehashFailed; | |||
| 1925 | } | |||
| 1926 | ||||
| 1927 | // We can't fail from here on, so update table parameters. | |||
| 1928 | mRemovedCount = 0; | |||
| 1929 | incrementGeneration(); | |||
| 1930 | setHashShift(kHashNumberBits - newLog2); | |||
| 1931 | mTable = newTable; | |||
| 1932 | ||||
| 1933 | // Copy only live entries, leaving removed ones behind. | |||
| 1934 | forEachSlot(oldTable, oldCapacity, [&](Slot& slot) { | |||
| 1935 | if (slot.isLive()) { | |||
| 1936 | HashNumber hn = slot.getKeyHash(); | |||
| 1937 | findNonLiveSlot(hn).setLive( | |||
| 1938 | hn, std::move(const_cast<typename Entry::NonConstT&>(slot.get()))); | |||
| 1939 | } | |||
| 1940 | ||||
| 1941 | slot.clear(); | |||
| 1942 | }); | |||
| 1943 | ||||
| 1944 | // All entries have been destroyed, no need to destroyTable. | |||
| 1945 | freeTable(*this, oldTable, oldCapacity); | |||
| 1946 | return Rehashed; | |||
| 1947 | } | |||
| 1948 | ||||
| 1949 | RebuildStatus rehashIfOverloaded( | |||
| 1950 | FailureBehavior aReportFailure = ReportFailure) { | |||
| 1951 | static_assert(sMaxCapacity <= UINT32_MAX(4294967295U) / sMaxAlphaNumerator, | |||
| 1952 | "multiplication below could overflow"); | |||
| 1953 | ||||
| 1954 | // Note: if capacity() is zero, this will always succeed, which is | |||
| 1955 | // what we want. | |||
| 1956 | bool overloaded = mEntryCount + mRemovedCount >= | |||
| 1957 | capacity() * sMaxAlphaNumerator / sAlphaDenominator; | |||
| 1958 | ||||
| 1959 | if (!overloaded) { | |||
| 1960 | return NotOverloaded; | |||
| 1961 | } | |||
| 1962 | ||||
| 1963 | // Succeed if a quarter or more of all entries are removed. Note that this | |||
| 1964 | // always succeeds if capacity() == 0 (i.e. entry storage has not been | |||
| 1965 | // allocated), which is what we want, because it means changeTableSize() | |||
| 1966 | // will allocate the requested capacity rather than doubling it. | |||
| 1967 | bool manyRemoved = mRemovedCount >= (capacity() >> 2); | |||
| 1968 | uint32_t newCapacity = manyRemoved ? rawCapacity() : rawCapacity() * 2; | |||
| 1969 | return changeTableSize(newCapacity, aReportFailure); | |||
| 1970 | } | |||
| 1971 | ||||
| 1972 | void infallibleRehashIfOverloaded() { | |||
| 1973 | if (rehashIfOverloaded(DontReportFailure) == RehashFailed) { | |||
| 1974 | rehashTableInPlace(); | |||
| 1975 | } | |||
| 1976 | } | |||
| 1977 | ||||
| 1978 | void remove(Slot& aSlot) { | |||
| 1979 | MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mTable)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mTable", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1979); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { MOZ_CrashSequence(__null, 1979); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1980 | ||||
| 1981 | if (aSlot.hasCollision()) { | |||
| 1982 | aSlot.removeLive(); | |||
| 1983 | mRemovedCount++; | |||
| 1984 | } else { | |||
| 1985 | aSlot.clearLive(); | |||
| 1986 | } | |||
| 1987 | mEntryCount--; | |||
| 1988 | #ifdef DEBUG1 | |||
| 1989 | mMutationCount++; | |||
| 1990 | #endif | |||
| 1991 | } | |||
| 1992 | ||||
| 1993 | void shrinkIfUnderloaded() { | |||
| 1994 | static_assert(sMaxCapacity <= UINT32_MAX(4294967295U) / sMinAlphaNumerator, | |||
| 1995 | "multiplication below could overflow"); | |||
| 1996 | bool underloaded = | |||
| 1997 | capacity() > sMinCapacity && | |||
| 1998 | mEntryCount <= capacity() * sMinAlphaNumerator / sAlphaDenominator; | |||
| 1999 | ||||
| 2000 | if (underloaded) { | |||
| 2001 | (void)changeTableSize(capacity() / 2, DontReportFailure); | |||
| 2002 | } | |||
| 2003 | } | |||
| 2004 | ||||
| 2005 | // This is identical to changeTableSize(currentSize), but without requiring | |||
| 2006 | // a second table. We do this by recycling the collision bits to tell us if | |||
| 2007 | // the element is already inserted or still waiting to be inserted. Since | |||
| 2008 | // already-inserted elements win any conflicts, we get the same table as we | |||
| 2009 | // would have gotten through random insertion order. | |||
| 2010 | void rehashTableInPlace() { | |||
| 2011 | mRemovedCount = 0; | |||
| 2012 | incrementGeneration(); | |||
| 2013 | forEachSlot(mTable, capacity(), [&](Slot& slot) { slot.unsetCollision(); }); | |||
| 2014 | for (uint32_t i = 0; i < capacity();) { | |||
| 2015 | Slot src = slotForIndex(i); | |||
| 2016 | ||||
| 2017 | if (!src.isLive() || src.hasCollision()) { | |||
| 2018 | ++i; | |||
| 2019 | continue; | |||
| 2020 | } | |||
| 2021 | ||||
| 2022 | HashNumber keyHash = src.getKeyHash(); | |||
| 2023 | HashNumber h1 = hash1(keyHash); | |||
| 2024 | DoubleHash dh = hash2(keyHash); | |||
| 2025 | Slot tgt = slotForIndex(h1); | |||
| 2026 | while (true) { | |||
| 2027 | if (!tgt.hasCollision()) { | |||
| 2028 | src.swap(tgt); | |||
| 2029 | tgt.setCollision(); | |||
| 2030 | break; | |||
| 2031 | } | |||
| 2032 | ||||
| 2033 | h1 = applyDoubleHash(h1, dh); | |||
| 2034 | tgt = slotForIndex(h1); | |||
| 2035 | } | |||
| 2036 | } | |||
| 2037 | ||||
| 2038 | // TODO: this algorithm leaves collision bits on *all* elements, even if | |||
| 2039 | // they are on no collision path. We have the option of setting the | |||
| 2040 | // collision bits correctly on a subsequent pass or skipping the rehash | |||
| 2041 | // unless we are totally filled with tombstones: benchmark to find out | |||
| 2042 | // which approach is best. | |||
| 2043 | } | |||
| 2044 | ||||
| 2045 | // Prefer to use putNewInfallible; this function does not check | |||
| 2046 | // invariants. | |||
| 2047 | template <typename... Args> | |||
| 2048 | void putNewInfallibleInternal(HashNumber aKeyHash, Args&&... aArgs) { | |||
| 2049 | MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mTable)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mTable", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2049); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { MOZ_CrashSequence(__null, 2049); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2050 | ||||
| 2051 | Slot slot = findNonLiveSlot(aKeyHash); | |||
| 2052 | ||||
| 2053 | if (slot.isRemoved()) { | |||
| 2054 | mRemovedCount--; | |||
| 2055 | aKeyHash |= sCollisionBit; | |||
| 2056 | } | |||
| 2057 | ||||
| 2058 | slot.setLive(aKeyHash, std::forward<Args>(aArgs)...); | |||
| 2059 | mEntryCount++; | |||
| 2060 | #ifdef DEBUG1 | |||
| 2061 | mMutationCount++; | |||
| 2062 | #endif | |||
| 2063 | } | |||
| 2064 | ||||
| 2065 | public: | |||
| 2066 | void clear() { | |||
| 2067 | forEachSlot(mTable, capacity(), [&](Slot& slot) { slot.clear(); }); | |||
| 2068 | mRemovedCount = 0; | |||
| 2069 | mEntryCount = 0; | |||
| 2070 | #ifdef DEBUG1 | |||
| 2071 | mMutationCount++; | |||
| 2072 | #endif | |||
| 2073 | } | |||
| 2074 | ||||
| 2075 | // Minimise the memory used. If there are no entries the table is freed, | |||
| 2076 | // otherwise the table is resized to the smallest capacity that doesn't | |||
| 2077 | // overload the table and that is at least sMinCapacity entries. | |||
| 2078 | // | |||
| 2079 | // Since we shrink the table after every remove, you only need to call this if | |||
| 2080 | // you want to free the table when it's empty. | |||
| 2081 | void compact() { | |||
| 2082 | if (empty()) { | |||
| 2083 | // Free the entry storage. | |||
| 2084 | freeTable(*this, mTable, capacity()); | |||
| 2085 | incrementGeneration(); | |||
| 2086 | setHashShift( | |||
| 2087 | hashShiftForLength(0)); // gives minimum capacity on regrowth | |||
| 2088 | mTable = nullptr; | |||
| 2089 | mRemovedCount = 0; | |||
| 2090 | return; | |||
| 2091 | } | |||
| 2092 | ||||
| 2093 | shrinkToBestCapacity(); | |||
| 2094 | } | |||
| 2095 | ||||
| 2096 | void shrinkToBestCapacity() { | |||
| 2097 | uint32_t bestCapacity = this->bestCapacity(mEntryCount); | |||
| 2098 | if (bestCapacity < capacity()) { | |||
| 2099 | (void)changeTableSize(bestCapacity, DontReportFailure); | |||
| 2100 | } | |||
| 2101 | } | |||
| 2102 | ||||
| 2103 | void clearAndCompact() { | |||
| 2104 | clear(); | |||
| 2105 | compact(); | |||
| 2106 | } | |||
| 2107 | ||||
| 2108 | [[nodiscard]] bool reserve(uint32_t aLen) { | |||
| 2109 | if (aLen == 0) { | |||
| 2110 | return true; | |||
| 2111 | } | |||
| 2112 | ||||
| 2113 | if (MOZ_UNLIKELY(aLen > sMaxInit)(__builtin_expect(!!(aLen > sMaxInit), 0))) { | |||
| 2114 | this->reportAllocOverflow(); | |||
| 2115 | return false; | |||
| 2116 | } | |||
| 2117 | ||||
| 2118 | uint32_t bestCapacity = this->bestCapacity(aLen); | |||
| 2119 | if (bestCapacity <= capacity()) { | |||
| 2120 | return true; // Capacity is already sufficient. | |||
| 2121 | } | |||
| 2122 | ||||
| 2123 | RebuildStatus status = changeTableSize(bestCapacity, ReportFailure); | |||
| 2124 | MOZ_ASSERT(status != NotOverloaded)do { static_assert( mozilla::detail::AssertionConditionType< decltype(status != NotOverloaded)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(status != NotOverloaded))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("status != NotOverloaded" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2124); AnnotateMozCrashReason("MOZ_ASSERT" "(" "status != NotOverloaded" ")"); do { MOZ_CrashSequence(__null, 2124); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2125 | return status != RehashFailed; | |||
| 2126 | } | |||
| 2127 | ||||
| 2128 | Iterator iter() const { return Iterator(*this); } | |||
| 2129 | ||||
| 2130 | ModIterator modIter() { return ModIterator(*this); } | |||
| 2131 | ||||
| 2132 | bool empty() const { return mEntryCount == 0; } | |||
| 2133 | ||||
| 2134 | uint32_t count() const { return mEntryCount; } | |||
| 2135 | ||||
| 2136 | uint32_t rawCapacity() const { return 1u << (kHashNumberBits - hashShift()); } | |||
| 2137 | ||||
| 2138 | uint32_t capacity() const { return mTable ? rawCapacity() : 0; } | |||
| 2139 | ||||
| 2140 | Generation generation() const { return Generation(gen()); } | |||
| 2141 | ||||
| 2142 | size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { | |||
| 2143 | return aMallocSizeOf(mTable); | |||
| 2144 | } | |||
| 2145 | ||||
| 2146 | size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { | |||
| 2147 | return aMallocSizeOf(this) + shallowSizeOfExcludingThis(aMallocSizeOf); | |||
| 2148 | } | |||
| 2149 | ||||
| 2150 | MOZ_ALWAYS_INLINEinline Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const { | |||
| 2151 | if (empty()) { | |||
| 2152 | return Ptr(); | |||
| 2153 | } | |||
| 2154 | ||||
| 2155 | HashNumber inputHash; | |||
| 2156 | if (!MaybeGetHash<HashPolicy>(aLookup, &inputHash)) { | |||
| 2157 | return Ptr(); | |||
| 2158 | } | |||
| 2159 | ||||
| 2160 | HashNumber keyHash = prepareHash(inputHash); | |||
| 2161 | return Ptr(lookup<ForNonAdd>(aLookup, keyHash), *this); | |||
| 2162 | } | |||
| 2163 | ||||
| 2164 | MOZ_ALWAYS_INLINEinline Ptr lookup(const Lookup& aLookup) const { | |||
| 2165 | ReentrancyGuard g(*this); | |||
| 2166 | return readonlyThreadsafeLookup(aLookup); | |||
| 2167 | } | |||
| 2168 | ||||
| 2169 | MOZ_ALWAYS_INLINEinline AddPtr lookupForAdd(const Lookup& aLookup) { | |||
| 2170 | ReentrancyGuard g(*this); | |||
| 2171 | ||||
| 2172 | HashNumber inputHash; | |||
| 2173 | if (!EnsureHash<HashPolicy>(aLookup, &inputHash)) { | |||
| 2174 | return AddPtr(); | |||
| 2175 | } | |||
| 2176 | ||||
| 2177 | HashNumber keyHash = prepareHash(inputHash); | |||
| 2178 | ||||
| 2179 | if (!mTable) { | |||
| 2180 | return AddPtr(*this, keyHash); | |||
| 2181 | } | |||
| 2182 | ||||
| 2183 | // Directly call the constructor in the return statement to avoid | |||
| 2184 | // excess copying when building with Visual Studio 2017. | |||
| 2185 | // See bug 1385181. | |||
| 2186 | return AddPtr(lookup<ForAdd>(aLookup, keyHash), *this, keyHash); | |||
| 2187 | } | |||
| 2188 | ||||
| 2189 | template <typename... Args> | |||
| 2190 | [[nodiscard]] bool add(AddPtr& aPtr, Args&&... aArgs) { | |||
| 2191 | ReentrancyGuard g(*this); | |||
| 2192 | MOZ_ASSERT_IF(aPtr.isValid(), mTable)do { if (aPtr.isValid()) { do { static_assert( mozilla::detail ::AssertionConditionType<decltype(mTable)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mTable", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2192); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { MOZ_CrashSequence(__null, 2192); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); } } while ( false); | |||
| 2193 | MOZ_ASSERT_IF(aPtr.isValid(), aPtr.mTable == this)do { if (aPtr.isValid()) { do { static_assert( mozilla::detail ::AssertionConditionType<decltype(aPtr.mTable == this)> ::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(aPtr.mTable == this))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("aPtr.mTable == this", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2193); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mTable == this" ")"); do { MOZ_CrashSequence(__null, 2193); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); } } while ( false); | |||
| 2194 | MOZ_ASSERT(!aPtr.found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!aPtr.found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!aPtr.found()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!aPtr.found()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2194); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!aPtr.found()" ")"); do { MOZ_CrashSequence(__null, 2194); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2195 | MOZ_ASSERT(!(aPtr.mKeyHash & sCollisionBit))do { static_assert( mozilla::detail::AssertionConditionType< decltype(!(aPtr.mKeyHash & sCollisionBit))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!(aPtr.mKeyHash & sCollisionBit )))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("!(aPtr.mKeyHash & sCollisionBit)", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2195); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!(aPtr.mKeyHash & sCollisionBit)" ")"); do { MOZ_CrashSequence(__null, 2195); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2196 | ||||
| 2197 | // Check for error from ensureHash() here. | |||
| 2198 | if (!aPtr.isLive()) { | |||
| 2199 | return false; | |||
| 2200 | } | |||
| 2201 | ||||
| 2202 | MOZ_ASSERT(aPtr.mGeneration == generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(aPtr.mGeneration == generation())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aPtr.mGeneration == generation ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("aPtr.mGeneration == generation()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2202); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mGeneration == generation()" ")"); do { MOZ_CrashSequence(__null, 2202); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2203 | #ifdef DEBUG1 | |||
| 2204 | MOZ_ASSERT(aPtr.mMutationCount == mMutationCount)do { static_assert( mozilla::detail::AssertionConditionType< decltype(aPtr.mMutationCount == mMutationCount)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(! !(aPtr.mMutationCount == mMutationCount))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("aPtr.mMutationCount == mMutationCount" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2204); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mMutationCount == mMutationCount" ")"); do { MOZ_CrashSequence(__null, 2204); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2205 | #endif | |||
| 2206 | ||||
| 2207 | if (!aPtr.isValid()) { | |||
| 2208 | MOZ_ASSERT(!mTable && mEntryCount == 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype(!mTable && mEntryCount == 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!mTable && mEntryCount == 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("!mTable && mEntryCount == 0", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2208); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mTable && mEntryCount == 0" ")"); do { MOZ_CrashSequence(__null, 2208); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2209 | uint32_t newCapacity = rawCapacity(); | |||
| 2210 | RebuildStatus status = changeTableSize(newCapacity, ReportFailure); | |||
| 2211 | MOZ_ASSERT(status != NotOverloaded)do { static_assert( mozilla::detail::AssertionConditionType< decltype(status != NotOverloaded)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(status != NotOverloaded))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("status != NotOverloaded" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2211); AnnotateMozCrashReason("MOZ_ASSERT" "(" "status != NotOverloaded" ")"); do { MOZ_CrashSequence(__null, 2211); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2212 | if (status == RehashFailed) { | |||
| 2213 | return false; | |||
| 2214 | } | |||
| 2215 | aPtr.mSlot = findNonLiveSlot(aPtr.mKeyHash); | |||
| 2216 | ||||
| 2217 | } else if (aPtr.mSlot.isRemoved()) { | |||
| 2218 | // Changing an entry from removed to live does not affect whether we are | |||
| 2219 | // overloaded and can be handled separately. | |||
| 2220 | if (!this->checkSimulatedOOM()) { | |||
| 2221 | return false; | |||
| 2222 | } | |||
| 2223 | mRemovedCount--; | |||
| 2224 | aPtr.mKeyHash |= sCollisionBit; | |||
| 2225 | ||||
| 2226 | } else { | |||
| 2227 | // Preserve the validity of |aPtr.mSlot|. | |||
| 2228 | RebuildStatus status = rehashIfOverloaded(); | |||
| 2229 | if (status == RehashFailed) { | |||
| 2230 | return false; | |||
| 2231 | } | |||
| 2232 | if (status == NotOverloaded && !this->checkSimulatedOOM()) { | |||
| 2233 | return false; | |||
| 2234 | } | |||
| 2235 | if (status == Rehashed) { | |||
| 2236 | aPtr.mSlot = findNonLiveSlot(aPtr.mKeyHash); | |||
| 2237 | } | |||
| 2238 | } | |||
| 2239 | ||||
| 2240 | aPtr.mSlot.setLive(aPtr.mKeyHash, std::forward<Args>(aArgs)...); | |||
| 2241 | mEntryCount++; | |||
| 2242 | #ifdef DEBUG1 | |||
| 2243 | mMutationCount++; | |||
| 2244 | aPtr.mGeneration = generation(); | |||
| 2245 | aPtr.mMutationCount = mMutationCount; | |||
| 2246 | #endif | |||
| 2247 | return true; | |||
| 2248 | } | |||
| 2249 | ||||
| 2250 | // Note: |aLookup| may reference pieces of arguments in |aArgs|, so this | |||
| 2251 | // function must take care not to use |aLookup| after moving |aArgs|. | |||
| 2252 | template <typename... Args> | |||
| 2253 | void putNewInfallible(const Lookup& aLookup, Args&&... aArgs) { | |||
| 2254 | MOZ_ASSERT(!lookup(aLookup).found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!lookup(aLookup).found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!lookup(aLookup).found()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!lookup(aLookup).found()" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2254); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!lookup(aLookup).found()" ")"); do { MOZ_CrashSequence(__null, 2254); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2255 | ReentrancyGuard g(*this); | |||
| 2256 | HashNumber keyHash = prepareHash(HashPolicy::hash(aLookup)); | |||
| 2257 | putNewInfallibleInternal(keyHash, std::forward<Args>(aArgs)...); | |||
| 2258 | } | |||
| 2259 | ||||
| 2260 | // Note: |aLookup| may alias arguments in |aArgs|, so this function must take | |||
| 2261 | // care not to use |aLookup| after moving |aArgs|. | |||
| 2262 | template <typename... Args> | |||
| 2263 | [[nodiscard]] bool putNew(const Lookup& aLookup, Args&&... aArgs) { | |||
| 2264 | MOZ_ASSERT(!lookup(aLookup).found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!lookup(aLookup).found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!lookup(aLookup).found()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!lookup(aLookup).found()" , "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2264); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!lookup(aLookup).found()" ")"); do { MOZ_CrashSequence(__null, 2264); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2265 | ReentrancyGuard g(*this); | |||
| 2266 | if (!this->checkSimulatedOOM()) { | |||
| 2267 | return false; | |||
| 2268 | } | |||
| 2269 | HashNumber inputHash; | |||
| 2270 | if (!EnsureHash<HashPolicy>(aLookup, &inputHash)) { | |||
| 2271 | return false; | |||
| 2272 | } | |||
| 2273 | HashNumber keyHash = prepareHash(inputHash); | |||
| 2274 | if (rehashIfOverloaded() == RehashFailed) { | |||
| 2275 | return false; | |||
| 2276 | } | |||
| 2277 | putNewInfallibleInternal(keyHash, std::forward<Args>(aArgs)...); | |||
| 2278 | return true; | |||
| 2279 | } | |||
| 2280 | ||||
| 2281 | // Note: |aLookup| may be a reference pieces of arguments in |aArgs|, so this | |||
| 2282 | // function must take care not to use |aLookup| after moving |aArgs|. | |||
| 2283 | template <typename... Args> | |||
| 2284 | [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, const Lookup& aLookup, | |||
| 2285 | Args&&... aArgs) { | |||
| 2286 | // Check for error from ensureHash() here. | |||
| 2287 | if (!aPtr.isLive()) { | |||
| 2288 | return false; | |||
| 2289 | } | |||
| 2290 | #ifdef DEBUG1 | |||
| 2291 | aPtr.mGeneration = generation(); | |||
| 2292 | aPtr.mMutationCount = mMutationCount; | |||
| 2293 | #endif | |||
| 2294 | if (mTable) { | |||
| 2295 | ReentrancyGuard g(*this); | |||
| 2296 | // Check that aLookup has not been destroyed. | |||
| 2297 | MOZ_ASSERT(prepareHash(HashPolicy::hash(aLookup)) == aPtr.mKeyHash)do { static_assert( mozilla::detail::AssertionConditionType< decltype(prepareHash(HashPolicy::hash(aLookup)) == aPtr.mKeyHash )>::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(prepareHash(HashPolicy::hash(aLookup)) == aPtr.mKeyHash ))), 0))) { do { } while (false); MOZ_ReportAssertionFailure( "prepareHash(HashPolicy::hash(aLookup)) == aPtr.mKeyHash", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2297); AnnotateMozCrashReason("MOZ_ASSERT" "(" "prepareHash(HashPolicy::hash(aLookup)) == aPtr.mKeyHash" ")"); do { MOZ_CrashSequence(__null, 2297); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2298 | aPtr.mSlot = lookup<ForAdd>(aLookup, aPtr.mKeyHash); | |||
| 2299 | if (aPtr.found()) { | |||
| 2300 | return true; | |||
| 2301 | } | |||
| 2302 | } else { | |||
| 2303 | // Clear aPtr so it's invalid; add() will allocate storage and redo the | |||
| 2304 | // lookup. | |||
| 2305 | aPtr.mSlot = Slot(nullptr, nullptr); | |||
| 2306 | } | |||
| 2307 | return add(aPtr, std::forward<Args>(aArgs)...); | |||
| 2308 | } | |||
| 2309 | ||||
| 2310 | void remove(Ptr aPtr) { | |||
| 2311 | MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mTable)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mTable", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2311); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { MOZ_CrashSequence(__null, 2311); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2312 | ReentrancyGuard g(*this); | |||
| 2313 | MOZ_ASSERT(aPtr.found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(aPtr.found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aPtr.found()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("aPtr.found()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2313); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.found()" ")"); do { MOZ_CrashSequence(__null, 2313); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2314 | MOZ_ASSERT(aPtr.mGeneration == generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(aPtr.mGeneration == generation())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aPtr.mGeneration == generation ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("aPtr.mGeneration == generation()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2314); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mGeneration == generation()" ")"); do { MOZ_CrashSequence(__null, 2314); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2315 | remove(aPtr.mSlot); | |||
| 2316 | shrinkIfUnderloaded(); | |||
| 2317 | } | |||
| 2318 | ||||
| 2319 | template <typename KeyInput> | |||
| 2320 | void rekeyWithoutRehash(Ptr aPtr, const Lookup& aLookup, KeyInput&& aKey) { | |||
| 2321 | MOZ_ASSERT(mTable)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mTable)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(mTable))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mTable", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2321); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { MOZ_CrashSequence(__null, 2321); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2322 | ReentrancyGuard g(*this); | |||
| 2323 | MOZ_ASSERT(aPtr.found())do { static_assert( mozilla::detail::AssertionConditionType< decltype(aPtr.found())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aPtr.found()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("aPtr.found()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2323); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.found()" ")"); do { MOZ_CrashSequence(__null, 2323); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2324 | MOZ_ASSERT(aPtr.mGeneration == generation())do { static_assert( mozilla::detail::AssertionConditionType< decltype(aPtr.mGeneration == generation())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aPtr.mGeneration == generation ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("aPtr.mGeneration == generation()", "/root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2324); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mGeneration == generation()" ")"); do { MOZ_CrashSequence(__null, 2324); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2325 | typename HashTableEntry<T>::NonConstT t(std::move(*aPtr)); | |||
| 2326 | HashPolicy::setKey(t, std::forward<KeyInput>(aKey)); | |||
| 2327 | remove(aPtr.mSlot); | |||
| 2328 | HashNumber keyHash = prepareHash(HashPolicy::hash(aLookup)); | |||
| 2329 | putNewInfallibleInternal(keyHash, std::move(t)); | |||
| 2330 | } | |||
| 2331 | ||||
| 2332 | template <typename KeyInput> | |||
| 2333 | void rekeyAndMaybeRehash(Ptr aPtr, const Lookup& aLookup, KeyInput&& aKey) { | |||
| 2334 | rekeyWithoutRehash(aPtr, aLookup, std::forward<KeyInput>(aKey)); | |||
| 2335 | infallibleRehashIfOverloaded(); | |||
| 2336 | } | |||
| 2337 | ||||
| 2338 | static size_t offsetOfHashShift() { | |||
| 2339 | static_assert(sHashShiftBits == 8, | |||
| 2340 | "callers assume hash shift is stored in a byte"); | |||
| 2341 | // The hash shift is stored in the least significant bits of | |||
| 2342 | // mGenAndHashShift. On little-endian platforms, this is the | |||
| 2343 | // same offset as mGenAndHashShift itself. On big-endian platforms, | |||
| 2344 | // we have to add an additional offset to point to the last byte. | |||
| 2345 | // (Or we would if we had JIT support for any big-endian platforms.) | |||
| 2346 | if constexpr (std::endian::native == std::endian::big) { | |||
| 2347 | return offsetof(HashTable, mGenAndHashShift)__builtin_offsetof(HashTable, mGenAndHashShift) + sizeof(mGenAndHashShift) - | |||
| 2348 | sizeof(uint8_t); | |||
| 2349 | } else { | |||
| 2350 | return offsetof(HashTable, mGenAndHashShift)__builtin_offsetof(HashTable, mGenAndHashShift); | |||
| 2351 | } | |||
| 2352 | } | |||
| 2353 | static size_t offsetOfTable() { return offsetof(HashTable, mTable)__builtin_offsetof(HashTable, mTable); } | |||
| 2354 | static size_t offsetOfEntryCount() { | |||
| 2355 | return offsetof(HashTable, mEntryCount)__builtin_offsetof(HashTable, mEntryCount); | |||
| 2356 | } | |||
| 2357 | }; | |||
| 2358 | ||||
| 2359 | } // namespace detail | |||
| 2360 | } // namespace mozilla | |||
| 2361 | ||||
| 2362 | #endif /* mozilla_HashTable_h */ |