| File: | root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h |
| Warning: | line 1206, 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 (!sBuilders) { | |||
| 94 | return 0; | |||
| 95 | } | |||
| 96 | // aInnerWindowId might be a descendant WindowContext. Rather than walking the | |||
| 97 | // WindowContext ancestry for each builder, we just ask each builder whether | |||
| 98 | // it contains this id, since there won't be many builders. | |||
| 99 | for (const PdfStructTreeBuilder& builder : *sBuilders) { | |||
| 100 | if (int pdfId = builder.GetPdfIdInternal(aInnerWindowId, aAccId)) { | |||
| 101 | return pdfId; | |||
| 102 | } | |||
| 103 | } | |||
| 104 | return 0; | |||
| 105 | } | |||
| 106 | ||||
| 107 | /* static */ | |||
| 108 | PdfStructTreeBuilder::GlobalAccessibleId PdfStructTreeBuilder::GetAccId( | |||
| 109 | nsIFrame* aFrame) { | |||
| 110 | if (!StaticPrefs::accessibility_tagged_pdf_output_enabled()) { | |||
| 111 | return {}; | |||
| 112 | } | |||
| 113 | nsIContent* content = aFrame->GetContent(); | |||
| 114 | if (!content) { | |||
| 115 | return {}; | |||
| 116 | } | |||
| 117 | dom::Document* doc = content->OwnerDoc(); | |||
| 118 | // This should only ever be called for a document being printed and those are | |||
| 119 | // always static documents. | |||
| 120 | 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" , 120); AnnotateMozCrashReason("MOZ_ASSERT" "(" "doc->IsStaticDocument()" ")"); do { MOZ_CrashSequence(__null, 120); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 121 | DocAccessible* docAcc = GetExistingDocAccessible(doc); | |||
| 122 | if (!docAcc) { | |||
| 123 | return {}; | |||
| 124 | } | |||
| 125 | Accessible* acc = docAcc->GetAccessible(content); | |||
| 126 | if (!acc) { | |||
| 127 | return {}; | |||
| 128 | } | |||
| 129 | uint64_t innerWindowId = doc->InnerWindowID(); | |||
| 130 | if (!innerWindowId) { | |||
| 131 | return {}; | |||
| 132 | } | |||
| 133 | return {innerWindowId, acc->ID()}; | |||
| 134 | } | |||
| 135 | ||||
| 136 | PdfStructTreeBuilder::PdfStructTreeBuilder(uint64_t aInnerWindowId) | |||
| 137 | : mRootInnerWindowId(aInnerWindowId) { | |||
| 138 | mReadyPromise = new ReadyPromise::Private(__func__); | |||
| 139 | } | |||
| 140 | ||||
| 141 | void PdfStructTreeBuilder::InitInternal(dom::WindowContext* aWindowContext) { | |||
| 142 | if (aWindowContext->InnerWindowId() != mRootInnerWindowId) { | |||
| 143 | // We've just received the document for an out-of-process iframe. | |||
| 144 | 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" , 144); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mPendingOopIframes > 0" ")"); do { MOZ_CrashSequence(__null, 144); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 145 | --mPendingOopIframes; | |||
| 146 | } | |||
| 147 | if (dom::BrowserParent* bp = | |||
| 148 | aWindowContext->Canonical()->GetBrowserParent()) { | |||
| 149 | // Request the accessibility tree for each descendant out-of-process | |||
| 150 | // iframe. While all of the direct children should be reachable, some of the | |||
| 151 | // deeper descendants might not be yet, so we also traverse the descendants | |||
| 152 | // for each OOP iframe when InitInternal is called for it , skipping any | |||
| 153 | // that have already been requested. We could instead walk only the direct | |||
| 154 | // children, but walking descendants means we benefit from greater | |||
| 155 | // parallelism in the case that deeper descendants *are* already reachable. | |||
| 156 | bp->VisitAllDescendants([this](dom::BrowserParent* descBp) { | |||
| 157 | if (mRequestedBrowserParentIds.EnsureInserted(descBp->GetTabId())) { | |||
| 158 | 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" , 158); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!descBp->GetTopLevelDocAccessible()" ")"); do { MOZ_CrashSequence(__null, 158); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 159 | (void)descBp->SendRequestDocAccessibleForPrint(); | |||
| 160 | ++mPendingOopIframes; | |||
| 161 | } | |||
| 162 | }); | |||
| 163 | } | |||
| 164 | // XXX support out-of-process iframes inside a parent process document. | |||
| 165 | if (mPendingOopIframes == 0) { | |||
| 166 | // Once we've received all pending out-of-process iframes, we are ready to | |||
| 167 | // build the PDF struct tree. | |||
| 168 | mReadyPromise->Resolve(mozilla::Ok(), __func__); | |||
| 169 | } | |||
| 170 | } | |||
| 171 | ||||
| 172 | bool PdfStructTreeBuilder::BuildStructTree(SkPDF::StructureElementNode& aRoot) { | |||
| 173 | RefPtr wgp = dom::WindowGlobalParent::GetByInnerWindowId(mRootInnerWindowId); | |||
| 174 | if (!wgp) { | |||
| ||||
| 175 | return false; | |||
| 176 | } | |||
| 177 | Accessible* rootAcc = nullptr; | |||
| 178 | if (wgp->IsInProcess()) { | |||
| 179 | if (dom::Document* doc = wgp->GetDocument()) { | |||
| 180 | rootAcc = GetExistingDocAccessible(doc); | |||
| 181 | } | |||
| 182 | } else { | |||
| 183 | rootAcc = DocAccessibleParent::GetFrom(wgp); | |||
| 184 | } | |||
| 185 | if (!rootAcc) { | |||
| 186 | return false; | |||
| 187 | } | |||
| 188 | BuildStructSubtree(rootAcc, aRoot); | |||
| 189 | return true; | |||
| 190 | } | |||
| 191 | ||||
| 192 | // Returns the inner window id of the document containing aAcc. This must match | |||
| 193 | // the id computed by GetAccId in the process rendering that document. | |||
| 194 | static uint64_t InnerWindowIdFor(Accessible* aAcc) { | |||
| 195 | if (RemoteAccessible* remoteAcc = aAcc->AsRemote()) { | |||
| 196 | return remoteAcc->Document()->Manager()->InnerWindowId(); | |||
| 197 | } | |||
| 198 | return aAcc->AsLocal()->Document()->DocumentNode()->InnerWindowID(); | |||
| 199 | } | |||
| 200 | ||||
| 201 | int PdfStructTreeBuilder::GeneratePdfId(Accessible* aAcc) { | |||
| 202 | // This can be called more than once for the same Accessible; e.g. when | |||
| 203 | // referencing table cell headers. It should always return the same id for the | |||
| 204 | // same Accessible. | |||
| 205 | GlobalAccessibleId key = {InnerWindowIdFor(aAcc), aAcc->ID()}; | |||
| 206 | auto entry = mAccToPdf.lookupForAdd(key); | |||
| 207 | if (!entry) { | |||
| 208 | // We haven't seen this Accessible before. Generate a new PDF id. | |||
| 209 | 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" , 209); AnnotateMozCrashReason("MOZ_CRASH(" "mAccToPdf.add(entry, key, ++mLastPdfId)" ")"); do { MOZ_CrashSequence(__null, 209); __attribute__((nomerge )) ::abort(); } while (false); } while (false); } } while (false ); | |||
| 210 | } | |||
| 211 | return entry->value(); | |||
| 212 | } | |||
| 213 | ||||
| 214 | void PdfStructTreeBuilder::BuildStructSubtree( | |||
| 215 | Accessible* aAcc, SkPDF::StructureElementNode& aPdf) { | |||
| 216 | aPdf.fNodeId = GeneratePdfId(aAcc); | |||
| 217 | switch (aAcc->Role()) { | |||
| 218 | case roles::ARTICLE: | |||
| 219 | aPdf.fTypeString = "Art"; | |||
| 220 | break; | |||
| 221 | case roles::BLOCKQUOTE: | |||
| 222 | aPdf.fTypeString = "BlockQuote"; | |||
| 223 | break; | |||
| 224 | case roles::CAPTION: | |||
| 225 | aPdf.fTypeString = "Caption"; | |||
| 226 | break; | |||
| 227 | case roles::CELL: | |||
| 228 | case roles::GRID_CELL: { | |||
| 229 | aPdf.fTypeString = "TD"; | |||
| 230 | TableCellAccessible* cell = aAcc->AsTableCell(); | |||
| 231 | if (!cell) { | |||
| 232 | break; | |||
| 233 | } | |||
| 234 | // Query each axis separately so one doesn't suppress the other's implicit | |||
| 235 | // headers. | |||
| 236 | nsTArray<Accessible*> accHeaders; | |||
| 237 | cell->ColHeaderCells(&accHeaders); | |||
| 238 | nsTArray<Accessible*> accRowHeaders; | |||
| 239 | cell->RowHeaderCells(&accRowHeaders); | |||
| 240 | accHeaders.AppendElements(std::move(accRowHeaders)); | |||
| 241 | std::vector<int> pdfHeaders; | |||
| 242 | pdfHeaders.reserve(accHeaders.Length()); | |||
| 243 | for (Accessible* accHeader : accHeaders) { | |||
| 244 | pdfHeaders.push_back(GeneratePdfId(accHeader)); | |||
| 245 | } | |||
| 246 | aPdf.fAttributes.appendNodeIdArray("Table", "Headers", pdfHeaders); | |||
| 247 | break; | |||
| 248 | } | |||
| 249 | case roles::CODE: | |||
| 250 | aPdf.fTypeString = "Code"; | |||
| 251 | break; | |||
| 252 | case roles::COLUMNHEADER: | |||
| 253 | aPdf.fTypeString = "TH"; | |||
| 254 | aPdf.fAttributes.appendName("Table", "Scope", "Column"); | |||
| 255 | break; | |||
| 256 | case roles::DOCUMENT: | |||
| 257 | aPdf.fTypeString = "Document"; | |||
| 258 | break; | |||
| 259 | case roles::EMPHASIS: | |||
| 260 | aPdf.fTypeString = "Em"; | |||
| 261 | break; | |||
| 262 | case roles::GRID: | |||
| 263 | case roles::TABLE: | |||
| 264 | case roles::TREE_TABLE: | |||
| 265 | aPdf.fTypeString = "Table"; | |||
| 266 | break; | |||
| 267 | case roles::GROUPING: | |||
| 268 | aPdf.fTypeString = "Div"; | |||
| 269 | break; | |||
| 270 | case roles::GRAPHIC: | |||
| 271 | aPdf.fTypeString = "Figure"; | |||
| 272 | AccNameToPdfAlt(aAcc, aPdf); | |||
| 273 | // XXX We should ideally expose a BBox attribute, but how do we calculate | |||
| 274 | // this? | |||
| 275 | break; | |||
| 276 | case roles::HEADING: { | |||
| 277 | // For the PDF outline, SkPDF can accumulate text from headings itself, | |||
| 278 | // but it requires that glyph runs include text, whereas we provide glyph | |||
| 279 | // indexes when drawing. Rather than plumbing the text through to the draw | |||
| 280 | // target, we instead explicitly provide the heading name as alt text | |||
| 281 | // here, since it's readily available. | |||
| 282 | AccNameToPdfAlt(aAcc, aPdf); | |||
| 283 | aPdf.fExposeAlt = false; | |||
| 284 | int32_t level = aAcc->GroupPosition().level; | |||
| 285 | // PDF has H1 through H6. | |||
| 286 | if (1 <= level && level <= 6) { | |||
| 287 | nsAutoCString type; | |||
| 288 | type.AppendPrintf("H%d", level); | |||
| 289 | aPdf.fTypeString = SkString(type.get()); | |||
| 290 | break; | |||
| 291 | } | |||
| 292 | // Otherwise, use the generic H. | |||
| 293 | aPdf.fTypeString = "H"; | |||
| 294 | break; | |||
| 295 | } | |||
| 296 | case roles::LANDMARK: | |||
| 297 | if (aAcc->LandmarkRole() == nsGkAtoms::complementary) { | |||
| 298 | aPdf.fTypeString = "Aside"; | |||
| 299 | } | |||
| 300 | break; | |||
| 301 | case roles::LINK: | |||
| 302 | aPdf.fTypeString = "Link"; | |||
| 303 | break; | |||
| 304 | case roles::LIST: | |||
| 305 | aPdf.fTypeString = "L"; | |||
| 306 | break; | |||
| 307 | case roles::LISTITEM: | |||
| 308 | aPdf.fTypeString = "LI"; | |||
| 309 | break; | |||
| 310 | case roles::LISTITEM_MARKER: | |||
| 311 | aPdf.fTypeString = "Lbl"; | |||
| 312 | break; | |||
| 313 | case roles::PARAGRAPH: | |||
| 314 | aPdf.fTypeString = "P"; | |||
| 315 | break; | |||
| 316 | case roles::ROW: | |||
| 317 | aPdf.fTypeString = "TR"; | |||
| 318 | break; | |||
| 319 | case roles::ROWHEADER: | |||
| 320 | aPdf.fTypeString = "TH"; | |||
| 321 | aPdf.fAttributes.appendName("Table", "Scope", "Row"); | |||
| 322 | break; | |||
| 323 | case roles::STRONG: | |||
| 324 | aPdf.fTypeString = "Strong"; | |||
| 325 | break; | |||
| 326 | default: | |||
| 327 | aPdf.fTypeString = "NonStruct"; | |||
| 328 | } | |||
| 329 | if (TableCellAccessible* cell = aAcc->AsTableCell()) { | |||
| 330 | uint32_t rowSpan = cell->RowExtent(); | |||
| 331 | if (rowSpan > 1) { | |||
| 332 | aPdf.fAttributes.appendInt("Table", "RowSpan", static_cast<int>(rowSpan)); | |||
| 333 | } | |||
| 334 | uint32_t colSpan = cell->ColExtent(); | |||
| 335 | if (colSpan > 1) { | |||
| 336 | aPdf.fAttributes.appendInt("Table", "ColSpan", static_cast<int>(colSpan)); | |||
| 337 | } | |||
| 338 | } | |||
| 339 | ||||
| 340 | uint32_t count = aAcc->ChildCount(); | |||
| 341 | aPdf.fChildVector.resize(count); | |||
| 342 | for (uint32_t c = 0; c < count; ++c) { | |||
| 343 | aPdf.fChildVector[c] = std::make_unique<SkPDF::StructureElementNode>(); | |||
| 344 | BuildStructSubtree(aAcc->ChildAt(c), *aPdf.fChildVector[c]); | |||
| 345 | } | |||
| 346 | } | |||
| 347 | ||||
| 348 | int PdfStructTreeBuilder::GetPdfIdInternal(uint64_t aInnerWindowId, | |||
| 349 | uint64_t aAccId) const { | |||
| 350 | if (aInnerWindowId == 0) { | |||
| 351 | // This indicates that the following drawing instructions are not associated | |||
| 352 | // with anything in the struct tree; e.g. page headers and footers. | |||
| 353 | MOZ_ASSERT(aAccId == 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype(aAccId == 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(aAccId == 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("aAccId == 0", "/root/firefox-clang/accessible/pdf/PdfStructTreeBuilder.cpp" , 353); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aAccId == 0" ")"); do { MOZ_CrashSequence(__null, 353); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 354 | return 0; | |||
| 355 | } | |||
| 356 | if (auto entry = mAccToPdf.lookup({aInnerWindowId, aAccId})) { | |||
| 357 | return entry->value(); | |||
| 358 | } | |||
| 359 | MOZ_ASSERT_UNREACHABLE(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: " "Display list contains Accessible id which isn't in the map!" ")", "/root/firefox-clang/accessible/pdf/PdfStructTreeBuilder.cpp" , 360); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") (" "MOZ_ASSERT_UNREACHABLE: " "Display list contains Accessible id which isn't in the map!" ")"); do { MOZ_CrashSequence(__null, 360); __attribute__((nomerge )) ::abort(); } while (false); } } while (false) | |||
| 360 | "Display list contains Accessible id which isn't in the map!")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: " "Display list contains Accessible id which isn't in the map!" ")", "/root/firefox-clang/accessible/pdf/PdfStructTreeBuilder.cpp" , 360); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") (" "MOZ_ASSERT_UNREACHABLE: " "Display list contains Accessible id which isn't in the map!" ")"); do { MOZ_CrashSequence(__null, 360); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 361 | return 0; | |||
| 362 | } | |||
| 363 | ||||
| 364 | } // 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 | // HashMap is not copyable or assignable. | |||
| 145 | HashMap(const HashMap& hm) = delete; | |||
| 146 | HashMap& operator=(const HashMap& hm) = delete; | |||
| 147 | ||||
| 148 | using TableEntry = HashMapEntry<Key, Value>; | |||
| 149 | ||||
| 150 | struct MapHashPolicy : HashPolicy { | |||
| 151 | using Base = HashPolicy; | |||
| 152 | using KeyType = Key; | |||
| 153 | ||||
| 154 | static const Key& getKey(TableEntry& aEntry) { return aEntry.key(); } | |||
| 155 | ||||
| 156 | template <typename KeyInput> | |||
| 157 | static void setKey(TableEntry& aEntry, KeyInput&& aKey) { | |||
| 158 | HashPolicy::rekey(aEntry.mutableKey(), std::forward<KeyInput>(aKey)); | |||
| 159 | } | |||
| 160 | }; | |||
| 161 | ||||
| 162 | using Impl = detail::HashTable<TableEntry, MapHashPolicy, AllocPolicy>; | |||
| 163 | Impl mImpl; | |||
| 164 | ||||
| 165 | public: | |||
| 166 | using Lookup = typename HashPolicy::Lookup; | |||
| 167 | using Entry = TableEntry; | |||
| 168 | ||||
| 169 | // -- Initialization ------------------------------------------------------- | |||
| 170 | ||||
| 171 | constexpr explicit HashMap(AllocPolicy aAllocPolicy = AllocPolicy(), | |||
| 172 | uint32_t aLen = Impl::sDefaultLen) | |||
| 173 | : mImpl(std::move(aAllocPolicy), aLen) {} | |||
| 174 | ||||
| 175 | explicit HashMap(uint32_t aLen) : mImpl(AllocPolicy(), aLen) {} | |||
| 176 | ||||
| 177 | // HashMap is movable. | |||
| 178 | HashMap(HashMap&& aRhs) = default; | |||
| 179 | HashMap& operator=(HashMap&& aRhs) = default; | |||
| 180 | ||||
| 181 | // Swap the contents of this hash map with another. | |||
| 182 | void swap(HashMap& aOther) { mImpl.swap(aOther.mImpl); } | |||
| 183 | ||||
| 184 | // -- Status and sizing ---------------------------------------------------- | |||
| 185 | ||||
| 186 | // The map's current generation. | |||
| 187 | Generation generation() const { return mImpl.generation(); } | |||
| 188 | ||||
| 189 | // Is the map empty? | |||
| 190 | bool empty() const { return mImpl.empty(); } | |||
| 191 | ||||
| 192 | // Number of keys/values in the map. | |||
| 193 | uint32_t count() const { return mImpl.count(); } | |||
| 194 | ||||
| 195 | // Number of key/value slots in the map. Note: resize will happen well before | |||
| 196 | // count() == capacity(). | |||
| 197 | uint32_t capacity() const { return mImpl.capacity(); } | |||
| 198 | ||||
| 199 | // The size of the map's entry storage, in bytes. If the keys/values contain | |||
| 200 | // pointers to other heap blocks, you must iterate over the map and measure | |||
| 201 | // them separately; hence the "shallow" prefix. | |||
| 202 | size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { | |||
| 203 | return mImpl.shallowSizeOfExcludingThis(aMallocSizeOf); | |||
| 204 | } | |||
| 205 | size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { | |||
| 206 | return aMallocSizeOf(this) + | |||
| 207 | mImpl.shallowSizeOfExcludingThis(aMallocSizeOf); | |||
| 208 | } | |||
| 209 | ||||
| 210 | // Attempt to minimize the capacity(). If the table is empty, this will free | |||
| 211 | // the empty storage and upon regrowth it will be given the minimum capacity. | |||
| 212 | void compact() { mImpl.compact(); } | |||
| 213 | ||||
| 214 | // Attempt to reserve enough space to fit at least |aLen| elements. This is | |||
| 215 | // total capacity, including elements already present. Does nothing if the | |||
| 216 | // map already has sufficient capacity. | |||
| 217 | [[nodiscard]] bool reserve(uint32_t aLen) { return mImpl.reserve(aLen); } | |||
| 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 | // HashSet is not copyable or assignable. | |||
| 472 | HashSet(const HashSet& hs) = delete; | |||
| 473 | HashSet& operator=(const HashSet& hs) = delete; | |||
| 474 | ||||
| 475 | struct SetHashPolicy : HashPolicy { | |||
| 476 | using Base = HashPolicy; | |||
| 477 | using KeyType = T; | |||
| 478 | ||||
| 479 | static const KeyType& getKey(const T& aT) { return aT; } | |||
| 480 | ||||
| 481 | template <typename KeyInput> | |||
| 482 | static void setKey(T& aT, KeyInput&& aKey) { | |||
| 483 | HashPolicy::rekey(aT, std::forward<KeyInput>(aKey)); | |||
| 484 | } | |||
| 485 | }; | |||
| 486 | ||||
| 487 | using Impl = detail::HashTable<const T, SetHashPolicy, AllocPolicy>; | |||
| 488 | Impl mImpl; | |||
| 489 | ||||
| 490 | public: | |||
| 491 | using Lookup = typename HashPolicy::Lookup; | |||
| 492 | using Entry = T; | |||
| 493 | ||||
| 494 | // -- Initialization ------------------------------------------------------- | |||
| 495 | ||||
| 496 | explicit HashSet(AllocPolicy aAllocPolicy = AllocPolicy(), | |||
| 497 | uint32_t aLen = Impl::sDefaultLen) | |||
| 498 | : mImpl(std::move(aAllocPolicy), aLen) {} | |||
| 499 | ||||
| 500 | explicit HashSet(uint32_t aLen) : mImpl(AllocPolicy(), aLen) {} | |||
| 501 | ||||
| 502 | // HashSet is movable. | |||
| 503 | HashSet(HashSet&& aRhs) = default; | |||
| 504 | HashSet& operator=(HashSet&& aRhs) = default; | |||
| 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 | private: | |||
| 1019 | HashMapEntry(const HashMapEntry&) = delete; | |||
| 1020 | void operator=(const HashMapEntry&) = delete; | |||
| 1021 | }; | |||
| 1022 | ||||
| 1023 | namespace detail { | |||
| 1024 | ||||
| 1025 | static const HashNumber kHashTableFreeKey = 0; | |||
| 1026 | static const HashNumber kHashTableRemovedKey = 1; | |||
| 1027 | static const HashNumber kHashTableCollisionBit = 1; | |||
| 1028 | ||||
| 1029 | template <class T, class HashPolicy, class AllocPolicy> | |||
| 1030 | class HashTable; | |||
| 1031 | ||||
| 1032 | template <typename T> | |||
| 1033 | class EntrySlot; | |||
| 1034 | ||||
| 1035 | template <typename T> | |||
| 1036 | class HashTableEntry { | |||
| 1037 | private: | |||
| 1038 | using NonConstT = std::remove_const_t<T>; | |||
| 1039 | ||||
| 1040 | // Instead of having a hash table entry store that looks like this: | |||
| 1041 | // | |||
| 1042 | // +--------+--------+--------+--------+ | |||
| 1043 | // | entry0 | entry1 | .... | entryN | | |||
| 1044 | // +--------+--------+--------+--------+ | |||
| 1045 | // | |||
| 1046 | // where the entries contained their cached hash code, we're going to lay out | |||
| 1047 | // the entry store thusly: | |||
| 1048 | // | |||
| 1049 | // +-------+-------+-------+-------+--------+--------+--------+--------+ | |||
| 1050 | // | hash0 | hash1 | ... | hashN | entry0 | entry1 | .... | entryN | | |||
| 1051 | // +-------+-------+-------+-------+--------+--------+--------+--------+ | |||
| 1052 | // | |||
| 1053 | // with all the cached hashes prior to the actual entries themselves. | |||
| 1054 | // | |||
| 1055 | // We do this because implementing the first strategy requires us to make | |||
| 1056 | // HashTableEntry look roughly like: | |||
| 1057 | // | |||
| 1058 | // template <typename T> | |||
| 1059 | // class HashTableEntry { | |||
| 1060 | // HashNumber mKeyHash; | |||
| 1061 | // T mValue; | |||
| 1062 | // }; | |||
| 1063 | // | |||
| 1064 | // The problem with this setup is that, depending on the layout of `T`, there | |||
| 1065 | // may be platform ABI-mandated padding between `mKeyHash` and the first | |||
| 1066 | // member of `T`. This ABI-mandated padding is wasted space, and can be | |||
| 1067 | // surprisingly common, e.g. when `T` is a single pointer on 64-bit platforms. | |||
| 1068 | // In such cases, we're throwing away a quarter of our entry store on padding, | |||
| 1069 | // which is undesirable. | |||
| 1070 | // | |||
| 1071 | // The second layout above, namely: | |||
| 1072 | // | |||
| 1073 | // +-------+-------+-------+-------+--------+--------+--------+--------+ | |||
| 1074 | // | hash0 | hash1 | ... | hashN | entry0 | entry1 | .... | entryN | | |||
| 1075 | // +-------+-------+-------+-------+--------+--------+--------+--------+ | |||
| 1076 | // | |||
| 1077 | // means there is no wasted space between the hashes themselves, and no wasted | |||
| 1078 | // space between the entries themselves. However, we would also like there to | |||
| 1079 | // be no gap between the last hash and the first entry. The memory allocator | |||
| 1080 | // guarantees the alignment of the start of the hashes. The use of a | |||
| 1081 | // power-of-two capacity of at least 4 guarantees that the alignment of the | |||
| 1082 | // *end* of the hash array is no less than the alignment of the start. | |||
| 1083 | // Finally, the static_asserts here guarantee that the entries themselves | |||
| 1084 | // don't need to be any more aligned than the alignment of the entry store | |||
| 1085 | // itself. | |||
| 1086 | // | |||
| 1087 | // This assertion is safe for 32-bit builds because on both Windows and Linux | |||
| 1088 | // (including Android), the minimum alignment for allocations larger than 8 | |||
| 1089 | // bytes is 8 bytes, and the actual data for entries in our entry store is | |||
| 1090 | // guaranteed to have that alignment as well, thanks to the power-of-two | |||
| 1091 | // number of cached hash values stored prior to the entry data. | |||
| 1092 | ||||
| 1093 | // The allocation policy must allocate a table with at least this much | |||
| 1094 | // alignment. | |||
| 1095 | static constexpr size_t kMinimumAlignment = 8; | |||
| 1096 | ||||
| 1097 | static_assert(alignof(HashNumber) <= kMinimumAlignment, | |||
| 1098 | "[N*2 hashes, N*2 T values] allocation's alignment must be " | |||
| 1099 | "enough to align each hash"); | |||
| 1100 | static_assert(alignof(NonConstT) <= 2 * sizeof(HashNumber), | |||
| 1101 | "subsequent N*2 T values must not require more than an even " | |||
| 1102 | "number of HashNumbers provides"); | |||
| 1103 | ||||
| 1104 | static const HashNumber sFreeKey = kHashTableFreeKey; | |||
| 1105 | static const HashNumber sRemovedKey = kHashTableRemovedKey; | |||
| 1106 | static const HashNumber sCollisionBit = kHashTableCollisionBit; | |||
| 1107 | ||||
| 1108 | alignas(NonConstT) unsigned char mValueData[sizeof(NonConstT)]; | |||
| 1109 | ||||
| 1110 | private: | |||
| 1111 | template <class, class, class> | |||
| 1112 | friend class HashTable; | |||
| 1113 | template <typename> | |||
| 1114 | friend class EntrySlot; | |||
| 1115 | ||||
| 1116 | // Some versions of GCC treat it as a -Wstrict-aliasing violation (ergo a | |||
| 1117 | // -Werror compile error) to reinterpret_cast<> |mValueData| to |T*|, even | |||
| 1118 | // through |void*|. Placing the latter cast in these separate functions | |||
| 1119 | // breaks the chain such that affected GCC versions no longer warn/error. | |||
| 1120 | void* rawValuePtr() { return mValueData; } | |||
| 1121 | ||||
| 1122 | static bool isLiveHash(HashNumber hash) { return hash > sRemovedKey; } | |||
| 1123 | ||||
| 1124 | HashTableEntry(const HashTableEntry&) = delete; | |||
| 1125 | void operator=(const HashTableEntry&) = delete; | |||
| 1126 | ||||
| 1127 | NonConstT* valuePtr() { return reinterpret_cast<NonConstT*>(rawValuePtr()); } | |||
| 1128 | ||||
| 1129 | void destroyStoredT() { | |||
| 1130 | NonConstT* ptr = valuePtr(); | |||
| 1131 | ptr->~T(); | |||
| 1132 | MOZ_MAKE_MEM_UNDEFINED(ptr, sizeof(*ptr))do { } while (0); | |||
| 1133 | } | |||
| 1134 | ||||
| 1135 | public: | |||
| 1136 | HashTableEntry() = default; | |||
| 1137 | ||||
| 1138 | ~HashTableEntry() { MOZ_MAKE_MEM_UNDEFINED(this, sizeof(*this))do { } while (0); } | |||
| 1139 | ||||
| 1140 | void destroy() { destroyStoredT(); } | |||
| 1141 | ||||
| 1142 | void swap(HashTableEntry* aOther, bool aOtherIsLive) { | |||
| 1143 | // This allows types to use Argument-Dependent-Lookup, and thus use a custom | |||
| 1144 | // std::swap, which is needed by types like JS::Heap and such. | |||
| 1145 | using std::swap; | |||
| 1146 | ||||
| 1147 | if (this == aOther) { | |||
| 1148 | return; | |||
| 1149 | } | |||
| 1150 | if (aOtherIsLive) { | |||
| 1151 | swap(*valuePtr(), *aOther->valuePtr()); | |||
| 1152 | } else { | |||
| 1153 | new (KnownNotNull, aOther->valuePtr()) NonConstT(std::move(*valuePtr())); | |||
| 1154 | destroy(); | |||
| 1155 | } | |||
| 1156 | } | |||
| 1157 | ||||
| 1158 | T& get() { return *valuePtr(); } | |||
| 1159 | ||||
| 1160 | NonConstT& getMutable() { return *valuePtr(); } | |||
| 1161 | }; | |||
| 1162 | ||||
| 1163 | // A slot represents a cached hash value and its associated entry stored | |||
| 1164 | // in the hash table. These two things are not stored in contiguous memory. | |||
| 1165 | template <class T> | |||
| 1166 | class EntrySlot { | |||
| 1167 | using NonConstT = std::remove_const_t<T>; | |||
| 1168 | ||||
| 1169 | using Entry = HashTableEntry<T>; | |||
| 1170 | ||||
| 1171 | Entry* mEntry; | |||
| 1172 | HashNumber* mKeyHash; | |||
| 1173 | ||||
| 1174 | template <class, class, class> | |||
| 1175 | friend class HashTable; | |||
| 1176 | ||||
| 1177 | EntrySlot(Entry* aEntry, HashNumber* aKeyHash) | |||
| 1178 | : mEntry(aEntry), mKeyHash(aKeyHash) {} | |||
| 1179 | ||||
| 1180 | public: | |||
| 1181 | static bool isLiveHash(HashNumber hash) { return hash > Entry::sRemovedKey; } | |||
| 1182 | ||||
| 1183 | EntrySlot(const EntrySlot&) = default; | |||
| 1184 | EntrySlot(EntrySlot&& aOther) = default; | |||
| 1185 | ||||
| 1186 | EntrySlot& operator=(const EntrySlot&) = default; | |||
| 1187 | EntrySlot& operator=(EntrySlot&&) = default; | |||
| 1188 | ||||
| 1189 | bool operator==(const EntrySlot& aRhs) const { return mEntry == aRhs.mEntry; } | |||
| 1190 | ||||
| 1191 | bool operator<(const EntrySlot& aRhs) const { return mEntry < aRhs.mEntry; } | |||
| 1192 | ||||
| 1193 | EntrySlot& operator++() { | |||
| 1194 | ++mEntry; | |||
| 1195 | ++mKeyHash; | |||
| 1196 | return *this; | |||
| 1197 | } | |||
| 1198 | ||||
| 1199 | void destroy() { mEntry->destroy(); } | |||
| 1200 | ||||
| 1201 | void swap(EntrySlot& aOther) { | |||
| 1202 | mEntry->swap(aOther.mEntry, aOther.isLive()); | |||
| 1203 | std::swap(*mKeyHash, *aOther.mKeyHash); | |||
| 1204 | } | |||
| 1205 | ||||
| 1206 | T& get() const { return mEntry->get(); } | |||
| ||||
| 1207 | ||||
| 1208 | NonConstT& getMutable() { return mEntry->getMutable(); } | |||
| 1209 | ||||
| 1210 | bool isFree() const { return *mKeyHash == Entry::sFreeKey; } | |||
| 1211 | ||||
| 1212 | void clearLive() { | |||
| 1213 | 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" , 1213); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { MOZ_CrashSequence(__null, 1213); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1214 | *mKeyHash = Entry::sFreeKey; | |||
| 1215 | mEntry->destroyStoredT(); | |||
| 1216 | } | |||
| 1217 | ||||
| 1218 | void clear() { | |||
| 1219 | if (isLive()) { | |||
| 1220 | mEntry->destroyStoredT(); | |||
| 1221 | } | |||
| 1222 | MOZ_MAKE_MEM_UNDEFINED(mEntry, sizeof(*mEntry))do { } while (0); | |||
| 1223 | *mKeyHash = Entry::sFreeKey; | |||
| 1224 | } | |||
| 1225 | ||||
| 1226 | bool isRemoved() const { return *mKeyHash == Entry::sRemovedKey; } | |||
| 1227 | ||||
| 1228 | void removeLive() { | |||
| 1229 | 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" , 1229); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { MOZ_CrashSequence(__null, 1229); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1230 | *mKeyHash = Entry::sRemovedKey; | |||
| 1231 | mEntry->destroyStoredT(); | |||
| 1232 | } | |||
| 1233 | ||||
| 1234 | bool isLive() const { return isLiveHash(*mKeyHash); } | |||
| 1235 | ||||
| 1236 | void setCollision() { | |||
| 1237 | 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" , 1237); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { MOZ_CrashSequence(__null, 1237); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1238 | *mKeyHash |= Entry::sCollisionBit; | |||
| 1239 | } | |||
| 1240 | void unsetCollision() { *mKeyHash &= ~Entry::sCollisionBit; } | |||
| 1241 | bool hasCollision() const { return *mKeyHash & Entry::sCollisionBit; } | |||
| 1242 | bool matchHash(HashNumber hn) { | |||
| 1243 | return (*mKeyHash & ~Entry::sCollisionBit) == hn; | |||
| 1244 | } | |||
| 1245 | HashNumber getKeyHash() const { return *mKeyHash & ~Entry::sCollisionBit; } | |||
| 1246 | ||||
| 1247 | template <typename... Args> | |||
| 1248 | void setLive(HashNumber aHashNumber, Args&&... aArgs) { | |||
| 1249 | 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" , 1249); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!isLive()" ")" ); do { MOZ_CrashSequence(__null, 1249); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1250 | *mKeyHash = aHashNumber; | |||
| 1251 | new (KnownNotNull, mEntry->valuePtr()) T(std::forward<Args>(aArgs)...); | |||
| 1252 | 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" , 1252); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { MOZ_CrashSequence(__null, 1252); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1253 | } | |||
| 1254 | ||||
| 1255 | Entry* toEntry() const { return mEntry; } | |||
| 1256 | }; | |||
| 1257 | ||||
| 1258 | template <class T, class HashPolicy, class AllocPolicy> | |||
| 1259 | class MOZ_STANDALONE_DEBUG[[clang::standalone_debug]] HashTable : private AllocPolicy { | |||
| 1260 | friend class mozilla::ReentrancyGuard; | |||
| 1261 | ||||
| 1262 | using NonConstT = std::remove_const_t<T>; | |||
| 1263 | using Key = typename HashPolicy::KeyType; | |||
| 1264 | using Lookup = typename HashPolicy::Lookup; | |||
| 1265 | ||||
| 1266 | public: | |||
| 1267 | using Entry = HashTableEntry<T>; | |||
| 1268 | using Slot = EntrySlot<T>; | |||
| 1269 | ||||
| 1270 | template <typename F> | |||
| 1271 | static void forEachSlot(char* aTable, uint32_t aCapacity, F&& f) { | |||
| 1272 | auto hashes = reinterpret_cast<HashNumber*>(aTable); | |||
| 1273 | auto entries = reinterpret_cast<Entry*>(&hashes[aCapacity]); | |||
| 1274 | Slot slot(entries, hashes); | |||
| 1275 | for (size_t i = 0; i < size_t(aCapacity); ++i) { | |||
| 1276 | f(slot); | |||
| 1277 | ++slot; | |||
| 1278 | } | |||
| 1279 | } | |||
| 1280 | ||||
| 1281 | // A nullable pointer to a hash table element. A Ptr |p| can be tested | |||
| 1282 | // either explicitly |if (p.found()) p->...| or using boolean conversion | |||
| 1283 | // |if (p) p->...|. Ptr objects must not be used after any mutating hash | |||
| 1284 | // table operations unless |generation()| is tested. | |||
| 1285 | class Ptr { | |||
| 1286 | friend class HashTable; | |||
| 1287 | ||||
| 1288 | Slot mSlot; | |||
| 1289 | #ifdef DEBUG1 | |||
| 1290 | const HashTable* mTable; | |||
| 1291 | Generation mGeneration; | |||
| 1292 | #endif | |||
| 1293 | ||||
| 1294 | protected: | |||
| 1295 | Ptr(Slot aSlot, const HashTable& aTable) | |||
| 1296 | : mSlot(aSlot) | |||
| 1297 | #ifdef DEBUG1 | |||
| 1298 | , | |||
| 1299 | mTable(&aTable), | |||
| 1300 | mGeneration(aTable.generation()) | |||
| 1301 | #endif | |||
| 1302 | { | |||
| 1303 | } | |||
| 1304 | ||||
| 1305 | // This constructor is used only by AddPtr() within lookupForAdd(). | |||
| 1306 | explicit Ptr(const HashTable& aTable) | |||
| 1307 | : mSlot(nullptr, nullptr) | |||
| 1308 | #ifdef DEBUG1 | |||
| 1309 | , | |||
| 1310 | mTable(&aTable), | |||
| 1311 | mGeneration(aTable.generation()) | |||
| 1312 | #endif | |||
| 1313 | { | |||
| 1314 | } | |||
| 1315 | ||||
| 1316 | bool isValid() const { return !!mSlot.toEntry(); } | |||
| 1317 | ||||
| 1318 | public: | |||
| 1319 | Ptr() | |||
| 1320 | : mSlot(nullptr, nullptr) | |||
| 1321 | #ifdef DEBUG1 | |||
| 1322 | , | |||
| 1323 | mTable(nullptr), | |||
| 1324 | mGeneration(0) | |||
| 1325 | #endif | |||
| 1326 | { | |||
| 1327 | } | |||
| 1328 | ||||
| 1329 | bool found() const { | |||
| 1330 | if (!isValid()) { | |||
| 1331 | return false; | |||
| 1332 | } | |||
| 1333 | #ifdef DEBUG1 | |||
| 1334 | 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" , 1334); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()" ")"); do { MOZ_CrashSequence(__null, 1334); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1335 | #endif | |||
| 1336 | return mSlot.isLive(); | |||
| 1337 | } | |||
| 1338 | ||||
| 1339 | explicit operator bool() const { return found(); } | |||
| 1340 | ||||
| 1341 | bool operator==(const Ptr& aRhs) const { | |||
| 1342 | 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" , 1342); AnnotateMozCrashReason("MOZ_ASSERT" "(" "found() && aRhs.found()" ")"); do { MOZ_CrashSequence(__null, 1342); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1343 | return mSlot == aRhs.mSlot; | |||
| 1344 | } | |||
| 1345 | ||||
| 1346 | bool operator!=(const Ptr& aRhs) const { | |||
| 1347 | #ifdef DEBUG1 | |||
| 1348 | 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" , 1348); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()" ")"); do { MOZ_CrashSequence(__null, 1348); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1349 | #endif | |||
| 1350 | return !(*this == aRhs); | |||
| 1351 | } | |||
| 1352 | ||||
| 1353 | T& operator*() const { | |||
| 1354 | #ifdef DEBUG1 | |||
| 1355 | 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" , 1355); AnnotateMozCrashReason("MOZ_ASSERT" "(" "found()" ")" ); do { MOZ_CrashSequence(__null, 1355); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1356 | 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" , 1356); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()" ")"); do { MOZ_CrashSequence(__null, 1356); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1357 | #endif | |||
| 1358 | return mSlot.get(); | |||
| 1359 | } | |||
| 1360 | ||||
| 1361 | T* operator->() const { | |||
| 1362 | #ifdef DEBUG1 | |||
| 1363 | 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" , 1363); AnnotateMozCrashReason("MOZ_ASSERT" "(" "found()" ")" ); do { MOZ_CrashSequence(__null, 1363); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1364 | 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" , 1364); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()" ")"); do { MOZ_CrashSequence(__null, 1364); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1365 | #endif | |||
| 1366 | return &mSlot.get(); | |||
| 1367 | } | |||
| 1368 | }; | |||
| 1369 | ||||
| 1370 | // A Ptr that can be used to add a key after a failed lookup. | |||
| 1371 | class AddPtr : public Ptr { | |||
| 1372 | friend class HashTable; | |||
| 1373 | ||||
| 1374 | HashNumber mKeyHash; | |||
| 1375 | #ifdef DEBUG1 | |||
| 1376 | uint64_t mMutationCount; | |||
| 1377 | #endif | |||
| 1378 | ||||
| 1379 | AddPtr(Slot aSlot, const HashTable& aTable, HashNumber aHashNumber) | |||
| 1380 | : Ptr(aSlot, aTable), | |||
| 1381 | mKeyHash(aHashNumber) | |||
| 1382 | #ifdef DEBUG1 | |||
| 1383 | , | |||
| 1384 | mMutationCount(aTable.mMutationCount) | |||
| 1385 | #endif | |||
| 1386 | { | |||
| 1387 | } | |||
| 1388 | ||||
| 1389 | // This constructor is used when lookupForAdd() is performed on a table | |||
| 1390 | // lacking entry storage; it leaves mSlot null but initializes everything | |||
| 1391 | // else. | |||
| 1392 | AddPtr(const HashTable& aTable, HashNumber aHashNumber) | |||
| 1393 | : Ptr(aTable), | |||
| 1394 | mKeyHash(aHashNumber) | |||
| 1395 | #ifdef DEBUG1 | |||
| 1396 | , | |||
| 1397 | mMutationCount(aTable.mMutationCount) | |||
| 1398 | #endif | |||
| 1399 | { | |||
| 1400 | 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" , 1400); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { MOZ_CrashSequence(__null, 1400); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1401 | } | |||
| 1402 | ||||
| 1403 | bool isLive() const { return isLiveHash(mKeyHash); } | |||
| 1404 | ||||
| 1405 | public: | |||
| 1406 | AddPtr() : mKeyHash(0) {} | |||
| 1407 | }; | |||
| 1408 | ||||
| 1409 | // A hash table iterator that (mostly) doesn't allow table modifications. | |||
| 1410 | // As with Ptr/AddPtr, Iterator objects must not be used after any mutating | |||
| 1411 | // hash table operation unless the |generation()| is tested. | |||
| 1412 | class Iterator { | |||
| 1413 | void moveToNextLiveEntry() { | |||
| 1414 | while (++mCur < mEnd && !mCur.isLive()) { | |||
| 1415 | continue; | |||
| 1416 | } | |||
| 1417 | } | |||
| 1418 | ||||
| 1419 | protected: | |||
| 1420 | friend class HashTable; | |||
| 1421 | ||||
| 1422 | explicit Iterator(const HashTable& aTable) | |||
| 1423 | : mCur(aTable.slotForIndex(0)), | |||
| 1424 | mEnd(aTable.slotForIndex(aTable.capacity())) | |||
| 1425 | #ifdef DEBUG1 | |||
| 1426 | , | |||
| 1427 | mTable(aTable), | |||
| 1428 | mMutationCount(aTable.mMutationCount), | |||
| 1429 | mGeneration(aTable.generation()), | |||
| 1430 | mValidEntry(true) | |||
| 1431 | #endif | |||
| 1432 | { | |||
| 1433 | if (!done() && !mCur.isLive()) { | |||
| 1434 | moveToNextLiveEntry(); | |||
| 1435 | } | |||
| 1436 | } | |||
| 1437 | ||||
| 1438 | Slot mCur; | |||
| 1439 | Slot mEnd; | |||
| 1440 | #ifdef DEBUG1 | |||
| 1441 | const HashTable& mTable; | |||
| 1442 | uint64_t mMutationCount; | |||
| 1443 | Generation mGeneration; | |||
| 1444 | bool mValidEntry; | |||
| 1445 | #endif | |||
| 1446 | ||||
| 1447 | public: | |||
| 1448 | bool done() const { | |||
| 1449 | 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" , 1449); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable.generation()" ")"); do { MOZ_CrashSequence(__null, 1449); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1450 | 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" , 1450); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mMutationCount == mTable.mMutationCount" ")"); do { MOZ_CrashSequence(__null, 1450); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1451 | return mCur == mEnd; | |||
| 1452 | } | |||
| 1453 | ||||
| 1454 | T& get() const { | |||
| 1455 | 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" , 1455); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!done()" ")" ); do { MOZ_CrashSequence(__null, 1455); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1456 | 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" , 1456); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mValidEntry" ")"); do { MOZ_CrashSequence(__null, 1456); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1457 | 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" , 1457); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable.generation()" ")"); do { MOZ_CrashSequence(__null, 1457); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1458 | 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" , 1458); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mMutationCount == mTable.mMutationCount" ")"); do { MOZ_CrashSequence(__null, 1458); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1459 | return mCur.get(); | |||
| 1460 | } | |||
| 1461 | ||||
| 1462 | void next() { | |||
| 1463 | 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" , 1463); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!done()" ")" ); do { MOZ_CrashSequence(__null, 1463); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1464 | 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" , 1464); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable.generation()" ")"); do { MOZ_CrashSequence(__null, 1464); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1465 | 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" , 1465); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mMutationCount == mTable.mMutationCount" ")"); do { MOZ_CrashSequence(__null, 1465); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1466 | moveToNextLiveEntry(); | |||
| 1467 | #ifdef DEBUG1 | |||
| 1468 | mValidEntry = true; | |||
| 1469 | #endif | |||
| 1470 | } | |||
| 1471 | }; | |||
| 1472 | ||||
| 1473 | // A hash table iterator that permits modification, removal and rekeying. | |||
| 1474 | // Since rehashing when elements were removed during iteration would be | |||
| 1475 | // bad, it is postponed until the ModIterator is destructed. Since the | |||
| 1476 | // ModIterator's destructor touches the hash table, the user must ensure | |||
| 1477 | // that the hash table is still alive when the destructor runs. | |||
| 1478 | class ModIterator : public Iterator { | |||
| 1479 | friend class HashTable; | |||
| 1480 | ||||
| 1481 | HashTable& mTable; | |||
| 1482 | bool mRekeyed; | |||
| 1483 | bool mRemoved; | |||
| 1484 | ||||
| 1485 | // ModIterator is movable but not copyable. | |||
| 1486 | ModIterator(const ModIterator&) = delete; | |||
| 1487 | void operator=(const ModIterator&) = delete; | |||
| 1488 | ||||
| 1489 | protected: | |||
| 1490 | explicit ModIterator(HashTable& aTable) | |||
| 1491 | : Iterator(aTable), mTable(aTable), mRekeyed(false), mRemoved(false) {} | |||
| 1492 | ||||
| 1493 | public: | |||
| 1494 | MOZ_IMPLICIT ModIterator(ModIterator&& aOther) | |||
| 1495 | : Iterator(aOther), | |||
| 1496 | mTable(aOther.mTable), | |||
| 1497 | mRekeyed(aOther.mRekeyed), | |||
| 1498 | mRemoved(aOther.mRemoved) { | |||
| 1499 | aOther.mRekeyed = false; | |||
| 1500 | aOther.mRemoved = false; | |||
| 1501 | } | |||
| 1502 | ||||
| 1503 | // Removes the current element from the table, leaving |get()| invalid until | |||
| 1504 | // the next call to |next()|. | |||
| 1505 | // | |||
| 1506 | // See the comments on ~ModIterator about table resizing after removing | |||
| 1507 | // entries. | |||
| 1508 | void remove() { | |||
| 1509 | mTable.remove(this->mCur); | |||
| 1510 | mRemoved = true; | |||
| 1511 | #ifdef DEBUG1 | |||
| 1512 | this->mValidEntry = false; | |||
| 1513 | this->mMutationCount = mTable.mMutationCount; | |||
| 1514 | #endif | |||
| 1515 | } | |||
| 1516 | ||||
| 1517 | NonConstT& getMutable() { | |||
| 1518 | 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" , 1518); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!this->done()" ")"); do { MOZ_CrashSequence(__null, 1518); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1519 | 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" , 1519); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this->mValidEntry" ")"); do { MOZ_CrashSequence(__null, 1519); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1520 | 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" , 1520); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this->mGeneration == this->Iterator::mTable.generation()" ")"); do { MOZ_CrashSequence(__null, 1520); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1521 | 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" , 1521); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this->mMutationCount == this->Iterator::mTable.mMutationCount" ")"); do { MOZ_CrashSequence(__null, 1521); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1522 | return this->mCur.getMutable(); | |||
| 1523 | } | |||
| 1524 | ||||
| 1525 | // Removes the current element and re-inserts it into the table with | |||
| 1526 | // a new key at the new Lookup position. |get()| is invalid after | |||
| 1527 | // this operation until the next call to |next()|. | |||
| 1528 | template <typename KeyInput> | |||
| 1529 | void rekey(const Lookup& l, KeyInput&& k) { | |||
| 1530 | 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" , 1533); 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, 1533); __attribute__((nomerge)) :: abort(); } while (false); } } while (false) | |||
| 1531 | 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" , 1533); 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, 1533); __attribute__((nomerge)) :: abort(); } while (false); } } while (false) | |||
| 1532 | 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" , 1533); 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, 1533); __attribute__((nomerge)) :: abort(); } while (false); } } while (false) | |||
| 1533 | "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" , 1533); 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, 1533); __attribute__((nomerge)) :: abort(); } while (false); } } while (false); | |||
| 1534 | Ptr p(this->mCur, mTable); | |||
| 1535 | mTable.rekeyWithoutRehash(p, l, std::forward<KeyInput>(k)); | |||
| 1536 | mRekeyed = true; | |||
| 1537 | #ifdef DEBUG1 | |||
| 1538 | this->mValidEntry = false; | |||
| 1539 | this->mMutationCount = mTable.mMutationCount; | |||
| 1540 | #endif | |||
| 1541 | } | |||
| 1542 | ||||
| 1543 | void rekey(const Lookup& l) { rekey(l, l); } | |||
| 1544 | ||||
| 1545 | // This can rehash the table or resize it if entries were removed. | |||
| 1546 | // | |||
| 1547 | // This does not go as far as freeing the table if it is now empty, as that | |||
| 1548 | // can lead to repeatedly allocating and freeing the table when a small | |||
| 1549 | // number of entries are repeatedly added and removed. If callers require | |||
| 1550 | // memory to be minimised after removing entries they should call compact(). | |||
| 1551 | ~ModIterator() { | |||
| 1552 | if (mRekeyed) { | |||
| 1553 | mTable.incrementGeneration(); | |||
| 1554 | mTable.infallibleRehashIfOverloaded(); | |||
| 1555 | } | |||
| 1556 | ||||
| 1557 | if (mRemoved) { | |||
| 1558 | mTable.shrinkToBestCapacity(); | |||
| 1559 | } | |||
| 1560 | } | |||
| 1561 | }; | |||
| 1562 | ||||
| 1563 | // HashTable is movable | |||
| 1564 | HashTable(HashTable&& aRhs) : AllocPolicy(std::move(aRhs)) { moveFrom(aRhs); } | |||
| 1565 | HashTable& operator=(HashTable&& aRhs) { | |||
| 1566 | 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" , 1566); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this != &aRhs" ") (" "self-move assignment is prohibited" ")"); do { MOZ_CrashSequence (__null, 1566); __attribute__((nomerge)) ::abort(); } while ( false); } } while (false); | |||
| 1567 | if (mTable) { | |||
| 1568 | destroyTable(*this, mTable, capacity()); | |||
| 1569 | } | |||
| 1570 | AllocPolicy::operator=(std::move(aRhs)); | |||
| 1571 | moveFrom(aRhs); | |||
| 1572 | return *this; | |||
| 1573 | } | |||
| 1574 | ||||
| 1575 | void swap(HashTable& aOther) { | |||
| 1576 | ReentrancyGuard g1(*this); | |||
| 1577 | ReentrancyGuard g2(aOther); | |||
| 1578 | ||||
| 1579 | std::swap(mGenAndHashShift, aOther.mGenAndHashShift); | |||
| 1580 | std::swap(mTable, aOther.mTable); | |||
| 1581 | std::swap(mEntryCount, aOther.mEntryCount); | |||
| 1582 | std::swap(mRemovedCount, aOther.mRemovedCount); | |||
| 1583 | #ifdef DEBUG1 | |||
| 1584 | std::swap(mMutationCount, aOther.mMutationCount); | |||
| 1585 | std::swap(mEntered, aOther.mEntered); | |||
| 1586 | #endif | |||
| 1587 | } | |||
| 1588 | ||||
| 1589 | AllocPolicy& allocPolicy() { return *this; } | |||
| 1590 | const AllocPolicy& allocPolicy() const { return *this; } | |||
| 1591 | ||||
| 1592 | template <typename F> | |||
| 1593 | void traceOwnedAllocs(F&& aTraceFunc) { | |||
| 1594 | if (mTable) { | |||
| 1595 | aTraceFunc(&mTable); | |||
| 1596 | } | |||
| 1597 | } | |||
| 1598 | ||||
| 1599 | private: | |||
| 1600 | void moveFrom(HashTable& aRhs) { | |||
| 1601 | mGenAndHashShift = aRhs.mGenAndHashShift; | |||
| 1602 | mTable = aRhs.mTable; | |||
| 1603 | mEntryCount = aRhs.mEntryCount; | |||
| 1604 | mRemovedCount = aRhs.mRemovedCount; | |||
| 1605 | #ifdef DEBUG1 | |||
| 1606 | mMutationCount = aRhs.mMutationCount; | |||
| 1607 | mEntered = aRhs.mEntered; | |||
| 1608 | #endif | |||
| 1609 | aRhs.mTable = nullptr; | |||
| 1610 | aRhs.clearAndCompact(); | |||
| 1611 | } | |||
| 1612 | ||||
| 1613 | // HashTable is not copyable or assignable | |||
| 1614 | HashTable(const HashTable&) = delete; | |||
| 1615 | void operator=(const HashTable&) = delete; | |||
| 1616 | ||||
| 1617 | static const uint32_t CAP_BITS = 30; | |||
| 1618 | ||||
| 1619 | public: | |||
| 1620 | uint64_t mGenAndHashShift; // entry storage generation number (56 bits) | |||
| 1621 | // and multiplicative hash shift (8 bits) | |||
| 1622 | char* mTable; // entry storage | |||
| 1623 | uint32_t mEntryCount; // number of entries in mTable | |||
| 1624 | uint32_t mRemovedCount; // removed entry sentinels in mTable | |||
| 1625 | ||||
| 1626 | #ifdef DEBUG1 | |||
| 1627 | uint64_t mMutationCount; | |||
| 1628 | mutable bool mEntered; | |||
| 1629 | #endif | |||
| 1630 | ||||
| 1631 | // The default initial capacity is 32 (enough to hold 16 elements), but it | |||
| 1632 | // can be as low as 4. | |||
| 1633 | static const uint32_t sDefaultLen = 16; | |||
| 1634 | static const uint32_t sMinCapacity = 4; | |||
| 1635 | // See the comments in HashTableEntry about this value. | |||
| 1636 | static_assert(sMinCapacity >= 4, "too-small sMinCapacity breaks assumptions"); | |||
| 1637 | static const uint32_t sMaxInit = 1u << (CAP_BITS - 1); | |||
| 1638 | static const uint32_t sMaxCapacity = 1u << CAP_BITS; | |||
| 1639 | ||||
| 1640 | // Hash-table alpha is conceptually a fraction, but to avoid floating-point | |||
| 1641 | // math we implement it as a ratio of integers. | |||
| 1642 | static const uint8_t sAlphaDenominator = 4; | |||
| 1643 | static const uint8_t sMinAlphaNumerator = 1; // min alpha: 1/4 | |||
| 1644 | static const uint8_t sMaxAlphaNumerator = 3; // max alpha: 3/4 | |||
| 1645 | ||||
| 1646 | static const HashNumber sFreeKey = Entry::sFreeKey; | |||
| 1647 | static const HashNumber sRemovedKey = Entry::sRemovedKey; | |||
| 1648 | static const HashNumber sCollisionBit = Entry::sCollisionBit; | |||
| 1649 | ||||
| 1650 | static const uint64_t sHashShiftBits = 8; | |||
| 1651 | static const uint64_t sHashShiftMask = (1 << sHashShiftBits) - 1; | |||
| 1652 | static const uint64_t sGenerationShift = sHashShiftBits; | |||
| 1653 | ||||
| 1654 | MOZ_ALWAYS_INLINEinline uint8_t hashShift() const { | |||
| 1655 | return uint8_t(mGenAndHashShift & sHashShiftMask); | |||
| 1656 | } | |||
| 1657 | MOZ_ALWAYS_INLINEinline uint64_t gen() const { | |||
| 1658 | return mGenAndHashShift >> sGenerationShift; | |||
| 1659 | } | |||
| 1660 | ||||
| 1661 | private: | |||
| 1662 | void setGenAndHashShift(uint64_t aGeneration, uint8_t aHashShift) { | |||
| 1663 | mGenAndHashShift = aGeneration << sGenerationShift | aHashShift; | |||
| 1664 | } | |||
| 1665 | ||||
| 1666 | public: | |||
| 1667 | void incrementGeneration() { setGenAndHashShift(gen() + 1, hashShift()); } | |||
| 1668 | void setHashShift(uint32_t aHashShift) { | |||
| 1669 | 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" , 1669); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(aHashShift & sHashShiftMask) == aHashShift" ")"); do { MOZ_CrashSequence(__null, 1669); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1670 | mGenAndHashShift = (mGenAndHashShift & ~sHashShiftMask) | aHashShift; | |||
| 1671 | } | |||
| 1672 | ||||
| 1673 | constexpr static uint32_t bestCapacity(uint32_t aLen) { | |||
| 1674 | static_assert( | |||
| 1675 | (sMaxInit * sAlphaDenominator) / sAlphaDenominator == sMaxInit, | |||
| 1676 | "multiplication in numerator below could overflow"); | |||
| 1677 | static_assert( | |||
| 1678 | sMaxInit * sAlphaDenominator <= UINT32_MAX(4294967295U) - sMaxAlphaNumerator, | |||
| 1679 | "numerator calculation below could potentially overflow"); | |||
| 1680 | ||||
| 1681 | // Callers should ensure this is true. | |||
| 1682 | 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" , 1682); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aLen <= sMaxInit" ")"); do { MOZ_CrashSequence(__null, 1682); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1683 | ||||
| 1684 | // Compute the smallest capacity allowing |aLen| elements to be | |||
| 1685 | // inserted without rehashing: ceil(aLen / max-alpha). (Ceiling | |||
| 1686 | // integral division: <http://stackoverflow.com/a/2745086>.) | |||
| 1687 | uint32_t capacity = (aLen * sAlphaDenominator + sMaxAlphaNumerator - 1) / | |||
| 1688 | sMaxAlphaNumerator; | |||
| 1689 | capacity = (capacity < sMinCapacity) ? sMinCapacity : RoundUpPow2(capacity); | |||
| 1690 | ||||
| 1691 | 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" , 1691); AnnotateMozCrashReason("MOZ_ASSERT" "(" "capacity >= aLen" ")"); do { MOZ_CrashSequence(__null, 1691); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1692 | 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" , 1692); AnnotateMozCrashReason("MOZ_ASSERT" "(" "capacity <= sMaxCapacity" ")"); do { MOZ_CrashSequence(__null, 1692); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1693 | ||||
| 1694 | return capacity; | |||
| 1695 | } | |||
| 1696 | ||||
| 1697 | constexpr static uint32_t hashShiftForLength(uint32_t aLen) { | |||
| 1698 | // Reject all lengths whose initial computed capacity would exceed | |||
| 1699 | // sMaxCapacity. Round that maximum aLen down to the nearest power of two | |||
| 1700 | // for speedier code. | |||
| 1701 | if (MOZ_UNLIKELY(aLen > sMaxInit)(__builtin_expect(!!(aLen > sMaxInit), 0))) { | |||
| 1702 | 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" , 1702); AnnotateMozCrashReason("MOZ_CRASH(" "initial length is too large" ")"); do { MOZ_CrashSequence(__null, 1702); __attribute__((nomerge )) ::abort(); } while (false); } while (false); | |||
| 1703 | } | |||
| 1704 | ||||
| 1705 | return kHashNumberBits - mozilla::CeilingLog2(bestCapacity(aLen)); | |||
| 1706 | } | |||
| 1707 | ||||
| 1708 | static bool isLiveHash(HashNumber aHash) { return Entry::isLiveHash(aHash); } | |||
| 1709 | ||||
| 1710 | static HashNumber prepareHash(HashNumber aInputHash) { | |||
| 1711 | HashNumber keyHash = ScrambleHashCode(aInputHash); | |||
| 1712 | ||||
| 1713 | // Avoid reserved hash codes. | |||
| 1714 | if (!isLiveHash(keyHash)) { | |||
| 1715 | keyHash -= (sRemovedKey + 1); | |||
| 1716 | } | |||
| 1717 | return keyHash & ~sCollisionBit; | |||
| 1718 | } | |||
| 1719 | ||||
| 1720 | enum FailureBehavior { DontReportFailure = false, ReportFailure = true }; | |||
| 1721 | ||||
| 1722 | // Fake a struct that we're going to alloc. See the comments in | |||
| 1723 | // HashTableEntry about how the table is laid out, and why it's safe. | |||
| 1724 | struct FakeSlot { | |||
| 1725 | unsigned char c[sizeof(HashNumber) + sizeof(typename Entry::NonConstT)]; | |||
| 1726 | }; | |||
| 1727 | ||||
| 1728 | static char* createTable(AllocPolicy& aAllocPolicy, uint32_t aCapacity, | |||
| 1729 | FailureBehavior aReportFailure = ReportFailure) { | |||
| 1730 | FakeSlot* fake = | |||
| 1731 | aReportFailure | |||
| 1732 | ? aAllocPolicy.template pod_malloc<FakeSlot>(aCapacity) | |||
| 1733 | : aAllocPolicy.template maybe_pod_malloc<FakeSlot>(aCapacity); | |||
| 1734 | ||||
| 1735 | 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" , 1736); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) == 0" ")"); do { MOZ_CrashSequence(__null, 1736); __attribute__((nomerge )) ::abort(); } while (false); } } while (false) | |||
| 1736 | 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" , 1736); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) == 0" ")"); do { MOZ_CrashSequence(__null, 1736); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1737 | ||||
| 1738 | char* table = reinterpret_cast<char*>(fake); | |||
| 1739 | if (table) { | |||
| 1740 | forEachSlot(table, aCapacity, [&](Slot& slot) { | |||
| 1741 | *slot.mKeyHash = sFreeKey; | |||
| 1742 | new (KnownNotNull, slot.toEntry()) Entry(); | |||
| 1743 | }); | |||
| 1744 | } | |||
| 1745 | return table; | |||
| 1746 | } | |||
| 1747 | ||||
| 1748 | static void destroyTable(AllocPolicy& aAllocPolicy, char* aOldTable, | |||
| 1749 | uint32_t aCapacity) { | |||
| 1750 | forEachSlot(aOldTable, aCapacity, [&](const Slot& slot) { | |||
| 1751 | if (slot.isLive()) { | |||
| 1752 | slot.toEntry()->destroyStoredT(); | |||
| 1753 | } | |||
| 1754 | }); | |||
| 1755 | freeTable(aAllocPolicy, aOldTable, aCapacity); | |||
| 1756 | } | |||
| 1757 | ||||
| 1758 | static void freeTable(AllocPolicy& aAllocPolicy, char* aOldTable, | |||
| 1759 | uint32_t aCapacity) { | |||
| 1760 | FakeSlot* fake = reinterpret_cast<FakeSlot*>(aOldTable); | |||
| 1761 | aAllocPolicy.free_(fake, aCapacity); | |||
| 1762 | } | |||
| 1763 | ||||
| 1764 | public: | |||
| 1765 | constexpr HashTable(AllocPolicy aAllocPolicy, uint32_t aLen) | |||
| 1766 | : AllocPolicy(std::move(aAllocPolicy)), | |||
| 1767 | mGenAndHashShift(hashShiftForLength(aLen)), | |||
| 1768 | mTable(nullptr), | |||
| 1769 | mEntryCount(0), | |||
| 1770 | mRemovedCount(0) | |||
| 1771 | #ifdef DEBUG1 | |||
| 1772 | , | |||
| 1773 | mMutationCount(0), | |||
| 1774 | mEntered(false) | |||
| 1775 | #endif | |||
| 1776 | { | |||
| 1777 | } | |||
| 1778 | ||||
| 1779 | explicit HashTable(AllocPolicy aAllocPolicy) | |||
| 1780 | : HashTable(aAllocPolicy, sDefaultLen) {} | |||
| 1781 | ||||
| 1782 | ~HashTable() { | |||
| 1783 | if (mTable) { | |||
| 1784 | destroyTable(*this, mTable, capacity()); | |||
| 1785 | } | |||
| 1786 | } | |||
| 1787 | ||||
| 1788 | private: | |||
| 1789 | HashNumber hash1(HashNumber aHash0) const { return aHash0 >> hashShift(); } | |||
| 1790 | ||||
| 1791 | struct DoubleHash { | |||
| 1792 | HashNumber mHash2; | |||
| 1793 | HashNumber mSizeMask; | |||
| 1794 | }; | |||
| 1795 | ||||
| 1796 | DoubleHash hash2(HashNumber aCurKeyHash) const { | |||
| 1797 | uint32_t sizeLog2 = kHashNumberBits - hashShift(); | |||
| 1798 | DoubleHash dh = {((aCurKeyHash << sizeLog2) >> hashShift()) | 1, | |||
| 1799 | (HashNumber(1) << sizeLog2) - 1}; | |||
| 1800 | return dh; | |||
| 1801 | } | |||
| 1802 | ||||
| 1803 | static HashNumber applyDoubleHash(HashNumber aHash1, | |||
| 1804 | const DoubleHash& aDoubleHash) { | |||
| 1805 | return WrappingSubtract(aHash1, aDoubleHash.mHash2) & aDoubleHash.mSizeMask; | |||
| 1806 | } | |||
| 1807 | ||||
| 1808 | static MOZ_ALWAYS_INLINEinline bool match(T& aEntry, const Lookup& aLookup) { | |||
| 1809 | return HashPolicy::match(HashPolicy::getKey(aEntry), aLookup); | |||
| 1810 | } | |||
| 1811 | ||||
| 1812 | enum LookupReason { ForNonAdd, ForAdd }; | |||
| 1813 | ||||
| 1814 | Slot slotForIndex(HashNumber aIndex) const { | |||
| 1815 | auto hashes = reinterpret_cast<HashNumber*>(mTable); | |||
| 1816 | auto entries = reinterpret_cast<Entry*>(&hashes[capacity()]); | |||
| 1817 | return Slot(&entries[aIndex], &hashes[aIndex]); | |||
| 1818 | } | |||
| 1819 | ||||
| 1820 | // Warning: in order for readonlyThreadsafeLookup() to be safe this | |||
| 1821 | // function must not modify the table in any way when Reason==ForNonAdd. | |||
| 1822 | template <LookupReason Reason> | |||
| 1823 | MOZ_ALWAYS_INLINEinline Slot lookup(const Lookup& aLookup, | |||
| 1824 | HashNumber aKeyHash) const { | |||
| 1825 | 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" , 1825); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLiveHash(aKeyHash)" ")"); do { MOZ_CrashSequence(__null, 1825); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1826 | 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" , 1826); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!(aKeyHash & sCollisionBit)" ")"); do { MOZ_CrashSequence(__null, 1826); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1827 | 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" , 1827); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { MOZ_CrashSequence(__null, 1827); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1828 | ||||
| 1829 | // Compute the primary hash address. | |||
| 1830 | HashNumber h1 = hash1(aKeyHash); | |||
| 1831 | Slot slot = slotForIndex(h1); | |||
| 1832 | ||||
| 1833 | // Miss: return space for a new entry. | |||
| 1834 | if (slot.isFree()) { | |||
| 1835 | return slot; | |||
| 1836 | } | |||
| 1837 | ||||
| 1838 | // Hit: return entry. | |||
| 1839 | if (slot.matchHash(aKeyHash) && match(slot.get(), aLookup)) { | |||
| 1840 | return slot; | |||
| 1841 | } | |||
| 1842 | ||||
| 1843 | // Collision: double hash. | |||
| 1844 | DoubleHash dh = hash2(aKeyHash); | |||
| 1845 | ||||
| 1846 | // Save the first removed entry pointer so we can recycle later. | |||
| 1847 | Maybe<Slot> firstRemoved; | |||
| 1848 | ||||
| 1849 | while (true) { | |||
| 1850 | if (Reason == ForAdd && !firstRemoved) { | |||
| 1851 | if (MOZ_UNLIKELY(slot.isRemoved())(__builtin_expect(!!(slot.isRemoved()), 0))) { | |||
| 1852 | firstRemoved.emplace(slot); | |||
| 1853 | } else { | |||
| 1854 | slot.setCollision(); | |||
| 1855 | } | |||
| 1856 | } | |||
| 1857 | ||||
| 1858 | h1 = applyDoubleHash(h1, dh); | |||
| 1859 | ||||
| 1860 | slot = slotForIndex(h1); | |||
| 1861 | if (slot.isFree()) { | |||
| 1862 | return firstRemoved.refOr(slot); | |||
| 1863 | } | |||
| 1864 | ||||
| 1865 | if (slot.matchHash(aKeyHash) && match(slot.get(), aLookup)) { | |||
| 1866 | return slot; | |||
| 1867 | } | |||
| 1868 | } | |||
| 1869 | } | |||
| 1870 | ||||
| 1871 | // This is a copy of lookup() hardcoded to the assumptions: | |||
| 1872 | // 1. the lookup is for an add; | |||
| 1873 | // 2. the key, whose |keyHash| has been passed, is not in the table. | |||
| 1874 | Slot findNonLiveSlot(HashNumber aKeyHash) { | |||
| 1875 | 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" , 1875); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!(aKeyHash & sCollisionBit)" ")"); do { MOZ_CrashSequence(__null, 1875); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1876 | 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" , 1876); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { MOZ_CrashSequence(__null, 1876); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1877 | ||||
| 1878 | // We assume 'aKeyHash' has already been distributed. | |||
| 1879 | ||||
| 1880 | // Compute the primary hash address. | |||
| 1881 | HashNumber h1 = hash1(aKeyHash); | |||
| 1882 | Slot slot = slotForIndex(h1); | |||
| 1883 | ||||
| 1884 | // Miss: return space for a new entry. | |||
| 1885 | if (!slot.isLive()) { | |||
| 1886 | return slot; | |||
| 1887 | } | |||
| 1888 | ||||
| 1889 | // Collision: double hash. | |||
| 1890 | DoubleHash dh = hash2(aKeyHash); | |||
| 1891 | ||||
| 1892 | while (true) { | |||
| 1893 | slot.setCollision(); | |||
| 1894 | ||||
| 1895 | h1 = applyDoubleHash(h1, dh); | |||
| 1896 | ||||
| 1897 | slot = slotForIndex(h1); | |||
| 1898 | if (!slot.isLive()) { | |||
| 1899 | return slot; | |||
| 1900 | } | |||
| 1901 | } | |||
| 1902 | } | |||
| 1903 | ||||
| 1904 | enum RebuildStatus { NotOverloaded, Rehashed, RehashFailed }; | |||
| 1905 | ||||
| 1906 | RebuildStatus changeTableSize( | |||
| 1907 | uint32_t newCapacity, FailureBehavior aReportFailure = ReportFailure) { | |||
| 1908 | 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" , 1908); AnnotateMozCrashReason("MOZ_ASSERT" "(" "std::has_single_bit(newCapacity)" ")"); do { MOZ_CrashSequence(__null, 1908); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1909 | 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" , 1909); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!!mTable == !!capacity()" ")"); do { MOZ_CrashSequence(__null, 1909); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1910 | ||||
| 1911 | // Look, but don't touch, until we succeed in getting new entry store. | |||
| 1912 | char* oldTable = mTable; | |||
| 1913 | uint32_t oldCapacity = capacity(); | |||
| 1914 | uint32_t newLog2 = mozilla::CeilingLog2(newCapacity); | |||
| 1915 | ||||
| 1916 | if (MOZ_UNLIKELY(newCapacity > sMaxCapacity)(__builtin_expect(!!(newCapacity > sMaxCapacity), 0))) { | |||
| 1917 | if (aReportFailure) { | |||
| 1918 | this->reportAllocOverflow(); | |||
| 1919 | } | |||
| 1920 | return RehashFailed; | |||
| 1921 | } | |||
| 1922 | ||||
| 1923 | char* newTable = createTable(*this, newCapacity, aReportFailure); | |||
| 1924 | if (!newTable) { | |||
| 1925 | return RehashFailed; | |||
| 1926 | } | |||
| 1927 | ||||
| 1928 | // We can't fail from here on, so update table parameters. | |||
| 1929 | mRemovedCount = 0; | |||
| 1930 | incrementGeneration(); | |||
| 1931 | setHashShift(kHashNumberBits - newLog2); | |||
| 1932 | mTable = newTable; | |||
| 1933 | ||||
| 1934 | // Copy only live entries, leaving removed ones behind. | |||
| 1935 | forEachSlot(oldTable, oldCapacity, [&](Slot& slot) { | |||
| 1936 | if (slot.isLive()) { | |||
| 1937 | HashNumber hn = slot.getKeyHash(); | |||
| 1938 | findNonLiveSlot(hn).setLive( | |||
| 1939 | hn, std::move(const_cast<typename Entry::NonConstT&>(slot.get()))); | |||
| 1940 | } | |||
| 1941 | ||||
| 1942 | slot.clear(); | |||
| 1943 | }); | |||
| 1944 | ||||
| 1945 | // All entries have been destroyed, no need to destroyTable. | |||
| 1946 | freeTable(*this, oldTable, oldCapacity); | |||
| 1947 | return Rehashed; | |||
| 1948 | } | |||
| 1949 | ||||
| 1950 | RebuildStatus rehashIfOverloaded( | |||
| 1951 | FailureBehavior aReportFailure = ReportFailure) { | |||
| 1952 | static_assert(sMaxCapacity <= UINT32_MAX(4294967295U) / sMaxAlphaNumerator, | |||
| 1953 | "multiplication below could overflow"); | |||
| 1954 | ||||
| 1955 | // Note: if capacity() is zero, this will always succeed, which is | |||
| 1956 | // what we want. | |||
| 1957 | bool overloaded = mEntryCount + mRemovedCount >= | |||
| 1958 | capacity() * sMaxAlphaNumerator / sAlphaDenominator; | |||
| 1959 | ||||
| 1960 | if (!overloaded) { | |||
| 1961 | return NotOverloaded; | |||
| 1962 | } | |||
| 1963 | ||||
| 1964 | // Succeed if a quarter or more of all entries are removed. Note that this | |||
| 1965 | // always succeeds if capacity() == 0 (i.e. entry storage has not been | |||
| 1966 | // allocated), which is what we want, because it means changeTableSize() | |||
| 1967 | // will allocate the requested capacity rather than doubling it. | |||
| 1968 | bool manyRemoved = mRemovedCount >= (capacity() >> 2); | |||
| 1969 | uint32_t newCapacity = manyRemoved ? rawCapacity() : rawCapacity() * 2; | |||
| 1970 | return changeTableSize(newCapacity, aReportFailure); | |||
| 1971 | } | |||
| 1972 | ||||
| 1973 | void infallibleRehashIfOverloaded() { | |||
| 1974 | if (rehashIfOverloaded(DontReportFailure) == RehashFailed) { | |||
| 1975 | rehashTableInPlace(); | |||
| 1976 | } | |||
| 1977 | } | |||
| 1978 | ||||
| 1979 | void remove(Slot& aSlot) { | |||
| 1980 | 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" , 1980); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { MOZ_CrashSequence(__null, 1980); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 1981 | ||||
| 1982 | if (aSlot.hasCollision()) { | |||
| 1983 | aSlot.removeLive(); | |||
| 1984 | mRemovedCount++; | |||
| 1985 | } else { | |||
| 1986 | aSlot.clearLive(); | |||
| 1987 | } | |||
| 1988 | mEntryCount--; | |||
| 1989 | #ifdef DEBUG1 | |||
| 1990 | mMutationCount++; | |||
| 1991 | #endif | |||
| 1992 | } | |||
| 1993 | ||||
| 1994 | void shrinkIfUnderloaded() { | |||
| 1995 | static_assert(sMaxCapacity <= UINT32_MAX(4294967295U) / sMinAlphaNumerator, | |||
| 1996 | "multiplication below could overflow"); | |||
| 1997 | bool underloaded = | |||
| 1998 | capacity() > sMinCapacity && | |||
| 1999 | mEntryCount <= capacity() * sMinAlphaNumerator / sAlphaDenominator; | |||
| 2000 | ||||
| 2001 | if (underloaded) { | |||
| 2002 | (void)changeTableSize(capacity() / 2, DontReportFailure); | |||
| 2003 | } | |||
| 2004 | } | |||
| 2005 | ||||
| 2006 | // This is identical to changeTableSize(currentSize), but without requiring | |||
| 2007 | // a second table. We do this by recycling the collision bits to tell us if | |||
| 2008 | // the element is already inserted or still waiting to be inserted. Since | |||
| 2009 | // already-inserted elements win any conflicts, we get the same table as we | |||
| 2010 | // would have gotten through random insertion order. | |||
| 2011 | void rehashTableInPlace() { | |||
| 2012 | mRemovedCount = 0; | |||
| 2013 | incrementGeneration(); | |||
| 2014 | forEachSlot(mTable, capacity(), [&](Slot& slot) { slot.unsetCollision(); }); | |||
| 2015 | for (uint32_t i = 0; i < capacity();) { | |||
| 2016 | Slot src = slotForIndex(i); | |||
| 2017 | ||||
| 2018 | if (!src.isLive() || src.hasCollision()) { | |||
| 2019 | ++i; | |||
| 2020 | continue; | |||
| 2021 | } | |||
| 2022 | ||||
| 2023 | HashNumber keyHash = src.getKeyHash(); | |||
| 2024 | HashNumber h1 = hash1(keyHash); | |||
| 2025 | DoubleHash dh = hash2(keyHash); | |||
| 2026 | Slot tgt = slotForIndex(h1); | |||
| 2027 | while (true) { | |||
| 2028 | if (!tgt.hasCollision()) { | |||
| 2029 | src.swap(tgt); | |||
| 2030 | tgt.setCollision(); | |||
| 2031 | break; | |||
| 2032 | } | |||
| 2033 | ||||
| 2034 | h1 = applyDoubleHash(h1, dh); | |||
| 2035 | tgt = slotForIndex(h1); | |||
| 2036 | } | |||
| 2037 | } | |||
| 2038 | ||||
| 2039 | // TODO: this algorithm leaves collision bits on *all* elements, even if | |||
| 2040 | // they are on no collision path. We have the option of setting the | |||
| 2041 | // collision bits correctly on a subsequent pass or skipping the rehash | |||
| 2042 | // unless we are totally filled with tombstones: benchmark to find out | |||
| 2043 | // which approach is best. | |||
| 2044 | } | |||
| 2045 | ||||
| 2046 | // Prefer to use putNewInfallible; this function does not check | |||
| 2047 | // invariants. | |||
| 2048 | template <typename... Args> | |||
| 2049 | void putNewInfallibleInternal(HashNumber aKeyHash, Args&&... aArgs) { | |||
| 2050 | 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" , 2050); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { MOZ_CrashSequence(__null, 2050); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2051 | ||||
| 2052 | Slot slot = findNonLiveSlot(aKeyHash); | |||
| 2053 | ||||
| 2054 | if (slot.isRemoved()) { | |||
| 2055 | mRemovedCount--; | |||
| 2056 | aKeyHash |= sCollisionBit; | |||
| 2057 | } | |||
| 2058 | ||||
| 2059 | slot.setLive(aKeyHash, std::forward<Args>(aArgs)...); | |||
| 2060 | mEntryCount++; | |||
| 2061 | #ifdef DEBUG1 | |||
| 2062 | mMutationCount++; | |||
| 2063 | #endif | |||
| 2064 | } | |||
| 2065 | ||||
| 2066 | public: | |||
| 2067 | void clear() { | |||
| 2068 | forEachSlot(mTable, capacity(), [&](Slot& slot) { slot.clear(); }); | |||
| 2069 | mRemovedCount = 0; | |||
| 2070 | mEntryCount = 0; | |||
| 2071 | #ifdef DEBUG1 | |||
| 2072 | mMutationCount++; | |||
| 2073 | #endif | |||
| 2074 | } | |||
| 2075 | ||||
| 2076 | // Minimise the memory used. If there are no entries the table is freed, | |||
| 2077 | // otherwise the table is resized to the smallest capacity that doesn't | |||
| 2078 | // overload the table and that is at least sMinCapacity entries. | |||
| 2079 | // | |||
| 2080 | // Since we shrink the table after every remove, you only need to call this if | |||
| 2081 | // you want to free the table when it's empty. | |||
| 2082 | void compact() { | |||
| 2083 | if (empty()) { | |||
| 2084 | // Free the entry storage. | |||
| 2085 | freeTable(*this, mTable, capacity()); | |||
| 2086 | incrementGeneration(); | |||
| 2087 | setHashShift( | |||
| 2088 | hashShiftForLength(0)); // gives minimum capacity on regrowth | |||
| 2089 | mTable = nullptr; | |||
| 2090 | mRemovedCount = 0; | |||
| 2091 | return; | |||
| 2092 | } | |||
| 2093 | ||||
| 2094 | shrinkToBestCapacity(); | |||
| 2095 | } | |||
| 2096 | ||||
| 2097 | void shrinkToBestCapacity() { | |||
| 2098 | uint32_t bestCapacity = this->bestCapacity(mEntryCount); | |||
| 2099 | if (bestCapacity < capacity()) { | |||
| 2100 | (void)changeTableSize(bestCapacity, DontReportFailure); | |||
| 2101 | } | |||
| 2102 | } | |||
| 2103 | ||||
| 2104 | void clearAndCompact() { | |||
| 2105 | clear(); | |||
| 2106 | compact(); | |||
| 2107 | } | |||
| 2108 | ||||
| 2109 | [[nodiscard]] bool reserve(uint32_t aLen) { | |||
| 2110 | if (aLen == 0) { | |||
| 2111 | return true; | |||
| 2112 | } | |||
| 2113 | ||||
| 2114 | if (MOZ_UNLIKELY(aLen > sMaxInit)(__builtin_expect(!!(aLen > sMaxInit), 0))) { | |||
| 2115 | this->reportAllocOverflow(); | |||
| 2116 | return false; | |||
| 2117 | } | |||
| 2118 | ||||
| 2119 | uint32_t bestCapacity = this->bestCapacity(aLen); | |||
| 2120 | if (bestCapacity <= capacity()) { | |||
| 2121 | return true; // Capacity is already sufficient. | |||
| 2122 | } | |||
| 2123 | ||||
| 2124 | RebuildStatus status = changeTableSize(bestCapacity, ReportFailure); | |||
| 2125 | 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" , 2125); AnnotateMozCrashReason("MOZ_ASSERT" "(" "status != NotOverloaded" ")"); do { MOZ_CrashSequence(__null, 2125); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2126 | return status != RehashFailed; | |||
| 2127 | } | |||
| 2128 | ||||
| 2129 | Iterator iter() const { return Iterator(*this); } | |||
| 2130 | ||||
| 2131 | ModIterator modIter() { return ModIterator(*this); } | |||
| 2132 | ||||
| 2133 | bool empty() const { return mEntryCount == 0; } | |||
| 2134 | ||||
| 2135 | uint32_t count() const { return mEntryCount; } | |||
| 2136 | ||||
| 2137 | uint32_t rawCapacity() const { return 1u << (kHashNumberBits - hashShift()); } | |||
| 2138 | ||||
| 2139 | uint32_t capacity() const { return mTable ? rawCapacity() : 0; } | |||
| 2140 | ||||
| 2141 | Generation generation() const { return Generation(gen()); } | |||
| 2142 | ||||
| 2143 | size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { | |||
| 2144 | return aMallocSizeOf(mTable); | |||
| 2145 | } | |||
| 2146 | ||||
| 2147 | size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { | |||
| 2148 | return aMallocSizeOf(this) + shallowSizeOfExcludingThis(aMallocSizeOf); | |||
| 2149 | } | |||
| 2150 | ||||
| 2151 | MOZ_ALWAYS_INLINEinline Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const { | |||
| 2152 | if (empty()) { | |||
| 2153 | return Ptr(); | |||
| 2154 | } | |||
| 2155 | ||||
| 2156 | HashNumber inputHash; | |||
| 2157 | if (!MaybeGetHash<HashPolicy>(aLookup, &inputHash)) { | |||
| 2158 | return Ptr(); | |||
| 2159 | } | |||
| 2160 | ||||
| 2161 | HashNumber keyHash = prepareHash(inputHash); | |||
| 2162 | return Ptr(lookup<ForNonAdd>(aLookup, keyHash), *this); | |||
| 2163 | } | |||
| 2164 | ||||
| 2165 | MOZ_ALWAYS_INLINEinline Ptr lookup(const Lookup& aLookup) const { | |||
| 2166 | ReentrancyGuard g(*this); | |||
| 2167 | return readonlyThreadsafeLookup(aLookup); | |||
| 2168 | } | |||
| 2169 | ||||
| 2170 | MOZ_ALWAYS_INLINEinline AddPtr lookupForAdd(const Lookup& aLookup) { | |||
| 2171 | ReentrancyGuard g(*this); | |||
| 2172 | ||||
| 2173 | HashNumber inputHash; | |||
| 2174 | if (!EnsureHash<HashPolicy>(aLookup, &inputHash)) { | |||
| 2175 | return AddPtr(); | |||
| 2176 | } | |||
| 2177 | ||||
| 2178 | HashNumber keyHash = prepareHash(inputHash); | |||
| 2179 | ||||
| 2180 | if (!mTable) { | |||
| 2181 | return AddPtr(*this, keyHash); | |||
| 2182 | } | |||
| 2183 | ||||
| 2184 | // Directly call the constructor in the return statement to avoid | |||
| 2185 | // excess copying when building with Visual Studio 2017. | |||
| 2186 | // See bug 1385181. | |||
| 2187 | return AddPtr(lookup<ForAdd>(aLookup, keyHash), *this, keyHash); | |||
| 2188 | } | |||
| 2189 | ||||
| 2190 | template <typename... Args> | |||
| 2191 | [[nodiscard]] bool add(AddPtr& aPtr, Args&&... aArgs) { | |||
| 2192 | ReentrancyGuard g(*this); | |||
| 2193 | 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" , 2193); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { MOZ_CrashSequence(__null, 2193); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); } } while ( false); | |||
| 2194 | 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" , 2194); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mTable == this" ")"); do { MOZ_CrashSequence(__null, 2194); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); } } while ( false); | |||
| 2195 | 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" , 2195); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!aPtr.found()" ")"); do { MOZ_CrashSequence(__null, 2195); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2196 | 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" , 2196); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!(aPtr.mKeyHash & sCollisionBit)" ")"); do { MOZ_CrashSequence(__null, 2196); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2197 | ||||
| 2198 | // Check for error from ensureHash() here. | |||
| 2199 | if (!aPtr.isLive()) { | |||
| 2200 | return false; | |||
| 2201 | } | |||
| 2202 | ||||
| 2203 | 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" , 2203); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mGeneration == generation()" ")"); do { MOZ_CrashSequence(__null, 2203); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2204 | #ifdef DEBUG1 | |||
| 2205 | 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" , 2205); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mMutationCount == mMutationCount" ")"); do { MOZ_CrashSequence(__null, 2205); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2206 | #endif | |||
| 2207 | ||||
| 2208 | if (!aPtr.isValid()) { | |||
| 2209 | 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" , 2209); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mTable && mEntryCount == 0" ")"); do { MOZ_CrashSequence(__null, 2209); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2210 | uint32_t newCapacity = rawCapacity(); | |||
| 2211 | RebuildStatus status = changeTableSize(newCapacity, ReportFailure); | |||
| 2212 | 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" , 2212); AnnotateMozCrashReason("MOZ_ASSERT" "(" "status != NotOverloaded" ")"); do { MOZ_CrashSequence(__null, 2212); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2213 | if (status == RehashFailed) { | |||
| 2214 | return false; | |||
| 2215 | } | |||
| 2216 | aPtr.mSlot = findNonLiveSlot(aPtr.mKeyHash); | |||
| 2217 | ||||
| 2218 | } else if (aPtr.mSlot.isRemoved()) { | |||
| 2219 | // Changing an entry from removed to live does not affect whether we are | |||
| 2220 | // overloaded and can be handled separately. | |||
| 2221 | if (!this->checkSimulatedOOM()) { | |||
| 2222 | return false; | |||
| 2223 | } | |||
| 2224 | mRemovedCount--; | |||
| 2225 | aPtr.mKeyHash |= sCollisionBit; | |||
| 2226 | ||||
| 2227 | } else { | |||
| 2228 | // Preserve the validity of |aPtr.mSlot|. | |||
| 2229 | RebuildStatus status = rehashIfOverloaded(); | |||
| 2230 | if (status == RehashFailed) { | |||
| 2231 | return false; | |||
| 2232 | } | |||
| 2233 | if (status == NotOverloaded && !this->checkSimulatedOOM()) { | |||
| 2234 | return false; | |||
| 2235 | } | |||
| 2236 | if (status == Rehashed) { | |||
| 2237 | aPtr.mSlot = findNonLiveSlot(aPtr.mKeyHash); | |||
| 2238 | } | |||
| 2239 | } | |||
| 2240 | ||||
| 2241 | aPtr.mSlot.setLive(aPtr.mKeyHash, std::forward<Args>(aArgs)...); | |||
| 2242 | mEntryCount++; | |||
| 2243 | #ifdef DEBUG1 | |||
| 2244 | mMutationCount++; | |||
| 2245 | aPtr.mGeneration = generation(); | |||
| 2246 | aPtr.mMutationCount = mMutationCount; | |||
| 2247 | #endif | |||
| 2248 | return true; | |||
| 2249 | } | |||
| 2250 | ||||
| 2251 | // Note: |aLookup| may reference pieces of arguments in |aArgs|, so this | |||
| 2252 | // function must take care not to use |aLookup| after moving |aArgs|. | |||
| 2253 | template <typename... Args> | |||
| 2254 | void putNewInfallible(const Lookup& aLookup, Args&&... aArgs) { | |||
| 2255 | 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" , 2255); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!lookup(aLookup).found()" ")"); do { MOZ_CrashSequence(__null, 2255); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2256 | ReentrancyGuard g(*this); | |||
| 2257 | HashNumber keyHash = prepareHash(HashPolicy::hash(aLookup)); | |||
| 2258 | putNewInfallibleInternal(keyHash, std::forward<Args>(aArgs)...); | |||
| 2259 | } | |||
| 2260 | ||||
| 2261 | // Note: |aLookup| may alias arguments in |aArgs|, so this function must take | |||
| 2262 | // care not to use |aLookup| after moving |aArgs|. | |||
| 2263 | template <typename... Args> | |||
| 2264 | [[nodiscard]] bool putNew(const Lookup& aLookup, Args&&... aArgs) { | |||
| 2265 | 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" , 2265); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!lookup(aLookup).found()" ")"); do { MOZ_CrashSequence(__null, 2265); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2266 | ReentrancyGuard g(*this); | |||
| 2267 | if (!this->checkSimulatedOOM()) { | |||
| 2268 | return false; | |||
| 2269 | } | |||
| 2270 | HashNumber inputHash; | |||
| 2271 | if (!EnsureHash<HashPolicy>(aLookup, &inputHash)) { | |||
| 2272 | return false; | |||
| 2273 | } | |||
| 2274 | HashNumber keyHash = prepareHash(inputHash); | |||
| 2275 | if (rehashIfOverloaded() == RehashFailed) { | |||
| 2276 | return false; | |||
| 2277 | } | |||
| 2278 | putNewInfallibleInternal(keyHash, std::forward<Args>(aArgs)...); | |||
| 2279 | return true; | |||
| 2280 | } | |||
| 2281 | ||||
| 2282 | // Note: |aLookup| may be a reference pieces of arguments in |aArgs|, so this | |||
| 2283 | // function must take care not to use |aLookup| after moving |aArgs|. | |||
| 2284 | template <typename... Args> | |||
| 2285 | [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, const Lookup& aLookup, | |||
| 2286 | Args&&... aArgs) { | |||
| 2287 | // Check for error from ensureHash() here. | |||
| 2288 | if (!aPtr.isLive()) { | |||
| 2289 | return false; | |||
| 2290 | } | |||
| 2291 | #ifdef DEBUG1 | |||
| 2292 | aPtr.mGeneration = generation(); | |||
| 2293 | aPtr.mMutationCount = mMutationCount; | |||
| 2294 | #endif | |||
| 2295 | if (mTable) { | |||
| 2296 | ReentrancyGuard g(*this); | |||
| 2297 | // Check that aLookup has not been destroyed. | |||
| 2298 | 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" , 2298); AnnotateMozCrashReason("MOZ_ASSERT" "(" "prepareHash(HashPolicy::hash(aLookup)) == aPtr.mKeyHash" ")"); do { MOZ_CrashSequence(__null, 2298); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2299 | aPtr.mSlot = lookup<ForAdd>(aLookup, aPtr.mKeyHash); | |||
| 2300 | if (aPtr.found()) { | |||
| 2301 | return true; | |||
| 2302 | } | |||
| 2303 | } else { | |||
| 2304 | // Clear aPtr so it's invalid; add() will allocate storage and redo the | |||
| 2305 | // lookup. | |||
| 2306 | aPtr.mSlot = Slot(nullptr, nullptr); | |||
| 2307 | } | |||
| 2308 | return add(aPtr, std::forward<Args>(aArgs)...); | |||
| 2309 | } | |||
| 2310 | ||||
| 2311 | void remove(Ptr aPtr) { | |||
| 2312 | 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" , 2312); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { MOZ_CrashSequence(__null, 2312); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2313 | ReentrancyGuard g(*this); | |||
| 2314 | 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" , 2314); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.found()" ")"); do { MOZ_CrashSequence(__null, 2314); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2315 | 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" , 2315); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mGeneration == generation()" ")"); do { MOZ_CrashSequence(__null, 2315); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2316 | remove(aPtr.mSlot); | |||
| 2317 | shrinkIfUnderloaded(); | |||
| 2318 | } | |||
| 2319 | ||||
| 2320 | template <typename KeyInput> | |||
| 2321 | void rekeyWithoutRehash(Ptr aPtr, const Lookup& aLookup, KeyInput&& aKey) { | |||
| 2322 | 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" , 2322); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { MOZ_CrashSequence(__null, 2322); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2323 | ReentrancyGuard g(*this); | |||
| 2324 | 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" , 2324); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.found()" ")"); do { MOZ_CrashSequence(__null, 2324); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2325 | 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" , 2325); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mGeneration == generation()" ")"); do { MOZ_CrashSequence(__null, 2325); __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
| 2326 | typename HashTableEntry<T>::NonConstT t(std::move(*aPtr)); | |||
| 2327 | HashPolicy::setKey(t, std::forward<KeyInput>(aKey)); | |||
| 2328 | remove(aPtr.mSlot); | |||
| 2329 | HashNumber keyHash = prepareHash(HashPolicy::hash(aLookup)); | |||
| 2330 | putNewInfallibleInternal(keyHash, std::move(t)); | |||
| 2331 | } | |||
| 2332 | ||||
| 2333 | template <typename KeyInput> | |||
| 2334 | void rekeyAndMaybeRehash(Ptr aPtr, const Lookup& aLookup, KeyInput&& aKey) { | |||
| 2335 | rekeyWithoutRehash(aPtr, aLookup, std::forward<KeyInput>(aKey)); | |||
| 2336 | infallibleRehashIfOverloaded(); | |||
| 2337 | } | |||
| 2338 | ||||
| 2339 | static size_t offsetOfHashShift() { | |||
| 2340 | static_assert(sHashShiftBits == 8, | |||
| 2341 | "callers assume hash shift is stored in a byte"); | |||
| 2342 | // The hash shift is stored in the least significant bits of | |||
| 2343 | // mGenAndHashShift. On little-endian platforms, this is the | |||
| 2344 | // same offset as mGenAndHashShift itself. On big-endian platforms, | |||
| 2345 | // we have to add an additional offset to point to the last byte. | |||
| 2346 | // (Or we would if we had JIT support for any big-endian platforms.) | |||
| 2347 | if constexpr (std::endian::native == std::endian::big) { | |||
| 2348 | return offsetof(HashTable, mGenAndHashShift)__builtin_offsetof(HashTable, mGenAndHashShift) + sizeof(mGenAndHashShift) - | |||
| 2349 | sizeof(uint8_t); | |||
| 2350 | } else { | |||
| 2351 | return offsetof(HashTable, mGenAndHashShift)__builtin_offsetof(HashTable, mGenAndHashShift); | |||
| 2352 | } | |||
| 2353 | } | |||
| 2354 | static size_t offsetOfTable() { return offsetof(HashTable, mTable)__builtin_offsetof(HashTable, mTable); } | |||
| 2355 | static size_t offsetOfEntryCount() { | |||
| 2356 | return offsetof(HashTable, mEntryCount)__builtin_offsetof(HashTable, mEntryCount); | |||
| 2357 | } | |||
| 2358 | }; | |||
| 2359 | ||||
| 2360 | } // namespace detail | |||
| 2361 | } // namespace mozilla | |||
| 2362 | ||||
| 2363 | #endif /* mozilla_HashTable_h */ |