File: | var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h |
Warning: | line 1754, column 7 Use of memory after it is freed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ | |||
2 | /* This Source Code Form is subject to the terms of the Mozilla Public | |||
3 | * License, v. 2.0. If a copy of the MPL was not distributed with this | |||
4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |||
5 | ||||
6 | #include "HeapSnapshot.h" | |||
7 | ||||
8 | #include <google/protobuf/io/coded_stream.h> | |||
9 | #include <google/protobuf/io/gzip_stream.h> | |||
10 | #include <google/protobuf/io/zero_copy_stream_impl_lite.h> | |||
11 | ||||
12 | #include "js/Array.h" // JS::NewArrayObject | |||
13 | #include "js/ColumnNumber.h" // JS::LimitedColumnNumberOneOrigin, JS::TaggedColumnNumberOneOrigin | |||
14 | #include "js/Debug.h" | |||
15 | #include "js/PropertyAndElement.h" // JS_DefineProperty | |||
16 | #include "js/TypeDecls.h" | |||
17 | #include "js/UbiNodeBreadthFirst.h" | |||
18 | #include "js/UbiNodeCensus.h" | |||
19 | #include "js/UbiNodeDominatorTree.h" | |||
20 | #include "js/UbiNodeShortestPaths.h" | |||
21 | #include "mozilla/Attributes.h" | |||
22 | #include "mozilla/CycleCollectedJSContext.h" | |||
23 | #include "mozilla/devtools/AutoMemMap.h" | |||
24 | #include "mozilla/devtools/CoreDump.pb.h" | |||
25 | #include "mozilla/devtools/DeserializedNode.h" | |||
26 | #include "mozilla/devtools/DominatorTree.h" | |||
27 | #include "mozilla/devtools/FileDescriptorOutputStream.h" | |||
28 | #include "mozilla/devtools/HeapSnapshotTempFileHelperChild.h" | |||
29 | #include "mozilla/devtools/ZeroCopyNSIOutputStream.h" | |||
30 | #include "mozilla/dom/ChromeUtils.h" | |||
31 | #include "mozilla/dom/ContentChild.h" | |||
32 | #include "mozilla/dom/HeapSnapshotBinding.h" | |||
33 | #include "mozilla/RangedPtr.h" | |||
34 | #include "mozilla/Telemetry.h" | |||
35 | #include "mozilla/Unused.h" | |||
36 | ||||
37 | #include "jsapi.h" | |||
38 | #include "jsfriendapi.h" | |||
39 | #include "js/GCVector.h" | |||
40 | #include "js/MapAndSet.h" | |||
41 | #include "js/Object.h" // JS::GetCompartment | |||
42 | #include "nsComponentManagerUtils.h" // do_CreateInstance | |||
43 | #include "nsCycleCollectionParticipant.h" | |||
44 | #include "nsCRTGlue.h" | |||
45 | #include "nsIFile.h" | |||
46 | #include "nsIOutputStream.h" | |||
47 | #include "nsISupportsImpl.h" | |||
48 | #include "nsNetUtil.h" | |||
49 | #include "nsPrintfCString.h" | |||
50 | #include "prerror.h" | |||
51 | #include "prio.h" | |||
52 | #include "prtypes.h" | |||
53 | #include "SpecialSystemDirectory.h" | |||
54 | ||||
55 | namespace mozilla { | |||
56 | namespace devtools { | |||
57 | ||||
58 | using namespace JS; | |||
59 | using namespace dom; | |||
60 | ||||
61 | using ::google::protobuf::io::ArrayInputStream; | |||
62 | using ::google::protobuf::io::CodedInputStream; | |||
63 | using ::google::protobuf::io::GzipInputStream; | |||
64 | using ::google::protobuf::io::ZeroCopyInputStream; | |||
65 | ||||
66 | using JS::ubi::AtomOrTwoByteChars; | |||
67 | using JS::ubi::ShortestPaths; | |||
68 | ||||
69 | MallocSizeOf GetCurrentThreadDebuggerMallocSizeOf() { | |||
70 | auto ccjscx = CycleCollectedJSContext::Get(); | |||
71 | MOZ_ASSERT(ccjscx)do { static_assert( mozilla::detail::AssertionConditionType< decltype(ccjscx)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(ccjscx))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("ccjscx", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 71); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ccjscx" ")"); do { *((volatile int*)__null) = 71; __attribute__((nomerge)) ::abort(); } while (false); } } while (false); | |||
72 | auto cx = ccjscx->Context(); | |||
73 | MOZ_ASSERT(cx)do { static_assert( mozilla::detail::AssertionConditionType< decltype(cx)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(cx))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("cx", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 73); AnnotateMozCrashReason("MOZ_ASSERT" "(" "cx" ")"); do { *((volatile int*)__null) = 73; __attribute__((nomerge)) ::abort (); } while (false); } } while (false); | |||
74 | auto mallocSizeOf = JS::dbg::GetDebuggerMallocSizeOf(cx); | |||
75 | MOZ_ASSERT(mallocSizeOf)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mallocSizeOf)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(mallocSizeOf))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mallocSizeOf", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 75); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mallocSizeOf" ")"); do { *((volatile int*)__null) = 75; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
76 | return mallocSizeOf; | |||
77 | } | |||
78 | ||||
79 | /*** Cycle Collection Boilerplate *********************************************/ | |||
80 | ||||
81 | NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(HeapSnapshot, mParent)static_assert(std::is_base_of<nsWrapperCache, HeapSnapshot >::value, "Class should inherit nsWrapperCache"); HeapSnapshot ::cycleCollection HeapSnapshot::_cycleCollectorGlobal( nsCycleCollectionParticipant ::FlagMaybeSingleZoneJSHolder); void HeapSnapshot::cycleCollection ::Trace( void* p, const TraceCallbacks& aCallbacks, void* aClosure) { HeapSnapshot* tmp = DowncastCCParticipant<HeapSnapshot >(p); TraceWrapper(p, aCallbacks, aClosure); (void)tmp; } void HeapSnapshot::cycleCollection::TraceWrapper( void* p, const TraceCallbacks & aCallbacks, void* aClosure) { HeapSnapshot* tmp = DowncastCCParticipant <HeapSnapshot>(p); tmp->TraceWrapper(aCallbacks, aClosure ); } void HeapSnapshot::cycleCollection::Unlink(void* p) { HeapSnapshot * tmp = DowncastCCParticipant<HeapSnapshot>(p); ImplCycleCollectionUnlink (tmp->mParent); tmp->ReleaseWrapper(p); (void)tmp; } nsresult HeapSnapshot::cycleCollection::TraverseNative( void* p, nsCycleCollectionTraversalCallback & cb) { HeapSnapshot* tmp = DowncastCCParticipant<HeapSnapshot >(p); cb.DescribeRefCountedNode(tmp->mRefCnt.get(), "HeapSnapshot" ); ImplCycleCollectionTraverse(cb, tmp->mParent, "mParent" , 0); (void)tmp; return NS_OK; } | |||
82 | ||||
83 | NS_IMPL_CYCLE_COLLECTING_ADDREF(HeapSnapshot)MozExternalRefCountType HeapSnapshot::AddRef(void) { static_assert (!std::is_destructible_v<HeapSnapshot>, "Reference-counted class " "HeapSnapshot" " should not have a public destructor. " "Make this class's destructor non-public" ); do { static_assert( mozilla::detail::AssertionConditionType <decltype(int32_t(mRefCnt) >= 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(int32_t(mRefCnt) >= 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("int32_t(mRefCnt) >= 0" " (" "illegal refcnt" ")", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 83); AnnotateMozCrashReason("MOZ_ASSERT" "(" "int32_t(mRefCnt) >= 0" ") (" "illegal refcnt" ")"); do { *((volatile int*)__null) = 83; __attribute__((nomerge)) ::abort(); } while (false); } } while (false); _mOwningThread.AssertOwnership("HeapSnapshot" " not thread-safe"); nsISupports* base = HeapSnapshot::cycleCollection ::Upcast(this); nsrefcnt count = mRefCnt.incr(base); NS_LogAddRef ((this), (count), ("HeapSnapshot"), (uint32_t)(sizeof(*this)) ); return count; } | |||
84 | NS_IMPL_CYCLE_COLLECTING_RELEASE(HeapSnapshot)MozExternalRefCountType HeapSnapshot::Release(void) { do { static_assert ( mozilla::detail::AssertionConditionType<decltype(int32_t (mRefCnt) > 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(int32_t(mRefCnt) > 0))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("int32_t(mRefCnt) > 0" " (" "dup release" ")", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 84); AnnotateMozCrashReason("MOZ_ASSERT" "(" "int32_t(mRefCnt) > 0" ") (" "dup release" ")"); do { *((volatile int*)__null) = 84 ; __attribute__((nomerge)) ::abort(); } while (false); } } while (false); _mOwningThread.AssertOwnership("HeapSnapshot" " not thread-safe" ); nsISupports* base = HeapSnapshot::cycleCollection::Upcast( this); nsrefcnt count = mRefCnt.decr(base); NS_LogRelease((this ), (count), ("HeapSnapshot")); return count; } void HeapSnapshot ::DeleteCycleCollectable(void) { delete (this); } | |||
85 | ||||
86 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(HeapSnapshot)nsresult HeapSnapshot::QueryInterface(const nsIID& aIID, void ** aInstancePtr) { do { if (!(aInstancePtr)) { NS_DebugBreak( NS_DEBUG_ASSERTION, "QueryInterface requires a non-NULL destination!" , "aInstancePtr", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 86); MOZ_PretendNoReturn(); } } while (0); nsISupports* foundInterface ; if (TopThreeWordsEquals( aIID, (nsXPCOMCycleCollectionParticipant ::COMTypeInfo<nsXPCOMCycleCollectionParticipant, void>:: kIID), (nsCycleCollectionISupports::COMTypeInfo<nsCycleCollectionISupports , void>::kIID)) && (LowWordEquals(aIID, (nsXPCOMCycleCollectionParticipant ::COMTypeInfo<nsXPCOMCycleCollectionParticipant, void>:: kIID)) || LowWordEquals(aIID, (nsCycleCollectionISupports::COMTypeInfo <nsCycleCollectionISupports, void>::kIID)))) { if (LowWordEquals (aIID, (nsXPCOMCycleCollectionParticipant::COMTypeInfo<nsXPCOMCycleCollectionParticipant , void>::kIID))) { *aInstancePtr = HeapSnapshot::cycleCollection ::GetParticipant(); return NS_OK; } if (LowWordEquals(aIID, ( nsCycleCollectionISupports::COMTypeInfo<nsCycleCollectionISupports , void>::kIID))) { *aInstancePtr = HeapSnapshot::cycleCollection ::Upcast(this); return NS_OK; } foundInterface = nullptr; } else | |||
87 | NS_WRAPPERCACHE_INTERFACE_MAP_ENTRYif (aIID.Equals((nsWrapperCache::COMTypeInfo<nsWrapperCache , void>::kIID))) { *aInstancePtr = static_cast<nsWrapperCache *>(this); return NS_OK; } else | |||
88 | NS_INTERFACE_MAP_ENTRY(nsISupports)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t <decltype(*this)>, nsISupports>)) foundInterface = static_cast <nsISupports*>(this); else | |||
89 | NS_INTERFACE_MAP_ENDfoundInterface = 0; nsresult status; if (!foundInterface) { do { static_assert( mozilla::detail::AssertionConditionType< decltype(!aIID.Equals((nsISupports::COMTypeInfo<nsISupports , void>::kIID)))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!aIID.Equals((nsISupports::COMTypeInfo <nsISupports, void>::kIID))))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("!aIID.Equals((nsISupports::COMTypeInfo<nsISupports, void>::kIID))" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 89); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!aIID.Equals((nsISupports::COMTypeInfo<nsISupports, void>::kIID))" ")"); do { *((volatile int*)__null) = 89; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); status = NS_NOINTERFACE ; } else { (foundInterface)->AddRef(); status = NS_OK; } * aInstancePtr = foundInterface; return status; } | |||
90 | ||||
91 | /* virtual */ | |||
92 | JSObject* HeapSnapshot::WrapObject(JSContext* aCx, | |||
93 | JS::Handle<JSObject*> aGivenProto) { | |||
94 | return HeapSnapshot_Binding::Wrap(aCx, this, aGivenProto); | |||
95 | } | |||
96 | ||||
97 | /*** Reading Heap Snapshots ***************************************************/ | |||
98 | ||||
99 | /* static */ | |||
100 | already_AddRefed<HeapSnapshot> HeapSnapshot::Create(JSContext* cx, | |||
101 | GlobalObject& global, | |||
102 | const uint8_t* buffer, | |||
103 | uint32_t size, | |||
104 | ErrorResult& rv) { | |||
105 | RefPtr<HeapSnapshot> snapshot = new HeapSnapshot(cx, global.GetAsSupports()); | |||
106 | if (!snapshot->init(cx, buffer, size)) { | |||
107 | rv.Throw(NS_ERROR_UNEXPECTED); | |||
108 | return nullptr; | |||
109 | } | |||
110 | return snapshot.forget(); | |||
111 | } | |||
112 | ||||
113 | template <typename MessageType> | |||
114 | static bool parseMessage(ZeroCopyInputStream& stream, uint32_t sizeOfMessage, | |||
115 | MessageType& message) { | |||
116 | // We need to create a new `CodedInputStream` for each message so that the | |||
117 | // 64MB limit is applied per-message rather than to the whole stream. | |||
118 | CodedInputStream codedStream(&stream); | |||
119 | ||||
120 | // The protobuf message nesting that core dumps exhibit is dominated by | |||
121 | // allocation stacks' frames. In the most deeply nested case, each frame has | |||
122 | // two messages: a StackFrame message and a StackFrame::Data message. These | |||
123 | // frames are on top of a small constant of other messages. There are a | |||
124 | // MAX_STACK_DEPTH number of frames, so we multiply this by 3 to make room for | |||
125 | // the two messages per frame plus some head room for the constant number of | |||
126 | // non-dominating messages. | |||
127 | codedStream.SetRecursionLimit(HeapSnapshot::MAX_STACK_DEPTH * 3); | |||
128 | ||||
129 | auto limit = codedStream.PushLimit(sizeOfMessage); | |||
130 | if (NS_WARN_IF(!message.ParseFromCodedStream(&codedStream))NS_warn_if_impl(!message.ParseFromCodedStream(&codedStream ), "!message.ParseFromCodedStream(&codedStream)", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 130) || | |||
131 | NS_WARN_IF(!codedStream.ConsumedEntireMessage())NS_warn_if_impl(!codedStream.ConsumedEntireMessage(), "!codedStream.ConsumedEntireMessage()" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 131) || | |||
132 | NS_WARN_IF(codedStream.BytesUntilLimit() != 0)NS_warn_if_impl(codedStream.BytesUntilLimit() != 0, "codedStream.BytesUntilLimit() != 0" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 132)) { | |||
133 | return false; | |||
134 | } | |||
135 | ||||
136 | codedStream.PopLimit(limit); | |||
137 | return true; | |||
138 | } | |||
139 | ||||
140 | template <typename CharT, typename InternedStringSet> | |||
141 | struct GetOrInternStringMatcher { | |||
142 | InternedStringSet& internedStrings; | |||
143 | ||||
144 | explicit GetOrInternStringMatcher(InternedStringSet& strings) | |||
145 | : internedStrings(strings) {} | |||
146 | ||||
147 | const CharT* operator()(const std::string* str) { | |||
148 | MOZ_ASSERT(str)do { static_assert( mozilla::detail::AssertionConditionType< decltype(str)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(str))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("str", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 148); AnnotateMozCrashReason("MOZ_ASSERT" "(" "str" ")"); do { *((volatile int*)__null) = 148; __attribute__((nomerge)) :: abort(); } while (false); } } while (false); | |||
149 | size_t length = str->length() / sizeof(CharT); | |||
150 | auto tempString = reinterpret_cast<const CharT*>(str->data()); | |||
151 | ||||
152 | UniqueFreePtr<CharT[]> owned(NS_xstrndup(tempString, length)); | |||
153 | if (!internedStrings.append(std::move(owned))) return nullptr; | |||
154 | ||||
155 | return internedStrings.back().get(); | |||
156 | } | |||
157 | ||||
158 | const CharT* operator()(uint64_t ref) { | |||
159 | if (MOZ_LIKELY(ref < internedStrings.length())(__builtin_expect(!!(ref < internedStrings.length()), 1))) { | |||
160 | auto& string = internedStrings[ref]; | |||
161 | MOZ_ASSERT(string)do { static_assert( mozilla::detail::AssertionConditionType< decltype(string)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(string))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("string", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 161); AnnotateMozCrashReason("MOZ_ASSERT" "(" "string" ")") ; do { *((volatile int*)__null) = 161; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
162 | return string.get(); | |||
163 | } | |||
164 | ||||
165 | return nullptr; | |||
166 | } | |||
167 | }; | |||
168 | ||||
169 | template < | |||
170 | // Either char or char16_t. | |||
171 | typename CharT, | |||
172 | // A reference to either `internedOneByteStrings` or | |||
173 | // `internedTwoByteStrings` if CharT is char or char16_t respectively. | |||
174 | typename InternedStringSet> | |||
175 | const CharT* HeapSnapshot::getOrInternString( | |||
176 | InternedStringSet& internedStrings, Maybe<StringOrRef>& maybeStrOrRef) { | |||
177 | // Incomplete message: has neither a string nor a reference to an already | |||
178 | // interned string. | |||
179 | if (MOZ_UNLIKELY(maybeStrOrRef.isNothing())(__builtin_expect(!!(maybeStrOrRef.isNothing()), 0))) return nullptr; | |||
180 | ||||
181 | GetOrInternStringMatcher<CharT, InternedStringSet> m(internedStrings); | |||
182 | return maybeStrOrRef->match(m); | |||
183 | } | |||
184 | ||||
185 | // Get a de-duplicated string as a Maybe<StringOrRef> from the given `msg`. | |||
186 | #define GET_STRING_OR_REF_WITH_PROP_NAMES(msg, strPropertyName, \ | |||
187 | refPropertyName) \ | |||
188 | (msg.has_##refPropertyName() ? Some(StringOrRef(msg.refPropertyName())) \ | |||
189 | : msg.has_##strPropertyName() ? Some(StringOrRef(&msg.strPropertyName())) \ | |||
190 | : Nothing()) | |||
191 | ||||
192 | #define GET_STRING_OR_REF(msg, property) \ | |||
193 | (msg.has_##property##ref() ? Some(StringOrRef(msg.property##ref())) \ | |||
194 | : msg.has_##property() ? Some(StringOrRef(&msg.property())) \ | |||
195 | : Nothing()) | |||
196 | ||||
197 | bool HeapSnapshot::saveNode(const protobuf::Node& node, | |||
198 | NodeIdSet& edgeReferents) { | |||
199 | // NB: de-duplicated string properties must be read back and interned in the | |||
200 | // same order here as they are written and serialized in | |||
201 | // `CoreDumpWriter::writeNode` or else indices in references to already | |||
202 | // serialized strings will be off. | |||
203 | ||||
204 | if (NS_WARN_IF(!node.has_id())NS_warn_if_impl(!node.has_id(), "!node.has_id()", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 204)) return false; | |||
205 | NodeId id = node.id(); | |||
206 | ||||
207 | // NodeIds are derived from pointers (at most 48 bits) and we rely on them | |||
208 | // fitting into JS numbers (IEEE 754 doubles, can precisely store 53 bit | |||
209 | // integers) despite storing them on disk as 64 bit integers. | |||
210 | if (NS_WARN_IF(!JS::Value::isNumberRepresentable(id))NS_warn_if_impl(!JS::Value::isNumberRepresentable(id), "!JS::Value::isNumberRepresentable(id)" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 210)) return false; | |||
211 | ||||
212 | // Should only deserialize each node once. | |||
213 | if (NS_WARN_IF(nodes.has(id))NS_warn_if_impl(nodes.has(id), "nodes.has(id)", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 213)) return false; | |||
214 | ||||
215 | if (NS_WARN_IF(!JS::ubi::Uint32IsValidCoarseType(node.coarsetype()))NS_warn_if_impl(!JS::ubi::Uint32IsValidCoarseType(node.coarsetype ()), "!JS::ubi::Uint32IsValidCoarseType(node.coarsetype())", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 215)) | |||
216 | return false; | |||
217 | auto coarseType = JS::ubi::Uint32ToCoarseType(node.coarsetype()); | |||
218 | ||||
219 | Maybe<StringOrRef> typeNameOrRef = | |||
220 | GET_STRING_OR_REF_WITH_PROP_NAMES(node, typename_, typenameref); | |||
221 | auto typeName = | |||
222 | getOrInternString<char16_t>(internedTwoByteStrings, typeNameOrRef); | |||
223 | if (NS_WARN_IF(!typeName)NS_warn_if_impl(!typeName, "!typeName", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 223)) return false; | |||
224 | ||||
225 | if (NS_WARN_IF(!node.has_size())NS_warn_if_impl(!node.has_size(), "!node.has_size()", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 225)) return false; | |||
226 | uint64_t size = node.size(); | |||
227 | ||||
228 | auto edgesLength = node.edges_size(); | |||
229 | DeserializedNode::EdgeVector edges; | |||
230 | if (NS_WARN_IF(!edges.reserve(edgesLength))NS_warn_if_impl(!edges.reserve(edgesLength), "!edges.reserve(edgesLength)" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 230)) return false; | |||
231 | for (decltype(edgesLength) i = 0; i < edgesLength; i++) { | |||
232 | auto& protoEdge = node.edges(i); | |||
233 | ||||
234 | if (NS_WARN_IF(!protoEdge.has_referent())NS_warn_if_impl(!protoEdge.has_referent(), "!protoEdge.has_referent()" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 234)) return false; | |||
235 | NodeId referent = protoEdge.referent(); | |||
236 | ||||
237 | if (NS_WARN_IF(!edgeReferents.put(referent))NS_warn_if_impl(!edgeReferents.put(referent), "!edgeReferents.put(referent)" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 237)) return false; | |||
238 | ||||
239 | const char16_t* edgeName = nullptr; | |||
240 | if (protoEdge.EdgeNameOrRef_case() != | |||
241 | protobuf::Edge::EDGENAMEORREF_NOT_SET) { | |||
242 | Maybe<StringOrRef> edgeNameOrRef = GET_STRING_OR_REF(protoEdge, name); | |||
243 | edgeName = | |||
244 | getOrInternString<char16_t>(internedTwoByteStrings, edgeNameOrRef); | |||
245 | if (NS_WARN_IF(!edgeName)NS_warn_if_impl(!edgeName, "!edgeName", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 245)) return false; | |||
246 | } | |||
247 | ||||
248 | edges.infallibleAppend(DeserializedEdge(referent, edgeName)); | |||
249 | } | |||
250 | ||||
251 | Maybe<StackFrameId> allocationStack; | |||
252 | if (node.has_allocationstack()) { | |||
253 | StackFrameId id = 0; | |||
254 | if (NS_WARN_IF(!saveStackFrame(node.allocationstack(), id))NS_warn_if_impl(!saveStackFrame(node.allocationstack(), id), "!saveStackFrame(node.allocationstack(), id)" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 254)) return false; | |||
255 | allocationStack.emplace(id); | |||
256 | } | |||
257 | MOZ_ASSERT(allocationStack.isSome() == node.has_allocationstack())do { static_assert( mozilla::detail::AssertionConditionType< decltype(allocationStack.isSome() == node.has_allocationstack ())>::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(allocationStack.isSome() == node.has_allocationstack ()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("allocationStack.isSome() == node.has_allocationstack()", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 257); AnnotateMozCrashReason("MOZ_ASSERT" "(" "allocationStack.isSome() == node.has_allocationstack()" ")"); do { *((volatile int*)__null) = 257; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
258 | ||||
259 | const char* jsObjectClassName = nullptr; | |||
260 | if (node.JSObjectClassNameOrRef_case() != | |||
261 | protobuf::Node::JSOBJECTCLASSNAMEORREF_NOT_SET) { | |||
262 | Maybe<StringOrRef> clsNameOrRef = | |||
263 | GET_STRING_OR_REF(node, jsobjectclassname); | |||
264 | jsObjectClassName = | |||
265 | getOrInternString<char>(internedOneByteStrings, clsNameOrRef); | |||
266 | if (NS_WARN_IF(!jsObjectClassName)NS_warn_if_impl(!jsObjectClassName, "!jsObjectClassName", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 266)) return false; | |||
267 | } | |||
268 | ||||
269 | const char* scriptFilename = nullptr; | |||
270 | if (node.ScriptFilenameOrRef_case() != | |||
271 | protobuf::Node::SCRIPTFILENAMEORREF_NOT_SET) { | |||
272 | Maybe<StringOrRef> scriptFilenameOrRef = | |||
273 | GET_STRING_OR_REF(node, scriptfilename); | |||
274 | scriptFilename = | |||
275 | getOrInternString<char>(internedOneByteStrings, scriptFilenameOrRef); | |||
276 | if (NS_WARN_IF(!scriptFilename)NS_warn_if_impl(!scriptFilename, "!scriptFilename", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 276)) return false; | |||
277 | } | |||
278 | ||||
279 | const char16_t* descriptiveTypeName = nullptr; | |||
280 | if (node.descriptiveTypeNameOrRef_case() != | |||
281 | protobuf::Node::DESCRIPTIVETYPENAMEORREF_NOT_SET) { | |||
282 | Maybe<StringOrRef> descriptiveTypeNameOrRef = | |||
283 | GET_STRING_OR_REF(node, descriptivetypename); | |||
284 | descriptiveTypeName = getOrInternString<char16_t>(internedTwoByteStrings, | |||
285 | descriptiveTypeNameOrRef); | |||
286 | if (NS_WARN_IF(!descriptiveTypeName)NS_warn_if_impl(!descriptiveTypeName, "!descriptiveTypeName", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 286)) return false; | |||
287 | } | |||
288 | ||||
289 | if (NS_WARN_IF(!nodes.putNew(NS_warn_if_impl(!nodes.putNew( id, DeserializedNode(id, coarseType , typeName, size, std::move(edges), allocationStack, jsObjectClassName , scriptFilename, descriptiveTypeName, *this)), "!nodes.putNew( id, DeserializedNode(id, coarseType, typeName, size, std::move(edges), allocationStack, jsObjectClassName, scriptFilename, descriptiveTypeName, *this))" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 292) | |||
290 | id, DeserializedNode(id, coarseType, typeName, size, std::move(edges),NS_warn_if_impl(!nodes.putNew( id, DeserializedNode(id, coarseType , typeName, size, std::move(edges), allocationStack, jsObjectClassName , scriptFilename, descriptiveTypeName, *this)), "!nodes.putNew( id, DeserializedNode(id, coarseType, typeName, size, std::move(edges), allocationStack, jsObjectClassName, scriptFilename, descriptiveTypeName, *this))" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 292) | |||
291 | allocationStack, jsObjectClassName,NS_warn_if_impl(!nodes.putNew( id, DeserializedNode(id, coarseType , typeName, size, std::move(edges), allocationStack, jsObjectClassName , scriptFilename, descriptiveTypeName, *this)), "!nodes.putNew( id, DeserializedNode(id, coarseType, typeName, size, std::move(edges), allocationStack, jsObjectClassName, scriptFilename, descriptiveTypeName, *this))" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 292) | |||
292 | scriptFilename, descriptiveTypeName, *this)))NS_warn_if_impl(!nodes.putNew( id, DeserializedNode(id, coarseType , typeName, size, std::move(edges), allocationStack, jsObjectClassName , scriptFilename, descriptiveTypeName, *this)), "!nodes.putNew( id, DeserializedNode(id, coarseType, typeName, size, std::move(edges), allocationStack, jsObjectClassName, scriptFilename, descriptiveTypeName, *this))" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 292)) { | |||
293 | return false; | |||
294 | }; | |||
295 | ||||
296 | return true; | |||
297 | } | |||
298 | ||||
299 | bool HeapSnapshot::saveStackFrame(const protobuf::StackFrame& frame, | |||
300 | StackFrameId& outFrameId) { | |||
301 | // NB: de-duplicated string properties must be read in the same order here as | |||
302 | // they are written in `CoreDumpWriter::getProtobufStackFrame` or else indices | |||
303 | // in references to already serialized strings will be off. | |||
304 | ||||
305 | if (frame.has_ref()) { | |||
306 | // We should only get a reference to the previous frame if we have already | |||
307 | // seen the previous frame. | |||
308 | if (!frames.has(frame.ref())) return false; | |||
309 | ||||
310 | outFrameId = frame.ref(); | |||
311 | return true; | |||
312 | } | |||
313 | ||||
314 | // Incomplete message. | |||
315 | if (!frame.has_data()) return false; | |||
316 | ||||
317 | auto data = frame.data(); | |||
318 | ||||
319 | if (!data.has_id()) return false; | |||
320 | StackFrameId id = data.id(); | |||
321 | ||||
322 | // This should be the first and only time we see this frame. | |||
323 | if (frames.has(id)) return false; | |||
324 | ||||
325 | if (!data.has_line()) return false; | |||
326 | uint32_t line = data.line(); | |||
327 | ||||
328 | if (!data.has_column()) return false; | |||
329 | JS::TaggedColumnNumberOneOrigin column( | |||
330 | JS::LimitedColumnNumberOneOrigin(data.column())); | |||
331 | ||||
332 | if (!data.has_issystem()) return false; | |||
333 | bool isSystem = data.issystem(); | |||
334 | ||||
335 | if (!data.has_isselfhosted()) return false; | |||
336 | bool isSelfHosted = data.isselfhosted(); | |||
337 | ||||
338 | Maybe<StringOrRef> sourceOrRef = GET_STRING_OR_REF(data, source); | |||
339 | auto source = | |||
340 | getOrInternString<char16_t>(internedTwoByteStrings, sourceOrRef); | |||
341 | if (!source) return false; | |||
342 | ||||
343 | const char16_t* functionDisplayName = nullptr; | |||
344 | if (data.FunctionDisplayNameOrRef_case() != | |||
345 | protobuf::StackFrame_Data::FUNCTIONDISPLAYNAMEORREF_NOT_SET) { | |||
346 | Maybe<StringOrRef> nameOrRef = GET_STRING_OR_REF(data, functiondisplayname); | |||
347 | functionDisplayName = | |||
348 | getOrInternString<char16_t>(internedTwoByteStrings, nameOrRef); | |||
349 | if (!functionDisplayName) return false; | |||
350 | } | |||
351 | ||||
352 | Maybe<StackFrameId> parent; | |||
353 | if (data.has_parent()) { | |||
354 | StackFrameId parentId = 0; | |||
355 | if (!saveStackFrame(data.parent(), parentId)) return false; | |||
356 | parent = Some(parentId); | |||
357 | } | |||
358 | ||||
359 | if (!frames.putNew(id, | |||
360 | DeserializedStackFrame(id, parent, line, column, source, | |||
361 | functionDisplayName, isSystem, | |||
362 | isSelfHosted, *this))) { | |||
363 | return false; | |||
364 | } | |||
365 | ||||
366 | outFrameId = id; | |||
367 | return true; | |||
368 | } | |||
369 | ||||
370 | #undef GET_STRING_OR_REF_WITH_PROP_NAMES | |||
371 | #undef GET_STRING_OR_REF | |||
372 | ||||
373 | // Because protobuf messages aren't self-delimiting, we serialize each message | |||
374 | // preceded by its size in bytes. When deserializing, we read this size and then | |||
375 | // limit reading from the stream to the given byte size. If we didn't, then the | |||
376 | // first message would consume the entire stream. | |||
377 | static bool readSizeOfNextMessage(ZeroCopyInputStream& stream, | |||
378 | uint32_t* sizep) { | |||
379 | MOZ_ASSERT(sizep)do { static_assert( mozilla::detail::AssertionConditionType< decltype(sizep)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(sizep))), 0))) { do { } while ( false); MOZ_ReportAssertionFailure("sizep", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 379); AnnotateMozCrashReason("MOZ_ASSERT" "(" "sizep" ")"); do { *((volatile int*)__null) = 379; __attribute__((nomerge) ) ::abort(); } while (false); } } while (false); | |||
380 | CodedInputStream codedStream(&stream); | |||
381 | return codedStream.ReadVarint32(sizep) && *sizep > 0; | |||
382 | } | |||
383 | ||||
384 | bool HeapSnapshot::init(JSContext* cx, const uint8_t* buffer, uint32_t size) { | |||
385 | ArrayInputStream stream(buffer, size); | |||
386 | GzipInputStream gzipStream(&stream); | |||
387 | uint32_t sizeOfMessage = 0; | |||
388 | ||||
389 | // First is the metadata. | |||
390 | ||||
391 | protobuf::Metadata metadata; | |||
392 | if (NS_WARN_IF(!readSizeOfNextMessage(gzipStream, &sizeOfMessage))NS_warn_if_impl(!readSizeOfNextMessage(gzipStream, &sizeOfMessage ), "!readSizeOfNextMessage(gzipStream, &sizeOfMessage)", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 392)) | |||
393 | return false; | |||
394 | if (!parseMessage(gzipStream, sizeOfMessage, metadata)) return false; | |||
395 | if (metadata.has_timestamp()) timestamp.emplace(metadata.timestamp()); | |||
396 | ||||
397 | // Next is the root node. | |||
398 | ||||
399 | protobuf::Node root; | |||
400 | if (NS_WARN_IF(!readSizeOfNextMessage(gzipStream, &sizeOfMessage))NS_warn_if_impl(!readSizeOfNextMessage(gzipStream, &sizeOfMessage ), "!readSizeOfNextMessage(gzipStream, &sizeOfMessage)", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 400)) | |||
401 | return false; | |||
402 | if (!parseMessage(gzipStream, sizeOfMessage, root)) return false; | |||
403 | ||||
404 | // Although the id is optional in the protobuf format for future proofing, we | |||
405 | // can't currently do anything without it. | |||
406 | if (NS_WARN_IF(!root.has_id())NS_warn_if_impl(!root.has_id(), "!root.has_id()", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 406)) return false; | |||
407 | rootId = root.id(); | |||
408 | ||||
409 | // The set of all node ids we've found edges pointing to. | |||
410 | NodeIdSet edgeReferents(cx); | |||
411 | ||||
412 | if (NS_WARN_IF(!saveNode(root, edgeReferents))NS_warn_if_impl(!saveNode(root, edgeReferents), "!saveNode(root, edgeReferents)" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 412)) return false; | |||
413 | ||||
414 | // Finally, the rest of the nodes in the core dump. | |||
415 | ||||
416 | // Test for the end of the stream. The protobuf library gives no way to tell | |||
417 | // the difference between an underlying read error and the stream being | |||
418 | // done. All we can do is attempt to read the size of the next message and | |||
419 | // extrapolate guestimations from the result of that operation. | |||
420 | while (readSizeOfNextMessage(gzipStream, &sizeOfMessage)) { | |||
421 | protobuf::Node node; | |||
422 | if (!parseMessage(gzipStream, sizeOfMessage, node)) return false; | |||
423 | if (NS_WARN_IF(!saveNode(node, edgeReferents))NS_warn_if_impl(!saveNode(node, edgeReferents), "!saveNode(node, edgeReferents)" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 423)) return false; | |||
424 | } | |||
425 | ||||
426 | // Check the set of node ids referred to by edges we found and ensure that we | |||
427 | // have the node corresponding to each id. If we don't have all of them, it is | |||
428 | // unsafe to perform analyses of this heap snapshot. | |||
429 | for (auto iter = edgeReferents.iter(); !iter.done(); iter.next()) { | |||
430 | if (NS_WARN_IF(!nodes.has(iter.get()))NS_warn_if_impl(!nodes.has(iter.get()), "!nodes.has(iter.get())" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 430)) return false; | |||
431 | } | |||
432 | ||||
433 | return true; | |||
434 | } | |||
435 | ||||
436 | /*** Heap Snapshot Analyses ***************************************************/ | |||
437 | ||||
438 | void HeapSnapshot::TakeCensus(JSContext* cx, JS::Handle<JSObject*> options, | |||
439 | JS::MutableHandle<JS::Value> rval, | |||
440 | ErrorResult& rv) { | |||
441 | JS::ubi::Census census(cx); | |||
442 | ||||
443 | JS::ubi::CountTypePtr rootType; | |||
444 | if (NS_WARN_IF(!JS::ubi::ParseCensusOptions(cx, census, options, rootType))NS_warn_if_impl(!JS::ubi::ParseCensusOptions(cx, census, options , rootType), "!JS::ubi::ParseCensusOptions(cx, census, options, rootType)" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 444)) { | |||
445 | rv.Throw(NS_ERROR_UNEXPECTED); | |||
446 | return; | |||
447 | } | |||
448 | ||||
449 | JS::ubi::RootedCount rootCount(cx, rootType->makeCount()); | |||
450 | if (NS_WARN_IF(!rootCount)NS_warn_if_impl(!rootCount, "!rootCount", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 450)) { | |||
451 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
452 | return; | |||
453 | } | |||
454 | ||||
455 | JS::ubi::CensusHandler handler(census, rootCount, | |||
456 | GetCurrentThreadDebuggerMallocSizeOf()); | |||
457 | ||||
458 | { | |||
459 | JS::AutoCheckCannotGC nogc; | |||
460 | ||||
461 | JS::ubi::CensusTraversal traversal(cx, handler, nogc); | |||
462 | ||||
463 | if (NS_WARN_IF(!traversal.addStart(getRoot()))NS_warn_if_impl(!traversal.addStart(getRoot()), "!traversal.addStart(getRoot())" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 463)) { | |||
464 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
465 | return; | |||
466 | } | |||
467 | ||||
468 | if (NS_WARN_IF(!traversal.traverse())NS_warn_if_impl(!traversal.traverse(), "!traversal.traverse()" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 468)) { | |||
469 | rv.Throw(NS_ERROR_UNEXPECTED); | |||
470 | return; | |||
471 | } | |||
472 | } | |||
473 | ||||
474 | if (NS_WARN_IF(!handler.report(cx, rval))NS_warn_if_impl(!handler.report(cx, rval), "!handler.report(cx, rval)" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 474)) { | |||
475 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
476 | return; | |||
477 | } | |||
478 | } | |||
479 | ||||
480 | void HeapSnapshot::DescribeNode(JSContext* cx, JS::Handle<JSObject*> breakdown, | |||
481 | uint64_t nodeId, | |||
482 | JS::MutableHandle<JS::Value> rval, | |||
483 | ErrorResult& rv) { | |||
484 | MOZ_ASSERT(breakdown)do { static_assert( mozilla::detail::AssertionConditionType< decltype(breakdown)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(breakdown))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("breakdown", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 484); AnnotateMozCrashReason("MOZ_ASSERT" "(" "breakdown" ")" ); do { *((volatile int*)__null) = 484; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
485 | JS::Rooted<JS::Value> breakdownVal(cx, JS::ObjectValue(*breakdown)); | |||
486 | JS::Rooted<JS::GCVector<JSLinearString*>> seen(cx, cx); | |||
487 | JS::ubi::CountTypePtr rootType = | |||
488 | JS::ubi::ParseBreakdown(cx, breakdownVal, &seen); | |||
489 | if (NS_WARN_IF(!rootType)NS_warn_if_impl(!rootType, "!rootType", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 489)) { | |||
490 | rv.Throw(NS_ERROR_UNEXPECTED); | |||
491 | return; | |||
492 | } | |||
493 | ||||
494 | JS::ubi::RootedCount rootCount(cx, rootType->makeCount()); | |||
495 | if (NS_WARN_IF(!rootCount)NS_warn_if_impl(!rootCount, "!rootCount", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 495)) { | |||
496 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
497 | return; | |||
498 | } | |||
499 | ||||
500 | JS::ubi::Node::Id id(nodeId); | |||
501 | Maybe<JS::ubi::Node> node = getNodeById(id); | |||
502 | if (NS_WARN_IF(node.isNothing())NS_warn_if_impl(node.isNothing(), "node.isNothing()", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 502)) { | |||
503 | rv.Throw(NS_ERROR_INVALID_ARG); | |||
504 | return; | |||
505 | } | |||
506 | ||||
507 | MallocSizeOf mallocSizeOf = GetCurrentThreadDebuggerMallocSizeOf(); | |||
508 | if (NS_WARN_IF(!rootCount->count(mallocSizeOf, *node))NS_warn_if_impl(!rootCount->count(mallocSizeOf, *node), "!rootCount->count(mallocSizeOf, *node)" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 508)) { | |||
509 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
510 | return; | |||
511 | } | |||
512 | ||||
513 | if (NS_WARN_IF(!rootCount->report(cx, rval))NS_warn_if_impl(!rootCount->report(cx, rval), "!rootCount->report(cx, rval)" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 513)) { | |||
514 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
515 | return; | |||
516 | } | |||
517 | } | |||
518 | ||||
519 | already_AddRefed<DominatorTree> HeapSnapshot::ComputeDominatorTree( | |||
520 | ErrorResult& rv) { | |||
521 | Maybe<JS::ubi::DominatorTree> maybeTree; | |||
522 | { | |||
523 | auto ccjscx = CycleCollectedJSContext::Get(); | |||
524 | MOZ_ASSERT(ccjscx)do { static_assert( mozilla::detail::AssertionConditionType< decltype(ccjscx)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(ccjscx))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("ccjscx", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 524); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ccjscx" ")") ; do { *((volatile int*)__null) = 524; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
525 | auto cx = ccjscx->Context(); | |||
526 | MOZ_ASSERT(cx)do { static_assert( mozilla::detail::AssertionConditionType< decltype(cx)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(cx))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("cx", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 526); AnnotateMozCrashReason("MOZ_ASSERT" "(" "cx" ")"); do { *((volatile int*)__null) = 526; __attribute__((nomerge)) :: abort(); } while (false); } } while (false); | |||
527 | JS::AutoCheckCannotGC nogc(cx); | |||
528 | maybeTree = JS::ubi::DominatorTree::Create(cx, nogc, getRoot()); | |||
529 | } | |||
530 | ||||
531 | if (NS_WARN_IF(maybeTree.isNothing())NS_warn_if_impl(maybeTree.isNothing(), "maybeTree.isNothing()" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 531)) { | |||
532 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
533 | return nullptr; | |||
534 | } | |||
535 | ||||
536 | return MakeAndAddRef<DominatorTree>(std::move(*maybeTree), this, mParent); | |||
537 | } | |||
538 | ||||
539 | void HeapSnapshot::ComputeShortestPaths(JSContext* cx, uint64_t start, | |||
540 | const Sequence<uint64_t>& targets, | |||
541 | uint64_t maxNumPaths, | |||
542 | JS::MutableHandle<JSObject*> results, | |||
543 | ErrorResult& rv) { | |||
544 | // First ensure that our inputs are valid. | |||
545 | ||||
546 | if (NS_WARN_IF(maxNumPaths == 0)NS_warn_if_impl(maxNumPaths == 0, "maxNumPaths == 0", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 546)) { | |||
| ||||
547 | rv.Throw(NS_ERROR_INVALID_ARG); | |||
548 | return; | |||
549 | } | |||
550 | ||||
551 | Maybe<JS::ubi::Node> startNode = getNodeById(start); | |||
552 | if (NS_WARN_IF(startNode.isNothing())NS_warn_if_impl(startNode.isNothing(), "startNode.isNothing()" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 552)) { | |||
553 | rv.Throw(NS_ERROR_INVALID_ARG); | |||
554 | return; | |||
555 | } | |||
556 | ||||
557 | if (NS_WARN_IF(targets.Length() == 0)NS_warn_if_impl(targets.Length() == 0, "targets.Length() == 0" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 557)) { | |||
558 | rv.Throw(NS_ERROR_INVALID_ARG); | |||
559 | return; | |||
560 | } | |||
561 | ||||
562 | // Aggregate the targets into a set and make sure that they exist in the heap | |||
563 | // snapshot. | |||
564 | ||||
565 | JS::ubi::NodeSet targetsSet; | |||
566 | ||||
567 | for (const auto& target : targets) { | |||
568 | Maybe<JS::ubi::Node> targetNode = getNodeById(target); | |||
569 | if (NS_WARN_IF(targetNode.isNothing())NS_warn_if_impl(targetNode.isNothing(), "targetNode.isNothing()" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 569)) { | |||
570 | rv.Throw(NS_ERROR_INVALID_ARG); | |||
571 | return; | |||
572 | } | |||
573 | ||||
574 | if (NS_WARN_IF(!targetsSet.put(*targetNode))NS_warn_if_impl(!targetsSet.put(*targetNode), "!targetsSet.put(*targetNode)" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 574)) { | |||
575 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
576 | return; | |||
577 | } | |||
578 | } | |||
579 | ||||
580 | // Walk the heap graph and find the shortest paths. | |||
581 | ||||
582 | Maybe<ShortestPaths> maybeShortestPaths; | |||
583 | { | |||
584 | JS::AutoCheckCannotGC nogc(cx); | |||
585 | maybeShortestPaths = ShortestPaths::Create( | |||
586 | cx, nogc, maxNumPaths, *startNode, std::move(targetsSet)); | |||
587 | } | |||
588 | ||||
589 | if (NS_WARN_IF(maybeShortestPaths.isNothing())NS_warn_if_impl(maybeShortestPaths.isNothing(), "maybeShortestPaths.isNothing()" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 589)) { | |||
590 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
591 | return; | |||
592 | } | |||
593 | ||||
594 | auto& shortestPaths = *maybeShortestPaths; | |||
595 | ||||
596 | // Convert the results into a Map object mapping target node IDs to arrays of | |||
597 | // paths found. | |||
598 | ||||
599 | JS::Rooted<JSObject*> resultsMap(cx, JS::NewMapObject(cx)); | |||
600 | if (NS_WARN_IF(!resultsMap)NS_warn_if_impl(!resultsMap, "!resultsMap", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 600)) { | |||
601 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
602 | return; | |||
603 | } | |||
604 | ||||
605 | for (auto iter = shortestPaths.targetIter(); !iter.done(); iter.next()) { | |||
606 | JS::Rooted<JS::Value> key(cx, JS::NumberValue(iter.get().identifier())); | |||
607 | JS::RootedVector<JS::Value> paths(cx); | |||
608 | ||||
609 | bool ok = shortestPaths.forEachPath(iter.get(), [&](JS::ubi::Path& path) { | |||
610 | JS::RootedVector<JS::Value> pathValues(cx); | |||
611 | ||||
612 | for (JS::ubi::BackEdge* edge : path) { | |||
613 | JS::Rooted<JSObject*> pathPart(cx, JS_NewPlainObject(cx)); | |||
614 | if (!pathPart) { | |||
615 | return false; | |||
616 | } | |||
617 | ||||
618 | JS::Rooted<JS::Value> predecessor( | |||
619 | cx, NumberValue(edge->predecessor().identifier())); | |||
620 | if (!JS_DefineProperty(cx, pathPart, "predecessor", predecessor, | |||
621 | JSPROP_ENUMERATE)) { | |||
622 | return false; | |||
623 | } | |||
624 | ||||
625 | JS::Rooted<JS::Value> edgeNameVal(cx, NullValue()); | |||
626 | if (edge->name()) { | |||
627 | JS::Rooted<JSString*> edgeName( | |||
628 | cx, JS_AtomizeUCString(cx, edge->name().get())); | |||
629 | if (!edgeName) { | |||
630 | return false; | |||
631 | } | |||
632 | edgeNameVal = StringValue(edgeName); | |||
633 | } | |||
634 | ||||
635 | if (!JS_DefineProperty(cx, pathPart, "edge", edgeNameVal, | |||
636 | JSPROP_ENUMERATE)) { | |||
637 | return false; | |||
638 | } | |||
639 | ||||
640 | if (!pathValues.append(ObjectValue(*pathPart))) { | |||
641 | return false; | |||
642 | } | |||
643 | } | |||
644 | ||||
645 | JS::Rooted<JSObject*> pathObj(cx, JS::NewArrayObject(cx, pathValues)); | |||
646 | return pathObj && paths.append(ObjectValue(*pathObj)); | |||
647 | }); | |||
648 | ||||
649 | if (NS_WARN_IF(!ok)NS_warn_if_impl(!ok, "!ok", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 649)) { | |||
650 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
651 | return; | |||
652 | } | |||
653 | ||||
654 | JS::Rooted<JSObject*> pathsArray(cx, JS::NewArrayObject(cx, paths)); | |||
655 | if (NS_WARN_IF(!pathsArray)NS_warn_if_impl(!pathsArray, "!pathsArray", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 655)) { | |||
656 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
657 | return; | |||
658 | } | |||
659 | ||||
660 | JS::Rooted<JS::Value> pathsVal(cx, ObjectValue(*pathsArray)); | |||
661 | if (NS_WARN_IF(!JS::MapSet(cx, resultsMap, key, pathsVal))NS_warn_if_impl(!JS::MapSet(cx, resultsMap, key, pathsVal), "!JS::MapSet(cx, resultsMap, key, pathsVal)" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 661)) { | |||
662 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
663 | return; | |||
664 | } | |||
665 | } | |||
666 | ||||
667 | results.set(resultsMap); | |||
668 | } | |||
669 | ||||
670 | /*** Saving Heap Snapshots ****************************************************/ | |||
671 | ||||
672 | // If we are only taking a snapshot of the heap affected by the given set of | |||
673 | // globals, find the set of compartments the globals are allocated | |||
674 | // within. Returns false on OOM failure. | |||
675 | static bool PopulateCompartmentsWithGlobals( | |||
676 | CompartmentSet& compartments, JS::HandleVector<JSObject*> globals) { | |||
677 | unsigned length = globals.length(); | |||
678 | for (unsigned i = 0; i < length; i++) { | |||
679 | if (!compartments.put(JS::GetCompartment(globals[i]))) return false; | |||
680 | } | |||
681 | ||||
682 | return true; | |||
683 | } | |||
684 | ||||
685 | // Add the given set of globals as explicit roots in the given roots | |||
686 | // list. Returns false on OOM failure. | |||
687 | static bool AddGlobalsAsRoots(JS::HandleVector<JSObject*> globals, | |||
688 | ubi::RootList& roots) { | |||
689 | unsigned length = globals.length(); | |||
690 | for (unsigned i = 0; i < length; i++) { | |||
691 | if (!roots.addRoot(ubi::Node(globals[i].get()), u"heap snapshot global")) { | |||
692 | return false; | |||
693 | } | |||
694 | } | |||
695 | return true; | |||
696 | } | |||
697 | ||||
698 | // Choose roots and limits for a traversal, given `boundaries`. Set `roots` to | |||
699 | // the set of nodes within the boundaries that are referred to by nodes | |||
700 | // outside. If `boundaries` does not include all JS compartments, initialize | |||
701 | // `compartments` to the set of included compartments; otherwise, leave | |||
702 | // `compartments` uninitialized. (You can use compartments.initialized() to | |||
703 | // check.) | |||
704 | // | |||
705 | // If `boundaries` is incoherent, or we encounter an error while trying to | |||
706 | // handle it, or we run out of memory, set `rv` appropriately and return | |||
707 | // `false`. | |||
708 | // | |||
709 | // Return value is a pair of the status and an AutoCheckCannotGC token, | |||
710 | // forwarded from ubi::RootList::init(), to ensure that the caller does | |||
711 | // not GC while the RootList is live and initialized. | |||
712 | static std::pair<bool, AutoCheckCannotGC> EstablishBoundaries( | |||
713 | JSContext* cx, ErrorResult& rv, const HeapSnapshotBoundaries& boundaries, | |||
714 | ubi::RootList& roots, CompartmentSet& compartments) { | |||
715 | MOZ_ASSERT(!roots.initialized())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!roots.initialized())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!roots.initialized()))), 0)) ) { do { } while (false); MOZ_ReportAssertionFailure("!roots.initialized()" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 715); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!roots.initialized()" ")"); do { *((volatile int*)__null) = 715; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
716 | MOZ_ASSERT(compartments.empty())do { static_assert( mozilla::detail::AssertionConditionType< decltype(compartments.empty())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(compartments.empty()))), 0)) ) { do { } while (false); MOZ_ReportAssertionFailure("compartments.empty()" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 716); AnnotateMozCrashReason("MOZ_ASSERT" "(" "compartments.empty()" ")"); do { *((volatile int*)__null) = 716; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
717 | ||||
718 | bool foundBoundaryProperty = false; | |||
719 | ||||
720 | if (boundaries.mRuntime.WasPassed()) { | |||
721 | foundBoundaryProperty = true; | |||
722 | ||||
723 | if (!boundaries.mRuntime.Value()) { | |||
724 | rv.Throw(NS_ERROR_INVALID_ARG); | |||
725 | return {false, AutoCheckCannotGC(cx)}; | |||
726 | } | |||
727 | ||||
728 | auto [ok, nogc] = roots.init(); | |||
729 | if (!ok) { | |||
730 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
731 | return {false, nogc}; | |||
732 | } | |||
733 | } | |||
734 | ||||
735 | if (boundaries.mDebugger.WasPassed()) { | |||
736 | if (foundBoundaryProperty) { | |||
737 | rv.Throw(NS_ERROR_INVALID_ARG); | |||
738 | return {false, AutoCheckCannotGC(cx)}; | |||
739 | } | |||
740 | foundBoundaryProperty = true; | |||
741 | ||||
742 | JSObject* dbgObj = boundaries.mDebugger.Value(); | |||
743 | if (!dbgObj || !dbg::IsDebugger(*dbgObj)) { | |||
744 | rv.Throw(NS_ERROR_INVALID_ARG); | |||
745 | return {false, AutoCheckCannotGC(cx)}; | |||
746 | } | |||
747 | ||||
748 | JS::RootedVector<JSObject*> globals(cx); | |||
749 | if (!dbg::GetDebuggeeGlobals(cx, *dbgObj, &globals) || | |||
750 | !PopulateCompartmentsWithGlobals(compartments, globals) || | |||
751 | !roots.init(compartments).first || !AddGlobalsAsRoots(globals, roots)) { | |||
752 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
753 | return {false, AutoCheckCannotGC(cx)}; | |||
754 | } | |||
755 | } | |||
756 | ||||
757 | if (boundaries.mGlobals.WasPassed()) { | |||
758 | if (foundBoundaryProperty) { | |||
759 | rv.Throw(NS_ERROR_INVALID_ARG); | |||
760 | return {false, AutoCheckCannotGC(cx)}; | |||
761 | } | |||
762 | foundBoundaryProperty = true; | |||
763 | ||||
764 | uint32_t length = boundaries.mGlobals.Value().Length(); | |||
765 | if (length == 0) { | |||
766 | rv.Throw(NS_ERROR_INVALID_ARG); | |||
767 | return {false, AutoCheckCannotGC(cx)}; | |||
768 | } | |||
769 | ||||
770 | JS::RootedVector<JSObject*> globals(cx); | |||
771 | for (uint32_t i = 0; i < length; i++) { | |||
772 | JSObject* global = boundaries.mGlobals.Value().ElementAt(i); | |||
773 | if (!JS_IsGlobalObject(global)) { | |||
774 | rv.Throw(NS_ERROR_INVALID_ARG); | |||
775 | return {false, AutoCheckCannotGC(cx)}; | |||
776 | } | |||
777 | if (!globals.append(global)) { | |||
778 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
779 | return {false, AutoCheckCannotGC(cx)}; | |||
780 | } | |||
781 | } | |||
782 | ||||
783 | if (!PopulateCompartmentsWithGlobals(compartments, globals) || | |||
784 | !roots.init(compartments).first || !AddGlobalsAsRoots(globals, roots)) { | |||
785 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); | |||
786 | return {false, AutoCheckCannotGC(cx)}; | |||
787 | } | |||
788 | } | |||
789 | AutoCheckCannotGC nogc(cx); | |||
790 | ||||
791 | if (!foundBoundaryProperty) { | |||
792 | rv.Throw(NS_ERROR_INVALID_ARG); | |||
793 | return {false, nogc}; | |||
794 | } | |||
795 | ||||
796 | MOZ_ASSERT(roots.initialized())do { static_assert( mozilla::detail::AssertionConditionType< decltype(roots.initialized())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(roots.initialized()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("roots.initialized()" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 796); AnnotateMozCrashReason("MOZ_ASSERT" "(" "roots.initialized()" ")"); do { *((volatile int*)__null) = 796; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
797 | return {true, nogc}; | |||
798 | } | |||
799 | ||||
800 | // A variant covering all the various two-byte strings that we can get from the | |||
801 | // ubi::Node API. | |||
802 | class TwoByteString | |||
803 | : public Variant<JSAtom*, const char16_t*, JS::ubi::EdgeName> { | |||
804 | using Base = Variant<JSAtom*, const char16_t*, JS::ubi::EdgeName>; | |||
805 | ||||
806 | struct CopyToBufferMatcher { | |||
807 | RangedPtr<char16_t> destination; | |||
808 | size_t maxLength; | |||
809 | ||||
810 | CopyToBufferMatcher(RangedPtr<char16_t> destination, size_t maxLength) | |||
811 | : destination(destination), maxLength(maxLength) {} | |||
812 | ||||
813 | size_t operator()(JS::ubi::EdgeName& ptr) { | |||
814 | return ptr ? operator()(ptr.get()) : 0; | |||
815 | } | |||
816 | ||||
817 | size_t operator()(JSAtom* atom) { | |||
818 | MOZ_ASSERT(atom)do { static_assert( mozilla::detail::AssertionConditionType< decltype(atom)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(atom))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("atom", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 818); AnnotateMozCrashReason("MOZ_ASSERT" "(" "atom" ")"); do { *((volatile int*)__null) = 818; __attribute__((nomerge)) :: abort(); } while (false); } } while (false); | |||
819 | JS::ubi::AtomOrTwoByteChars s(atom); | |||
820 | return s.copyToBuffer(destination, maxLength); | |||
821 | } | |||
822 | ||||
823 | size_t operator()(const char16_t* chars) { | |||
824 | MOZ_ASSERT(chars)do { static_assert( mozilla::detail::AssertionConditionType< decltype(chars)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(chars))), 0))) { do { } while ( false); MOZ_ReportAssertionFailure("chars", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 824); AnnotateMozCrashReason("MOZ_ASSERT" "(" "chars" ")"); do { *((volatile int*)__null) = 824; __attribute__((nomerge) ) ::abort(); } while (false); } } while (false); | |||
825 | JS::ubi::AtomOrTwoByteChars s(chars); | |||
826 | return s.copyToBuffer(destination, maxLength); | |||
827 | } | |||
828 | }; | |||
829 | ||||
830 | public: | |||
831 | template <typename T> | |||
832 | MOZ_IMPLICIT TwoByteString(T&& rhs) : Base(std::forward<T>(rhs)) {} | |||
833 | ||||
834 | template <typename T> | |||
835 | TwoByteString& operator=(T&& rhs) { | |||
836 | MOZ_ASSERT(this != &rhs, "self-move disallowed")do { static_assert( mozilla::detail::AssertionConditionType< decltype(this != &rhs)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(this != &rhs))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("this != &rhs" " (" "self-move disallowed" ")", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 836); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this != &rhs" ") (" "self-move disallowed" ")"); do { *((volatile int*)__null ) = 836; __attribute__((nomerge)) ::abort(); } while (false); } } while (false); | |||
837 | this->~TwoByteString(); | |||
838 | new (this) TwoByteString(std::forward<T>(rhs)); | |||
839 | return *this; | |||
840 | } | |||
841 | ||||
842 | TwoByteString(const TwoByteString&) = delete; | |||
843 | TwoByteString& operator=(const TwoByteString&) = delete; | |||
844 | ||||
845 | // Rewrap the inner value of a JS::ubi::AtomOrTwoByteChars as a TwoByteString. | |||
846 | static TwoByteString from(JS::ubi::AtomOrTwoByteChars&& s) { | |||
847 | return s.match([](auto* a) { return TwoByteString(a); }); | |||
848 | } | |||
849 | ||||
850 | // Returns true if the given TwoByteString is non-null, false otherwise. | |||
851 | bool isNonNull() const { | |||
852 | return match([](auto& t) { return t != nullptr; }); | |||
853 | } | |||
854 | ||||
855 | // Return the length of the string, 0 if it is null. | |||
856 | size_t length() const { | |||
857 | return match( | |||
858 | [](JSAtom* atom) -> size_t { | |||
859 | MOZ_ASSERT(atom)do { static_assert( mozilla::detail::AssertionConditionType< decltype(atom)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(atom))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("atom", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 859); AnnotateMozCrashReason("MOZ_ASSERT" "(" "atom" ")"); do { *((volatile int*)__null) = 859; __attribute__((nomerge)) :: abort(); } while (false); } } while (false); | |||
860 | JS::ubi::AtomOrTwoByteChars s(atom); | |||
861 | return s.length(); | |||
862 | }, | |||
863 | [](const char16_t* chars) -> size_t { | |||
864 | MOZ_ASSERT(chars)do { static_assert( mozilla::detail::AssertionConditionType< decltype(chars)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(chars))), 0))) { do { } while ( false); MOZ_ReportAssertionFailure("chars", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 864); AnnotateMozCrashReason("MOZ_ASSERT" "(" "chars" ")"); do { *((volatile int*)__null) = 864; __attribute__((nomerge) ) ::abort(); } while (false); } } while (false); | |||
865 | return NS_strlen(chars); | |||
866 | }, | |||
867 | [](const JS::ubi::EdgeName& ptr) -> size_t { | |||
868 | MOZ_ASSERT(ptr)do { static_assert( mozilla::detail::AssertionConditionType< decltype(ptr)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("ptr", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 868); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ")"); do { *((volatile int*)__null) = 868; __attribute__((nomerge)) :: abort(); } while (false); } } while (false); | |||
869 | return NS_strlen(ptr.get()); | |||
870 | }); | |||
871 | } | |||
872 | ||||
873 | // Copy the contents of a TwoByteString into the provided buffer. The buffer | |||
874 | // is NOT null terminated. The number of characters written is returned. | |||
875 | size_t copyToBuffer(RangedPtr<char16_t> destination, size_t maxLength) { | |||
876 | CopyToBufferMatcher m(destination, maxLength); | |||
877 | return match(m); | |||
878 | } | |||
879 | ||||
880 | struct HashPolicy; | |||
881 | }; | |||
882 | ||||
883 | // A hashing policy for TwoByteString. | |||
884 | // | |||
885 | // Atoms are pointer hashed and use pointer equality, which means that we | |||
886 | // tolerate some duplication across atoms and the other two types of two-byte | |||
887 | // strings. In practice, we expect the amount of this duplication to be very low | |||
888 | // because each type is generally a different semantic thing in addition to | |||
889 | // having a slightly different representation. For example, the set of edge | |||
890 | // names and the set stack frames' source names naturally tend not to overlap | |||
891 | // very much if at all. | |||
892 | struct TwoByteString::HashPolicy { | |||
893 | using Lookup = TwoByteString; | |||
894 | ||||
895 | static js::HashNumber hash(const Lookup& l) { | |||
896 | return l.match( | |||
897 | [](const JSAtom* atom) { | |||
898 | return js::DefaultHasher<const JSAtom*>::hash(atom); | |||
899 | }, | |||
900 | [](const char16_t* chars) { | |||
901 | MOZ_ASSERT(chars)do { static_assert( mozilla::detail::AssertionConditionType< decltype(chars)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(chars))), 0))) { do { } while ( false); MOZ_ReportAssertionFailure("chars", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 901); AnnotateMozCrashReason("MOZ_ASSERT" "(" "chars" ")"); do { *((volatile int*)__null) = 901; __attribute__((nomerge) ) ::abort(); } while (false); } } while (false); | |||
902 | auto length = NS_strlen(chars); | |||
903 | return HashString(chars, length); | |||
904 | }, | |||
905 | [](const JS::ubi::EdgeName& ptr) { | |||
906 | const char16_t* chars = ptr.get(); | |||
907 | MOZ_ASSERT(chars)do { static_assert( mozilla::detail::AssertionConditionType< decltype(chars)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(chars))), 0))) { do { } while ( false); MOZ_ReportAssertionFailure("chars", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 907); AnnotateMozCrashReason("MOZ_ASSERT" "(" "chars" ")"); do { *((volatile int*)__null) = 907; __attribute__((nomerge) ) ::abort(); } while (false); } } while (false); | |||
908 | auto length = NS_strlen(chars); | |||
909 | return HashString(chars, length); | |||
910 | }); | |||
911 | } | |||
912 | ||||
913 | struct EqualityMatcher { | |||
914 | const TwoByteString& rhs; | |||
915 | explicit EqualityMatcher(const TwoByteString& rhs) : rhs(rhs) {} | |||
916 | ||||
917 | bool operator()(const JSAtom* atom) { | |||
918 | return rhs.is<JSAtom*>() && rhs.as<JSAtom*>() == atom; | |||
919 | } | |||
920 | ||||
921 | bool operator()(const char16_t* chars) { | |||
922 | MOZ_ASSERT(chars)do { static_assert( mozilla::detail::AssertionConditionType< decltype(chars)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(chars))), 0))) { do { } while ( false); MOZ_ReportAssertionFailure("chars", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 922); AnnotateMozCrashReason("MOZ_ASSERT" "(" "chars" ")"); do { *((volatile int*)__null) = 922; __attribute__((nomerge) ) ::abort(); } while (false); } } while (false); | |||
923 | ||||
924 | const char16_t* rhsChars = nullptr; | |||
925 | if (rhs.is<const char16_t*>()) | |||
926 | rhsChars = rhs.as<const char16_t*>(); | |||
927 | else if (rhs.is<JS::ubi::EdgeName>()) | |||
928 | rhsChars = rhs.as<JS::ubi::EdgeName>().get(); | |||
929 | else | |||
930 | return false; | |||
931 | MOZ_ASSERT(rhsChars)do { static_assert( mozilla::detail::AssertionConditionType< decltype(rhsChars)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(rhsChars))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("rhsChars", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 931); AnnotateMozCrashReason("MOZ_ASSERT" "(" "rhsChars" ")" ); do { *((volatile int*)__null) = 931; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
932 | ||||
933 | auto length = NS_strlen(chars); | |||
934 | if (NS_strlen(rhsChars) != length) return false; | |||
935 | ||||
936 | return memcmp(chars, rhsChars, length * sizeof(char16_t)) == 0; | |||
937 | } | |||
938 | ||||
939 | bool operator()(const JS::ubi::EdgeName& ptr) { | |||
940 | MOZ_ASSERT(ptr)do { static_assert( mozilla::detail::AssertionConditionType< decltype(ptr)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("ptr", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 940); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ")"); do { *((volatile int*)__null) = 940; __attribute__((nomerge)) :: abort(); } while (false); } } while (false); | |||
941 | return operator()(ptr.get()); | |||
942 | } | |||
943 | }; | |||
944 | ||||
945 | static bool match(const TwoByteString& k, const Lookup& l) { | |||
946 | EqualityMatcher eq(l); | |||
947 | return k.match(eq); | |||
948 | } | |||
949 | ||||
950 | static void rekey(TwoByteString& k, TwoByteString&& newKey) { | |||
951 | k = std::move(newKey); | |||
952 | } | |||
953 | }; | |||
954 | ||||
955 | // Returns whether `edge` should be included in a heap snapshot of | |||
956 | // `compartments`. The optional `policy` out-param is set to INCLUDE_EDGES | |||
957 | // if we want to include the referent's edges, or EXCLUDE_EDGES if we don't | |||
958 | // want to include them. | |||
959 | static bool ShouldIncludeEdge(JS::CompartmentSet* compartments, | |||
960 | const ubi::Node& origin, const ubi::Edge& edge, | |||
961 | CoreDumpWriter::EdgePolicy* policy = nullptr) { | |||
962 | if (policy) { | |||
963 | *policy = CoreDumpWriter::INCLUDE_EDGES; | |||
964 | } | |||
965 | ||||
966 | if (!compartments) { | |||
967 | // We aren't targeting a particular set of compartments, so serialize all | |||
968 | // the things! | |||
969 | return true; | |||
970 | } | |||
971 | ||||
972 | // We are targeting a particular set of compartments. If this node is in our | |||
973 | // target set, serialize it and all of its edges. If this node is _not_ in our | |||
974 | // target set, we also serialize under the assumption that it is a shared | |||
975 | // resource being used by something in our target compartments since we | |||
976 | // reached it by traversing the heap graph. However, we do not serialize its | |||
977 | // outgoing edges and we abandon further traversal from this node. | |||
978 | // | |||
979 | // If the node does not belong to any compartment, we also serialize its | |||
980 | // outgoing edges. This case is relevant for Shapes: they don't belong to a | |||
981 | // specific compartment and contain edges to parent/kids Shapes we want to | |||
982 | // include. Note that these Shapes may contain pointers into our target | |||
983 | // compartment (the Shape's getter/setter JSObjects). However, we do not | |||
984 | // serialize nodes in other compartments that are reachable from these | |||
985 | // non-compartment nodes. | |||
986 | ||||
987 | JS::Compartment* compartment = edge.referent.compartment(); | |||
988 | ||||
989 | if (!compartment || compartments->has(compartment)) { | |||
990 | return true; | |||
991 | } | |||
992 | ||||
993 | if (policy) { | |||
994 | *policy = CoreDumpWriter::EXCLUDE_EDGES; | |||
995 | } | |||
996 | ||||
997 | return !!origin.compartment(); | |||
998 | } | |||
999 | ||||
1000 | // A `CoreDumpWriter` that serializes nodes to protobufs and writes them to the | |||
1001 | // given `ZeroCopyOutputStream`. | |||
1002 | class MOZ_STACK_CLASS StreamWriter : public CoreDumpWriter { | |||
1003 | using FrameSet = js::HashSet<uint64_t>; | |||
1004 | using TwoByteStringMap = | |||
1005 | js::HashMap<TwoByteString, uint64_t, TwoByteString::HashPolicy>; | |||
1006 | using OneByteStringMap = js::HashMap<const char*, uint64_t>; | |||
1007 | ||||
1008 | JSContext* cx; | |||
1009 | bool wantNames; | |||
1010 | // The set of |JS::ubi::StackFrame::identifier()|s that have already been | |||
1011 | // serialized and written to the core dump. | |||
1012 | FrameSet framesAlreadySerialized; | |||
1013 | // The set of two-byte strings that have already been serialized and written | |||
1014 | // to the core dump. | |||
1015 | TwoByteStringMap twoByteStringsAlreadySerialized; | |||
1016 | // The set of one-byte strings that have already been serialized and written | |||
1017 | // to the core dump. | |||
1018 | OneByteStringMap oneByteStringsAlreadySerialized; | |||
1019 | ||||
1020 | ::google::protobuf::io::ZeroCopyOutputStream& stream; | |||
1021 | ||||
1022 | JS::CompartmentSet* compartments; | |||
1023 | ||||
1024 | bool writeMessage(const ::google::protobuf::MessageLite& message) { | |||
1025 | // We have to create a new CodedOutputStream when writing each message so | |||
1026 | // that the 64MB size limit used by Coded{Output,Input}Stream to prevent | |||
1027 | // integer overflow is enforced per message rather than on the whole stream. | |||
1028 | ::google::protobuf::io::CodedOutputStream codedStream(&stream); | |||
1029 | codedStream.WriteVarint32(message.ByteSizeLong()); | |||
1030 | message.SerializeWithCachedSizes(&codedStream); | |||
1031 | return !codedStream.HadError(); | |||
1032 | } | |||
1033 | ||||
1034 | // Attach the full two-byte string or a reference to a two-byte string that | |||
1035 | // has already been serialized to a protobuf message. | |||
1036 | template <typename SetStringFunction, typename SetRefFunction> | |||
1037 | bool attachTwoByteString(TwoByteString& string, SetStringFunction setString, | |||
1038 | SetRefFunction setRef) { | |||
1039 | auto ptr = twoByteStringsAlreadySerialized.lookupForAdd(string); | |||
1040 | if (ptr) { | |||
1041 | setRef(ptr->value()); | |||
1042 | return true; | |||
1043 | } | |||
1044 | ||||
1045 | auto length = string.length(); | |||
1046 | auto stringData = MakeUnique<std::string>(length * sizeof(char16_t), '\0'); | |||
1047 | if (!stringData) return false; | |||
1048 | ||||
1049 | auto buf = const_cast<char16_t*>( | |||
1050 | reinterpret_cast<const char16_t*>(stringData->data())); | |||
1051 | string.copyToBuffer(RangedPtr<char16_t>(buf, length), length); | |||
1052 | ||||
1053 | uint64_t ref = twoByteStringsAlreadySerialized.count(); | |||
1054 | if (!twoByteStringsAlreadySerialized.add(ptr, std::move(string), ref)) | |||
1055 | return false; | |||
1056 | ||||
1057 | setString(stringData.release()); | |||
1058 | return true; | |||
1059 | } | |||
1060 | ||||
1061 | // Attach the full one-byte string or a reference to a one-byte string that | |||
1062 | // has already been serialized to a protobuf message. | |||
1063 | template <typename SetStringFunction, typename SetRefFunction> | |||
1064 | bool attachOneByteString(const char* string, SetStringFunction setString, | |||
1065 | SetRefFunction setRef) { | |||
1066 | auto ptr = oneByteStringsAlreadySerialized.lookupForAdd(string); | |||
1067 | if (ptr) { | |||
1068 | setRef(ptr->value()); | |||
1069 | return true; | |||
1070 | } | |||
1071 | ||||
1072 | auto length = strlen(string); | |||
1073 | auto stringData = MakeUnique<std::string>(string, length); | |||
1074 | if (!stringData) return false; | |||
1075 | ||||
1076 | uint64_t ref = oneByteStringsAlreadySerialized.count(); | |||
1077 | if (!oneByteStringsAlreadySerialized.add(ptr, string, ref)) return false; | |||
1078 | ||||
1079 | setString(stringData.release()); | |||
1080 | return true; | |||
1081 | } | |||
1082 | ||||
1083 | protobuf::StackFrame* getProtobufStackFrame(JS::ubi::StackFrame& frame, | |||
1084 | size_t depth = 1) { | |||
1085 | // NB: de-duplicated string properties must be written in the same order | |||
1086 | // here as they are read in `HeapSnapshot::saveStackFrame` or else indices | |||
1087 | // in references to already serialized strings will be off. | |||
1088 | ||||
1089 | MOZ_ASSERT(frame,do { static_assert( mozilla::detail::AssertionConditionType< decltype(frame)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(frame))), 0))) { do { } while ( false); MOZ_ReportAssertionFailure("frame" " (" "null frames should be represented as the lack of a serialized " "stack frame" ")", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1091); AnnotateMozCrashReason("MOZ_ASSERT" "(" "frame" ") (" "null frames should be represented as the lack of a serialized " "stack frame" ")"); do { *((volatile int*)__null) = 1091; __attribute__ ((nomerge)) ::abort(); } while (false); } } while (false) | |||
1090 | "null frames should be represented as the lack of a serialized "do { static_assert( mozilla::detail::AssertionConditionType< decltype(frame)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(frame))), 0))) { do { } while ( false); MOZ_ReportAssertionFailure("frame" " (" "null frames should be represented as the lack of a serialized " "stack frame" ")", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1091); AnnotateMozCrashReason("MOZ_ASSERT" "(" "frame" ") (" "null frames should be represented as the lack of a serialized " "stack frame" ")"); do { *((volatile int*)__null) = 1091; __attribute__ ((nomerge)) ::abort(); } while (false); } } while (false) | |||
1091 | "stack frame")do { static_assert( mozilla::detail::AssertionConditionType< decltype(frame)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(frame))), 0))) { do { } while ( false); MOZ_ReportAssertionFailure("frame" " (" "null frames should be represented as the lack of a serialized " "stack frame" ")", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1091); AnnotateMozCrashReason("MOZ_ASSERT" "(" "frame" ") (" "null frames should be represented as the lack of a serialized " "stack frame" ")"); do { *((volatile int*)__null) = 1091; __attribute__ ((nomerge)) ::abort(); } while (false); } } while (false); | |||
1092 | ||||
1093 | auto id = frame.identifier(); | |||
1094 | auto protobufStackFrame = MakeUnique<protobuf::StackFrame>(); | |||
1095 | if (!protobufStackFrame) return nullptr; | |||
1096 | ||||
1097 | if (framesAlreadySerialized.has(id)) { | |||
1098 | protobufStackFrame->set_ref(id); | |||
1099 | return protobufStackFrame.release(); | |||
1100 | } | |||
1101 | ||||
1102 | auto data = MakeUnique<protobuf::StackFrame_Data>(); | |||
1103 | if (!data) return nullptr; | |||
1104 | ||||
1105 | data->set_id(id); | |||
1106 | data->set_line(frame.line()); | |||
1107 | data->set_column(frame.column().oneOriginValue()); | |||
1108 | data->set_issystem(frame.isSystem()); | |||
1109 | data->set_isselfhosted(frame.isSelfHosted(cx)); | |||
1110 | ||||
1111 | auto dupeSource = TwoByteString::from(frame.source()); | |||
1112 | if (!attachTwoByteString( | |||
1113 | dupeSource, | |||
1114 | [&](std::string* source) { data->set_allocated_source(source); }, | |||
1115 | [&](uint64_t ref) { data->set_sourceref(ref); })) { | |||
1116 | return nullptr; | |||
1117 | } | |||
1118 | ||||
1119 | auto dupeName = TwoByteString::from(frame.functionDisplayName()); | |||
1120 | if (dupeName.isNonNull()) { | |||
1121 | if (!attachTwoByteString( | |||
1122 | dupeName, | |||
1123 | [&](std::string* name) { | |||
1124 | data->set_allocated_functiondisplayname(name); | |||
1125 | }, | |||
1126 | [&](uint64_t ref) { data->set_functiondisplaynameref(ref); })) { | |||
1127 | return nullptr; | |||
1128 | } | |||
1129 | } | |||
1130 | ||||
1131 | auto parent = frame.parent(); | |||
1132 | if (parent && depth < HeapSnapshot::MAX_STACK_DEPTH) { | |||
1133 | auto protobufParent = getProtobufStackFrame(parent, depth + 1); | |||
1134 | if (!protobufParent) return nullptr; | |||
1135 | data->set_allocated_parent(protobufParent); | |||
1136 | } | |||
1137 | ||||
1138 | protobufStackFrame->set_allocated_data(data.release()); | |||
1139 | ||||
1140 | if (!framesAlreadySerialized.put(id)) return nullptr; | |||
1141 | ||||
1142 | return protobufStackFrame.release(); | |||
1143 | } | |||
1144 | ||||
1145 | public: | |||
1146 | StreamWriter(JSContext* cx, | |||
1147 | ::google::protobuf::io::ZeroCopyOutputStream& stream, | |||
1148 | bool wantNames, JS::CompartmentSet* compartments) | |||
1149 | : cx(cx), | |||
1150 | wantNames(wantNames), | |||
1151 | framesAlreadySerialized(cx), | |||
1152 | twoByteStringsAlreadySerialized(cx), | |||
1153 | oneByteStringsAlreadySerialized(cx), | |||
1154 | stream(stream), | |||
1155 | compartments(compartments) {} | |||
1156 | ||||
1157 | ~StreamWriter() override {} | |||
1158 | ||||
1159 | bool writeMetadata(uint64_t timestamp) final { | |||
1160 | protobuf::Metadata metadata; | |||
1161 | metadata.set_timestamp(timestamp); | |||
1162 | return writeMessage(metadata); | |||
1163 | } | |||
1164 | ||||
1165 | bool writeNode(const JS::ubi::Node& ubiNode, EdgePolicy includeEdges) final { | |||
1166 | // NB: de-duplicated string properties must be written in the same order | |||
1167 | // here as they are read in `HeapSnapshot::saveNode` or else indices in | |||
1168 | // references to already serialized strings will be off. | |||
1169 | ||||
1170 | protobuf::Node protobufNode; | |||
1171 | protobufNode.set_id(ubiNode.identifier()); | |||
1172 | ||||
1173 | protobufNode.set_coarsetype( | |||
1174 | JS::ubi::CoarseTypeToUint32(ubiNode.coarseType())); | |||
1175 | ||||
1176 | auto typeName = TwoByteString(ubiNode.typeName()); | |||
1177 | if (NS_WARN_IF(!attachTwoByteString(NS_warn_if_impl(!attachTwoByteString( typeName, [&](std:: string* name) { protobufNode.set_allocated_typename_(name); } , [&](uint64_t ref) { protobufNode.set_typenameref(ref); } ), "!attachTwoByteString( typeName, [&](std::string* name) { protobufNode.set_allocated_typename_(name); }, [&](uint64_t ref) { protobufNode.set_typenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1182) | |||
1178 | typeName,NS_warn_if_impl(!attachTwoByteString( typeName, [&](std:: string* name) { protobufNode.set_allocated_typename_(name); } , [&](uint64_t ref) { protobufNode.set_typenameref(ref); } ), "!attachTwoByteString( typeName, [&](std::string* name) { protobufNode.set_allocated_typename_(name); }, [&](uint64_t ref) { protobufNode.set_typenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1182) | |||
1179 | [&](std::string* name) {NS_warn_if_impl(!attachTwoByteString( typeName, [&](std:: string* name) { protobufNode.set_allocated_typename_(name); } , [&](uint64_t ref) { protobufNode.set_typenameref(ref); } ), "!attachTwoByteString( typeName, [&](std::string* name) { protobufNode.set_allocated_typename_(name); }, [&](uint64_t ref) { protobufNode.set_typenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1182) | |||
1180 | protobufNode.set_allocated_typename_(name);NS_warn_if_impl(!attachTwoByteString( typeName, [&](std:: string* name) { protobufNode.set_allocated_typename_(name); } , [&](uint64_t ref) { protobufNode.set_typenameref(ref); } ), "!attachTwoByteString( typeName, [&](std::string* name) { protobufNode.set_allocated_typename_(name); }, [&](uint64_t ref) { protobufNode.set_typenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1182) | |||
1181 | },NS_warn_if_impl(!attachTwoByteString( typeName, [&](std:: string* name) { protobufNode.set_allocated_typename_(name); } , [&](uint64_t ref) { protobufNode.set_typenameref(ref); } ), "!attachTwoByteString( typeName, [&](std::string* name) { protobufNode.set_allocated_typename_(name); }, [&](uint64_t ref) { protobufNode.set_typenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1182) | |||
1182 | [&](uint64_t ref) { protobufNode.set_typenameref(ref); }))NS_warn_if_impl(!attachTwoByteString( typeName, [&](std:: string* name) { protobufNode.set_allocated_typename_(name); } , [&](uint64_t ref) { protobufNode.set_typenameref(ref); } ), "!attachTwoByteString( typeName, [&](std::string* name) { protobufNode.set_allocated_typename_(name); }, [&](uint64_t ref) { protobufNode.set_typenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1182)) { | |||
1183 | return false; | |||
1184 | } | |||
1185 | ||||
1186 | mozilla::MallocSizeOf mallocSizeOf = dbg::GetDebuggerMallocSizeOf(cx); | |||
1187 | MOZ_ASSERT(mallocSizeOf)do { static_assert( mozilla::detail::AssertionConditionType< decltype(mallocSizeOf)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(mallocSizeOf))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("mallocSizeOf", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1187); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mallocSizeOf" ")"); do { *((volatile int*)__null) = 1187; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1188 | protobufNode.set_size(ubiNode.size(mallocSizeOf)); | |||
1189 | ||||
1190 | if (includeEdges) { | |||
1191 | auto edges = ubiNode.edges(cx, wantNames); | |||
1192 | if (NS_WARN_IF(!edges)NS_warn_if_impl(!edges, "!edges", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1192)) return false; | |||
1193 | ||||
1194 | for (; !edges->empty(); edges->popFront()) { | |||
1195 | ubi::Edge& ubiEdge = edges->front(); | |||
1196 | if (!ShouldIncludeEdge(compartments, ubiNode, ubiEdge)) { | |||
1197 | continue; | |||
1198 | } | |||
1199 | ||||
1200 | protobuf::Edge* protobufEdge = protobufNode.add_edges(); | |||
1201 | if (NS_WARN_IF(!protobufEdge)NS_warn_if_impl(!protobufEdge, "!protobufEdge", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1201)) { | |||
1202 | return false; | |||
1203 | } | |||
1204 | ||||
1205 | protobufEdge->set_referent(ubiEdge.referent.identifier()); | |||
1206 | ||||
1207 | if (wantNames && ubiEdge.name) { | |||
1208 | TwoByteString edgeName(std::move(ubiEdge.name)); | |||
1209 | if (NS_WARN_IF(!attachTwoByteString(NS_warn_if_impl(!attachTwoByteString( edgeName, [&](std:: string* name) { protobufEdge->set_allocated_name(name); }, [&](uint64_t ref) { protobufEdge->set_nameref(ref); } ), "!attachTwoByteString( edgeName, [&](std::string* name) { protobufEdge->set_allocated_name(name); }, [&](uint64_t ref) { protobufEdge->set_nameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1214) | |||
1210 | edgeName,NS_warn_if_impl(!attachTwoByteString( edgeName, [&](std:: string* name) { protobufEdge->set_allocated_name(name); }, [&](uint64_t ref) { protobufEdge->set_nameref(ref); } ), "!attachTwoByteString( edgeName, [&](std::string* name) { protobufEdge->set_allocated_name(name); }, [&](uint64_t ref) { protobufEdge->set_nameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1214) | |||
1211 | [&](std::string* name) {NS_warn_if_impl(!attachTwoByteString( edgeName, [&](std:: string* name) { protobufEdge->set_allocated_name(name); }, [&](uint64_t ref) { protobufEdge->set_nameref(ref); } ), "!attachTwoByteString( edgeName, [&](std::string* name) { protobufEdge->set_allocated_name(name); }, [&](uint64_t ref) { protobufEdge->set_nameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1214) | |||
1212 | protobufEdge->set_allocated_name(name);NS_warn_if_impl(!attachTwoByteString( edgeName, [&](std:: string* name) { protobufEdge->set_allocated_name(name); }, [&](uint64_t ref) { protobufEdge->set_nameref(ref); } ), "!attachTwoByteString( edgeName, [&](std::string* name) { protobufEdge->set_allocated_name(name); }, [&](uint64_t ref) { protobufEdge->set_nameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1214) | |||
1213 | },NS_warn_if_impl(!attachTwoByteString( edgeName, [&](std:: string* name) { protobufEdge->set_allocated_name(name); }, [&](uint64_t ref) { protobufEdge->set_nameref(ref); } ), "!attachTwoByteString( edgeName, [&](std::string* name) { protobufEdge->set_allocated_name(name); }, [&](uint64_t ref) { protobufEdge->set_nameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1214) | |||
1214 | [&](uint64_t ref) { protobufEdge->set_nameref(ref); }))NS_warn_if_impl(!attachTwoByteString( edgeName, [&](std:: string* name) { protobufEdge->set_allocated_name(name); }, [&](uint64_t ref) { protobufEdge->set_nameref(ref); } ), "!attachTwoByteString( edgeName, [&](std::string* name) { protobufEdge->set_allocated_name(name); }, [&](uint64_t ref) { protobufEdge->set_nameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1214)) { | |||
1215 | return false; | |||
1216 | } | |||
1217 | } | |||
1218 | } | |||
1219 | } | |||
1220 | ||||
1221 | if (ubiNode.hasAllocationStack()) { | |||
1222 | auto ubiStackFrame = ubiNode.allocationStack(); | |||
1223 | auto protoStackFrame = getProtobufStackFrame(ubiStackFrame); | |||
1224 | if (NS_WARN_IF(!protoStackFrame)NS_warn_if_impl(!protoStackFrame, "!protoStackFrame", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1224)) return false; | |||
1225 | protobufNode.set_allocated_allocationstack(protoStackFrame); | |||
1226 | } | |||
1227 | ||||
1228 | if (auto className = ubiNode.jsObjectClassName()) { | |||
1229 | if (NS_WARN_IF(!attachOneByteString(NS_warn_if_impl(!attachOneByteString( className, [&](std:: string* name) { protobufNode.set_allocated_jsobjectclassname( name); }, [&](uint64_t ref) { protobufNode.set_jsobjectclassnameref (ref); }), "!attachOneByteString( className, [&](std::string* name) { protobufNode.set_allocated_jsobjectclassname(name); }, [&](uint64_t ref) { protobufNode.set_jsobjectclassnameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1236) | |||
1230 | className,NS_warn_if_impl(!attachOneByteString( className, [&](std:: string* name) { protobufNode.set_allocated_jsobjectclassname( name); }, [&](uint64_t ref) { protobufNode.set_jsobjectclassnameref (ref); }), "!attachOneByteString( className, [&](std::string* name) { protobufNode.set_allocated_jsobjectclassname(name); }, [&](uint64_t ref) { protobufNode.set_jsobjectclassnameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1236) | |||
1231 | [&](std::string* name) {NS_warn_if_impl(!attachOneByteString( className, [&](std:: string* name) { protobufNode.set_allocated_jsobjectclassname( name); }, [&](uint64_t ref) { protobufNode.set_jsobjectclassnameref (ref); }), "!attachOneByteString( className, [&](std::string* name) { protobufNode.set_allocated_jsobjectclassname(name); }, [&](uint64_t ref) { protobufNode.set_jsobjectclassnameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1236) | |||
1232 | protobufNode.set_allocated_jsobjectclassname(name);NS_warn_if_impl(!attachOneByteString( className, [&](std:: string* name) { protobufNode.set_allocated_jsobjectclassname( name); }, [&](uint64_t ref) { protobufNode.set_jsobjectclassnameref (ref); }), "!attachOneByteString( className, [&](std::string* name) { protobufNode.set_allocated_jsobjectclassname(name); }, [&](uint64_t ref) { protobufNode.set_jsobjectclassnameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1236) | |||
1233 | },NS_warn_if_impl(!attachOneByteString( className, [&](std:: string* name) { protobufNode.set_allocated_jsobjectclassname( name); }, [&](uint64_t ref) { protobufNode.set_jsobjectclassnameref (ref); }), "!attachOneByteString( className, [&](std::string* name) { protobufNode.set_allocated_jsobjectclassname(name); }, [&](uint64_t ref) { protobufNode.set_jsobjectclassnameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1236) | |||
1234 | [&](uint64_t ref) {NS_warn_if_impl(!attachOneByteString( className, [&](std:: string* name) { protobufNode.set_allocated_jsobjectclassname( name); }, [&](uint64_t ref) { protobufNode.set_jsobjectclassnameref (ref); }), "!attachOneByteString( className, [&](std::string* name) { protobufNode.set_allocated_jsobjectclassname(name); }, [&](uint64_t ref) { protobufNode.set_jsobjectclassnameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1236) | |||
1235 | protobufNode.set_jsobjectclassnameref(ref);NS_warn_if_impl(!attachOneByteString( className, [&](std:: string* name) { protobufNode.set_allocated_jsobjectclassname( name); }, [&](uint64_t ref) { protobufNode.set_jsobjectclassnameref (ref); }), "!attachOneByteString( className, [&](std::string* name) { protobufNode.set_allocated_jsobjectclassname(name); }, [&](uint64_t ref) { protobufNode.set_jsobjectclassnameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1236) | |||
1236 | }))NS_warn_if_impl(!attachOneByteString( className, [&](std:: string* name) { protobufNode.set_allocated_jsobjectclassname( name); }, [&](uint64_t ref) { protobufNode.set_jsobjectclassnameref (ref); }), "!attachOneByteString( className, [&](std::string* name) { protobufNode.set_allocated_jsobjectclassname(name); }, [&](uint64_t ref) { protobufNode.set_jsobjectclassnameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1236)) { | |||
1237 | return false; | |||
1238 | } | |||
1239 | } | |||
1240 | ||||
1241 | if (auto scriptFilename = ubiNode.scriptFilename()) { | |||
1242 | if (NS_WARN_IF(!attachOneByteString(NS_warn_if_impl(!attachOneByteString( scriptFilename, [&] (std::string* name) { protobufNode.set_allocated_scriptfilename (name); }, [&](uint64_t ref) { protobufNode.set_scriptfilenameref (ref); }), "!attachOneByteString( scriptFilename, [&](std::string* name) { protobufNode.set_allocated_scriptfilename(name); }, [&](uint64_t ref) { protobufNode.set_scriptfilenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1249) | |||
1243 | scriptFilename,NS_warn_if_impl(!attachOneByteString( scriptFilename, [&] (std::string* name) { protobufNode.set_allocated_scriptfilename (name); }, [&](uint64_t ref) { protobufNode.set_scriptfilenameref (ref); }), "!attachOneByteString( scriptFilename, [&](std::string* name) { protobufNode.set_allocated_scriptfilename(name); }, [&](uint64_t ref) { protobufNode.set_scriptfilenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1249) | |||
1244 | [&](std::string* name) {NS_warn_if_impl(!attachOneByteString( scriptFilename, [&] (std::string* name) { protobufNode.set_allocated_scriptfilename (name); }, [&](uint64_t ref) { protobufNode.set_scriptfilenameref (ref); }), "!attachOneByteString( scriptFilename, [&](std::string* name) { protobufNode.set_allocated_scriptfilename(name); }, [&](uint64_t ref) { protobufNode.set_scriptfilenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1249) | |||
1245 | protobufNode.set_allocated_scriptfilename(name);NS_warn_if_impl(!attachOneByteString( scriptFilename, [&] (std::string* name) { protobufNode.set_allocated_scriptfilename (name); }, [&](uint64_t ref) { protobufNode.set_scriptfilenameref (ref); }), "!attachOneByteString( scriptFilename, [&](std::string* name) { protobufNode.set_allocated_scriptfilename(name); }, [&](uint64_t ref) { protobufNode.set_scriptfilenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1249) | |||
1246 | },NS_warn_if_impl(!attachOneByteString( scriptFilename, [&] (std::string* name) { protobufNode.set_allocated_scriptfilename (name); }, [&](uint64_t ref) { protobufNode.set_scriptfilenameref (ref); }), "!attachOneByteString( scriptFilename, [&](std::string* name) { protobufNode.set_allocated_scriptfilename(name); }, [&](uint64_t ref) { protobufNode.set_scriptfilenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1249) | |||
1247 | [&](uint64_t ref) {NS_warn_if_impl(!attachOneByteString( scriptFilename, [&] (std::string* name) { protobufNode.set_allocated_scriptfilename (name); }, [&](uint64_t ref) { protobufNode.set_scriptfilenameref (ref); }), "!attachOneByteString( scriptFilename, [&](std::string* name) { protobufNode.set_allocated_scriptfilename(name); }, [&](uint64_t ref) { protobufNode.set_scriptfilenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1249) | |||
1248 | protobufNode.set_scriptfilenameref(ref);NS_warn_if_impl(!attachOneByteString( scriptFilename, [&] (std::string* name) { protobufNode.set_allocated_scriptfilename (name); }, [&](uint64_t ref) { protobufNode.set_scriptfilenameref (ref); }), "!attachOneByteString( scriptFilename, [&](std::string* name) { protobufNode.set_allocated_scriptfilename(name); }, [&](uint64_t ref) { protobufNode.set_scriptfilenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1249) | |||
1249 | }))NS_warn_if_impl(!attachOneByteString( scriptFilename, [&] (std::string* name) { protobufNode.set_allocated_scriptfilename (name); }, [&](uint64_t ref) { protobufNode.set_scriptfilenameref (ref); }), "!attachOneByteString( scriptFilename, [&](std::string* name) { protobufNode.set_allocated_scriptfilename(name); }, [&](uint64_t ref) { protobufNode.set_scriptfilenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1249)) { | |||
1250 | return false; | |||
1251 | } | |||
1252 | } | |||
1253 | ||||
1254 | if (ubiNode.descriptiveTypeName()) { | |||
1255 | auto descriptiveTypeName = TwoByteString(ubiNode.descriptiveTypeName()); | |||
1256 | if (NS_WARN_IF(!attachTwoByteString(NS_warn_if_impl(!attachTwoByteString( descriptiveTypeName, [& ](std::string* name) { protobufNode.set_allocated_descriptivetypename (name); }, [&](uint64_t ref) { protobufNode.set_descriptivetypenameref (ref); }), "!attachTwoByteString( descriptiveTypeName, [&](std::string* name) { protobufNode.set_allocated_descriptivetypename(name); }, [&](uint64_t ref) { protobufNode.set_descriptivetypenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1263) | |||
1257 | descriptiveTypeName,NS_warn_if_impl(!attachTwoByteString( descriptiveTypeName, [& ](std::string* name) { protobufNode.set_allocated_descriptivetypename (name); }, [&](uint64_t ref) { protobufNode.set_descriptivetypenameref (ref); }), "!attachTwoByteString( descriptiveTypeName, [&](std::string* name) { protobufNode.set_allocated_descriptivetypename(name); }, [&](uint64_t ref) { protobufNode.set_descriptivetypenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1263) | |||
1258 | [&](std::string* name) {NS_warn_if_impl(!attachTwoByteString( descriptiveTypeName, [& ](std::string* name) { protobufNode.set_allocated_descriptivetypename (name); }, [&](uint64_t ref) { protobufNode.set_descriptivetypenameref (ref); }), "!attachTwoByteString( descriptiveTypeName, [&](std::string* name) { protobufNode.set_allocated_descriptivetypename(name); }, [&](uint64_t ref) { protobufNode.set_descriptivetypenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1263) | |||
1259 | protobufNode.set_allocated_descriptivetypename(name);NS_warn_if_impl(!attachTwoByteString( descriptiveTypeName, [& ](std::string* name) { protobufNode.set_allocated_descriptivetypename (name); }, [&](uint64_t ref) { protobufNode.set_descriptivetypenameref (ref); }), "!attachTwoByteString( descriptiveTypeName, [&](std::string* name) { protobufNode.set_allocated_descriptivetypename(name); }, [&](uint64_t ref) { protobufNode.set_descriptivetypenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1263) | |||
1260 | },NS_warn_if_impl(!attachTwoByteString( descriptiveTypeName, [& ](std::string* name) { protobufNode.set_allocated_descriptivetypename (name); }, [&](uint64_t ref) { protobufNode.set_descriptivetypenameref (ref); }), "!attachTwoByteString( descriptiveTypeName, [&](std::string* name) { protobufNode.set_allocated_descriptivetypename(name); }, [&](uint64_t ref) { protobufNode.set_descriptivetypenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1263) | |||
1261 | [&](uint64_t ref) {NS_warn_if_impl(!attachTwoByteString( descriptiveTypeName, [& ](std::string* name) { protobufNode.set_allocated_descriptivetypename (name); }, [&](uint64_t ref) { protobufNode.set_descriptivetypenameref (ref); }), "!attachTwoByteString( descriptiveTypeName, [&](std::string* name) { protobufNode.set_allocated_descriptivetypename(name); }, [&](uint64_t ref) { protobufNode.set_descriptivetypenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1263) | |||
1262 | protobufNode.set_descriptivetypenameref(ref);NS_warn_if_impl(!attachTwoByteString( descriptiveTypeName, [& ](std::string* name) { protobufNode.set_allocated_descriptivetypename (name); }, [&](uint64_t ref) { protobufNode.set_descriptivetypenameref (ref); }), "!attachTwoByteString( descriptiveTypeName, [&](std::string* name) { protobufNode.set_allocated_descriptivetypename(name); }, [&](uint64_t ref) { protobufNode.set_descriptivetypenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1263) | |||
1263 | }))NS_warn_if_impl(!attachTwoByteString( descriptiveTypeName, [& ](std::string* name) { protobufNode.set_allocated_descriptivetypename (name); }, [&](uint64_t ref) { protobufNode.set_descriptivetypenameref (ref); }), "!attachTwoByteString( descriptiveTypeName, [&](std::string* name) { protobufNode.set_allocated_descriptivetypename(name); }, [&](uint64_t ref) { protobufNode.set_descriptivetypenameref(ref); })" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1263)) { | |||
1264 | return false; | |||
1265 | } | |||
1266 | } | |||
1267 | ||||
1268 | return writeMessage(protobufNode); | |||
1269 | } | |||
1270 | }; | |||
1271 | ||||
1272 | // A JS::ubi::BreadthFirst handler that serializes a snapshot of the heap into a | |||
1273 | // core dump. | |||
1274 | class MOZ_STACK_CLASS HeapSnapshotHandler { | |||
1275 | CoreDumpWriter& writer; | |||
1276 | JS::CompartmentSet* compartments; | |||
1277 | ||||
1278 | public: | |||
1279 | // For telemetry. | |||
1280 | uint32_t nodeCount; | |||
1281 | uint32_t edgeCount; | |||
1282 | ||||
1283 | HeapSnapshotHandler(CoreDumpWriter& writer, JS::CompartmentSet* compartments) | |||
1284 | : writer(writer), | |||
1285 | compartments(compartments), | |||
1286 | nodeCount(0), | |||
1287 | edgeCount(0) {} | |||
1288 | ||||
1289 | // JS::ubi::BreadthFirst handler interface. | |||
1290 | ||||
1291 | class NodeData {}; | |||
1292 | typedef JS::ubi::BreadthFirst<HeapSnapshotHandler> Traversal; | |||
1293 | bool operator()(Traversal& traversal, JS::ubi::Node origin, | |||
1294 | const JS::ubi::Edge& edge, NodeData*, bool first) { | |||
1295 | edgeCount++; | |||
1296 | ||||
1297 | // We're only interested in the first time we reach edge.referent, not in | |||
1298 | // every edge arriving at that node. "But, don't we want to serialize every | |||
1299 | // edge in the heap graph?" you ask. Don't worry! This edge is still | |||
1300 | // serialized into the core dump. Serializing a node also serializes each of | |||
1301 | // its edges, and if we are traversing a given edge, we must have already | |||
1302 | // visited and serialized the origin node and its edges. | |||
1303 | if (!first) return true; | |||
1304 | ||||
1305 | CoreDumpWriter::EdgePolicy policy; | |||
1306 | if (!ShouldIncludeEdge(compartments, origin, edge, &policy)) { | |||
1307 | // Because ShouldIncludeEdge considers the |origin| node as well, we don't | |||
1308 | // want to consider this node 'visited' until we write it to the core | |||
1309 | // dump. | |||
1310 | traversal.doNotMarkReferentAsVisited(); | |||
1311 | return true; | |||
1312 | } | |||
1313 | ||||
1314 | nodeCount++; | |||
1315 | ||||
1316 | if (policy == CoreDumpWriter::EXCLUDE_EDGES) traversal.abandonReferent(); | |||
1317 | ||||
1318 | return writer.writeNode(edge.referent, policy); | |||
1319 | } | |||
1320 | }; | |||
1321 | ||||
1322 | bool WriteHeapGraph(JSContext* cx, const JS::ubi::Node& node, | |||
1323 | CoreDumpWriter& writer, bool wantNames, | |||
1324 | JS::CompartmentSet* compartments, | |||
1325 | JS::AutoCheckCannotGC& noGC, uint32_t& outNodeCount, | |||
1326 | uint32_t& outEdgeCount) { | |||
1327 | // Serialize the starting node to the core dump. | |||
1328 | ||||
1329 | if (NS_WARN_IF(!writer.writeNode(node, CoreDumpWriter::INCLUDE_EDGES))NS_warn_if_impl(!writer.writeNode(node, CoreDumpWriter::INCLUDE_EDGES ), "!writer.writeNode(node, CoreDumpWriter::INCLUDE_EDGES)", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1329)) { | |||
1330 | return false; | |||
1331 | } | |||
1332 | ||||
1333 | // Walk the heap graph starting from the given node and serialize it into the | |||
1334 | // core dump. | |||
1335 | ||||
1336 | HeapSnapshotHandler handler(writer, compartments); | |||
1337 | HeapSnapshotHandler::Traversal traversal(cx, handler, noGC); | |||
1338 | traversal.wantNames = wantNames; | |||
1339 | ||||
1340 | bool ok = traversal.addStartVisited(node) && traversal.traverse(); | |||
1341 | ||||
1342 | if (ok) { | |||
1343 | outNodeCount = handler.nodeCount; | |||
1344 | outEdgeCount = handler.edgeCount; | |||
1345 | } | |||
1346 | ||||
1347 | return ok; | |||
1348 | } | |||
1349 | ||||
1350 | static unsigned long msSinceProcessCreation(const TimeStamp& now) { | |||
1351 | auto duration = now - TimeStamp::ProcessCreation(); | |||
1352 | return (unsigned long)duration.ToMilliseconds(); | |||
1353 | } | |||
1354 | ||||
1355 | /* static */ | |||
1356 | already_AddRefed<nsIFile> HeapSnapshot::CreateUniqueCoreDumpFile( | |||
1357 | ErrorResult& rv, const TimeStamp& now, nsAString& outFilePath, | |||
1358 | nsAString& outSnapshotId) { | |||
1359 | MOZ_RELEASE_ASSERT(XRE_IsParentProcess())do { static_assert( mozilla::detail::AssertionConditionType< decltype(XRE_IsParentProcess())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(XRE_IsParentProcess()))), 0) )) { do { } while (false); MOZ_ReportAssertionFailure("XRE_IsParentProcess()" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1359); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "XRE_IsParentProcess()" ")"); do { *((volatile int*)__null) = 1359; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | |||
1360 | nsCOMPtr<nsIFile> file; | |||
1361 | rv = GetSpecialSystemDirectory(OS_TemporaryDirectory, getter_AddRefs(file)); | |||
1362 | if (NS_WARN_IF(rv.Failed())NS_warn_if_impl(rv.Failed(), "rv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1362)) return nullptr; | |||
1363 | ||||
1364 | nsAutoString tempPath; | |||
1365 | rv = file->GetPath(tempPath); | |||
1366 | if (NS_WARN_IF(rv.Failed())NS_warn_if_impl(rv.Failed(), "rv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1366)) return nullptr; | |||
1367 | ||||
1368 | auto ms = msSinceProcessCreation(now); | |||
1369 | rv = file->AppendNative(nsPrintfCString("%lu.fxsnapshot", ms)); | |||
1370 | if (NS_WARN_IF(rv.Failed())NS_warn_if_impl(rv.Failed(), "rv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1370)) return nullptr; | |||
1371 | ||||
1372 | rv = file->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0666); | |||
1373 | if (NS_WARN_IF(rv.Failed())NS_warn_if_impl(rv.Failed(), "rv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1373)) return nullptr; | |||
1374 | ||||
1375 | rv = file->GetPath(outFilePath); | |||
1376 | if (NS_WARN_IF(rv.Failed())NS_warn_if_impl(rv.Failed(), "rv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1376)) return nullptr; | |||
1377 | ||||
1378 | // The snapshot ID must be computed in the process that created the | |||
1379 | // temp file, because TmpD may not be the same in all processes. | |||
1380 | outSnapshotId.Assign(Substring( | |||
1381 | outFilePath, tempPath.Length() + 1, | |||
1382 | outFilePath.Length() - tempPath.Length() - sizeof(".fxsnapshot"))); | |||
1383 | ||||
1384 | return file.forget(); | |||
1385 | } | |||
1386 | ||||
1387 | // Deletion policy for cleaning up PHeapSnapshotTempFileHelperChild pointers. | |||
1388 | class DeleteHeapSnapshotTempFileHelperChild { | |||
1389 | public: | |||
1390 | constexpr DeleteHeapSnapshotTempFileHelperChild() {} | |||
1391 | ||||
1392 | void operator()(PHeapSnapshotTempFileHelperChild* ptr) const { | |||
1393 | Unused << NS_WARN_IF(!HeapSnapshotTempFileHelperChild::Send__delete__(ptr))NS_warn_if_impl(!HeapSnapshotTempFileHelperChild::Send__delete__ (ptr), "!HeapSnapshotTempFileHelperChild::Send__delete__(ptr)" , "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1393); | |||
1394 | } | |||
1395 | }; | |||
1396 | ||||
1397 | // A UniquePtr alias to automatically manage PHeapSnapshotTempFileHelperChild | |||
1398 | // pointers. | |||
1399 | using UniqueHeapSnapshotTempFileHelperChild = | |||
1400 | UniquePtr<PHeapSnapshotTempFileHelperChild, | |||
1401 | DeleteHeapSnapshotTempFileHelperChild>; | |||
1402 | ||||
1403 | // Get an nsIOutputStream that we can write the heap snapshot to. In non-e10s | |||
1404 | // and in the e10s parent process, open a file directly and create an output | |||
1405 | // stream for it. In e10s child processes, we are sandboxed without access to | |||
1406 | // the filesystem. Use IPDL to request a file descriptor from the parent | |||
1407 | // process. | |||
1408 | static already_AddRefed<nsIOutputStream> getCoreDumpOutputStream( | |||
1409 | ErrorResult& rv, TimeStamp& start, nsAString& outFilePath, | |||
1410 | nsAString& outSnapshotId) { | |||
1411 | if (XRE_IsParentProcess()) { | |||
1412 | // Create the file and open the output stream directly. | |||
1413 | ||||
1414 | nsCOMPtr<nsIFile> file = HeapSnapshot::CreateUniqueCoreDumpFile( | |||
1415 | rv, start, outFilePath, outSnapshotId); | |||
1416 | if (NS_WARN_IF(rv.Failed())NS_warn_if_impl(rv.Failed(), "rv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1416)) return nullptr; | |||
1417 | ||||
1418 | nsCOMPtr<nsIOutputStream> outputStream; | |||
1419 | rv = NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), file, | |||
1420 | PR_WRONLY0x02, -1, 0); | |||
1421 | if (NS_WARN_IF(rv.Failed())NS_warn_if_impl(rv.Failed(), "rv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1421)) return nullptr; | |||
1422 | ||||
1423 | return outputStream.forget(); | |||
1424 | } | |||
1425 | // Request a file descriptor from the parent process over IPDL. | |||
1426 | ||||
1427 | auto cc = ContentChild::GetSingleton(); | |||
1428 | if (!cc) { | |||
1429 | rv.Throw(NS_ERROR_UNEXPECTED); | |||
1430 | return nullptr; | |||
1431 | } | |||
1432 | ||||
1433 | UniqueHeapSnapshotTempFileHelperChild helper( | |||
1434 | cc->SendPHeapSnapshotTempFileHelperConstructor()); | |||
1435 | if (NS_WARN_IF(!helper)NS_warn_if_impl(!helper, "!helper", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1435)) { | |||
1436 | rv.Throw(NS_ERROR_UNEXPECTED); | |||
1437 | return nullptr; | |||
1438 | } | |||
1439 | ||||
1440 | OpenHeapSnapshotTempFileResponse response; | |||
1441 | if (!helper->SendOpenHeapSnapshotTempFile(&response)) { | |||
1442 | rv.Throw(NS_ERROR_UNEXPECTED); | |||
1443 | return nullptr; | |||
1444 | } | |||
1445 | if (response.type() == OpenHeapSnapshotTempFileResponse::Tnsresult) { | |||
1446 | rv.Throw(response.get_nsresult()); | |||
1447 | return nullptr; | |||
1448 | } | |||
1449 | ||||
1450 | auto opened = response.get_OpenedFile(); | |||
1451 | outFilePath = opened.path(); | |||
1452 | outSnapshotId = opened.snapshotId(); | |||
1453 | nsCOMPtr<nsIOutputStream> outputStream = | |||
1454 | FileDescriptorOutputStream::Create(opened.descriptor()); | |||
1455 | if (NS_WARN_IF(!outputStream)NS_warn_if_impl(!outputStream, "!outputStream", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1455)) { | |||
1456 | rv.Throw(NS_ERROR_UNEXPECTED); | |||
1457 | return nullptr; | |||
1458 | } | |||
1459 | ||||
1460 | return outputStream.forget(); | |||
1461 | } | |||
1462 | ||||
1463 | } // namespace devtools | |||
1464 | ||||
1465 | namespace dom { | |||
1466 | ||||
1467 | using namespace JS; | |||
1468 | using namespace devtools; | |||
1469 | ||||
1470 | /* static */ | |||
1471 | void ChromeUtils::SaveHeapSnapshotShared( | |||
1472 | GlobalObject& global, const HeapSnapshotBoundaries& boundaries, | |||
1473 | nsAString& outFilePath, nsAString& outSnapshotId, ErrorResult& rv) { | |||
1474 | auto start = TimeStamp::Now(); | |||
1475 | ||||
1476 | bool wantNames = true; | |||
1477 | CompartmentSet compartments; | |||
1478 | uint32_t nodeCount = 0; | |||
1479 | uint32_t edgeCount = 0; | |||
1480 | ||||
1481 | nsCOMPtr<nsIOutputStream> outputStream = | |||
1482 | getCoreDumpOutputStream(rv, start, outFilePath, outSnapshotId); | |||
1483 | if (NS_WARN_IF(rv.Failed())NS_warn_if_impl(rv.Failed(), "rv.Failed()", "/var/lib/jenkins/workspace/firefox-scan-build/devtools/shared/heapsnapshot/HeapSnapshot.cpp" , 1483)) return; | |||
1484 | ||||
1485 | ZeroCopyNSIOutputStream zeroCopyStream(outputStream); | |||
1486 | ::google::protobuf::io::GzipOutputStream gzipStream(&zeroCopyStream); | |||
1487 | ||||
1488 | JSContext* cx = global.Context(); | |||
1489 | ||||
1490 | { | |||
1491 | ubi::RootList rootList(cx, wantNames); | |||
1492 | auto [ok, nogc] = | |||
1493 | EstablishBoundaries(cx, rv, boundaries, rootList, compartments); | |||
1494 | if (!ok) { | |||
1495 | return; | |||
1496 | } | |||
1497 | ||||
1498 | StreamWriter writer(cx, gzipStream, wantNames, | |||
1499 | !compartments.empty() ? &compartments : nullptr); | |||
1500 | ||||
1501 | ubi::Node roots(&rootList); | |||
1502 | ||||
1503 | // Serialize the initial heap snapshot metadata to the core dump. | |||
1504 | if (!writer.writeMetadata(PR_Now()) || | |||
1505 | // Serialize the heap graph to the core dump, starting from our list of | |||
1506 | // roots. | |||
1507 | !WriteHeapGraph(cx, roots, writer, wantNames, | |||
1508 | !compartments.empty() ? &compartments : nullptr, nogc, | |||
1509 | nodeCount, edgeCount)) { | |||
1510 | rv.Throw(zeroCopyStream.failed() ? zeroCopyStream.result() | |||
1511 | : NS_ERROR_UNEXPECTED); | |||
1512 | return; | |||
1513 | } | |||
1514 | } | |||
1515 | ||||
1516 | Telemetry::AccumulateTimeDelta(Telemetry::DEVTOOLS_SAVE_HEAP_SNAPSHOT_MS, | |||
1517 | start); | |||
1518 | Telemetry::Accumulate(Telemetry::DEVTOOLS_HEAP_SNAPSHOT_NODE_COUNT, | |||
1519 | nodeCount); | |||
1520 | Telemetry::Accumulate(Telemetry::DEVTOOLS_HEAP_SNAPSHOT_EDGE_COUNT, | |||
1521 | edgeCount); | |||
1522 | } | |||
1523 | ||||
1524 | /* static */ | |||
1525 | uint64_t ChromeUtils::GetObjectNodeId(GlobalObject& global, | |||
1526 | JS::Handle<JSObject*> val) { | |||
1527 | JS::Rooted<JSObject*> obj(global.Context(), val); | |||
1528 | ||||
1529 | JS::ubi::Node node(obj); | |||
1530 | return node.identifier(); | |||
1531 | } | |||
1532 | ||||
1533 | /* static */ | |||
1534 | void ChromeUtils::SaveHeapSnapshot(GlobalObject& global, | |||
1535 | const HeapSnapshotBoundaries& boundaries, | |||
1536 | nsAString& outFilePath, ErrorResult& rv) { | |||
1537 | nsAutoString snapshotId; | |||
1538 | SaveHeapSnapshotShared(global, boundaries, outFilePath, snapshotId, rv); | |||
1539 | } | |||
1540 | ||||
1541 | /* static */ | |||
1542 | void ChromeUtils::SaveHeapSnapshotGetId( | |||
1543 | GlobalObject& global, const HeapSnapshotBoundaries& boundaries, | |||
1544 | nsAString& outSnapshotId, ErrorResult& rv) { | |||
1545 | nsAutoString filePath; | |||
1546 | SaveHeapSnapshotShared(global, boundaries, filePath, outSnapshotId, rv); | |||
1547 | } | |||
1548 | ||||
1549 | /* static */ | |||
1550 | already_AddRefed<HeapSnapshot> ChromeUtils::ReadHeapSnapshot( | |||
1551 | GlobalObject& global, const nsAString& filePath, ErrorResult& rv) { | |||
1552 | auto start = TimeStamp::Now(); | |||
1553 | ||||
1554 | nsCOMPtr<nsIFile> snapshotFile; | |||
1555 | rv = NS_NewLocalFile(filePath, getter_AddRefs(snapshotFile)); | |||
1556 | if (rv.Failed()) { | |||
1557 | return nullptr; | |||
1558 | } | |||
1559 | ||||
1560 | AutoMemMap mm; | |||
1561 | rv = mm.init(snapshotFile); | |||
1562 | if (rv.Failed()) return nullptr; | |||
1563 | ||||
1564 | RefPtr<HeapSnapshot> snapshot = HeapSnapshot::Create( | |||
1565 | global.Context(), global, reinterpret_cast<const uint8_t*>(mm.address()), | |||
1566 | mm.size(), rv); | |||
1567 | ||||
1568 | if (!rv.Failed()) | |||
1569 | Telemetry::AccumulateTimeDelta(Telemetry::DEVTOOLS_READ_HEAP_SNAPSHOT_MS, | |||
1570 | start); | |||
1571 | ||||
1572 | return snapshot.forget(); | |||
1573 | } | |||
1574 | ||||
1575 | } // namespace dom | |||
1576 | } // namespace mozilla |
1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||||||||||||||
2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ | ||||||||||||||
3 | /* This Source Code Form is subject to the terms of the Mozilla Public | ||||||||||||||
4 | * License, v. 2.0. If a copy of the MPL was not distributed with this | ||||||||||||||
5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||||||||||||||
6 | |||||||||||||||
7 | /* A class for optional values and in-place lazy construction. */ | ||||||||||||||
8 | |||||||||||||||
9 | #ifndef mozilla_Maybe_h | ||||||||||||||
10 | #define mozilla_Maybe_h | ||||||||||||||
11 | |||||||||||||||
12 | #include <functional> | ||||||||||||||
13 | #include <new> // for placement new | ||||||||||||||
14 | #include <ostream> | ||||||||||||||
15 | #include <type_traits> | ||||||||||||||
16 | #include <utility> | ||||||||||||||
17 | |||||||||||||||
18 | #include "mozilla/Alignment.h" | ||||||||||||||
19 | #include "mozilla/Assertions.h" | ||||||||||||||
20 | #include "mozilla/Attributes.h" | ||||||||||||||
21 | #include "mozilla/MaybeStorageBase.h" | ||||||||||||||
22 | #include "mozilla/MemoryChecking.h" | ||||||||||||||
23 | #include "mozilla/OperatorNewExtensions.h" | ||||||||||||||
24 | #include "mozilla/Poison.h" | ||||||||||||||
25 | #include "mozilla/ThreadSafety.h" | ||||||||||||||
26 | |||||||||||||||
27 | class nsCycleCollectionTraversalCallback; | ||||||||||||||
28 | |||||||||||||||
29 | template <typename T> | ||||||||||||||
30 | inline void CycleCollectionNoteChild( | ||||||||||||||
31 | nsCycleCollectionTraversalCallback& aCallback, T* aChild, const char* aName, | ||||||||||||||
32 | uint32_t aFlags); | ||||||||||||||
33 | |||||||||||||||
34 | namespace mozilla { | ||||||||||||||
35 | |||||||||||||||
36 | struct Nothing {}; | ||||||||||||||
37 | |||||||||||||||
38 | inline constexpr bool operator==(const Nothing&, const Nothing&) { | ||||||||||||||
39 | return true; | ||||||||||||||
40 | } | ||||||||||||||
41 | |||||||||||||||
42 | template <class T> | ||||||||||||||
43 | class Maybe; | ||||||||||||||
44 | |||||||||||||||
45 | namespace detail { | ||||||||||||||
46 | |||||||||||||||
47 | // You would think that poisoning Maybe instances could just be a call | ||||||||||||||
48 | // to mozWritePoison. Unfortunately, using a simple call to | ||||||||||||||
49 | // mozWritePoison generates poor code on MSVC for small structures. The | ||||||||||||||
50 | // generated code contains (always not-taken) branches and does a bunch | ||||||||||||||
51 | // of setup for `rep stos{l,q}`, even though we know at compile time | ||||||||||||||
52 | // exactly how many words we're poisoning. Instead, we're going to | ||||||||||||||
53 | // force MSVC to generate the code we want via recursive templates. | ||||||||||||||
54 | |||||||||||||||
55 | // Write the given poisonValue into p at offset*sizeof(uintptr_t). | ||||||||||||||
56 | template <size_t offset> | ||||||||||||||
57 | inline void WritePoisonAtOffset(void* p, const uintptr_t poisonValue) { | ||||||||||||||
58 | memcpy(static_cast<char*>(p) + offset * sizeof(poisonValue), &poisonValue, | ||||||||||||||
59 | sizeof(poisonValue)); | ||||||||||||||
60 | } | ||||||||||||||
61 | |||||||||||||||
62 | template <size_t Offset, size_t NOffsets> | ||||||||||||||
63 | struct InlinePoisoner { | ||||||||||||||
64 | static void poison(void* p, const uintptr_t poisonValue) { | ||||||||||||||
65 | WritePoisonAtOffset<Offset>(p, poisonValue); | ||||||||||||||
66 | InlinePoisoner<Offset + 1, NOffsets>::poison(p, poisonValue); | ||||||||||||||
67 | } | ||||||||||||||
68 | }; | ||||||||||||||
69 | |||||||||||||||
70 | template <size_t N> | ||||||||||||||
71 | struct InlinePoisoner<N, N> { | ||||||||||||||
72 | static void poison(void*, const uintptr_t) { | ||||||||||||||
73 | // All done! | ||||||||||||||
74 | } | ||||||||||||||
75 | }; | ||||||||||||||
76 | |||||||||||||||
77 | // We can't generate inline code for large structures, though, because we'll | ||||||||||||||
78 | // blow out recursive template instantiation limits, and the code would be | ||||||||||||||
79 | // bloated to boot. So provide a fallback to the out-of-line poisoner. | ||||||||||||||
80 | template <size_t ObjectSize> | ||||||||||||||
81 | struct OutOfLinePoisoner { | ||||||||||||||
82 | static MOZ_NEVER_INLINE__attribute__((noinline)) void poison(void* p, const uintptr_t) { | ||||||||||||||
83 | mozWritePoison(p, ObjectSize); | ||||||||||||||
84 | } | ||||||||||||||
85 | }; | ||||||||||||||
86 | |||||||||||||||
87 | template <typename T> | ||||||||||||||
88 | inline void PoisonObject(T* p) { | ||||||||||||||
89 | const uintptr_t POISON = mozPoisonValue(); | ||||||||||||||
90 | std::conditional_t<(sizeof(T) <= 8 * sizeof(POISON)), | ||||||||||||||
91 | InlinePoisoner<0, sizeof(T) / sizeof(POISON)>, | ||||||||||||||
92 | OutOfLinePoisoner<sizeof(T)>>::poison(p, POISON); | ||||||||||||||
93 | } | ||||||||||||||
94 | |||||||||||||||
95 | template <typename T> | ||||||||||||||
96 | struct MaybePoisoner { | ||||||||||||||
97 | static const size_t N = sizeof(T); | ||||||||||||||
98 | |||||||||||||||
99 | static void poison(void* aPtr) { | ||||||||||||||
100 | #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED1 | ||||||||||||||
101 | if (N >= sizeof(uintptr_t)) { | ||||||||||||||
102 | PoisonObject(static_cast<std::remove_cv_t<T>*>(aPtr)); | ||||||||||||||
103 | } | ||||||||||||||
104 | #endif | ||||||||||||||
105 | MOZ_MAKE_MEM_UNDEFINED(aPtr, N)do { } while (0); | ||||||||||||||
106 | } | ||||||||||||||
107 | }; | ||||||||||||||
108 | |||||||||||||||
109 | template <typename T, | ||||||||||||||
110 | bool TriviallyDestructibleAndCopyable = | ||||||||||||||
111 | IsTriviallyDestructibleAndCopyable<T>, | ||||||||||||||
112 | bool Copyable = std::is_copy_constructible_v<T>, | ||||||||||||||
113 | bool Movable = std::is_move_constructible_v<T>> | ||||||||||||||
114 | class Maybe_CopyMove_Enabler; | ||||||||||||||
115 | |||||||||||||||
116 | #define MOZ_MAYBE_COPY_OPS() \ | ||||||||||||||
117 | Maybe_CopyMove_Enabler(const Maybe_CopyMove_Enabler& aOther) { \ | ||||||||||||||
118 | if (downcast(aOther).isSome()) { \ | ||||||||||||||
119 | downcast(*this).emplace(*downcast(aOther)); \ | ||||||||||||||
120 | } \ | ||||||||||||||
121 | } \ | ||||||||||||||
122 | \ | ||||||||||||||
123 | Maybe_CopyMove_Enabler& operator=(const Maybe_CopyMove_Enabler& aOther) { \ | ||||||||||||||
124 | return downcast(*this).template operator= <T>(downcast(aOther)); \ | ||||||||||||||
125 | } | ||||||||||||||
126 | |||||||||||||||
127 | #define MOZ_MAYBE_MOVE_OPS() \ | ||||||||||||||
128 | constexpr Maybe_CopyMove_Enabler(Maybe_CopyMove_Enabler&& aOther) { \ | ||||||||||||||
129 | if (downcast(aOther).isSome()) { \ | ||||||||||||||
130 | downcast(*this).emplace(std::move(*downcast(aOther))); \ | ||||||||||||||
131 | downcast(aOther).reset(); \ | ||||||||||||||
132 | } \ | ||||||||||||||
133 | } \ | ||||||||||||||
134 | \ | ||||||||||||||
135 | constexpr Maybe_CopyMove_Enabler& operator=( \ | ||||||||||||||
136 | Maybe_CopyMove_Enabler&& aOther) { \ | ||||||||||||||
137 | downcast(*this).template operator= <T>(std::move(downcast(aOther))); \ | ||||||||||||||
138 | \ | ||||||||||||||
139 | return *this; \ | ||||||||||||||
140 | } | ||||||||||||||
141 | |||||||||||||||
142 | #define MOZ_MAYBE_DOWNCAST() \ | ||||||||||||||
143 | static constexpr Maybe<T>& downcast(Maybe_CopyMove_Enabler& aObj) { \ | ||||||||||||||
144 | return static_cast<Maybe<T>&>(aObj); \ | ||||||||||||||
145 | } \ | ||||||||||||||
146 | static constexpr const Maybe<T>& downcast( \ | ||||||||||||||
147 | const Maybe_CopyMove_Enabler& aObj) { \ | ||||||||||||||
148 | return static_cast<const Maybe<T>&>(aObj); \ | ||||||||||||||
149 | } | ||||||||||||||
150 | |||||||||||||||
151 | template <typename T> | ||||||||||||||
152 | class Maybe_CopyMove_Enabler<T, true, true, true> { | ||||||||||||||
153 | public: | ||||||||||||||
154 | Maybe_CopyMove_Enabler() = default; | ||||||||||||||
155 | |||||||||||||||
156 | Maybe_CopyMove_Enabler(const Maybe_CopyMove_Enabler&) = default; | ||||||||||||||
157 | Maybe_CopyMove_Enabler& operator=(const Maybe_CopyMove_Enabler&) = default; | ||||||||||||||
158 | constexpr Maybe_CopyMove_Enabler(Maybe_CopyMove_Enabler&& aOther) { | ||||||||||||||
159 | downcast(aOther).reset(); | ||||||||||||||
160 | } | ||||||||||||||
161 | constexpr Maybe_CopyMove_Enabler& operator=(Maybe_CopyMove_Enabler&& aOther) { | ||||||||||||||
162 | downcast(aOther).reset(); | ||||||||||||||
163 | return *this; | ||||||||||||||
164 | } | ||||||||||||||
165 | |||||||||||||||
166 | private: | ||||||||||||||
167 | MOZ_MAYBE_DOWNCAST() | ||||||||||||||
168 | }; | ||||||||||||||
169 | |||||||||||||||
170 | template <typename T> | ||||||||||||||
171 | class Maybe_CopyMove_Enabler<T, true, false, true> { | ||||||||||||||
172 | public: | ||||||||||||||
173 | Maybe_CopyMove_Enabler() = default; | ||||||||||||||
174 | |||||||||||||||
175 | Maybe_CopyMove_Enabler(const Maybe_CopyMove_Enabler&) = delete; | ||||||||||||||
176 | Maybe_CopyMove_Enabler& operator=(const Maybe_CopyMove_Enabler&) = delete; | ||||||||||||||
177 | constexpr Maybe_CopyMove_Enabler(Maybe_CopyMove_Enabler&& aOther) { | ||||||||||||||
178 | downcast(aOther).reset(); | ||||||||||||||
179 | } | ||||||||||||||
180 | constexpr Maybe_CopyMove_Enabler& operator=(Maybe_CopyMove_Enabler&& aOther) { | ||||||||||||||
181 | downcast(aOther).reset(); | ||||||||||||||
182 | return *this; | ||||||||||||||
183 | } | ||||||||||||||
184 | |||||||||||||||
185 | private: | ||||||||||||||
186 | MOZ_MAYBE_DOWNCAST() | ||||||||||||||
187 | }; | ||||||||||||||
188 | |||||||||||||||
189 | template <typename T> | ||||||||||||||
190 | class Maybe_CopyMove_Enabler<T, false, true, true> { | ||||||||||||||
191 | public: | ||||||||||||||
192 | Maybe_CopyMove_Enabler() = default; | ||||||||||||||
193 | |||||||||||||||
194 | MOZ_MAYBE_COPY_OPS() | ||||||||||||||
195 | MOZ_MAYBE_MOVE_OPS() | ||||||||||||||
196 | |||||||||||||||
197 | private: | ||||||||||||||
198 | MOZ_MAYBE_DOWNCAST() | ||||||||||||||
199 | }; | ||||||||||||||
200 | |||||||||||||||
201 | template <typename T> | ||||||||||||||
202 | class Maybe_CopyMove_Enabler<T, false, false, true> { | ||||||||||||||
203 | public: | ||||||||||||||
204 | Maybe_CopyMove_Enabler() = default; | ||||||||||||||
205 | |||||||||||||||
206 | Maybe_CopyMove_Enabler(const Maybe_CopyMove_Enabler&) = delete; | ||||||||||||||
207 | Maybe_CopyMove_Enabler& operator=(const Maybe_CopyMove_Enabler&) = delete; | ||||||||||||||
208 | MOZ_MAYBE_MOVE_OPS() | ||||||||||||||
209 | |||||||||||||||
210 | private: | ||||||||||||||
211 | MOZ_MAYBE_DOWNCAST() | ||||||||||||||
212 | }; | ||||||||||||||
213 | |||||||||||||||
214 | template <typename T> | ||||||||||||||
215 | class Maybe_CopyMove_Enabler<T, false, true, false> { | ||||||||||||||
216 | public: | ||||||||||||||
217 | Maybe_CopyMove_Enabler() = default; | ||||||||||||||
218 | |||||||||||||||
219 | MOZ_MAYBE_COPY_OPS() | ||||||||||||||
220 | Maybe_CopyMove_Enabler(Maybe_CopyMove_Enabler&&) = delete; | ||||||||||||||
221 | Maybe_CopyMove_Enabler& operator=(Maybe_CopyMove_Enabler&&) = delete; | ||||||||||||||
222 | |||||||||||||||
223 | private: | ||||||||||||||
224 | MOZ_MAYBE_DOWNCAST() | ||||||||||||||
225 | }; | ||||||||||||||
226 | |||||||||||||||
227 | template <typename T, bool TriviallyDestructibleAndCopyable> | ||||||||||||||
228 | class Maybe_CopyMove_Enabler<T, TriviallyDestructibleAndCopyable, false, | ||||||||||||||
229 | false> { | ||||||||||||||
230 | public: | ||||||||||||||
231 | Maybe_CopyMove_Enabler() = default; | ||||||||||||||
232 | |||||||||||||||
233 | Maybe_CopyMove_Enabler(const Maybe_CopyMove_Enabler&) = delete; | ||||||||||||||
234 | Maybe_CopyMove_Enabler& operator=(const Maybe_CopyMove_Enabler&) = delete; | ||||||||||||||
235 | Maybe_CopyMove_Enabler(Maybe_CopyMove_Enabler&&) = delete; | ||||||||||||||
236 | Maybe_CopyMove_Enabler& operator=(Maybe_CopyMove_Enabler&&) = delete; | ||||||||||||||
237 | }; | ||||||||||||||
238 | |||||||||||||||
239 | #undef MOZ_MAYBE_COPY_OPS | ||||||||||||||
240 | #undef MOZ_MAYBE_MOVE_OPS | ||||||||||||||
241 | #undef MOZ_MAYBE_DOWNCAST | ||||||||||||||
242 | |||||||||||||||
243 | template <typename T, bool TriviallyDestructibleAndCopyable = | ||||||||||||||
244 | IsTriviallyDestructibleAndCopyable<T>> | ||||||||||||||
245 | struct MaybeStorage; | ||||||||||||||
246 | |||||||||||||||
247 | template <typename T> | ||||||||||||||
248 | struct MaybeStorage<T, false> : MaybeStorageBase<T> { | ||||||||||||||
249 | protected: | ||||||||||||||
250 | char mIsSome = false; // not bool -- guarantees minimal space consumption | ||||||||||||||
251 | |||||||||||||||
252 | constexpr MaybeStorage() = default; | ||||||||||||||
253 | explicit MaybeStorage(const T& aVal) | ||||||||||||||
254 | : MaybeStorageBase<T>{aVal}, mIsSome{true} {} | ||||||||||||||
255 | explicit MaybeStorage(T&& aVal) | ||||||||||||||
256 | : MaybeStorageBase<T>{std::move(aVal)}, mIsSome{true} {} | ||||||||||||||
257 | |||||||||||||||
258 | template <typename... Args> | ||||||||||||||
259 | explicit MaybeStorage(std::in_place_t, Args&&... aArgs) | ||||||||||||||
260 | : MaybeStorageBase<T>{std::in_place, std::forward<Args>(aArgs)...}, | ||||||||||||||
261 | mIsSome{true} {} | ||||||||||||||
262 | |||||||||||||||
263 | public: | ||||||||||||||
264 | // Copy and move operations are no-ops, since copying is moving is implemented | ||||||||||||||
265 | // by Maybe_CopyMove_Enabler. | ||||||||||||||
266 | |||||||||||||||
267 | MaybeStorage(const MaybeStorage&) : MaybeStorageBase<T>{} {} | ||||||||||||||
268 | MaybeStorage& operator=(const MaybeStorage&) { return *this; } | ||||||||||||||
269 | MaybeStorage(MaybeStorage&&) : MaybeStorageBase<T>{} {} | ||||||||||||||
270 | MaybeStorage& operator=(MaybeStorage&&) { return *this; } | ||||||||||||||
271 | |||||||||||||||
272 | ~MaybeStorage() { | ||||||||||||||
273 | if (mIsSome
| ||||||||||||||
274 | this->addr()->T::~T(); | ||||||||||||||
275 | } | ||||||||||||||
276 | } | ||||||||||||||
277 | }; | ||||||||||||||
278 | |||||||||||||||
279 | template <typename T> | ||||||||||||||
280 | struct MaybeStorage<T, true> : MaybeStorageBase<T> { | ||||||||||||||
281 | protected: | ||||||||||||||
282 | char mIsSome = false; // not bool -- guarantees minimal space consumption | ||||||||||||||
283 | |||||||||||||||
284 | constexpr MaybeStorage() = default; | ||||||||||||||
285 | constexpr explicit MaybeStorage(const T& aVal) | ||||||||||||||
286 | : MaybeStorageBase<T>{aVal}, mIsSome{true} {} | ||||||||||||||
287 | constexpr explicit MaybeStorage(T&& aVal) | ||||||||||||||
288 | : MaybeStorageBase<T>{std::move(aVal)}, mIsSome{true} {} | ||||||||||||||
289 | |||||||||||||||
290 | template <typename... Args> | ||||||||||||||
291 | constexpr explicit MaybeStorage(std::in_place_t, Args&&... aArgs) | ||||||||||||||
292 | : MaybeStorageBase<T>{std::in_place, std::forward<Args>(aArgs)...}, | ||||||||||||||
293 | mIsSome{true} {} | ||||||||||||||
294 | }; | ||||||||||||||
295 | |||||||||||||||
296 | template <typename T> | ||||||||||||||
297 | struct IsMaybeImpl : std::false_type {}; | ||||||||||||||
298 | |||||||||||||||
299 | template <typename T> | ||||||||||||||
300 | struct IsMaybeImpl<Maybe<T>> : std::true_type {}; | ||||||||||||||
301 | |||||||||||||||
302 | template <typename T> | ||||||||||||||
303 | using IsMaybe = IsMaybeImpl<std::decay_t<T>>; | ||||||||||||||
304 | |||||||||||||||
305 | } // namespace detail | ||||||||||||||
306 | |||||||||||||||
307 | template <typename T, typename U = typename std::remove_cv< | ||||||||||||||
308 | typename std::remove_reference<T>::type>::type> | ||||||||||||||
309 | constexpr Maybe<U> Some(T&& aValue); | ||||||||||||||
310 | |||||||||||||||
311 | /* | ||||||||||||||
312 | * Maybe is a container class which contains either zero or one elements. It | ||||||||||||||
313 | * serves two roles. It can represent values which are *semantically* optional, | ||||||||||||||
314 | * augmenting a type with an explicit 'Nothing' value. In this role, it provides | ||||||||||||||
315 | * methods that make it easy to work with values that may be missing, along with | ||||||||||||||
316 | * equality and comparison operators so that Maybe values can be stored in | ||||||||||||||
317 | * containers. Maybe values can be constructed conveniently in expressions using | ||||||||||||||
318 | * type inference, as follows: | ||||||||||||||
319 | * | ||||||||||||||
320 | * void doSomething(Maybe<Foo> aFoo) { | ||||||||||||||
321 | * if (aFoo) // Make sure that aFoo contains a value... | ||||||||||||||
322 | * aFoo->takeAction(); // and then use |aFoo->| to access it. | ||||||||||||||
323 | * } // |*aFoo| also works! | ||||||||||||||
324 | * | ||||||||||||||
325 | * doSomething(Nothing()); // Passes a Maybe<Foo> containing no value. | ||||||||||||||
326 | * doSomething(Some(Foo(100))); // Passes a Maybe<Foo> containing |Foo(100)|. | ||||||||||||||
327 | * | ||||||||||||||
328 | * You'll note that it's important to check whether a Maybe contains a value | ||||||||||||||
329 | * before using it, using conversion to bool, |isSome()|, or |isNothing()|. You | ||||||||||||||
330 | * can avoid these checks, and sometimes write more readable code, using | ||||||||||||||
331 | * |valueOr()|, |ptrOr()|, and |refOr()|, which allow you to retrieve the value | ||||||||||||||
332 | * in the Maybe and provide a default for the 'Nothing' case. You can also use | ||||||||||||||
333 | * |apply()| to call a function only if the Maybe holds a value, and |map()| to | ||||||||||||||
334 | * transform the value in the Maybe, returning another Maybe with a possibly | ||||||||||||||
335 | * different type. | ||||||||||||||
336 | * | ||||||||||||||
337 | * Maybe's other role is to support lazily constructing objects without using | ||||||||||||||
338 | * dynamic storage. A Maybe directly contains storage for a value, but it's | ||||||||||||||
339 | * empty by default. |emplace()|, as mentioned above, can be used to construct a | ||||||||||||||
340 | * value in Maybe's storage. The value a Maybe contains can be destroyed by | ||||||||||||||
341 | * calling |reset()|; this will happen automatically if a Maybe is destroyed | ||||||||||||||
342 | * while holding a value. | ||||||||||||||
343 | * | ||||||||||||||
344 | * It's a common idiom in C++ to use a pointer as a 'Maybe' type, with a null | ||||||||||||||
345 | * value meaning 'Nothing' and any other value meaning 'Some'. You can convert | ||||||||||||||
346 | * from such a pointer to a Maybe value using 'ToMaybe()'. | ||||||||||||||
347 | * | ||||||||||||||
348 | * Maybe is inspired by similar types in the standard library of many other | ||||||||||||||
349 | * languages (e.g. Haskell's Maybe and Rust's Option). In the C++ world it's | ||||||||||||||
350 | * very similar to std::optional, which was proposed for C++14 and originated in | ||||||||||||||
351 | * Boost. The most important differences between Maybe and std::optional are: | ||||||||||||||
352 | * | ||||||||||||||
353 | * - std::optional<T> may be compared with T. We deliberately forbid that. | ||||||||||||||
354 | * - std::optional has |valueOr()|, equivalent to Maybe's |valueOr()|, but | ||||||||||||||
355 | * lacks corresponding methods for |refOr()| and |ptrOr()|. | ||||||||||||||
356 | * - std::optional lacks |map()| and |apply()|, making it less suitable for | ||||||||||||||
357 | * functional-style code. | ||||||||||||||
358 | * - std::optional lacks many convenience functions that Maybe has. Most | ||||||||||||||
359 | * unfortunately, it lacks equivalents of the type-inferred constructor | ||||||||||||||
360 | * functions |Some()| and |Nothing()|. | ||||||||||||||
361 | */ | ||||||||||||||
362 | template <class T> | ||||||||||||||
363 | class MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS Maybe | ||||||||||||||
364 | : private detail::MaybeStorage<T>, | ||||||||||||||
365 | public detail::Maybe_CopyMove_Enabler<T> { | ||||||||||||||
366 | template <typename, bool, bool, bool> | ||||||||||||||
367 | friend class detail::Maybe_CopyMove_Enabler; | ||||||||||||||
368 | |||||||||||||||
369 | template <typename U, typename V> | ||||||||||||||
370 | friend constexpr Maybe<V> Some(U&& aValue); | ||||||||||||||
371 | |||||||||||||||
372 | struct SomeGuard {}; | ||||||||||||||
373 | |||||||||||||||
374 | template <typename U> | ||||||||||||||
375 | constexpr Maybe(U&& aValue, SomeGuard) | ||||||||||||||
376 | : detail::MaybeStorage<T>{std::forward<U>(aValue)} {} | ||||||||||||||
377 | |||||||||||||||
378 | using detail::MaybeStorage<T>::mIsSome; | ||||||||||||||
379 | using detail::MaybeStorage<T>::mStorage; | ||||||||||||||
380 | |||||||||||||||
381 | void poisonData() { detail::MaybePoisoner<T>::poison(&mStorage.val); } | ||||||||||||||
382 | |||||||||||||||
383 | public: | ||||||||||||||
384 | using ValueType = T; | ||||||||||||||
385 | |||||||||||||||
386 | MOZ_ALLOW_TEMPORARY constexpr Maybe() = default; | ||||||||||||||
387 | |||||||||||||||
388 | MOZ_ALLOW_TEMPORARY MOZ_IMPLICIT constexpr Maybe(Nothing) : Maybe{} {} | ||||||||||||||
389 | |||||||||||||||
390 | template <typename... Args> | ||||||||||||||
391 | constexpr explicit Maybe(std::in_place_t, Args&&... aArgs) | ||||||||||||||
392 | : detail::MaybeStorage<T>{std::in_place, std::forward<Args>(aArgs)...} {} | ||||||||||||||
393 | |||||||||||||||
394 | /** | ||||||||||||||
395 | * Maybe<T> can be copy-constructed from a Maybe<U> if T is constructible from | ||||||||||||||
396 | * a const U&. | ||||||||||||||
397 | */ | ||||||||||||||
398 | template <typename U, | ||||||||||||||
399 | std::enable_if_t<std::is_constructible_v<T, const U&>, bool> = true> | ||||||||||||||
400 | MOZ_IMPLICIT Maybe(const Maybe<U>& aOther) { | ||||||||||||||
401 | if (aOther.isSome()) { | ||||||||||||||
402 | emplace(*aOther); | ||||||||||||||
403 | } | ||||||||||||||
404 | } | ||||||||||||||
405 | |||||||||||||||
406 | template <typename U, std::enable_if_t<!std::is_constructible_v<T, const U&>, | ||||||||||||||
407 | bool> = true> | ||||||||||||||
408 | explicit Maybe(const Maybe<U>& aOther) = delete; | ||||||||||||||
409 | |||||||||||||||
410 | /** | ||||||||||||||
411 | * Maybe<T> can be move-constructed from a Maybe<U> if T is constructible from | ||||||||||||||
412 | * a U&&. | ||||||||||||||
413 | */ | ||||||||||||||
414 | template <typename U, | ||||||||||||||
415 | std::enable_if_t<std::is_constructible_v<T, U&&>, bool> = true> | ||||||||||||||
416 | MOZ_IMPLICIT Maybe(Maybe<U>&& aOther) { | ||||||||||||||
417 | if (aOther.isSome()) { | ||||||||||||||
418 | emplace(std::move(*aOther)); | ||||||||||||||
419 | aOther.reset(); | ||||||||||||||
420 | } | ||||||||||||||
421 | } | ||||||||||||||
422 | template <typename U, | ||||||||||||||
423 | std::enable_if_t<!std::is_constructible_v<T, U&&>, bool> = true> | ||||||||||||||
424 | explicit Maybe(Maybe<U>&& aOther) = delete; | ||||||||||||||
425 | |||||||||||||||
426 | template <typename U, | ||||||||||||||
427 | std::enable_if_t<std::is_constructible_v<T, const U&>, bool> = true> | ||||||||||||||
428 | Maybe& operator=(const Maybe<U>& aOther) { | ||||||||||||||
429 | if (aOther.isSome()) { | ||||||||||||||
430 | if (mIsSome) { | ||||||||||||||
431 | ref() = aOther.ref(); | ||||||||||||||
432 | } else { | ||||||||||||||
433 | emplace(*aOther); | ||||||||||||||
434 | } | ||||||||||||||
435 | } else { | ||||||||||||||
436 | reset(); | ||||||||||||||
437 | } | ||||||||||||||
438 | return *this; | ||||||||||||||
439 | } | ||||||||||||||
440 | |||||||||||||||
441 | template <typename U, std::enable_if_t<!std::is_constructible_v<T, const U&>, | ||||||||||||||
442 | bool> = true> | ||||||||||||||
443 | Maybe& operator=(const Maybe<U>& aOther) = delete; | ||||||||||||||
444 | |||||||||||||||
445 | template <typename U, | ||||||||||||||
446 | std::enable_if_t<std::is_constructible_v<T, U&&>, bool> = true> | ||||||||||||||
447 | Maybe& operator=(Maybe<U>&& aOther) { | ||||||||||||||
448 | if (aOther.isSome()) { | ||||||||||||||
449 | if (mIsSome) { | ||||||||||||||
450 | ref() = std::move(aOther.ref()); | ||||||||||||||
451 | } else { | ||||||||||||||
452 | emplace(std::move(*aOther)); | ||||||||||||||
453 | } | ||||||||||||||
454 | aOther.reset(); | ||||||||||||||
455 | } else { | ||||||||||||||
456 | reset(); | ||||||||||||||
457 | } | ||||||||||||||
458 | |||||||||||||||
459 | return *this; | ||||||||||||||
460 | } | ||||||||||||||
461 | |||||||||||||||
462 | template <typename U, | ||||||||||||||
463 | std::enable_if_t<!std::is_constructible_v<T, U&&>, bool> = true> | ||||||||||||||
464 | Maybe& operator=(Maybe<U>&& aOther) = delete; | ||||||||||||||
465 | |||||||||||||||
466 | constexpr Maybe& operator=(Nothing) { | ||||||||||||||
467 | reset(); | ||||||||||||||
468 | return *this; | ||||||||||||||
469 | } | ||||||||||||||
470 | |||||||||||||||
471 | /* Methods that check whether this Maybe contains a value */ | ||||||||||||||
472 | constexpr explicit operator bool() const { return isSome(); } | ||||||||||||||
473 | constexpr bool isSome() const { return mIsSome; } | ||||||||||||||
474 | constexpr bool isNothing() const { return !mIsSome; } | ||||||||||||||
475 | |||||||||||||||
476 | /* Returns the contents of this Maybe<T> by value. Unsafe unless |isSome()|. | ||||||||||||||
477 | */ | ||||||||||||||
478 | constexpr T value() const&; | ||||||||||||||
479 | constexpr T value() &&; | ||||||||||||||
480 | constexpr T value() const&&; | ||||||||||||||
481 | |||||||||||||||
482 | /** | ||||||||||||||
483 | * Move the contents of this Maybe<T> out of internal storage and return it | ||||||||||||||
484 | * without calling the destructor. The internal storage is also reset to | ||||||||||||||
485 | * avoid multiple calls. Unsafe unless |isSome()|. | ||||||||||||||
486 | */ | ||||||||||||||
487 | constexpr T extract() { | ||||||||||||||
488 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 488); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 488; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
489 | T v = std::move(mStorage.val); | ||||||||||||||
490 | reset(); | ||||||||||||||
491 | return v; | ||||||||||||||
492 | } | ||||||||||||||
493 | |||||||||||||||
494 | /** | ||||||||||||||
495 | * Returns the value (possibly |Nothing()|) by moving it out of this Maybe<T> | ||||||||||||||
496 | * and leaving |Nothing()| in its place. | ||||||||||||||
497 | */ | ||||||||||||||
498 | Maybe<T> take() { return std::exchange(*this, Nothing()); } | ||||||||||||||
499 | |||||||||||||||
500 | /* | ||||||||||||||
501 | * Returns the contents of this Maybe<T> by value. If |isNothing()|, returns | ||||||||||||||
502 | * the default value provided. | ||||||||||||||
503 | * | ||||||||||||||
504 | * Note: If the value passed to aDefault is not the result of a trivial | ||||||||||||||
505 | * expression, but expensive to evaluate, e.g. |valueOr(ExpensiveFunction())|, | ||||||||||||||
506 | * use |valueOrFrom| instead, e.g. | ||||||||||||||
507 | * |valueOrFrom([arg] { return ExpensiveFunction(arg); })|. This ensures | ||||||||||||||
508 | * that the expensive expression is only evaluated when its result will | ||||||||||||||
509 | * actually be used. | ||||||||||||||
510 | */ | ||||||||||||||
511 | template <typename V> | ||||||||||||||
512 | constexpr T valueOr(V&& aDefault) const { | ||||||||||||||
513 | if (isSome()) { | ||||||||||||||
514 | return ref(); | ||||||||||||||
515 | } | ||||||||||||||
516 | return std::forward<V>(aDefault); | ||||||||||||||
517 | } | ||||||||||||||
518 | |||||||||||||||
519 | /* | ||||||||||||||
520 | * Returns the contents of this Maybe<T> by value. If |isNothing()|, returns | ||||||||||||||
521 | * the value returned from the function or functor provided. | ||||||||||||||
522 | */ | ||||||||||||||
523 | template <typename F> | ||||||||||||||
524 | constexpr T valueOrFrom(F&& aFunc) const { | ||||||||||||||
525 | if (isSome()) { | ||||||||||||||
526 | return ref(); | ||||||||||||||
527 | } | ||||||||||||||
528 | return aFunc(); | ||||||||||||||
529 | } | ||||||||||||||
530 | |||||||||||||||
531 | /* Returns the contents of this Maybe<T> by pointer. Unsafe unless |isSome()|. | ||||||||||||||
532 | */ | ||||||||||||||
533 | T* ptr(); | ||||||||||||||
534 | constexpr const T* ptr() const; | ||||||||||||||
535 | |||||||||||||||
536 | /* | ||||||||||||||
537 | * Returns the contents of this Maybe<T> by pointer. If |isNothing()|, | ||||||||||||||
538 | * returns the default value provided. | ||||||||||||||
539 | */ | ||||||||||||||
540 | T* ptrOr(T* aDefault) { | ||||||||||||||
541 | if (isSome()) { | ||||||||||||||
542 | return ptr(); | ||||||||||||||
543 | } | ||||||||||||||
544 | return aDefault; | ||||||||||||||
545 | } | ||||||||||||||
546 | |||||||||||||||
547 | constexpr const T* ptrOr(const T* aDefault) const { | ||||||||||||||
548 | if (isSome()) { | ||||||||||||||
549 | return ptr(); | ||||||||||||||
550 | } | ||||||||||||||
551 | return aDefault; | ||||||||||||||
552 | } | ||||||||||||||
553 | |||||||||||||||
554 | /* | ||||||||||||||
555 | * Returns the contents of this Maybe<T> by pointer. If |isNothing()|, | ||||||||||||||
556 | * returns the value returned from the function or functor provided. | ||||||||||||||
557 | */ | ||||||||||||||
558 | template <typename F> | ||||||||||||||
559 | T* ptrOrFrom(F&& aFunc) { | ||||||||||||||
560 | if (isSome()) { | ||||||||||||||
561 | return ptr(); | ||||||||||||||
562 | } | ||||||||||||||
563 | return aFunc(); | ||||||||||||||
564 | } | ||||||||||||||
565 | |||||||||||||||
566 | template <typename F> | ||||||||||||||
567 | const T* ptrOrFrom(F&& aFunc) const { | ||||||||||||||
568 | if (isSome()) { | ||||||||||||||
569 | return ptr(); | ||||||||||||||
570 | } | ||||||||||||||
571 | return aFunc(); | ||||||||||||||
572 | } | ||||||||||||||
573 | |||||||||||||||
574 | constexpr T* operator->(); | ||||||||||||||
575 | constexpr const T* operator->() const; | ||||||||||||||
576 | |||||||||||||||
577 | /* Returns the contents of this Maybe<T> by ref. Unsafe unless |isSome()|. */ | ||||||||||||||
578 | constexpr T& ref() &; | ||||||||||||||
579 | constexpr const T& ref() const&; | ||||||||||||||
580 | constexpr T&& ref() &&; | ||||||||||||||
581 | constexpr const T&& ref() const&&; | ||||||||||||||
582 | |||||||||||||||
583 | /* | ||||||||||||||
584 | * Returns the contents of this Maybe<T> by ref. If |isNothing()|, returns | ||||||||||||||
585 | * the default value provided. | ||||||||||||||
586 | */ | ||||||||||||||
587 | constexpr T& refOr(T& aDefault) { | ||||||||||||||
588 | if (isSome()) { | ||||||||||||||
589 | return ref(); | ||||||||||||||
590 | } | ||||||||||||||
591 | return aDefault; | ||||||||||||||
592 | } | ||||||||||||||
593 | |||||||||||||||
594 | constexpr const T& refOr(const T& aDefault) const { | ||||||||||||||
595 | if (isSome()) { | ||||||||||||||
596 | return ref(); | ||||||||||||||
597 | } | ||||||||||||||
598 | return aDefault; | ||||||||||||||
599 | } | ||||||||||||||
600 | |||||||||||||||
601 | /* | ||||||||||||||
602 | * Returns the contents of this Maybe<T> by ref. If |isNothing()|, returns the | ||||||||||||||
603 | * value returned from the function or functor provided. | ||||||||||||||
604 | */ | ||||||||||||||
605 | template <typename F> | ||||||||||||||
606 | constexpr T& refOrFrom(F&& aFunc) { | ||||||||||||||
607 | if (isSome()) { | ||||||||||||||
608 | return ref(); | ||||||||||||||
609 | } | ||||||||||||||
610 | return aFunc(); | ||||||||||||||
611 | } | ||||||||||||||
612 | |||||||||||||||
613 | template <typename F> | ||||||||||||||
614 | constexpr const T& refOrFrom(F&& aFunc) const { | ||||||||||||||
615 | if (isSome()) { | ||||||||||||||
616 | return ref(); | ||||||||||||||
617 | } | ||||||||||||||
618 | return aFunc(); | ||||||||||||||
619 | } | ||||||||||||||
620 | |||||||||||||||
621 | constexpr T& operator*() &; | ||||||||||||||
622 | constexpr const T& operator*() const&; | ||||||||||||||
623 | constexpr T&& operator*() &&; | ||||||||||||||
624 | constexpr const T&& operator*() const&&; | ||||||||||||||
625 | |||||||||||||||
626 | /* If |isSome()|, runs the provided function or functor on the contents of | ||||||||||||||
627 | * this Maybe. */ | ||||||||||||||
628 | template <typename Func> | ||||||||||||||
629 | constexpr Maybe& apply(Func&& aFunc) & { | ||||||||||||||
630 | if (isSome()) { | ||||||||||||||
631 | std::forward<Func>(aFunc)(ref()); | ||||||||||||||
632 | } | ||||||||||||||
633 | return *this; | ||||||||||||||
634 | } | ||||||||||||||
635 | |||||||||||||||
636 | template <typename Func> | ||||||||||||||
637 | constexpr const Maybe& apply(Func&& aFunc) const& { | ||||||||||||||
638 | if (isSome()) { | ||||||||||||||
639 | std::forward<Func>(aFunc)(ref()); | ||||||||||||||
640 | } | ||||||||||||||
641 | return *this; | ||||||||||||||
642 | } | ||||||||||||||
643 | |||||||||||||||
644 | template <typename Func> | ||||||||||||||
645 | constexpr Maybe& apply(Func&& aFunc) && { | ||||||||||||||
646 | if (isSome()) { | ||||||||||||||
647 | std::forward<Func>(aFunc)(extract()); | ||||||||||||||
648 | } | ||||||||||||||
649 | return *this; | ||||||||||||||
650 | } | ||||||||||||||
651 | |||||||||||||||
652 | template <typename Func> | ||||||||||||||
653 | constexpr Maybe& apply(Func&& aFunc) const&& { | ||||||||||||||
654 | if (isSome()) { | ||||||||||||||
655 | std::forward<Func>(aFunc)(extract()); | ||||||||||||||
656 | } | ||||||||||||||
657 | return *this; | ||||||||||||||
658 | } | ||||||||||||||
659 | |||||||||||||||
660 | /* | ||||||||||||||
661 | * If |isSome()|, runs the provided function and returns the result wrapped | ||||||||||||||
662 | * in a Maybe. If |isNothing()|, returns an empty Maybe value with the same | ||||||||||||||
663 | * value type as what the provided function would have returned. | ||||||||||||||
664 | */ | ||||||||||||||
665 | template <typename Func> | ||||||||||||||
666 | constexpr auto map(Func&& aFunc) & { | ||||||||||||||
667 | if (isSome()) { | ||||||||||||||
668 | return Some(std::forward<Func>(aFunc)(ref())); | ||||||||||||||
669 | } | ||||||||||||||
670 | return Maybe<decltype(std::forward<Func>(aFunc)(ref()))>{}; | ||||||||||||||
671 | } | ||||||||||||||
672 | |||||||||||||||
673 | template <typename Func> | ||||||||||||||
674 | constexpr auto map(Func&& aFunc) const& { | ||||||||||||||
675 | if (isSome()) { | ||||||||||||||
676 | return Some(std::forward<Func>(aFunc)(ref())); | ||||||||||||||
677 | } | ||||||||||||||
678 | return Maybe<decltype(std::forward<Func>(aFunc)(ref()))>{}; | ||||||||||||||
679 | } | ||||||||||||||
680 | |||||||||||||||
681 | template <typename Func> | ||||||||||||||
682 | constexpr auto map(Func&& aFunc) && { | ||||||||||||||
683 | if (isSome()) { | ||||||||||||||
684 | return Some(std::forward<Func>(aFunc)(extract())); | ||||||||||||||
685 | } | ||||||||||||||
686 | return Maybe<decltype(std::forward<Func>(aFunc)(extract()))>{}; | ||||||||||||||
687 | } | ||||||||||||||
688 | |||||||||||||||
689 | template <typename Func> | ||||||||||||||
690 | constexpr auto map(Func&& aFunc) const&& { | ||||||||||||||
691 | if (isSome()) { | ||||||||||||||
692 | return Some(std::forward<Func>(aFunc)(extract())); | ||||||||||||||
693 | } | ||||||||||||||
694 | return Maybe<decltype(std::forward<Func>(aFunc)(extract()))>{}; | ||||||||||||||
695 | } | ||||||||||||||
696 | |||||||||||||||
697 | /* | ||||||||||||||
698 | * If |isSome()|, runs the provided function or functor on the contents of | ||||||||||||||
699 | * this Maybe and returns the result. Note that the provided function or | ||||||||||||||
700 | * functor must return a Maybe<U> of any type U. | ||||||||||||||
701 | * If |isNothing()|, returns an empty Maybe value with the same type as what | ||||||||||||||
702 | * the provided function would have returned. | ||||||||||||||
703 | */ | ||||||||||||||
704 | template <typename Func> | ||||||||||||||
705 | constexpr auto andThen(Func&& aFunc) & { | ||||||||||||||
706 | static_assert(std::is_invocable_v<Func, T&>); | ||||||||||||||
707 | using U = std::invoke_result_t<Func, T&>; | ||||||||||||||
708 | static_assert(detail::IsMaybe<U>::value); | ||||||||||||||
709 | if (isSome()) { | ||||||||||||||
710 | return std::invoke(std::forward<Func>(aFunc), ref()); | ||||||||||||||
711 | } | ||||||||||||||
712 | return std::remove_cv_t<std::remove_reference_t<U>>{}; | ||||||||||||||
713 | } | ||||||||||||||
714 | |||||||||||||||
715 | template <typename Func> | ||||||||||||||
716 | constexpr auto andThen(Func&& aFunc) const& { | ||||||||||||||
717 | static_assert(std::is_invocable_v<Func, const T&>); | ||||||||||||||
718 | using U = std::invoke_result_t<Func, const T&>; | ||||||||||||||
719 | static_assert(detail::IsMaybe<U>::value); | ||||||||||||||
720 | if (isSome()) { | ||||||||||||||
721 | return std::invoke(std::forward<Func>(aFunc), ref()); | ||||||||||||||
722 | } | ||||||||||||||
723 | return std::remove_cv_t<std::remove_reference_t<U>>{}; | ||||||||||||||
724 | } | ||||||||||||||
725 | |||||||||||||||
726 | template <typename Func> | ||||||||||||||
727 | constexpr auto andThen(Func&& aFunc) && { | ||||||||||||||
728 | static_assert(std::is_invocable_v<Func, T&&>); | ||||||||||||||
729 | using U = std::invoke_result_t<Func, T&&>; | ||||||||||||||
730 | static_assert(detail::IsMaybe<U>::value); | ||||||||||||||
731 | if (isSome()) { | ||||||||||||||
732 | return std::invoke(std::forward<Func>(aFunc), extract()); | ||||||||||||||
733 | } | ||||||||||||||
734 | return std::remove_cv_t<std::remove_reference_t<U>>{}; | ||||||||||||||
735 | } | ||||||||||||||
736 | |||||||||||||||
737 | template <typename Func> | ||||||||||||||
738 | constexpr auto andThen(Func&& aFunc) const&& { | ||||||||||||||
739 | static_assert(std::is_invocable_v<Func, const T&&>); | ||||||||||||||
740 | using U = std::invoke_result_t<Func, const T&&>; | ||||||||||||||
741 | static_assert(detail::IsMaybe<U>::value); | ||||||||||||||
742 | if (isSome()) { | ||||||||||||||
743 | return std::invoke(std::forward<Func>(aFunc), extract()); | ||||||||||||||
744 | } | ||||||||||||||
745 | return std::remove_cv_t<std::remove_reference_t<U>>{}; | ||||||||||||||
746 | } | ||||||||||||||
747 | |||||||||||||||
748 | /* | ||||||||||||||
749 | * If |isNothing()|, runs the provided function or functor and returns its | ||||||||||||||
750 | * result. If |isSome()|, returns the contained value wrapped in a Maybe. | ||||||||||||||
751 | */ | ||||||||||||||
752 | template <typename Func> | ||||||||||||||
753 | constexpr Maybe orElse(Func&& aFunc) & { | ||||||||||||||
754 | static_assert(std::is_invocable_v<Func>); | ||||||||||||||
755 | using U = std::invoke_result_t<Func>; | ||||||||||||||
756 | static_assert( | ||||||||||||||
757 | std::is_same_v<Maybe, std::remove_cv_t<std::remove_reference_t<U>>>); | ||||||||||||||
758 | if (isSome()) { | ||||||||||||||
759 | return *this; | ||||||||||||||
760 | } | ||||||||||||||
761 | return std::invoke(std::forward<Func>(aFunc)); | ||||||||||||||
762 | } | ||||||||||||||
763 | |||||||||||||||
764 | template <typename Func> | ||||||||||||||
765 | constexpr Maybe orElse(Func&& aFunc) const& { | ||||||||||||||
766 | static_assert(std::is_invocable_v<Func>); | ||||||||||||||
767 | using U = std::invoke_result_t<Func>; | ||||||||||||||
768 | static_assert( | ||||||||||||||
769 | std::is_same_v<Maybe, std::remove_cv_t<std::remove_reference_t<U>>>); | ||||||||||||||
770 | if (isSome()) { | ||||||||||||||
771 | return *this; | ||||||||||||||
772 | } | ||||||||||||||
773 | return std::invoke(std::forward<Func>(aFunc)); | ||||||||||||||
774 | } | ||||||||||||||
775 | |||||||||||||||
776 | template <typename Func> | ||||||||||||||
777 | constexpr Maybe orElse(Func&& aFunc) && { | ||||||||||||||
778 | static_assert(std::is_invocable_v<Func>); | ||||||||||||||
779 | using U = std::invoke_result_t<Func>; | ||||||||||||||
780 | static_assert( | ||||||||||||||
781 | std::is_same_v<Maybe, std::remove_cv_t<std::remove_reference_t<U>>>); | ||||||||||||||
782 | if (isSome()) { | ||||||||||||||
783 | return std::move(*this); | ||||||||||||||
784 | } | ||||||||||||||
785 | return std::invoke(std::forward<Func>(aFunc)); | ||||||||||||||
786 | } | ||||||||||||||
787 | |||||||||||||||
788 | template <typename Func> | ||||||||||||||
789 | constexpr Maybe orElse(Func&& aFunc) const&& { | ||||||||||||||
790 | static_assert(std::is_invocable_v<Func>); | ||||||||||||||
791 | using U = std::invoke_result_t<Func>; | ||||||||||||||
792 | static_assert( | ||||||||||||||
793 | std::is_same_v<Maybe, std::remove_cv_t<std::remove_reference_t<U>>>); | ||||||||||||||
794 | if (isSome()) { | ||||||||||||||
795 | return std::move(*this); | ||||||||||||||
796 | } | ||||||||||||||
797 | return std::invoke(std::forward<Func>(aFunc)); | ||||||||||||||
798 | } | ||||||||||||||
799 | |||||||||||||||
800 | /* If |isSome()|, empties this Maybe and destroys its contents. */ | ||||||||||||||
801 | constexpr void reset() { | ||||||||||||||
802 | if (isSome()) { | ||||||||||||||
803 | if constexpr (!std::is_trivially_destructible_v<T>) { | ||||||||||||||
804 | /* | ||||||||||||||
805 | * Static analyzer gets confused if we have Maybe<MutexAutoLock>, | ||||||||||||||
806 | * so we suppress thread-safety warnings here | ||||||||||||||
807 | */ | ||||||||||||||
808 | MOZ_PUSH_IGNORE_THREAD_SAFETYGCC diagnostic push
GCC diagnostic ignored "-Wthread-safety" | ||||||||||||||
809 | ref().T::~T(); | ||||||||||||||
810 | MOZ_POP_THREAD_SAFETYGCC diagnostic pop | ||||||||||||||
811 | poisonData(); | ||||||||||||||
812 | } | ||||||||||||||
813 | mIsSome = false; | ||||||||||||||
814 | } | ||||||||||||||
815 | } | ||||||||||||||
816 | |||||||||||||||
817 | /* | ||||||||||||||
818 | * Constructs a T value in-place in this empty Maybe<T>'s storage. The | ||||||||||||||
819 | * arguments to |emplace()| are the parameters to T's constructor. | ||||||||||||||
820 | */ | ||||||||||||||
821 | template <typename... Args> | ||||||||||||||
822 | constexpr void emplace(Args&&... aArgs); | ||||||||||||||
823 | |||||||||||||||
824 | template <typename U> | ||||||||||||||
825 | constexpr std::enable_if_t<std::is_same_v<T, U> && | ||||||||||||||
826 | std::is_copy_constructible_v<U> && | ||||||||||||||
827 | !std::is_move_constructible_v<U>> | ||||||||||||||
828 | emplace(U&& aArgs) { | ||||||||||||||
829 | emplace(aArgs); | ||||||||||||||
830 | } | ||||||||||||||
831 | |||||||||||||||
832 | friend std::ostream& operator<<(std::ostream& aStream, | ||||||||||||||
833 | const Maybe<T>& aMaybe) { | ||||||||||||||
834 | if (aMaybe) { | ||||||||||||||
835 | aStream << aMaybe.ref(); | ||||||||||||||
836 | } else { | ||||||||||||||
837 | aStream << "<Nothing>"; | ||||||||||||||
838 | } | ||||||||||||||
839 | return aStream; | ||||||||||||||
840 | } | ||||||||||||||
841 | }; | ||||||||||||||
842 | |||||||||||||||
843 | template <typename T> | ||||||||||||||
844 | class Maybe<T&> { | ||||||||||||||
845 | public: | ||||||||||||||
846 | constexpr Maybe() = default; | ||||||||||||||
847 | constexpr MOZ_IMPLICIT Maybe(Nothing) {} | ||||||||||||||
848 | |||||||||||||||
849 | void emplace(T& aRef) { mValue = &aRef; } | ||||||||||||||
850 | |||||||||||||||
851 | /* Methods that check whether this Maybe contains a value */ | ||||||||||||||
852 | constexpr explicit operator bool() const { return isSome(); } | ||||||||||||||
853 | constexpr bool isSome() const { return mValue; } | ||||||||||||||
854 | constexpr bool isNothing() const { return !mValue; } | ||||||||||||||
855 | |||||||||||||||
856 | T& ref() const { | ||||||||||||||
857 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 857); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 857; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
858 | return *mValue; | ||||||||||||||
859 | } | ||||||||||||||
860 | |||||||||||||||
861 | T* operator->() const { return &ref(); } | ||||||||||||||
862 | T& operator*() const { return ref(); } | ||||||||||||||
863 | |||||||||||||||
864 | // Deliberately not defining value and ptr accessors, as these may be | ||||||||||||||
865 | // confusing on a reference-typed Maybe. | ||||||||||||||
866 | |||||||||||||||
867 | // XXX Should we define refOr? | ||||||||||||||
868 | |||||||||||||||
869 | void reset() { mValue = nullptr; } | ||||||||||||||
870 | |||||||||||||||
871 | template <typename Func> | ||||||||||||||
872 | const Maybe& apply(Func&& aFunc) const { | ||||||||||||||
873 | if (isSome()) { | ||||||||||||||
874 | std::forward<Func>(aFunc)(ref()); | ||||||||||||||
875 | } | ||||||||||||||
876 | return *this; | ||||||||||||||
877 | } | ||||||||||||||
878 | |||||||||||||||
879 | template <typename Func> | ||||||||||||||
880 | auto map(Func&& aFunc) const { | ||||||||||||||
881 | Maybe<decltype(std::forward<Func>(aFunc)(ref()))> val; | ||||||||||||||
882 | if (isSome()) { | ||||||||||||||
883 | val.emplace(std::forward<Func>(aFunc)(ref())); | ||||||||||||||
884 | } | ||||||||||||||
885 | return val; | ||||||||||||||
886 | } | ||||||||||||||
887 | |||||||||||||||
888 | template <typename Func> | ||||||||||||||
889 | constexpr auto andThen(Func&& aFunc) const { | ||||||||||||||
890 | static_assert(std::is_invocable_v<Func, T&>); | ||||||||||||||
891 | using U = std::invoke_result_t<Func, T&>; | ||||||||||||||
892 | static_assert(detail::IsMaybe<U>::value); | ||||||||||||||
893 | if (isSome()) { | ||||||||||||||
894 | return std::invoke(std::forward<Func>(aFunc), ref()); | ||||||||||||||
895 | } | ||||||||||||||
896 | return std::remove_cv_t<std::remove_reference_t<U>>{}; | ||||||||||||||
897 | } | ||||||||||||||
898 | |||||||||||||||
899 | template <typename Func> | ||||||||||||||
900 | constexpr Maybe orElse(Func&& aFunc) const { | ||||||||||||||
901 | static_assert(std::is_invocable_v<Func>); | ||||||||||||||
902 | using U = std::invoke_result_t<Func>; | ||||||||||||||
903 | static_assert( | ||||||||||||||
904 | std::is_same_v<Maybe, std::remove_cv_t<std::remove_reference_t<U>>>); | ||||||||||||||
905 | if (isSome()) { | ||||||||||||||
906 | return *this; | ||||||||||||||
907 | } | ||||||||||||||
908 | return std::invoke(std::forward<Func>(aFunc)); | ||||||||||||||
909 | } | ||||||||||||||
910 | |||||||||||||||
911 | bool refEquals(const Maybe<T&>& aOther) const { | ||||||||||||||
912 | return mValue == aOther.mValue; | ||||||||||||||
913 | } | ||||||||||||||
914 | |||||||||||||||
915 | bool refEquals(const T& aOther) const { return mValue == &aOther; } | ||||||||||||||
916 | |||||||||||||||
917 | private: | ||||||||||||||
918 | T* mValue = nullptr; | ||||||||||||||
919 | }; | ||||||||||||||
920 | |||||||||||||||
921 | template <typename T> | ||||||||||||||
922 | constexpr T Maybe<T>::value() const& { | ||||||||||||||
923 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 923); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 923; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
924 | return ref(); | ||||||||||||||
925 | } | ||||||||||||||
926 | |||||||||||||||
927 | template <typename T> | ||||||||||||||
928 | constexpr T Maybe<T>::value() && { | ||||||||||||||
929 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 929); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 929; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
930 | return std::move(ref()); | ||||||||||||||
931 | } | ||||||||||||||
932 | |||||||||||||||
933 | template <typename T> | ||||||||||||||
934 | constexpr T Maybe<T>::value() const&& { | ||||||||||||||
935 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 935); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 935; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
936 | return std::move(ref()); | ||||||||||||||
937 | } | ||||||||||||||
938 | |||||||||||||||
939 | template <typename T> | ||||||||||||||
940 | T* Maybe<T>::ptr() { | ||||||||||||||
941 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 941); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 941; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
942 | return &ref(); | ||||||||||||||
943 | } | ||||||||||||||
944 | |||||||||||||||
945 | template <typename T> | ||||||||||||||
946 | constexpr const T* Maybe<T>::ptr() const { | ||||||||||||||
947 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 947); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 947; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
948 | return &ref(); | ||||||||||||||
949 | } | ||||||||||||||
950 | |||||||||||||||
951 | template <typename T> | ||||||||||||||
952 | constexpr T* Maybe<T>::operator->() { | ||||||||||||||
953 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 953); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 953; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
954 | return ptr(); | ||||||||||||||
955 | } | ||||||||||||||
956 | |||||||||||||||
957 | template <typename T> | ||||||||||||||
958 | constexpr const T* Maybe<T>::operator->() const { | ||||||||||||||
959 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 959); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 959; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
960 | return ptr(); | ||||||||||||||
961 | } | ||||||||||||||
962 | |||||||||||||||
963 | template <typename T> | ||||||||||||||
964 | constexpr T& Maybe<T>::ref() & { | ||||||||||||||
965 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 965); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 965; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
966 | return mStorage.val; | ||||||||||||||
967 | } | ||||||||||||||
968 | |||||||||||||||
969 | template <typename T> | ||||||||||||||
970 | constexpr const T& Maybe<T>::ref() const& { | ||||||||||||||
971 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 971); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 971; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
972 | return mStorage.val; | ||||||||||||||
973 | } | ||||||||||||||
974 | |||||||||||||||
975 | template <typename T> | ||||||||||||||
976 | constexpr T&& Maybe<T>::ref() && { | ||||||||||||||
977 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 977); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 977; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
978 | return std::move(mStorage.val); | ||||||||||||||
979 | } | ||||||||||||||
980 | |||||||||||||||
981 | template <typename T> | ||||||||||||||
982 | constexpr const T&& Maybe<T>::ref() const&& { | ||||||||||||||
983 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 983); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 983; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
984 | return std::move(mStorage.val); | ||||||||||||||
985 | } | ||||||||||||||
986 | |||||||||||||||
987 | template <typename T> | ||||||||||||||
988 | constexpr T& Maybe<T>::operator*() & { | ||||||||||||||
989 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 989); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 989; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
990 | return ref(); | ||||||||||||||
991 | } | ||||||||||||||
992 | |||||||||||||||
993 | template <typename T> | ||||||||||||||
994 | constexpr const T& Maybe<T>::operator*() const& { | ||||||||||||||
995 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 995); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 995; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
996 | return ref(); | ||||||||||||||
997 | } | ||||||||||||||
998 | |||||||||||||||
999 | template <typename T> | ||||||||||||||
1000 | constexpr T&& Maybe<T>::operator*() && { | ||||||||||||||
1001 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 1001); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 1001; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1002 | return std::move(ref()); | ||||||||||||||
1003 | } | ||||||||||||||
1004 | |||||||||||||||
1005 | template <typename T> | ||||||||||||||
1006 | constexpr const T&& Maybe<T>::operator*() const&& { | ||||||||||||||
1007 | MOZ_RELEASE_ASSERT(isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 1007); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "isSome()" ")"); do { *((volatile int*)__null) = 1007; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1008 | return std::move(ref()); | ||||||||||||||
1009 | } | ||||||||||||||
1010 | |||||||||||||||
1011 | template <typename T> | ||||||||||||||
1012 | template <typename... Args> | ||||||||||||||
1013 | constexpr void Maybe<T>::emplace(Args&&... aArgs) { | ||||||||||||||
1014 | MOZ_RELEASE_ASSERT(!isSome())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!isSome())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!isSome()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!isSome()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/Maybe.h" , 1014); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "!isSome()" ")"); do { *((volatile int*)__null) = 1014; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1015 | ::new (KnownNotNull, &mStorage.val) T(std::forward<Args>(aArgs)...); | ||||||||||||||
1016 | mIsSome = true; | ||||||||||||||
1017 | } | ||||||||||||||
1018 | |||||||||||||||
1019 | /* | ||||||||||||||
1020 | * Some() creates a Maybe<T> value containing the provided T value. If T has a | ||||||||||||||
1021 | * move constructor, it's used to make this as efficient as possible. | ||||||||||||||
1022 | * | ||||||||||||||
1023 | * Some() selects the type of Maybe it returns by removing any const, volatile, | ||||||||||||||
1024 | * or reference qualifiers from the type of the value you pass to it. This gives | ||||||||||||||
1025 | * it more intuitive behavior when used in expressions, but it also means that | ||||||||||||||
1026 | * if you need to construct a Maybe value that holds a const, volatile, or | ||||||||||||||
1027 | * reference value, you need to use emplace() instead. | ||||||||||||||
1028 | */ | ||||||||||||||
1029 | template <typename T, typename U> | ||||||||||||||
1030 | constexpr Maybe<U> Some(T&& aValue) { | ||||||||||||||
1031 | return {std::forward<T>(aValue), typename Maybe<U>::SomeGuard{}}; | ||||||||||||||
1032 | } | ||||||||||||||
1033 | |||||||||||||||
1034 | template <typename T> | ||||||||||||||
1035 | constexpr Maybe<T&> SomeRef(T& aValue) { | ||||||||||||||
1036 | Maybe<T&> value; | ||||||||||||||
1037 | value.emplace(aValue); | ||||||||||||||
1038 | return value; | ||||||||||||||
1039 | } | ||||||||||||||
1040 | |||||||||||||||
1041 | template <typename T> | ||||||||||||||
1042 | constexpr Maybe<T&> ToMaybeRef(T* const aPtr) { | ||||||||||||||
1043 | return aPtr ? SomeRef(*aPtr) : Nothing{}; | ||||||||||||||
1044 | } | ||||||||||||||
1045 | |||||||||||||||
1046 | template <typename T> | ||||||||||||||
1047 | Maybe<std::remove_cv_t<std::remove_reference_t<T>>> ToMaybe(T* aPtr) { | ||||||||||||||
1048 | if (aPtr) { | ||||||||||||||
1049 | return Some(*aPtr); | ||||||||||||||
1050 | } | ||||||||||||||
1051 | return Nothing(); | ||||||||||||||
1052 | } | ||||||||||||||
1053 | |||||||||||||||
1054 | /* | ||||||||||||||
1055 | * Two Maybe<T> values are equal if | ||||||||||||||
1056 | * - both are Nothing, or | ||||||||||||||
1057 | * - both are Some, and the values they contain are equal. | ||||||||||||||
1058 | */ | ||||||||||||||
1059 | template <typename T> | ||||||||||||||
1060 | constexpr bool operator==(const Maybe<T>& aLHS, const Maybe<T>& aRHS) { | ||||||||||||||
1061 | static_assert(!std::is_reference_v<T>, | ||||||||||||||
1062 | "operator== is not defined for Maybe<T&>, compare values or " | ||||||||||||||
1063 | "addresses explicitly instead"); | ||||||||||||||
1064 | if (aLHS.isNothing() != aRHS.isNothing()) { | ||||||||||||||
1065 | return false; | ||||||||||||||
1066 | } | ||||||||||||||
1067 | return aLHS.isNothing() || *aLHS == *aRHS; | ||||||||||||||
1068 | } | ||||||||||||||
1069 | |||||||||||||||
1070 | template <typename T> | ||||||||||||||
1071 | constexpr bool operator!=(const Maybe<T>& aLHS, const Maybe<T>& aRHS) { | ||||||||||||||
1072 | return !(aLHS == aRHS); | ||||||||||||||
1073 | } | ||||||||||||||
1074 | |||||||||||||||
1075 | /* | ||||||||||||||
1076 | * We support comparison to Nothing to allow reasonable expressions like: | ||||||||||||||
1077 | * if (maybeValue == Nothing()) { ... } | ||||||||||||||
1078 | */ | ||||||||||||||
1079 | template <typename T> | ||||||||||||||
1080 | constexpr bool operator==(const Maybe<T>& aLHS, const Nothing& aRHS) { | ||||||||||||||
1081 | return aLHS.isNothing(); | ||||||||||||||
1082 | } | ||||||||||||||
1083 | |||||||||||||||
1084 | template <typename T> | ||||||||||||||
1085 | constexpr bool operator!=(const Maybe<T>& aLHS, const Nothing& aRHS) { | ||||||||||||||
1086 | return !(aLHS == aRHS); | ||||||||||||||
1087 | } | ||||||||||||||
1088 | |||||||||||||||
1089 | template <typename T> | ||||||||||||||
1090 | constexpr bool operator==(const Nothing& aLHS, const Maybe<T>& aRHS) { | ||||||||||||||
1091 | return aRHS.isNothing(); | ||||||||||||||
1092 | } | ||||||||||||||
1093 | |||||||||||||||
1094 | template <typename T> | ||||||||||||||
1095 | constexpr bool operator!=(const Nothing& aLHS, const Maybe<T>& aRHS) { | ||||||||||||||
1096 | return !(aLHS == aRHS); | ||||||||||||||
1097 | } | ||||||||||||||
1098 | |||||||||||||||
1099 | /* | ||||||||||||||
1100 | * Maybe<T> values are ordered in the same way T values are ordered, except that | ||||||||||||||
1101 | * Nothing comes before anything else. | ||||||||||||||
1102 | */ | ||||||||||||||
1103 | template <typename T> | ||||||||||||||
1104 | constexpr bool operator<(const Maybe<T>& aLHS, const Maybe<T>& aRHS) { | ||||||||||||||
1105 | if (aLHS.isNothing()) { | ||||||||||||||
1106 | return aRHS.isSome(); | ||||||||||||||
1107 | } | ||||||||||||||
1108 | if (aRHS.isNothing()) { | ||||||||||||||
1109 | return false; | ||||||||||||||
1110 | } | ||||||||||||||
1111 | return *aLHS < *aRHS; | ||||||||||||||
1112 | } | ||||||||||||||
1113 | |||||||||||||||
1114 | template <typename T> | ||||||||||||||
1115 | constexpr bool operator>(const Maybe<T>& aLHS, const Maybe<T>& aRHS) { | ||||||||||||||
1116 | return !(aLHS < aRHS || aLHS == aRHS); | ||||||||||||||
1117 | } | ||||||||||||||
1118 | |||||||||||||||
1119 | template <typename T> | ||||||||||||||
1120 | constexpr bool operator<=(const Maybe<T>& aLHS, const Maybe<T>& aRHS) { | ||||||||||||||
1121 | return aLHS < aRHS || aLHS == aRHS; | ||||||||||||||
1122 | } | ||||||||||||||
1123 | |||||||||||||||
1124 | template <typename T> | ||||||||||||||
1125 | constexpr bool operator>=(const Maybe<T>& aLHS, const Maybe<T>& aRHS) { | ||||||||||||||
1126 | return !(aLHS < aRHS); | ||||||||||||||
1127 | } | ||||||||||||||
1128 | |||||||||||||||
1129 | template <typename T> | ||||||||||||||
1130 | inline void ImplCycleCollectionTraverse( | ||||||||||||||
1131 | nsCycleCollectionTraversalCallback& aCallback, mozilla::Maybe<T>& aField, | ||||||||||||||
1132 | const char* aName, uint32_t aFlags = 0) { | ||||||||||||||
1133 | if (aField) { | ||||||||||||||
1134 | ImplCycleCollectionTraverse(aCallback, aField.ref(), aName, aFlags); | ||||||||||||||
1135 | } | ||||||||||||||
1136 | } | ||||||||||||||
1137 | |||||||||||||||
1138 | template <typename T> | ||||||||||||||
1139 | inline void ImplCycleCollectionUnlink(mozilla::Maybe<T>& aField) { | ||||||||||||||
1140 | if (aField) { | ||||||||||||||
1141 | ImplCycleCollectionUnlink(aField.ref()); | ||||||||||||||
1142 | } | ||||||||||||||
1143 | } | ||||||||||||||
1144 | |||||||||||||||
1145 | } // namespace mozilla | ||||||||||||||
1146 | |||||||||||||||
1147 | #endif /* mozilla_Maybe_h */ |
1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
2 | * vim: set ts=8 sts=2 et sw=2 tw=80: |
3 | * This Source Code Form is subject to the terms of the Mozilla Public |
4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | |
7 | #ifndef js_UbiNodeShortestPaths_h |
8 | #define js_UbiNodeShortestPaths_h |
9 | |
10 | #include "mozilla/CheckedInt.h" |
11 | #include "mozilla/Maybe.h" |
12 | |
13 | #include <utility> |
14 | |
15 | #include "js/AllocPolicy.h" |
16 | #include "js/GCAPI.h" |
17 | #include "js/UbiNode.h" |
18 | #include "js/UbiNodeBreadthFirst.h" |
19 | #include "js/UniquePtr.h" |
20 | |
21 | namespace JS { |
22 | namespace ubi { |
23 | |
24 | /** |
25 | * A back edge along a path in the heap graph. |
26 | */ |
27 | struct JS_PUBLIC_API BackEdge { |
28 | private: |
29 | Node predecessor_; |
30 | EdgeName name_; |
31 | |
32 | public: |
33 | using Ptr = js::UniquePtr<BackEdge>; |
34 | |
35 | BackEdge() : predecessor_(), name_(nullptr) {} |
36 | |
37 | [[nodiscard]] bool init(const Node& predecessor, Edge& edge) { |
38 | MOZ_ASSERT(!predecessor_)do { static_assert( mozilla::detail::AssertionConditionType< decltype(!predecessor_)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!predecessor_))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!predecessor_", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 38); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!predecessor_" ")"); do { *((volatile int*)__null) = 38; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
39 | MOZ_ASSERT(!name_)do { static_assert( mozilla::detail::AssertionConditionType< decltype(!name_)>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(!name_))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!name_", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 39); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!name_" ")"); do { *((volatile int*)__null) = 39; __attribute__((nomerge)) ::abort(); } while (false); } } while (false); |
40 | |
41 | predecessor_ = predecessor; |
42 | name_ = std::move(edge.name); |
43 | return true; |
44 | } |
45 | |
46 | BackEdge(const BackEdge&) = delete; |
47 | BackEdge& operator=(const BackEdge&) = delete; |
48 | |
49 | BackEdge(BackEdge&& rhs) |
50 | : predecessor_(rhs.predecessor_), name_(std::move(rhs.name_)) { |
51 | MOZ_ASSERT(&rhs != this)do { static_assert( mozilla::detail::AssertionConditionType< decltype(&rhs != this)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(&rhs != this))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("&rhs != this" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 51); AnnotateMozCrashReason("MOZ_ASSERT" "(" "&rhs != this" ")"); do { *((volatile int*)__null) = 51; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
52 | } |
53 | |
54 | BackEdge& operator=(BackEdge&& rhs) { |
55 | this->~BackEdge(); |
56 | new (this) BackEdge(std::move(rhs)); |
57 | return *this; |
58 | } |
59 | |
60 | Ptr clone() const; |
61 | |
62 | const EdgeName& name() const { return name_; } |
63 | EdgeName& name() { return name_; } |
64 | |
65 | const JS::ubi::Node& predecessor() const { return predecessor_; } |
66 | }; |
67 | |
68 | /** |
69 | * A path is a series of back edges from which we discovered a target node. |
70 | */ |
71 | using Path = JS::ubi::Vector<BackEdge*>; |
72 | |
73 | /** |
74 | * The `JS::ubi::ShortestPaths` type represents a collection of up to N shortest |
75 | * retaining paths for each of a target set of nodes, starting from the same |
76 | * root node. |
77 | */ |
78 | struct JS_PUBLIC_API ShortestPaths { |
79 | private: |
80 | // Types, type aliases, and data members. |
81 | |
82 | using BackEdgeVector = JS::ubi::Vector<BackEdge::Ptr>; |
83 | using NodeToBackEdgeVectorMap = |
84 | js::HashMap<Node, BackEdgeVector, js::DefaultHasher<Node>, |
85 | js::SystemAllocPolicy>; |
86 | |
87 | struct Handler; |
88 | using Traversal = BreadthFirst<Handler>; |
89 | |
90 | /** |
91 | * A `JS::ubi::BreadthFirst` traversal handler that records back edges for |
92 | * how we reached each node, allowing us to reconstruct the shortest |
93 | * retaining paths after the traversal. |
94 | */ |
95 | struct Handler { |
96 | using NodeData = BackEdge; |
97 | |
98 | ShortestPaths& shortestPaths; |
99 | size_t totalMaxPathsToRecord; |
100 | size_t totalPathsRecorded; |
101 | |
102 | explicit Handler(ShortestPaths& shortestPaths) |
103 | : shortestPaths(shortestPaths), |
104 | totalMaxPathsToRecord(shortestPaths.targets_.count() * |
105 | shortestPaths.maxNumPaths_), |
106 | totalPathsRecorded(0) {} |
107 | |
108 | bool operator()(Traversal& traversal, const JS::ubi::Node& origin, |
109 | JS::ubi::Edge& edge, BackEdge* back, bool first) { |
110 | MOZ_ASSERT(back)do { static_assert( mozilla::detail::AssertionConditionType< decltype(back)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(back))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("back", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 110); AnnotateMozCrashReason("MOZ_ASSERT" "(" "back" ")"); do { *((volatile int*)__null) = 110; __attribute__((nomerge)) :: abort(); } while (false); } } while (false); |
111 | MOZ_ASSERT(origin == shortestPaths.root_ ||do { static_assert( mozilla::detail::AssertionConditionType< decltype(origin == shortestPaths.root_ || traversal.visited.has (origin))>::isValid, "invalid assertion condition"); if (( __builtin_expect(!!(!(!!(origin == shortestPaths.root_ || traversal .visited.has(origin)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("origin == shortestPaths.root_ || traversal.visited.has(origin)" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 112); AnnotateMozCrashReason("MOZ_ASSERT" "(" "origin == shortestPaths.root_ || traversal.visited.has(origin)" ")"); do { *((volatile int*)__null) = 112; __attribute__((nomerge )) ::abort(); } while (false); } } while (false) |
112 | traversal.visited.has(origin))do { static_assert( mozilla::detail::AssertionConditionType< decltype(origin == shortestPaths.root_ || traversal.visited.has (origin))>::isValid, "invalid assertion condition"); if (( __builtin_expect(!!(!(!!(origin == shortestPaths.root_ || traversal .visited.has(origin)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("origin == shortestPaths.root_ || traversal.visited.has(origin)" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 112); AnnotateMozCrashReason("MOZ_ASSERT" "(" "origin == shortestPaths.root_ || traversal.visited.has(origin)" ")"); do { *((volatile int*)__null) = 112; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
113 | MOZ_ASSERT(totalPathsRecorded < totalMaxPathsToRecord)do { static_assert( mozilla::detail::AssertionConditionType< decltype(totalPathsRecorded < totalMaxPathsToRecord)>:: isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(totalPathsRecorded < totalMaxPathsToRecord))), 0) )) { do { } while (false); MOZ_ReportAssertionFailure("totalPathsRecorded < totalMaxPathsToRecord" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 113); AnnotateMozCrashReason("MOZ_ASSERT" "(" "totalPathsRecorded < totalMaxPathsToRecord" ")"); do { *((volatile int*)__null) = 113; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
114 | |
115 | if (first && !back->init(origin, edge)) { |
116 | return false; |
117 | } |
118 | |
119 | if (!shortestPaths.targets_.has(edge.referent)) { |
120 | return true; |
121 | } |
122 | |
123 | // If `first` is true, then we moved the edge's name into `back` in |
124 | // the above call to `init`. So clone that back edge to get the |
125 | // correct edge name. If `first` is not true, then our edge name is |
126 | // still in `edge`. This accounts for the asymmetry between |
127 | // `back->clone()` in the first branch, and the `init` call in the |
128 | // second branch. |
129 | |
130 | if (first) { |
131 | BackEdgeVector paths; |
132 | if (!paths.reserve(shortestPaths.maxNumPaths_)) { |
133 | return false; |
134 | } |
135 | auto cloned = back->clone(); |
136 | if (!cloned) { |
137 | return false; |
138 | } |
139 | paths.infallibleAppend(std::move(cloned)); |
140 | if (!shortestPaths.paths_.putNew(edge.referent, std::move(paths))) { |
141 | return false; |
142 | } |
143 | totalPathsRecorded++; |
144 | } else { |
145 | auto ptr = shortestPaths.paths_.lookup(edge.referent); |
146 | MOZ_ASSERT(ptr,do { static_assert( mozilla::detail::AssertionConditionType< decltype(ptr)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("ptr" " (" "This isn't the first time we have seen the target node " "`edge.referent`. " "We should have inserted it into shortestPaths.paths_ the " "first time we " "saw it." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 151); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ") (" "This isn't the first time we have seen the target node " "`edge.referent`. " "We should have inserted it into shortestPaths.paths_ the " "first time we " "saw it." ")"); do { *((volatile int*)__null ) = 151; __attribute__((nomerge)) ::abort(); } while (false); } } while (false) |
147 | "This isn't the first time we have seen the target node "do { static_assert( mozilla::detail::AssertionConditionType< decltype(ptr)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("ptr" " (" "This isn't the first time we have seen the target node " "`edge.referent`. " "We should have inserted it into shortestPaths.paths_ the " "first time we " "saw it." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 151); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ") (" "This isn't the first time we have seen the target node " "`edge.referent`. " "We should have inserted it into shortestPaths.paths_ the " "first time we " "saw it." ")"); do { *((volatile int*)__null ) = 151; __attribute__((nomerge)) ::abort(); } while (false); } } while (false) |
148 | "`edge.referent`. "do { static_assert( mozilla::detail::AssertionConditionType< decltype(ptr)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("ptr" " (" "This isn't the first time we have seen the target node " "`edge.referent`. " "We should have inserted it into shortestPaths.paths_ the " "first time we " "saw it." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 151); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ") (" "This isn't the first time we have seen the target node " "`edge.referent`. " "We should have inserted it into shortestPaths.paths_ the " "first time we " "saw it." ")"); do { *((volatile int*)__null ) = 151; __attribute__((nomerge)) ::abort(); } while (false); } } while (false) |
149 | "We should have inserted it into shortestPaths.paths_ the "do { static_assert( mozilla::detail::AssertionConditionType< decltype(ptr)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("ptr" " (" "This isn't the first time we have seen the target node " "`edge.referent`. " "We should have inserted it into shortestPaths.paths_ the " "first time we " "saw it." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 151); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ") (" "This isn't the first time we have seen the target node " "`edge.referent`. " "We should have inserted it into shortestPaths.paths_ the " "first time we " "saw it." ")"); do { *((volatile int*)__null ) = 151; __attribute__((nomerge)) ::abort(); } while (false); } } while (false) |
150 | "first time we "do { static_assert( mozilla::detail::AssertionConditionType< decltype(ptr)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("ptr" " (" "This isn't the first time we have seen the target node " "`edge.referent`. " "We should have inserted it into shortestPaths.paths_ the " "first time we " "saw it." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 151); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ") (" "This isn't the first time we have seen the target node " "`edge.referent`. " "We should have inserted it into shortestPaths.paths_ the " "first time we " "saw it." ")"); do { *((volatile int*)__null ) = 151; __attribute__((nomerge)) ::abort(); } while (false); } } while (false) |
151 | "saw it.")do { static_assert( mozilla::detail::AssertionConditionType< decltype(ptr)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(ptr))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("ptr" " (" "This isn't the first time we have seen the target node " "`edge.referent`. " "We should have inserted it into shortestPaths.paths_ the " "first time we " "saw it." ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 151); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr" ") (" "This isn't the first time we have seen the target node " "`edge.referent`. " "We should have inserted it into shortestPaths.paths_ the " "first time we " "saw it." ")"); do { *((volatile int*)__null ) = 151; __attribute__((nomerge)) ::abort(); } while (false); } } while (false); |
152 | |
153 | if (ptr->value().length() < shortestPaths.maxNumPaths_) { |
154 | auto thisBackEdge = js::MakeUnique<BackEdge>(); |
155 | if (!thisBackEdge || !thisBackEdge->init(origin, edge)) { |
156 | return false; |
157 | } |
158 | ptr->value().infallibleAppend(std::move(thisBackEdge)); |
159 | totalPathsRecorded++; |
160 | } |
161 | } |
162 | |
163 | MOZ_ASSERT(totalPathsRecorded <= totalMaxPathsToRecord)do { static_assert( mozilla::detail::AssertionConditionType< decltype(totalPathsRecorded <= totalMaxPathsToRecord)>:: isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(totalPathsRecorded <= totalMaxPathsToRecord))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("totalPathsRecorded <= totalMaxPathsToRecord" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 163); AnnotateMozCrashReason("MOZ_ASSERT" "(" "totalPathsRecorded <= totalMaxPathsToRecord" ")"); do { *((volatile int*)__null) = 163; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
164 | if (totalPathsRecorded == totalMaxPathsToRecord) { |
165 | traversal.stop(); |
166 | } |
167 | |
168 | return true; |
169 | } |
170 | }; |
171 | |
172 | // The maximum number of paths to record for each node. |
173 | uint32_t maxNumPaths_; |
174 | |
175 | // The root node we are starting the search from. |
176 | Node root_; |
177 | |
178 | // The set of nodes we are searching for paths to. |
179 | NodeSet targets_; |
180 | |
181 | // The resulting paths. |
182 | NodeToBackEdgeVectorMap paths_; |
183 | |
184 | // Need to keep alive the traversal's back edges so we can walk them later |
185 | // when the traversal is over when recreating the shortest paths. |
186 | Traversal::NodeMap backEdges_; |
187 | |
188 | private: |
189 | // Private methods. |
190 | |
191 | ShortestPaths(uint32_t maxNumPaths, const Node& root, NodeSet&& targets) |
192 | : maxNumPaths_(maxNumPaths), |
193 | root_(root), |
194 | targets_(std::move(targets)), |
195 | paths_(targets_.count()), |
196 | backEdges_() { |
197 | MOZ_ASSERT(maxNumPaths_ > 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype(maxNumPaths_ > 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(maxNumPaths_ > 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("maxNumPaths_ > 0" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 197); AnnotateMozCrashReason("MOZ_ASSERT" "(" "maxNumPaths_ > 0" ")"); do { *((volatile int*)__null) = 197; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
198 | MOZ_ASSERT(root_)do { static_assert( mozilla::detail::AssertionConditionType< decltype(root_)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(root_))), 0))) { do { } while ( false); MOZ_ReportAssertionFailure("root_", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 198); AnnotateMozCrashReason("MOZ_ASSERT" "(" "root_" ")"); do { *((volatile int*)__null) = 198; __attribute__((nomerge) ) ::abort(); } while (false); } } while (false); |
199 | } |
200 | |
201 | public: |
202 | // Public methods. |
203 | |
204 | ShortestPaths(ShortestPaths&& rhs) |
205 | : maxNumPaths_(rhs.maxNumPaths_), |
206 | root_(rhs.root_), |
207 | targets_(std::move(rhs.targets_)), |
208 | paths_(std::move(rhs.paths_)), |
209 | backEdges_(std::move(rhs.backEdges_)) { |
210 | MOZ_ASSERT(this != &rhs, "self-move is not allowed")do { static_assert( mozilla::detail::AssertionConditionType< decltype(this != &rhs)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(this != &rhs))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("this != &rhs" " (" "self-move is not allowed" ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 210); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this != &rhs" ") (" "self-move is not allowed" ")"); do { *((volatile int* )__null) = 210; __attribute__((nomerge)) ::abort(); } while ( false); } } while (false); |
211 | } |
212 | |
213 | ShortestPaths& operator=(ShortestPaths&& rhs) { |
214 | this->~ShortestPaths(); |
215 | new (this) ShortestPaths(std::move(rhs)); |
216 | return *this; |
217 | } |
218 | |
219 | ShortestPaths(const ShortestPaths&) = delete; |
220 | ShortestPaths& operator=(const ShortestPaths&) = delete; |
221 | |
222 | /** |
223 | * Construct a new `JS::ubi::ShortestPaths`, finding up to `maxNumPaths` |
224 | * shortest retaining paths for each target node in `targets` starting from |
225 | * `root`. |
226 | * |
227 | * The resulting `ShortestPaths` instance must not outlive the |
228 | * `JS::ubi::Node` graph it was constructed from. |
229 | * |
230 | * - For `JS::ubi::Node` graphs backed by the live heap graph, this means |
231 | * that the `ShortestPaths`'s lifetime _must_ be contained within the |
232 | * scope of the provided `AutoCheckCannotGC` reference because a GC will |
233 | * invalidate the nodes. |
234 | * |
235 | * - For `JS::ubi::Node` graphs backed by some other offline structure |
236 | * provided by the embedder, the resulting `ShortestPaths`'s lifetime is |
237 | * bounded by that offline structure's lifetime. |
238 | * |
239 | * Returns `mozilla::Nothing()` on OOM failure. It is the caller's |
240 | * responsibility to handle and report the OOM. |
241 | */ |
242 | static mozilla::Maybe<ShortestPaths> Create(JSContext* cx, |
243 | AutoCheckCannotGC& noGC, |
244 | uint32_t maxNumPaths, |
245 | const Node& root, |
246 | NodeSet&& targets) { |
247 | MOZ_ASSERT(targets.count() > 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype(targets.count() > 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(targets.count() > 0))), 0 ))) { do { } while (false); MOZ_ReportAssertionFailure("targets.count() > 0" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 247); AnnotateMozCrashReason("MOZ_ASSERT" "(" "targets.count() > 0" ")"); do { *((volatile int*)__null) = 247; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
248 | MOZ_ASSERT(maxNumPaths > 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype(maxNumPaths > 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(maxNumPaths > 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("maxNumPaths > 0" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 248); AnnotateMozCrashReason("MOZ_ASSERT" "(" "maxNumPaths > 0" ")"); do { *((volatile int*)__null) = 248; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
249 | |
250 | mozilla::CheckedInt<uint32_t> max = maxNumPaths; |
251 | max *= targets.count(); |
252 | if (!max.isValid()) { |
253 | return mozilla::Nothing(); |
254 | } |
255 | |
256 | ShortestPaths paths(maxNumPaths, root, std::move(targets)); |
257 | |
258 | Handler handler(paths); |
259 | Traversal traversal(cx, handler, noGC); |
260 | traversal.wantNames = true; |
261 | if (!traversal.addStart(root) || !traversal.traverse()) { |
262 | return mozilla::Nothing(); |
263 | } |
264 | |
265 | // Take ownership of the back edges we created while traversing the |
266 | // graph so that we can follow them from `paths_` and don't |
267 | // use-after-free. |
268 | paths.backEdges_ = std::move(traversal.visited); |
269 | |
270 | return mozilla::Some(std::move(paths)); |
271 | } |
272 | |
273 | /** |
274 | * Get an iterator over each target node we searched for retaining paths |
275 | * for. The returned iterator must not outlive the `ShortestPaths` |
276 | * instance. |
277 | */ |
278 | NodeSet::Iterator targetIter() const { return targets_.iter(); } |
279 | |
280 | /** |
281 | * Invoke the provided functor/lambda/callable once for each retaining path |
282 | * discovered for `target`. The `func` is passed a single `JS::ubi::Path&` |
283 | * argument, which contains each edge along the path ordered starting from |
284 | * the root and ending at the target, and must not outlive the scope of the |
285 | * call. |
286 | * |
287 | * Note that it is possible that we did not find any paths from the root to |
288 | * the given target, in which case `func` will not be invoked. |
289 | */ |
290 | template <class Func> |
291 | [[nodiscard]] bool forEachPath(const Node& target, Func func) { |
292 | MOZ_ASSERT(targets_.has(target))do { static_assert( mozilla::detail::AssertionConditionType< decltype(targets_.has(target))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(targets_.has(target)))), 0)) ) { do { } while (false); MOZ_ReportAssertionFailure("targets_.has(target)" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 292); AnnotateMozCrashReason("MOZ_ASSERT" "(" "targets_.has(target)" ")"); do { *((volatile int*)__null) = 292; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
293 | |
294 | auto ptr = paths_.lookup(target); |
295 | |
296 | // We didn't find any paths to this target, so nothing to do here. |
297 | if (!ptr) { |
298 | return true; |
299 | } |
300 | |
301 | MOZ_ASSERT(ptr->value().length() <= maxNumPaths_)do { static_assert( mozilla::detail::AssertionConditionType< decltype(ptr->value().length() <= maxNumPaths_)>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!(ptr->value().length() <= maxNumPaths_))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("ptr->value().length() <= maxNumPaths_" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 301); AnnotateMozCrashReason("MOZ_ASSERT" "(" "ptr->value().length() <= maxNumPaths_" ")"); do { *((volatile int*)__null) = 301; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
302 | |
303 | Path path; |
304 | for (const auto& backEdge : ptr->value()) { |
305 | path.clear(); |
306 | |
307 | if (!path.append(backEdge.get())) { |
308 | return false; |
309 | } |
310 | |
311 | Node here = backEdge->predecessor(); |
312 | MOZ_ASSERT(here)do { static_assert( mozilla::detail::AssertionConditionType< decltype(here)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(here))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("here", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 312); AnnotateMozCrashReason("MOZ_ASSERT" "(" "here" ")"); do { *((volatile int*)__null) = 312; __attribute__((nomerge)) :: abort(); } while (false); } } while (false); |
313 | |
314 | while (here != root_) { |
315 | auto p = backEdges_.lookup(here); |
316 | MOZ_ASSERT(p)do { static_assert( mozilla::detail::AssertionConditionType< decltype(p)>::isValid, "invalid assertion condition"); if ( (__builtin_expect(!!(!(!!(p))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("p", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 316); AnnotateMozCrashReason("MOZ_ASSERT" "(" "p" ")"); do { *((volatile int*)__null) = 316; __attribute__((nomerge)) ::abort (); } while (false); } } while (false); |
317 | if (!path.append(&p->value())) { |
318 | return false; |
319 | } |
320 | here = p->value().predecessor(); |
321 | MOZ_ASSERT(here)do { static_assert( mozilla::detail::AssertionConditionType< decltype(here)>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(!!(here))), 0))) { do { } while (false ); MOZ_ReportAssertionFailure("here", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/UbiNodeShortestPaths.h" , 321); AnnotateMozCrashReason("MOZ_ASSERT" "(" "here" ")"); do { *((volatile int*)__null) = 321; __attribute__((nomerge)) :: abort(); } while (false); } } while (false); |
322 | } |
323 | |
324 | path.reverse(); |
325 | |
326 | if (!func(path)) { |
327 | return false; |
328 | } |
329 | } |
330 | |
331 | return true; |
332 | } |
333 | }; |
334 | |
335 | #ifdef DEBUG1 |
336 | // A helper function to dump the first `maxNumPaths` shortest retaining paths to |
337 | // `node` from the GC roots. Useful when GC things you expect to have been |
338 | // reclaimed by the collector haven't been! |
339 | // |
340 | // Usage: |
341 | // |
342 | // JSObject* foo = ...; |
343 | // JS::ubi::dumpPaths(rt, JS::ubi::Node(foo)); |
344 | JS_PUBLIC_API void dumpPaths(JSRuntime* rt, Node node, |
345 | uint32_t maxNumPaths = 10); |
346 | #endif |
347 | |
348 | } // namespace ubi |
349 | } // namespace JS |
350 | |
351 | #endif // js_UbiNodeShortestPaths_h |
1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||||||||||||||
2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ | ||||||||||||||
3 | /* This Source Code Form is subject to the terms of the Mozilla Public | ||||||||||||||
4 | * License, v. 2.0. If a copy of the MPL was not distributed with this | ||||||||||||||
5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||||||||||||||
6 | |||||||||||||||
7 | //--------------------------------------------------------------------------- | ||||||||||||||
8 | // Overview | ||||||||||||||
9 | //--------------------------------------------------------------------------- | ||||||||||||||
10 | // | ||||||||||||||
11 | // This file defines HashMap<Key, Value> and HashSet<T>, hash tables that are | ||||||||||||||
12 | // fast and have a nice API. | ||||||||||||||
13 | // | ||||||||||||||
14 | // Both hash tables have two optional template parameters. | ||||||||||||||
15 | // | ||||||||||||||
16 | // - HashPolicy. This defines the operations for hashing and matching keys. The | ||||||||||||||
17 | // default HashPolicy is appropriate when both of the following two | ||||||||||||||
18 | // conditions are true. | ||||||||||||||
19 | // | ||||||||||||||
20 | // - The key type stored in the table (|Key| for |HashMap<Key, Value>|, |T| | ||||||||||||||
21 | // for |HashSet<T>|) is an integer, pointer, UniquePtr, float, or double. | ||||||||||||||
22 | // | ||||||||||||||
23 | // - The type used for lookups (|Lookup|) is the same as the key type. This | ||||||||||||||
24 | // is usually the case, but not always. | ||||||||||||||
25 | // | ||||||||||||||
26 | // There is also a |CStringHasher| policy for |char*| keys. If your keys | ||||||||||||||
27 | // don't match any of the above cases, you must provide your own hash policy; | ||||||||||||||
28 | // see the "Hash Policy" section below. | ||||||||||||||
29 | // | ||||||||||||||
30 | // - AllocPolicy. This defines how allocations are done by the table. | ||||||||||||||
31 | // | ||||||||||||||
32 | // - |MallocAllocPolicy| is the default and is usually appropriate; note that | ||||||||||||||
33 | // operations (such as insertions) that might cause allocations are | ||||||||||||||
34 | // fallible and must be checked for OOM. These checks are enforced by the | ||||||||||||||
35 | // use of [[nodiscard]]. | ||||||||||||||
36 | // | ||||||||||||||
37 | // - |InfallibleAllocPolicy| is another possibility; it allows the | ||||||||||||||
38 | // abovementioned OOM checks to be done with MOZ_ALWAYS_TRUE(). | ||||||||||||||
39 | // | ||||||||||||||
40 | // Note that entry storage allocation is lazy, and not done until the first | ||||||||||||||
41 | // lookupForAdd(), put(), or putNew() is performed. | ||||||||||||||
42 | // | ||||||||||||||
43 | // See AllocPolicy.h for more details. | ||||||||||||||
44 | // | ||||||||||||||
45 | // Documentation on how to use HashMap and HashSet, including examples, is | ||||||||||||||
46 | // present within those classes. Search for "class HashMap" and "class | ||||||||||||||
47 | // HashSet". | ||||||||||||||
48 | // | ||||||||||||||
49 | // Both HashMap and HashSet are implemented on top of a third class, HashTable. | ||||||||||||||
50 | // You only need to look at HashTable if you want to understand the | ||||||||||||||
51 | // implementation. | ||||||||||||||
52 | // | ||||||||||||||
53 | // How does mozilla::HashTable (this file) compare with PLDHashTable (and its | ||||||||||||||
54 | // subclasses, such as nsTHashtable)? | ||||||||||||||
55 | // | ||||||||||||||
56 | // - mozilla::HashTable is a lot faster, largely because it uses templates | ||||||||||||||
57 | // throughout *and* inlines everything. PLDHashTable inlines operations much | ||||||||||||||
58 | // less aggressively, and also uses "virtual ops" for operations like hashing | ||||||||||||||
59 | // and matching entries that require function calls. | ||||||||||||||
60 | // | ||||||||||||||
61 | // - Correspondingly, mozilla::HashTable use is likely to increase executable | ||||||||||||||
62 | // size much more than PLDHashTable. | ||||||||||||||
63 | // | ||||||||||||||
64 | // - mozilla::HashTable has a nicer API, with a proper HashSet vs. HashMap | ||||||||||||||
65 | // distinction. | ||||||||||||||
66 | // | ||||||||||||||
67 | // - mozilla::HashTable requires more explicit OOM checking. As mentioned | ||||||||||||||
68 | // above, the use of |InfallibleAllocPolicy| can simplify things. | ||||||||||||||
69 | // | ||||||||||||||
70 | // - mozilla::HashTable has a default capacity on creation of 32 and a minimum | ||||||||||||||
71 | // capacity of 4. PLDHashTable has a default capacity on creation of 8 and a | ||||||||||||||
72 | // minimum capacity of 8. | ||||||||||||||
73 | |||||||||||||||
74 | #ifndef mozilla_HashTable_h | ||||||||||||||
75 | #define mozilla_HashTable_h | ||||||||||||||
76 | |||||||||||||||
77 | #include <utility> | ||||||||||||||
78 | #include <type_traits> | ||||||||||||||
79 | |||||||||||||||
80 | #include "mozilla/AllocPolicy.h" | ||||||||||||||
81 | #include "mozilla/Assertions.h" | ||||||||||||||
82 | #include "mozilla/Attributes.h" | ||||||||||||||
83 | #include "mozilla/Casting.h" | ||||||||||||||
84 | #include "mozilla/HashFunctions.h" | ||||||||||||||
85 | #include "mozilla/MathAlgorithms.h" | ||||||||||||||
86 | #include "mozilla/Maybe.h" | ||||||||||||||
87 | #include "mozilla/MemoryChecking.h" | ||||||||||||||
88 | #include "mozilla/MemoryReporting.h" | ||||||||||||||
89 | #include "mozilla/Opaque.h" | ||||||||||||||
90 | #include "mozilla/OperatorNewExtensions.h" | ||||||||||||||
91 | #include "mozilla/ReentrancyGuard.h" | ||||||||||||||
92 | #include "mozilla/UniquePtr.h" | ||||||||||||||
93 | #include "mozilla/WrappingOperations.h" | ||||||||||||||
94 | |||||||||||||||
95 | namespace mozilla { | ||||||||||||||
96 | |||||||||||||||
97 | template <class, class = void> | ||||||||||||||
98 | struct DefaultHasher; | ||||||||||||||
99 | |||||||||||||||
100 | template <class, class> | ||||||||||||||
101 | class HashMapEntry; | ||||||||||||||
102 | |||||||||||||||
103 | namespace detail { | ||||||||||||||
104 | |||||||||||||||
105 | template <typename T> | ||||||||||||||
106 | class HashTableEntry; | ||||||||||||||
107 | |||||||||||||||
108 | template <class T, class HashPolicy, class AllocPolicy> | ||||||||||||||
109 | class HashTable; | ||||||||||||||
110 | |||||||||||||||
111 | } // namespace detail | ||||||||||||||
112 | |||||||||||||||
113 | // The "generation" of a hash table is an opaque value indicating the state of | ||||||||||||||
114 | // modification of the hash table through its lifetime. If the generation of | ||||||||||||||
115 | // a hash table compares equal at times T1 and T2, then lookups in the hash | ||||||||||||||
116 | // table, pointers to (or into) hash table entries, etc. at time T1 are valid | ||||||||||||||
117 | // at time T2. If the generation compares unequal, these computations are all | ||||||||||||||
118 | // invalid and must be performed again to be used. | ||||||||||||||
119 | // | ||||||||||||||
120 | // Generations are meaningfully comparable only with respect to a single hash | ||||||||||||||
121 | // table. It's always nonsensical to compare the generation of distinct hash | ||||||||||||||
122 | // tables H1 and H2. | ||||||||||||||
123 | using Generation = Opaque<uint64_t>; | ||||||||||||||
124 | |||||||||||||||
125 | //--------------------------------------------------------------------------- | ||||||||||||||
126 | // HashMap | ||||||||||||||
127 | //--------------------------------------------------------------------------- | ||||||||||||||
128 | |||||||||||||||
129 | // HashMap is a fast hash-based map from keys to values. | ||||||||||||||
130 | // | ||||||||||||||
131 | // Template parameter requirements: | ||||||||||||||
132 | // - Key/Value: movable, destructible, assignable. | ||||||||||||||
133 | // - HashPolicy: see the "Hash Policy" section below. | ||||||||||||||
134 | // - AllocPolicy: see AllocPolicy.h. | ||||||||||||||
135 | // | ||||||||||||||
136 | // Note: | ||||||||||||||
137 | // - HashMap is not reentrant: Key/Value/HashPolicy/AllocPolicy members | ||||||||||||||
138 | // called by HashMap must not call back into the same HashMap object. | ||||||||||||||
139 | // | ||||||||||||||
140 | template <class Key, class Value, class HashPolicy = DefaultHasher<Key>, | ||||||||||||||
141 | class AllocPolicy = MallocAllocPolicy> | ||||||||||||||
142 | class HashMap { | ||||||||||||||
143 | // -- Implementation details ----------------------------------------------- | ||||||||||||||
144 | |||||||||||||||
145 | // HashMap is not copyable or assignable. | ||||||||||||||
146 | HashMap(const HashMap& hm) = delete; | ||||||||||||||
147 | HashMap& operator=(const HashMap& hm) = delete; | ||||||||||||||
148 | |||||||||||||||
149 | using TableEntry = HashMapEntry<Key, Value>; | ||||||||||||||
150 | |||||||||||||||
151 | struct MapHashPolicy : HashPolicy { | ||||||||||||||
152 | using Base = HashPolicy; | ||||||||||||||
153 | using KeyType = Key; | ||||||||||||||
154 | |||||||||||||||
155 | static const Key& getKey(TableEntry& aEntry) { return aEntry.key(); } | ||||||||||||||
156 | |||||||||||||||
157 | static void setKey(TableEntry& aEntry, Key& aKey) { | ||||||||||||||
158 | HashPolicy::rekey(aEntry.mutableKey(), aKey); | ||||||||||||||
159 | } | ||||||||||||||
160 | }; | ||||||||||||||
161 | |||||||||||||||
162 | using Impl = detail::HashTable<TableEntry, MapHashPolicy, AllocPolicy>; | ||||||||||||||
163 | Impl mImpl; | ||||||||||||||
164 | |||||||||||||||
165 | friend class Impl::Enum; | ||||||||||||||
166 | |||||||||||||||
167 | public: | ||||||||||||||
168 | using Lookup = typename HashPolicy::Lookup; | ||||||||||||||
169 | using Entry = TableEntry; | ||||||||||||||
170 | |||||||||||||||
171 | // -- Initialization ------------------------------------------------------- | ||||||||||||||
172 | |||||||||||||||
173 | explicit HashMap(AllocPolicy aAllocPolicy = AllocPolicy(), | ||||||||||||||
174 | uint32_t aLen = Impl::sDefaultLen) | ||||||||||||||
175 | : mImpl(std::move(aAllocPolicy), aLen) {} | ||||||||||||||
176 | |||||||||||||||
177 | explicit HashMap(uint32_t aLen) : mImpl(AllocPolicy(), aLen) {} | ||||||||||||||
178 | |||||||||||||||
179 | // HashMap is movable. | ||||||||||||||
180 | HashMap(HashMap&& aRhs) = default; | ||||||||||||||
181 | HashMap& operator=(HashMap&& aRhs) = default; | ||||||||||||||
182 | |||||||||||||||
183 | // Swap the contents of this hash map with another. | ||||||||||||||
184 | void swap(HashMap& aOther) { mImpl.swap(aOther.mImpl); } | ||||||||||||||
185 | |||||||||||||||
186 | // -- Status and sizing ---------------------------------------------------- | ||||||||||||||
187 | |||||||||||||||
188 | // The map's current generation. | ||||||||||||||
189 | Generation generation() const { return mImpl.generation(); } | ||||||||||||||
190 | |||||||||||||||
191 | // Is the map empty? | ||||||||||||||
192 | bool empty() const { return mImpl.empty(); } | ||||||||||||||
193 | |||||||||||||||
194 | // Number of keys/values in the map. | ||||||||||||||
195 | uint32_t count() const { return mImpl.count(); } | ||||||||||||||
196 | |||||||||||||||
197 | // Number of key/value slots in the map. Note: resize will happen well before | ||||||||||||||
198 | // count() == capacity(). | ||||||||||||||
199 | uint32_t capacity() const { return mImpl.capacity(); } | ||||||||||||||
200 | |||||||||||||||
201 | // The size of the map's entry storage, in bytes. If the keys/values contain | ||||||||||||||
202 | // pointers to other heap blocks, you must iterate over the map and measure | ||||||||||||||
203 | // them separately; hence the "shallow" prefix. | ||||||||||||||
204 | size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { | ||||||||||||||
205 | return mImpl.shallowSizeOfExcludingThis(aMallocSizeOf); | ||||||||||||||
206 | } | ||||||||||||||
207 | size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { | ||||||||||||||
208 | return aMallocSizeOf(this) + | ||||||||||||||
209 | mImpl.shallowSizeOfExcludingThis(aMallocSizeOf); | ||||||||||||||
210 | } | ||||||||||||||
211 | |||||||||||||||
212 | // Attempt to minimize the capacity(). If the table is empty, this will free | ||||||||||||||
213 | // the empty storage and upon regrowth it will be given the minimum capacity. | ||||||||||||||
214 | void compact() { mImpl.compact(); } | ||||||||||||||
215 | |||||||||||||||
216 | // Attempt to reserve enough space to fit at least |aLen| elements. This is | ||||||||||||||
217 | // total capacity, including elements already present. Does nothing if the | ||||||||||||||
218 | // map already has sufficient capacity. | ||||||||||||||
219 | [[nodiscard]] bool reserve(uint32_t aLen) { return mImpl.reserve(aLen); } | ||||||||||||||
220 | |||||||||||||||
221 | // -- Lookups -------------------------------------------------------------- | ||||||||||||||
222 | |||||||||||||||
223 | // Does the map contain a key/value matching |aLookup|? | ||||||||||||||
224 | bool has(const Lookup& aLookup) const { | ||||||||||||||
225 | return mImpl.lookup(aLookup).found(); | ||||||||||||||
226 | } | ||||||||||||||
227 | |||||||||||||||
228 | // Return a Ptr indicating whether a key/value matching |aLookup| is | ||||||||||||||
229 | // present in the map. E.g.: | ||||||||||||||
230 | // | ||||||||||||||
231 | // using HM = HashMap<int,char>; | ||||||||||||||
232 | // HM h; | ||||||||||||||
233 | // if (HM::Ptr p = h.lookup(3)) { | ||||||||||||||
234 | // assert(p->key() == 3); | ||||||||||||||
235 | // char val = p->value(); | ||||||||||||||
236 | // } | ||||||||||||||
237 | // | ||||||||||||||
238 | using Ptr = typename Impl::Ptr; | ||||||||||||||
239 | MOZ_ALWAYS_INLINEinline Ptr lookup(const Lookup& aLookup) const { | ||||||||||||||
240 | return mImpl.lookup(aLookup); | ||||||||||||||
241 | } | ||||||||||||||
242 | |||||||||||||||
243 | // Like lookup(), but does not assert if two threads call it at the same | ||||||||||||||
244 | // time. Only use this method when none of the threads will modify the map. | ||||||||||||||
245 | MOZ_ALWAYS_INLINEinline Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const { | ||||||||||||||
246 | return mImpl.readonlyThreadsafeLookup(aLookup); | ||||||||||||||
247 | } | ||||||||||||||
248 | |||||||||||||||
249 | // -- Insertions ----------------------------------------------------------- | ||||||||||||||
250 | |||||||||||||||
251 | // Overwrite existing value with |aValue|, or add it if not present. Returns | ||||||||||||||
252 | // false on OOM. | ||||||||||||||
253 | template <typename KeyInput, typename ValueInput> | ||||||||||||||
254 | [[nodiscard]] bool put(KeyInput&& aKey, ValueInput&& aValue) { | ||||||||||||||
255 | return put(aKey, std::forward<KeyInput>(aKey), | ||||||||||||||
256 | std::forward<ValueInput>(aValue)); | ||||||||||||||
257 | } | ||||||||||||||
258 | |||||||||||||||
259 | template <typename KeyInput, typename ValueInput> | ||||||||||||||
260 | [[nodiscard]] bool put(const Lookup& aLookup, KeyInput&& aKey, | ||||||||||||||
261 | ValueInput&& aValue) { | ||||||||||||||
262 | AddPtr p = lookupForAdd(aLookup); | ||||||||||||||
263 | if (p) { | ||||||||||||||
264 | p->value() = std::forward<ValueInput>(aValue); | ||||||||||||||
265 | return true; | ||||||||||||||
266 | } | ||||||||||||||
267 | return add(p, std::forward<KeyInput>(aKey), | ||||||||||||||
268 | std::forward<ValueInput>(aValue)); | ||||||||||||||
269 | } | ||||||||||||||
270 | |||||||||||||||
271 | // Like put(), but slightly faster. Must only be used when the given key is | ||||||||||||||
272 | // not already present. (In debug builds, assertions check this.) | ||||||||||||||
273 | template <typename KeyInput, typename ValueInput> | ||||||||||||||
274 | [[nodiscard]] bool putNew(KeyInput&& aKey, ValueInput&& aValue) { | ||||||||||||||
275 | return mImpl.putNew(aKey, std::forward<KeyInput>(aKey), | ||||||||||||||
276 | std::forward<ValueInput>(aValue)); | ||||||||||||||
277 | } | ||||||||||||||
278 | |||||||||||||||
279 | template <typename KeyInput, typename ValueInput> | ||||||||||||||
280 | [[nodiscard]] bool putNew(const Lookup& aLookup, KeyInput&& aKey, | ||||||||||||||
281 | ValueInput&& aValue) { | ||||||||||||||
282 | return mImpl.putNew(aLookup, std::forward<KeyInput>(aKey), | ||||||||||||||
283 | std::forward<ValueInput>(aValue)); | ||||||||||||||
284 | } | ||||||||||||||
285 | |||||||||||||||
286 | // Like putNew(), but should be only used when the table is known to be big | ||||||||||||||
287 | // enough for the insertion, and hashing cannot fail. Typically this is used | ||||||||||||||
288 | // to populate an empty map with known-unique keys after reserving space with | ||||||||||||||
289 | // reserve(), e.g. | ||||||||||||||
290 | // | ||||||||||||||
291 | // using HM = HashMap<int,char>; | ||||||||||||||
292 | // HM h; | ||||||||||||||
293 | // if (!h.reserve(3)) { | ||||||||||||||
294 | // MOZ_CRASH("OOM"); | ||||||||||||||
295 | // } | ||||||||||||||
296 | // h.putNewInfallible(1, 'a'); // unique key | ||||||||||||||
297 | // h.putNewInfallible(2, 'b'); // unique key | ||||||||||||||
298 | // h.putNewInfallible(3, 'c'); // unique key | ||||||||||||||
299 | // | ||||||||||||||
300 | template <typename KeyInput, typename ValueInput> | ||||||||||||||
301 | void putNewInfallible(KeyInput&& aKey, ValueInput&& aValue) { | ||||||||||||||
302 | mImpl.putNewInfallible(aKey, std::forward<KeyInput>(aKey), | ||||||||||||||
303 | std::forward<ValueInput>(aValue)); | ||||||||||||||
304 | } | ||||||||||||||
305 | |||||||||||||||
306 | // Like |lookup(l)|, but on miss, |p = lookupForAdd(l)| allows efficient | ||||||||||||||
307 | // insertion of Key |k| (where |HashPolicy::match(k,l) == true|) using | ||||||||||||||
308 | // |add(p,k,v)|. After |add(p,k,v)|, |p| points to the new key/value. E.g.: | ||||||||||||||
309 | // | ||||||||||||||
310 | // using HM = HashMap<int,char>; | ||||||||||||||
311 | // HM h; | ||||||||||||||
312 | // HM::AddPtr p = h.lookupForAdd(3); | ||||||||||||||
313 | // if (!p) { | ||||||||||||||
314 | // if (!h.add(p, 3, 'a')) { | ||||||||||||||
315 | // return false; | ||||||||||||||
316 | // } | ||||||||||||||
317 | // } | ||||||||||||||
318 | // assert(p->key() == 3); | ||||||||||||||
319 | // char val = p->value(); | ||||||||||||||
320 | // | ||||||||||||||
321 | // N.B. The caller must ensure that no mutating hash table operations occur | ||||||||||||||
322 | // between a pair of lookupForAdd() and add() calls. To avoid looking up the | ||||||||||||||
323 | // key a second time, the caller may use the more efficient relookupOrAdd() | ||||||||||||||
324 | // method. This method reuses part of the hashing computation to more | ||||||||||||||
325 | // efficiently insert the key if it has not been added. For example, a | ||||||||||||||
326 | // mutation-handling version of the previous example: | ||||||||||||||
327 | // | ||||||||||||||
328 | // HM::AddPtr p = h.lookupForAdd(3); | ||||||||||||||
329 | // if (!p) { | ||||||||||||||
330 | // call_that_may_mutate_h(); | ||||||||||||||
331 | // if (!h.relookupOrAdd(p, 3, 'a')) { | ||||||||||||||
332 | // return false; | ||||||||||||||
333 | // } | ||||||||||||||
334 | // } | ||||||||||||||
335 | // assert(p->key() == 3); | ||||||||||||||
336 | // char val = p->value(); | ||||||||||||||
337 | // | ||||||||||||||
338 | using AddPtr = typename Impl::AddPtr; | ||||||||||||||
339 | MOZ_ALWAYS_INLINEinline AddPtr lookupForAdd(const Lookup& aLookup) { | ||||||||||||||
340 | return mImpl.lookupForAdd(aLookup); | ||||||||||||||
341 | } | ||||||||||||||
342 | |||||||||||||||
343 | // Add a key/value. Returns false on OOM. | ||||||||||||||
344 | template <typename KeyInput, typename ValueInput> | ||||||||||||||
345 | [[nodiscard]] bool add(AddPtr& aPtr, KeyInput&& aKey, ValueInput&& aValue) { | ||||||||||||||
346 | return mImpl.add(aPtr, std::forward<KeyInput>(aKey), | ||||||||||||||
347 | std::forward<ValueInput>(aValue)); | ||||||||||||||
348 | } | ||||||||||||||
349 | |||||||||||||||
350 | // See the comment above lookupForAdd() for details. | ||||||||||||||
351 | template <typename KeyInput, typename ValueInput> | ||||||||||||||
352 | [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, KeyInput&& aKey, | ||||||||||||||
353 | ValueInput&& aValue) { | ||||||||||||||
354 | return mImpl.relookupOrAdd(aPtr, aKey, std::forward<KeyInput>(aKey), | ||||||||||||||
355 | std::forward<ValueInput>(aValue)); | ||||||||||||||
356 | } | ||||||||||||||
357 | |||||||||||||||
358 | // -- Removal -------------------------------------------------------------- | ||||||||||||||
359 | |||||||||||||||
360 | // Lookup and remove the key/value matching |aLookup|, if present. | ||||||||||||||
361 | void remove(const Lookup& aLookup) { | ||||||||||||||
362 | if (Ptr p = lookup(aLookup)) { | ||||||||||||||
363 | remove(p); | ||||||||||||||
364 | } | ||||||||||||||
365 | } | ||||||||||||||
366 | |||||||||||||||
367 | // Remove a previously found key/value (assuming aPtr.found()). The map must | ||||||||||||||
368 | // not have been mutated in the interim. | ||||||||||||||
369 | void remove(Ptr aPtr) { mImpl.remove(aPtr); } | ||||||||||||||
370 | |||||||||||||||
371 | // Remove all keys/values without changing the capacity. | ||||||||||||||
372 | void clear() { mImpl.clear(); } | ||||||||||||||
373 | |||||||||||||||
374 | // Like clear() followed by compact(). | ||||||||||||||
375 | void clearAndCompact() { mImpl.clearAndCompact(); } | ||||||||||||||
376 | |||||||||||||||
377 | // -- Rekeying ------------------------------------------------------------- | ||||||||||||||
378 | |||||||||||||||
379 | // Infallibly rekey one entry, if necessary. Requires that template | ||||||||||||||
380 | // parameters Key and HashPolicy::Lookup are the same type. | ||||||||||||||
381 | void rekeyIfMoved(const Key& aOldKey, const Key& aNewKey) { | ||||||||||||||
382 | if (aOldKey != aNewKey) { | ||||||||||||||
383 | rekeyAs(aOldKey, aNewKey, aNewKey); | ||||||||||||||
384 | } | ||||||||||||||
385 | } | ||||||||||||||
386 | |||||||||||||||
387 | // Infallibly rekey one entry if present, and return whether that happened. | ||||||||||||||
388 | bool rekeyAs(const Lookup& aOldLookup, const Lookup& aNewLookup, | ||||||||||||||
389 | const Key& aNewKey) { | ||||||||||||||
390 | if (Ptr p = lookup(aOldLookup)) { | ||||||||||||||
391 | mImpl.rekeyAndMaybeRehash(p, aNewLookup, aNewKey); | ||||||||||||||
392 | return true; | ||||||||||||||
393 | } | ||||||||||||||
394 | return false; | ||||||||||||||
395 | } | ||||||||||||||
396 | |||||||||||||||
397 | // -- Iteration ------------------------------------------------------------ | ||||||||||||||
398 | |||||||||||||||
399 | // |iter()| returns an Iterator: | ||||||||||||||
400 | // | ||||||||||||||
401 | // HashMap<int, char> h; | ||||||||||||||
402 | // for (auto iter = h.iter(); !iter.done(); iter.next()) { | ||||||||||||||
403 | // char c = iter.get().value(); | ||||||||||||||
404 | // } | ||||||||||||||
405 | // | ||||||||||||||
406 | using Iterator = typename Impl::Iterator; | ||||||||||||||
407 | Iterator iter() const { return mImpl.iter(); } | ||||||||||||||
408 | |||||||||||||||
409 | // |modIter()| returns a ModIterator: | ||||||||||||||
410 | // | ||||||||||||||
411 | // HashMap<int, char> h; | ||||||||||||||
412 | // for (auto iter = h.modIter(); !iter.done(); iter.next()) { | ||||||||||||||
413 | // if (iter.get().value() == 'l') { | ||||||||||||||
414 | // iter.remove(); | ||||||||||||||
415 | // } | ||||||||||||||
416 | // } | ||||||||||||||
417 | // | ||||||||||||||
418 | // Table resize may occur in ModIterator's destructor. | ||||||||||||||
419 | using ModIterator = typename Impl::ModIterator; | ||||||||||||||
420 | ModIterator modIter() { return mImpl.modIter(); } | ||||||||||||||
421 | |||||||||||||||
422 | // These are similar to Iterator/ModIterator/iter(), but use different | ||||||||||||||
423 | // terminology. | ||||||||||||||
424 | using Range = typename Impl::Range; | ||||||||||||||
425 | using Enum = typename Impl::Enum; | ||||||||||||||
426 | Range all() const { return mImpl.all(); } | ||||||||||||||
427 | }; | ||||||||||||||
428 | |||||||||||||||
429 | //--------------------------------------------------------------------------- | ||||||||||||||
430 | // HashSet | ||||||||||||||
431 | //--------------------------------------------------------------------------- | ||||||||||||||
432 | |||||||||||||||
433 | // HashSet is a fast hash-based set of values. | ||||||||||||||
434 | // | ||||||||||||||
435 | // Template parameter requirements: | ||||||||||||||
436 | // - T: movable, destructible, assignable. | ||||||||||||||
437 | // - HashPolicy: see the "Hash Policy" section below. | ||||||||||||||
438 | // - AllocPolicy: see AllocPolicy.h | ||||||||||||||
439 | // | ||||||||||||||
440 | // Note: | ||||||||||||||
441 | // - HashSet is not reentrant: T/HashPolicy/AllocPolicy members called by | ||||||||||||||
442 | // HashSet must not call back into the same HashSet object. | ||||||||||||||
443 | // | ||||||||||||||
444 | template <class T, class HashPolicy = DefaultHasher<T>, | ||||||||||||||
445 | class AllocPolicy = MallocAllocPolicy> | ||||||||||||||
446 | class HashSet { | ||||||||||||||
447 | // -- Implementation details ----------------------------------------------- | ||||||||||||||
448 | |||||||||||||||
449 | // HashSet is not copyable or assignable. | ||||||||||||||
450 | HashSet(const HashSet& hs) = delete; | ||||||||||||||
451 | HashSet& operator=(const HashSet& hs) = delete; | ||||||||||||||
452 | |||||||||||||||
453 | struct SetHashPolicy : HashPolicy { | ||||||||||||||
454 | using Base = HashPolicy; | ||||||||||||||
455 | using KeyType = T; | ||||||||||||||
456 | |||||||||||||||
457 | static const KeyType& getKey(const T& aT) { return aT; } | ||||||||||||||
458 | |||||||||||||||
459 | static void setKey(T& aT, KeyType& aKey) { HashPolicy::rekey(aT, aKey); } | ||||||||||||||
460 | }; | ||||||||||||||
461 | |||||||||||||||
462 | using Impl = detail::HashTable<const T, SetHashPolicy, AllocPolicy>; | ||||||||||||||
463 | Impl mImpl; | ||||||||||||||
464 | |||||||||||||||
465 | friend class Impl::Enum; | ||||||||||||||
466 | |||||||||||||||
467 | public: | ||||||||||||||
468 | using Lookup = typename HashPolicy::Lookup; | ||||||||||||||
469 | using Entry = T; | ||||||||||||||
470 | |||||||||||||||
471 | // -- Initialization ------------------------------------------------------- | ||||||||||||||
472 | |||||||||||||||
473 | explicit HashSet(AllocPolicy aAllocPolicy = AllocPolicy(), | ||||||||||||||
474 | uint32_t aLen = Impl::sDefaultLen) | ||||||||||||||
475 | : mImpl(std::move(aAllocPolicy), aLen) {} | ||||||||||||||
476 | |||||||||||||||
477 | explicit HashSet(uint32_t aLen) : mImpl(AllocPolicy(), aLen) {} | ||||||||||||||
478 | |||||||||||||||
479 | // HashSet is movable. | ||||||||||||||
480 | HashSet(HashSet&& aRhs) = default; | ||||||||||||||
481 | HashSet& operator=(HashSet&& aRhs) = default; | ||||||||||||||
482 | |||||||||||||||
483 | // Swap the contents of this hash set with another. | ||||||||||||||
484 | void swap(HashSet& aOther) { mImpl.swap(aOther.mImpl); } | ||||||||||||||
485 | |||||||||||||||
486 | // -- Status and sizing ---------------------------------------------------- | ||||||||||||||
487 | |||||||||||||||
488 | // The set's current generation. | ||||||||||||||
489 | Generation generation() const { return mImpl.generation(); } | ||||||||||||||
490 | |||||||||||||||
491 | // Is the set empty? | ||||||||||||||
492 | bool empty() const { return mImpl.empty(); } | ||||||||||||||
493 | |||||||||||||||
494 | // Number of elements in the set. | ||||||||||||||
495 | uint32_t count() const { return mImpl.count(); } | ||||||||||||||
496 | |||||||||||||||
497 | // Number of element slots in the set. Note: resize will happen well before | ||||||||||||||
498 | // count() == capacity(). | ||||||||||||||
499 | uint32_t capacity() const { return mImpl.capacity(); } | ||||||||||||||
500 | |||||||||||||||
501 | // The size of the set's entry storage, in bytes. If the elements contain | ||||||||||||||
502 | // pointers to other heap blocks, you must iterate over the set and measure | ||||||||||||||
503 | // them separately; hence the "shallow" prefix. | ||||||||||||||
504 | size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { | ||||||||||||||
505 | return mImpl.shallowSizeOfExcludingThis(aMallocSizeOf); | ||||||||||||||
506 | } | ||||||||||||||
507 | size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { | ||||||||||||||
508 | return aMallocSizeOf(this) + | ||||||||||||||
509 | mImpl.shallowSizeOfExcludingThis(aMallocSizeOf); | ||||||||||||||
510 | } | ||||||||||||||
511 | |||||||||||||||
512 | // Attempt to minimize the capacity(). If the table is empty, this will free | ||||||||||||||
513 | // the empty storage and upon regrowth it will be given the minimum capacity. | ||||||||||||||
514 | void compact() { mImpl.compact(); } | ||||||||||||||
515 | |||||||||||||||
516 | // Attempt to reserve enough space to fit at least |aLen| elements. This is | ||||||||||||||
517 | // total capacity, including elements already present. Does nothing if the | ||||||||||||||
518 | // map already has sufficient capacity. | ||||||||||||||
519 | [[nodiscard]] bool reserve(uint32_t aLen) { return mImpl.reserve(aLen); } | ||||||||||||||
520 | |||||||||||||||
521 | // -- Lookups -------------------------------------------------------------- | ||||||||||||||
522 | |||||||||||||||
523 | // Does the set contain an element matching |aLookup|? | ||||||||||||||
524 | bool has(const Lookup& aLookup) const { | ||||||||||||||
525 | return mImpl.lookup(aLookup).found(); | ||||||||||||||
526 | } | ||||||||||||||
527 | |||||||||||||||
528 | // Return a Ptr indicating whether an element matching |aLookup| is present | ||||||||||||||
529 | // in the set. E.g.: | ||||||||||||||
530 | // | ||||||||||||||
531 | // using HS = HashSet<int>; | ||||||||||||||
532 | // HS h; | ||||||||||||||
533 | // if (HS::Ptr p = h.lookup(3)) { | ||||||||||||||
534 | // assert(*p == 3); // p acts like a pointer to int | ||||||||||||||
535 | // } | ||||||||||||||
536 | // | ||||||||||||||
537 | using Ptr = typename Impl::Ptr; | ||||||||||||||
538 | MOZ_ALWAYS_INLINEinline Ptr lookup(const Lookup& aLookup) const { | ||||||||||||||
539 | return mImpl.lookup(aLookup); | ||||||||||||||
540 | } | ||||||||||||||
541 | |||||||||||||||
542 | // Like lookup(), but does not assert if two threads call it at the same | ||||||||||||||
543 | // time. Only use this method when none of the threads will modify the set. | ||||||||||||||
544 | MOZ_ALWAYS_INLINEinline Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const { | ||||||||||||||
545 | return mImpl.readonlyThreadsafeLookup(aLookup); | ||||||||||||||
546 | } | ||||||||||||||
547 | |||||||||||||||
548 | // -- Insertions ----------------------------------------------------------- | ||||||||||||||
549 | |||||||||||||||
550 | // Add |aU| if it is not present already. Returns false on OOM. | ||||||||||||||
551 | template <typename U> | ||||||||||||||
552 | [[nodiscard]] bool put(U&& aU) { | ||||||||||||||
553 | AddPtr p = lookupForAdd(aU); | ||||||||||||||
554 | return p ? true : add(p, std::forward<U>(aU)); | ||||||||||||||
555 | } | ||||||||||||||
556 | |||||||||||||||
557 | // Like put(), but slightly faster. Must only be used when the given element | ||||||||||||||
558 | // is not already present. (In debug builds, assertions check this.) | ||||||||||||||
559 | template <typename U> | ||||||||||||||
560 | [[nodiscard]] bool putNew(U&& aU) { | ||||||||||||||
561 | return mImpl.putNew(aU, std::forward<U>(aU)); | ||||||||||||||
562 | } | ||||||||||||||
563 | |||||||||||||||
564 | // Like the other putNew(), but for when |Lookup| is different to |T|. | ||||||||||||||
565 | template <typename U> | ||||||||||||||
566 | [[nodiscard]] bool putNew(const Lookup& aLookup, U&& aU) { | ||||||||||||||
567 | return mImpl.putNew(aLookup, std::forward<U>(aU)); | ||||||||||||||
568 | } | ||||||||||||||
569 | |||||||||||||||
570 | // Like putNew(), but should be only used when the table is known to be big | ||||||||||||||
571 | // enough for the insertion, and hashing cannot fail. Typically this is used | ||||||||||||||
572 | // to populate an empty set with known-unique elements after reserving space | ||||||||||||||
573 | // with reserve(), e.g. | ||||||||||||||
574 | // | ||||||||||||||
575 | // using HS = HashMap<int>; | ||||||||||||||
576 | // HS h; | ||||||||||||||
577 | // if (!h.reserve(3)) { | ||||||||||||||
578 | // MOZ_CRASH("OOM"); | ||||||||||||||
579 | // } | ||||||||||||||
580 | // h.putNewInfallible(1); // unique element | ||||||||||||||
581 | // h.putNewInfallible(2); // unique element | ||||||||||||||
582 | // h.putNewInfallible(3); // unique element | ||||||||||||||
583 | // | ||||||||||||||
584 | template <typename U> | ||||||||||||||
585 | void putNewInfallible(const Lookup& aLookup, U&& aU) { | ||||||||||||||
586 | mImpl.putNewInfallible(aLookup, std::forward<U>(aU)); | ||||||||||||||
587 | } | ||||||||||||||
588 | |||||||||||||||
589 | // Like |lookup(l)|, but on miss, |p = lookupForAdd(l)| allows efficient | ||||||||||||||
590 | // insertion of T value |t| (where |HashPolicy::match(t,l) == true|) using | ||||||||||||||
591 | // |add(p,t)|. After |add(p,t)|, |p| points to the new element. E.g.: | ||||||||||||||
592 | // | ||||||||||||||
593 | // using HS = HashSet<int>; | ||||||||||||||
594 | // HS h; | ||||||||||||||
595 | // HS::AddPtr p = h.lookupForAdd(3); | ||||||||||||||
596 | // if (!p) { | ||||||||||||||
597 | // if (!h.add(p, 3)) { | ||||||||||||||
598 | // return false; | ||||||||||||||
599 | // } | ||||||||||||||
600 | // } | ||||||||||||||
601 | // assert(*p == 3); // p acts like a pointer to int | ||||||||||||||
602 | // | ||||||||||||||
603 | // N.B. The caller must ensure that no mutating hash table operations occur | ||||||||||||||
604 | // between a pair of lookupForAdd() and add() calls. To avoid looking up the | ||||||||||||||
605 | // key a second time, the caller may use the more efficient relookupOrAdd() | ||||||||||||||
606 | // method. This method reuses part of the hashing computation to more | ||||||||||||||
607 | // efficiently insert the key if it has not been added. For example, a | ||||||||||||||
608 | // mutation-handling version of the previous example: | ||||||||||||||
609 | // | ||||||||||||||
610 | // HS::AddPtr p = h.lookupForAdd(3); | ||||||||||||||
611 | // if (!p) { | ||||||||||||||
612 | // call_that_may_mutate_h(); | ||||||||||||||
613 | // if (!h.relookupOrAdd(p, 3, 3)) { | ||||||||||||||
614 | // return false; | ||||||||||||||
615 | // } | ||||||||||||||
616 | // } | ||||||||||||||
617 | // assert(*p == 3); | ||||||||||||||
618 | // | ||||||||||||||
619 | // Note that relookupOrAdd(p,l,t) performs Lookup using |l| and adds the | ||||||||||||||
620 | // entry |t|, where the caller ensures match(l,t). | ||||||||||||||
621 | using AddPtr = typename Impl::AddPtr; | ||||||||||||||
622 | MOZ_ALWAYS_INLINEinline AddPtr lookupForAdd(const Lookup& aLookup) { | ||||||||||||||
623 | return mImpl.lookupForAdd(aLookup); | ||||||||||||||
624 | } | ||||||||||||||
625 | |||||||||||||||
626 | // Add an element. Returns false on OOM. | ||||||||||||||
627 | template <typename U> | ||||||||||||||
628 | [[nodiscard]] bool add(AddPtr& aPtr, U&& aU) { | ||||||||||||||
629 | return mImpl.add(aPtr, std::forward<U>(aU)); | ||||||||||||||
630 | } | ||||||||||||||
631 | |||||||||||||||
632 | // See the comment above lookupForAdd() for details. | ||||||||||||||
633 | template <typename U> | ||||||||||||||
634 | [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, const Lookup& aLookup, | ||||||||||||||
635 | U&& aU) { | ||||||||||||||
636 | return mImpl.relookupOrAdd(aPtr, aLookup, std::forward<U>(aU)); | ||||||||||||||
637 | } | ||||||||||||||
638 | |||||||||||||||
639 | // -- Removal -------------------------------------------------------------- | ||||||||||||||
640 | |||||||||||||||
641 | // Lookup and remove the element matching |aLookup|, if present. | ||||||||||||||
642 | void remove(const Lookup& aLookup) { | ||||||||||||||
643 | if (Ptr p = lookup(aLookup)) { | ||||||||||||||
644 | remove(p); | ||||||||||||||
645 | } | ||||||||||||||
646 | } | ||||||||||||||
647 | |||||||||||||||
648 | // Remove a previously found element (assuming aPtr.found()). The set must | ||||||||||||||
649 | // not have been mutated in the interim. | ||||||||||||||
650 | void remove(Ptr aPtr) { mImpl.remove(aPtr); } | ||||||||||||||
651 | |||||||||||||||
652 | // Remove all keys/values without changing the capacity. | ||||||||||||||
653 | void clear() { mImpl.clear(); } | ||||||||||||||
654 | |||||||||||||||
655 | // Like clear() followed by compact(). | ||||||||||||||
656 | void clearAndCompact() { mImpl.clearAndCompact(); } | ||||||||||||||
657 | |||||||||||||||
658 | // -- Rekeying ------------------------------------------------------------- | ||||||||||||||
659 | |||||||||||||||
660 | // Infallibly rekey one entry, if present. Requires that template parameters | ||||||||||||||
661 | // T and HashPolicy::Lookup are the same type. | ||||||||||||||
662 | void rekeyIfMoved(const Lookup& aOldValue, const T& aNewValue) { | ||||||||||||||
663 | if (aOldValue != aNewValue) { | ||||||||||||||
664 | rekeyAs(aOldValue, aNewValue, aNewValue); | ||||||||||||||
665 | } | ||||||||||||||
666 | } | ||||||||||||||
667 | |||||||||||||||
668 | // Infallibly rekey one entry if present, and return whether that happened. | ||||||||||||||
669 | bool rekeyAs(const Lookup& aOldLookup, const Lookup& aNewLookup, | ||||||||||||||
670 | const T& aNewValue) { | ||||||||||||||
671 | if (Ptr p = lookup(aOldLookup)) { | ||||||||||||||
672 | mImpl.rekeyAndMaybeRehash(p, aNewLookup, aNewValue); | ||||||||||||||
673 | return true; | ||||||||||||||
674 | } | ||||||||||||||
675 | return false; | ||||||||||||||
676 | } | ||||||||||||||
677 | |||||||||||||||
678 | // Infallibly replace the current key at |aPtr| with an equivalent key. | ||||||||||||||
679 | // Specifically, both HashPolicy::hash and HashPolicy::match must return | ||||||||||||||
680 | // identical results for the new and old key when applied against all | ||||||||||||||
681 | // possible matching values. | ||||||||||||||
682 | void replaceKey(Ptr aPtr, const Lookup& aLookup, const T& aNewValue) { | ||||||||||||||
683 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 683); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.found()" ")"); do { *((volatile int*)__null) = 683; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
684 | 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" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 684); AnnotateMozCrashReason("MOZ_ASSERT" "(" "*aPtr != aNewValue" ")"); do { *((volatile int*)__null) = 684; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
685 | 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)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 685); AnnotateMozCrashReason("MOZ_ASSERT" "(" "HashPolicy::match(*aPtr, aLookup)" ")"); do { *((volatile int*)__null) = 685; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
686 | MOZ_ASSERT(HashPolicy::match(aNewValue, aLookup))do { static_assert( mozilla::detail::AssertionConditionType< decltype(HashPolicy::match(aNewValue, aLookup))>::isValid, "invalid assertion condition"); if ((__builtin_expect(!!(!(! !(HashPolicy::match(aNewValue, aLookup)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("HashPolicy::match(aNewValue, aLookup)" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 686); AnnotateMozCrashReason("MOZ_ASSERT" "(" "HashPolicy::match(aNewValue, aLookup)" ")"); do { *((volatile int*)__null) = 686; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
687 | const_cast<T&>(*aPtr) = aNewValue; | ||||||||||||||
688 | 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", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 688); AnnotateMozCrashReason("MOZ_ASSERT" "(" "*lookup(aLookup) == aNewValue" ")"); do { *((volatile int*)__null) = 688; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
689 | } | ||||||||||||||
690 | void replaceKey(Ptr aPtr, const T& aNewValue) { | ||||||||||||||
691 | replaceKey(aPtr, aNewValue, aNewValue); | ||||||||||||||
692 | } | ||||||||||||||
693 | |||||||||||||||
694 | // -- Iteration ------------------------------------------------------------ | ||||||||||||||
695 | |||||||||||||||
696 | // |iter()| returns an Iterator: | ||||||||||||||
697 | // | ||||||||||||||
698 | // HashSet<int> h; | ||||||||||||||
699 | // for (auto iter = h.iter(); !iter.done(); iter.next()) { | ||||||||||||||
700 | // int i = iter.get(); | ||||||||||||||
701 | // } | ||||||||||||||
702 | // | ||||||||||||||
703 | using Iterator = typename Impl::Iterator; | ||||||||||||||
704 | Iterator iter() const { return mImpl.iter(); } | ||||||||||||||
705 | |||||||||||||||
706 | // |modIter()| returns a ModIterator: | ||||||||||||||
707 | // | ||||||||||||||
708 | // HashSet<int> h; | ||||||||||||||
709 | // for (auto iter = h.modIter(); !iter.done(); iter.next()) { | ||||||||||||||
710 | // if (iter.get() == 42) { | ||||||||||||||
711 | // iter.remove(); | ||||||||||||||
712 | // } | ||||||||||||||
713 | // } | ||||||||||||||
714 | // | ||||||||||||||
715 | // Table resize may occur in ModIterator's destructor. | ||||||||||||||
716 | using ModIterator = typename Impl::ModIterator; | ||||||||||||||
717 | ModIterator modIter() { return mImpl.modIter(); } | ||||||||||||||
718 | |||||||||||||||
719 | // These are similar to Iterator/ModIterator/iter(), but use different | ||||||||||||||
720 | // terminology. | ||||||||||||||
721 | using Range = typename Impl::Range; | ||||||||||||||
722 | using Enum = typename Impl::Enum; | ||||||||||||||
723 | Range all() const { return mImpl.all(); } | ||||||||||||||
724 | }; | ||||||||||||||
725 | |||||||||||||||
726 | //--------------------------------------------------------------------------- | ||||||||||||||
727 | // Hash Policy | ||||||||||||||
728 | //--------------------------------------------------------------------------- | ||||||||||||||
729 | |||||||||||||||
730 | // A hash policy |HP| for a hash table with key-type |Key| must provide: | ||||||||||||||
731 | // | ||||||||||||||
732 | // - a type |HP::Lookup| to use to lookup table entries; | ||||||||||||||
733 | // | ||||||||||||||
734 | // - a static member function |HP::hash| that hashes lookup values: | ||||||||||||||
735 | // | ||||||||||||||
736 | // static mozilla::HashNumber hash(const Lookup&); | ||||||||||||||
737 | // | ||||||||||||||
738 | // - a static member function |HP::match| that tests equality of key and | ||||||||||||||
739 | // lookup values: | ||||||||||||||
740 | // | ||||||||||||||
741 | // static bool match(const Key& aKey, const Lookup& aLookup); | ||||||||||||||
742 | // | ||||||||||||||
743 | // |aKey| and |aLookup| can have different hash numbers, only when a | ||||||||||||||
744 | // collision happens with |prepareHash| operation, which is less frequent. | ||||||||||||||
745 | // Thus, |HP::match| shouldn't assume the hash equality in the comparison, | ||||||||||||||
746 | // even if the hash numbers are almost always same between them. | ||||||||||||||
747 | // | ||||||||||||||
748 | // Normally, Lookup = Key. In general, though, different values and types of | ||||||||||||||
749 | // values can be used to lookup and store. If a Lookup value |l| is not equal | ||||||||||||||
750 | // to the added Key value |k|, the user must ensure that |HP::match(k,l)| is | ||||||||||||||
751 | // true. E.g.: | ||||||||||||||
752 | // | ||||||||||||||
753 | // mozilla::HashSet<Key, HP>::AddPtr p = h.lookup(l); | ||||||||||||||
754 | // if (!p) { | ||||||||||||||
755 | // assert(HP::match(k, l)); // must hold | ||||||||||||||
756 | // h.add(p, k); | ||||||||||||||
757 | // } | ||||||||||||||
758 | |||||||||||||||
759 | // A pointer hashing policy that uses HashGeneric() to create good hashes for | ||||||||||||||
760 | // pointers. Note that we don't shift out the lowest k bits because we don't | ||||||||||||||
761 | // want to assume anything about the alignment of the pointers. | ||||||||||||||
762 | template <typename Key> | ||||||||||||||
763 | struct PointerHasher { | ||||||||||||||
764 | using Lookup = Key; | ||||||||||||||
765 | |||||||||||||||
766 | static HashNumber hash(const Lookup& aLookup) { return HashGeneric(aLookup); } | ||||||||||||||
767 | |||||||||||||||
768 | static bool match(const Key& aKey, const Lookup& aLookup) { | ||||||||||||||
769 | return aKey == aLookup; | ||||||||||||||
770 | } | ||||||||||||||
771 | |||||||||||||||
772 | static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; } | ||||||||||||||
773 | }; | ||||||||||||||
774 | |||||||||||||||
775 | // The default hash policy, which only works with integers. | ||||||||||||||
776 | template <class Key, typename> | ||||||||||||||
777 | struct DefaultHasher { | ||||||||||||||
778 | using Lookup = Key; | ||||||||||||||
779 | |||||||||||||||
780 | static HashNumber hash(const Lookup& aLookup) { | ||||||||||||||
781 | // Just convert the integer to a HashNumber and use that as is. (This | ||||||||||||||
782 | // discards the high 32-bits of 64-bit integers!) ScrambleHashCode() is | ||||||||||||||
783 | // subsequently called on the value to improve the distribution. | ||||||||||||||
784 | return aLookup; | ||||||||||||||
785 | } | ||||||||||||||
786 | |||||||||||||||
787 | static bool match(const Key& aKey, const Lookup& aLookup) { | ||||||||||||||
788 | // Use builtin or overloaded operator==. | ||||||||||||||
789 | return aKey == aLookup; | ||||||||||||||
790 | } | ||||||||||||||
791 | |||||||||||||||
792 | static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; } | ||||||||||||||
793 | }; | ||||||||||||||
794 | |||||||||||||||
795 | // A DefaultHasher specialization for enums. | ||||||||||||||
796 | template <class T> | ||||||||||||||
797 | struct DefaultHasher<T, std::enable_if_t<std::is_enum_v<T>>> { | ||||||||||||||
798 | using Key = T; | ||||||||||||||
799 | using Lookup = Key; | ||||||||||||||
800 | |||||||||||||||
801 | static HashNumber hash(const Lookup& aLookup) { return HashGeneric(aLookup); } | ||||||||||||||
802 | |||||||||||||||
803 | static bool match(const Key& aKey, const Lookup& aLookup) { | ||||||||||||||
804 | // Use builtin or overloaded operator==. | ||||||||||||||
805 | return aKey == static_cast<Key>(aLookup); | ||||||||||||||
806 | } | ||||||||||||||
807 | |||||||||||||||
808 | static void rekey(Key& aKey, const Key& aNewKey) { aKey = aNewKey; } | ||||||||||||||
809 | }; | ||||||||||||||
810 | |||||||||||||||
811 | // A DefaultHasher specialization for pointers. | ||||||||||||||
812 | template <class T> | ||||||||||||||
813 | struct DefaultHasher<T*> : PointerHasher<T*> {}; | ||||||||||||||
814 | |||||||||||||||
815 | // A DefaultHasher specialization for mozilla::UniquePtr. | ||||||||||||||
816 | template <class T, class D> | ||||||||||||||
817 | struct DefaultHasher<UniquePtr<T, D>> { | ||||||||||||||
818 | using Key = UniquePtr<T, D>; | ||||||||||||||
819 | using Lookup = Key; | ||||||||||||||
820 | using PtrHasher = PointerHasher<T*>; | ||||||||||||||
821 | |||||||||||||||
822 | static HashNumber hash(const Lookup& aLookup) { | ||||||||||||||
823 | return PtrHasher::hash(aLookup.get()); | ||||||||||||||
824 | } | ||||||||||||||
825 | |||||||||||||||
826 | static bool match(const Key& aKey, const Lookup& aLookup) { | ||||||||||||||
827 | return PtrHasher::match(aKey.get(), aLookup.get()); | ||||||||||||||
828 | } | ||||||||||||||
829 | |||||||||||||||
830 | static void rekey(UniquePtr<T, D>& aKey, UniquePtr<T, D>&& aNewKey) { | ||||||||||||||
831 | aKey = std::move(aNewKey); | ||||||||||||||
832 | } | ||||||||||||||
833 | }; | ||||||||||||||
834 | |||||||||||||||
835 | // A DefaultHasher specialization for doubles. | ||||||||||||||
836 | template <> | ||||||||||||||
837 | struct DefaultHasher<double> { | ||||||||||||||
838 | using Key = double; | ||||||||||||||
839 | using Lookup = Key; | ||||||||||||||
840 | |||||||||||||||
841 | static HashNumber hash(const Lookup& aLookup) { | ||||||||||||||
842 | // Just xor the high bits with the low bits, and then treat the bits of the | ||||||||||||||
843 | // result as a uint32_t. | ||||||||||||||
844 | static_assert(sizeof(HashNumber) == 4, | ||||||||||||||
845 | "subsequent code assumes a four-byte hash"); | ||||||||||||||
846 | uint64_t u = BitwiseCast<uint64_t>(aLookup); | ||||||||||||||
847 | return HashNumber(u ^ (u >> 32)); | ||||||||||||||
848 | } | ||||||||||||||
849 | |||||||||||||||
850 | static bool match(const Key& aKey, const Lookup& aLookup) { | ||||||||||||||
851 | return BitwiseCast<uint64_t>(aKey) == BitwiseCast<uint64_t>(aLookup); | ||||||||||||||
852 | } | ||||||||||||||
853 | }; | ||||||||||||||
854 | |||||||||||||||
855 | // A DefaultHasher specialization for floats. | ||||||||||||||
856 | template <> | ||||||||||||||
857 | struct DefaultHasher<float> { | ||||||||||||||
858 | using Key = float; | ||||||||||||||
859 | using Lookup = Key; | ||||||||||||||
860 | |||||||||||||||
861 | static HashNumber hash(const Lookup& aLookup) { | ||||||||||||||
862 | // Just use the value as if its bits form an integer. ScrambleHashCode() is | ||||||||||||||
863 | // subsequently called on the value to improve the distribution. | ||||||||||||||
864 | static_assert(sizeof(HashNumber) == 4, | ||||||||||||||
865 | "subsequent code assumes a four-byte hash"); | ||||||||||||||
866 | return HashNumber(BitwiseCast<uint32_t>(aLookup)); | ||||||||||||||
867 | } | ||||||||||||||
868 | |||||||||||||||
869 | static bool match(const Key& aKey, const Lookup& aLookup) { | ||||||||||||||
870 | return BitwiseCast<uint32_t>(aKey) == BitwiseCast<uint32_t>(aLookup); | ||||||||||||||
871 | } | ||||||||||||||
872 | }; | ||||||||||||||
873 | |||||||||||||||
874 | // A hash policy for C strings. | ||||||||||||||
875 | struct CStringHasher { | ||||||||||||||
876 | using Key = const char*; | ||||||||||||||
877 | using Lookup = const char*; | ||||||||||||||
878 | |||||||||||||||
879 | static HashNumber hash(const Lookup& aLookup) { return HashString(aLookup); } | ||||||||||||||
880 | |||||||||||||||
881 | static bool match(const Key& aKey, const Lookup& aLookup) { | ||||||||||||||
882 | return strcmp(aKey, aLookup) == 0; | ||||||||||||||
883 | } | ||||||||||||||
884 | }; | ||||||||||||||
885 | |||||||||||||||
886 | //--------------------------------------------------------------------------- | ||||||||||||||
887 | // Fallible Hashing Interface | ||||||||||||||
888 | //--------------------------------------------------------------------------- | ||||||||||||||
889 | |||||||||||||||
890 | // Most of the time generating a hash code is infallible, but sometimes it is | ||||||||||||||
891 | // necessary to generate hash codes on demand in a way that can fail. Specialize | ||||||||||||||
892 | // this class for your own hash policy to provide fallible hashing. | ||||||||||||||
893 | // | ||||||||||||||
894 | // This is used by MovableCellHasher to handle the fact that generating a unique | ||||||||||||||
895 | // ID for cell pointer may fail due to OOM. | ||||||||||||||
896 | // | ||||||||||||||
897 | // The default implementations of these methods delegate to the usual HashPolicy | ||||||||||||||
898 | // implementation and always succeed. | ||||||||||||||
899 | template <typename HashPolicy> | ||||||||||||||
900 | struct FallibleHashMethods { | ||||||||||||||
901 | // Return true if a hashcode is already available for its argument, and | ||||||||||||||
902 | // sets |aHashOut|. Once this succeeds for a specific argument it | ||||||||||||||
903 | // must continue to do so. | ||||||||||||||
904 | // | ||||||||||||||
905 | // Return false if a hashcode is not already available. This implies that any | ||||||||||||||
906 | // lookup must fail, as the hash code would have to have been successfully | ||||||||||||||
907 | // created on insertion. | ||||||||||||||
908 | template <typename Lookup> | ||||||||||||||
909 | static bool maybeGetHash(Lookup&& aLookup, HashNumber* aHashOut) { | ||||||||||||||
910 | *aHashOut = HashPolicy::hash(aLookup); | ||||||||||||||
911 | return true; | ||||||||||||||
912 | } | ||||||||||||||
913 | |||||||||||||||
914 | // Fallible method to ensure a hashcode exists for its argument and create one | ||||||||||||||
915 | // if not. Sets |aHashOut| to the hashcode and retuns true on success. Returns | ||||||||||||||
916 | // false on error, e.g. out of memory. | ||||||||||||||
917 | template <typename Lookup> | ||||||||||||||
918 | static bool ensureHash(Lookup&& aLookup, HashNumber* aHashOut) { | ||||||||||||||
919 | *aHashOut = HashPolicy::hash(aLookup); | ||||||||||||||
920 | return true; | ||||||||||||||
921 | } | ||||||||||||||
922 | }; | ||||||||||||||
923 | |||||||||||||||
924 | template <typename HashPolicy, typename Lookup> | ||||||||||||||
925 | static bool MaybeGetHash(Lookup&& aLookup, HashNumber* aHashOut) { | ||||||||||||||
926 | return FallibleHashMethods<typename HashPolicy::Base>::maybeGetHash( | ||||||||||||||
927 | std::forward<Lookup>(aLookup), aHashOut); | ||||||||||||||
928 | } | ||||||||||||||
929 | |||||||||||||||
930 | template <typename HashPolicy, typename Lookup> | ||||||||||||||
931 | static bool EnsureHash(Lookup&& aLookup, HashNumber* aHashOut) { | ||||||||||||||
932 | return FallibleHashMethods<typename HashPolicy::Base>::ensureHash( | ||||||||||||||
933 | std::forward<Lookup>(aLookup), aHashOut); | ||||||||||||||
934 | } | ||||||||||||||
935 | |||||||||||||||
936 | //--------------------------------------------------------------------------- | ||||||||||||||
937 | // Implementation Details (HashMapEntry, HashTableEntry, HashTable) | ||||||||||||||
938 | //--------------------------------------------------------------------------- | ||||||||||||||
939 | |||||||||||||||
940 | // Both HashMap and HashSet are implemented by a single HashTable that is even | ||||||||||||||
941 | // more heavily parameterized than the other two. This leaves HashTable gnarly | ||||||||||||||
942 | // and extremely coupled to HashMap and HashSet; thus code should not use | ||||||||||||||
943 | // HashTable directly. | ||||||||||||||
944 | |||||||||||||||
945 | template <class Key, class Value> | ||||||||||||||
946 | class HashMapEntry { | ||||||||||||||
947 | Key key_; | ||||||||||||||
948 | Value value_; | ||||||||||||||
949 | |||||||||||||||
950 | template <class, class, class> | ||||||||||||||
951 | friend class detail::HashTable; | ||||||||||||||
952 | template <class> | ||||||||||||||
953 | friend class detail::HashTableEntry; | ||||||||||||||
954 | template <class, class, class, class> | ||||||||||||||
955 | friend class HashMap; | ||||||||||||||
956 | |||||||||||||||
957 | public: | ||||||||||||||
958 | template <typename KeyInput, typename ValueInput> | ||||||||||||||
959 | HashMapEntry(KeyInput&& aKey, ValueInput&& aValue) | ||||||||||||||
960 | : key_(std::forward<KeyInput>(aKey)), | ||||||||||||||
961 | value_(std::forward<ValueInput>(aValue)) {} | ||||||||||||||
962 | |||||||||||||||
963 | HashMapEntry(HashMapEntry&& aRhs) = default; | ||||||||||||||
964 | HashMapEntry& operator=(HashMapEntry&& aRhs) = default; | ||||||||||||||
965 | |||||||||||||||
966 | using KeyType = Key; | ||||||||||||||
967 | using ValueType = Value; | ||||||||||||||
968 | |||||||||||||||
969 | const Key& key() const { return key_; } | ||||||||||||||
970 | |||||||||||||||
971 | // Use this method with caution! If the key is changed such that its hash | ||||||||||||||
972 | // value also changes, the map will be left in an invalid state. | ||||||||||||||
973 | Key& mutableKey() { return key_; } | ||||||||||||||
974 | |||||||||||||||
975 | const Value& value() const { return value_; } | ||||||||||||||
976 | Value& value() { return value_; } | ||||||||||||||
977 | |||||||||||||||
978 | private: | ||||||||||||||
979 | HashMapEntry(const HashMapEntry&) = delete; | ||||||||||||||
980 | void operator=(const HashMapEntry&) = delete; | ||||||||||||||
981 | }; | ||||||||||||||
982 | |||||||||||||||
983 | namespace detail { | ||||||||||||||
984 | |||||||||||||||
985 | template <class T, class HashPolicy, class AllocPolicy> | ||||||||||||||
986 | class HashTable; | ||||||||||||||
987 | |||||||||||||||
988 | template <typename T> | ||||||||||||||
989 | class EntrySlot; | ||||||||||||||
990 | |||||||||||||||
991 | template <typename T> | ||||||||||||||
992 | class HashTableEntry { | ||||||||||||||
993 | private: | ||||||||||||||
994 | using NonConstT = std::remove_const_t<T>; | ||||||||||||||
995 | |||||||||||||||
996 | // Instead of having a hash table entry store that looks like this: | ||||||||||||||
997 | // | ||||||||||||||
998 | // +--------+--------+--------+--------+ | ||||||||||||||
999 | // | entry0 | entry1 | .... | entryN | | ||||||||||||||
1000 | // +--------+--------+--------+--------+ | ||||||||||||||
1001 | // | ||||||||||||||
1002 | // where the entries contained their cached hash code, we're going to lay out | ||||||||||||||
1003 | // the entry store thusly: | ||||||||||||||
1004 | // | ||||||||||||||
1005 | // +-------+-------+-------+-------+--------+--------+--------+--------+ | ||||||||||||||
1006 | // | hash0 | hash1 | ... | hashN | entry0 | entry1 | .... | entryN | | ||||||||||||||
1007 | // +-------+-------+-------+-------+--------+--------+--------+--------+ | ||||||||||||||
1008 | // | ||||||||||||||
1009 | // with all the cached hashes prior to the actual entries themselves. | ||||||||||||||
1010 | // | ||||||||||||||
1011 | // We do this because implementing the first strategy requires us to make | ||||||||||||||
1012 | // HashTableEntry look roughly like: | ||||||||||||||
1013 | // | ||||||||||||||
1014 | // template <typename T> | ||||||||||||||
1015 | // class HashTableEntry { | ||||||||||||||
1016 | // HashNumber mKeyHash; | ||||||||||||||
1017 | // T mValue; | ||||||||||||||
1018 | // }; | ||||||||||||||
1019 | // | ||||||||||||||
1020 | // The problem with this setup is that, depending on the layout of `T`, there | ||||||||||||||
1021 | // may be platform ABI-mandated padding between `mKeyHash` and the first | ||||||||||||||
1022 | // member of `T`. This ABI-mandated padding is wasted space, and can be | ||||||||||||||
1023 | // surprisingly common, e.g. when `T` is a single pointer on 64-bit platforms. | ||||||||||||||
1024 | // In such cases, we're throwing away a quarter of our entry store on padding, | ||||||||||||||
1025 | // which is undesirable. | ||||||||||||||
1026 | // | ||||||||||||||
1027 | // The second layout above, namely: | ||||||||||||||
1028 | // | ||||||||||||||
1029 | // +-------+-------+-------+-------+--------+--------+--------+--------+ | ||||||||||||||
1030 | // | hash0 | hash1 | ... | hashN | entry0 | entry1 | .... | entryN | | ||||||||||||||
1031 | // +-------+-------+-------+-------+--------+--------+--------+--------+ | ||||||||||||||
1032 | // | ||||||||||||||
1033 | // means there is no wasted space between the hashes themselves, and no wasted | ||||||||||||||
1034 | // space between the entries themselves. However, we would also like there to | ||||||||||||||
1035 | // be no gap between the last hash and the first entry. The memory allocator | ||||||||||||||
1036 | // guarantees the alignment of the start of the hashes. The use of a | ||||||||||||||
1037 | // power-of-two capacity of at least 4 guarantees that the alignment of the | ||||||||||||||
1038 | // *end* of the hash array is no less than the alignment of the start. | ||||||||||||||
1039 | // Finally, the static_asserts here guarantee that the entries themselves | ||||||||||||||
1040 | // don't need to be any more aligned than the alignment of the entry store | ||||||||||||||
1041 | // itself. | ||||||||||||||
1042 | // | ||||||||||||||
1043 | // This assertion is safe for 32-bit builds because on both Windows and Linux | ||||||||||||||
1044 | // (including Android), the minimum alignment for allocations larger than 8 | ||||||||||||||
1045 | // bytes is 8 bytes, and the actual data for entries in our entry store is | ||||||||||||||
1046 | // guaranteed to have that alignment as well, thanks to the power-of-two | ||||||||||||||
1047 | // number of cached hash values stored prior to the entry data. | ||||||||||||||
1048 | |||||||||||||||
1049 | // The allocation policy must allocate a table with at least this much | ||||||||||||||
1050 | // alignment. | ||||||||||||||
1051 | static constexpr size_t kMinimumAlignment = 8; | ||||||||||||||
1052 | |||||||||||||||
1053 | static_assert(alignof(HashNumber) <= kMinimumAlignment, | ||||||||||||||
1054 | "[N*2 hashes, N*2 T values] allocation's alignment must be " | ||||||||||||||
1055 | "enough to align each hash"); | ||||||||||||||
1056 | static_assert(alignof(NonConstT) <= 2 * sizeof(HashNumber), | ||||||||||||||
1057 | "subsequent N*2 T values must not require more than an even " | ||||||||||||||
1058 | "number of HashNumbers provides"); | ||||||||||||||
1059 | |||||||||||||||
1060 | static const HashNumber sFreeKey = 0; | ||||||||||||||
1061 | static const HashNumber sRemovedKey = 1; | ||||||||||||||
1062 | static const HashNumber sCollisionBit = 1; | ||||||||||||||
1063 | |||||||||||||||
1064 | alignas(NonConstT) unsigned char mValueData[sizeof(NonConstT)]; | ||||||||||||||
1065 | |||||||||||||||
1066 | private: | ||||||||||||||
1067 | template <class, class, class> | ||||||||||||||
1068 | friend class HashTable; | ||||||||||||||
1069 | template <typename> | ||||||||||||||
1070 | friend class EntrySlot; | ||||||||||||||
1071 | |||||||||||||||
1072 | // Some versions of GCC treat it as a -Wstrict-aliasing violation (ergo a | ||||||||||||||
1073 | // -Werror compile error) to reinterpret_cast<> |mValueData| to |T*|, even | ||||||||||||||
1074 | // through |void*|. Placing the latter cast in these separate functions | ||||||||||||||
1075 | // breaks the chain such that affected GCC versions no longer warn/error. | ||||||||||||||
1076 | void* rawValuePtr() { return mValueData; } | ||||||||||||||
1077 | |||||||||||||||
1078 | static bool isLiveHash(HashNumber hash) { return hash > sRemovedKey; } | ||||||||||||||
1079 | |||||||||||||||
1080 | HashTableEntry(const HashTableEntry&) = delete; | ||||||||||||||
1081 | void operator=(const HashTableEntry&) = delete; | ||||||||||||||
1082 | |||||||||||||||
1083 | NonConstT* valuePtr() { return reinterpret_cast<NonConstT*>(rawValuePtr()); } | ||||||||||||||
1084 | |||||||||||||||
1085 | void destroyStoredT() { | ||||||||||||||
1086 | NonConstT* ptr = valuePtr(); | ||||||||||||||
1087 | ptr->~T(); | ||||||||||||||
1088 | MOZ_MAKE_MEM_UNDEFINED(ptr, sizeof(*ptr))do { } while (0); | ||||||||||||||
1089 | } | ||||||||||||||
1090 | |||||||||||||||
1091 | public: | ||||||||||||||
1092 | HashTableEntry() = default; | ||||||||||||||
1093 | |||||||||||||||
1094 | ~HashTableEntry() { MOZ_MAKE_MEM_UNDEFINED(this, sizeof(*this))do { } while (0); } | ||||||||||||||
1095 | |||||||||||||||
1096 | void destroy() { destroyStoredT(); } | ||||||||||||||
1097 | |||||||||||||||
1098 | void swap(HashTableEntry* aOther, bool aIsLive) { | ||||||||||||||
1099 | // This allows types to use Argument-Dependent-Lookup, and thus use a custom | ||||||||||||||
1100 | // std::swap, which is needed by types like JS::Heap and such. | ||||||||||||||
1101 | using std::swap; | ||||||||||||||
1102 | |||||||||||||||
1103 | if (this == aOther) { | ||||||||||||||
1104 | return; | ||||||||||||||
1105 | } | ||||||||||||||
1106 | if (aIsLive) { | ||||||||||||||
1107 | swap(*valuePtr(), *aOther->valuePtr()); | ||||||||||||||
1108 | } else { | ||||||||||||||
1109 | *aOther->valuePtr() = std::move(*valuePtr()); | ||||||||||||||
1110 | destroy(); | ||||||||||||||
1111 | } | ||||||||||||||
1112 | } | ||||||||||||||
1113 | |||||||||||||||
1114 | T& get() { return *valuePtr(); } | ||||||||||||||
1115 | |||||||||||||||
1116 | NonConstT& getMutable() { return *valuePtr(); } | ||||||||||||||
1117 | }; | ||||||||||||||
1118 | |||||||||||||||
1119 | // A slot represents a cached hash value and its associated entry stored | ||||||||||||||
1120 | // in the hash table. These two things are not stored in contiguous memory. | ||||||||||||||
1121 | template <class T> | ||||||||||||||
1122 | class EntrySlot { | ||||||||||||||
1123 | using NonConstT = std::remove_const_t<T>; | ||||||||||||||
1124 | |||||||||||||||
1125 | using Entry = HashTableEntry<T>; | ||||||||||||||
1126 | |||||||||||||||
1127 | Entry* mEntry; | ||||||||||||||
1128 | HashNumber* mKeyHash; | ||||||||||||||
1129 | |||||||||||||||
1130 | template <class, class, class> | ||||||||||||||
1131 | friend class HashTable; | ||||||||||||||
1132 | |||||||||||||||
1133 | EntrySlot(Entry* aEntry, HashNumber* aKeyHash) | ||||||||||||||
1134 | : mEntry(aEntry), mKeyHash(aKeyHash) {} | ||||||||||||||
1135 | |||||||||||||||
1136 | public: | ||||||||||||||
1137 | static bool isLiveHash(HashNumber hash) { return hash > Entry::sRemovedKey; } | ||||||||||||||
1138 | |||||||||||||||
1139 | EntrySlot(const EntrySlot&) = default; | ||||||||||||||
1140 | EntrySlot(EntrySlot&& aOther) = default; | ||||||||||||||
1141 | |||||||||||||||
1142 | EntrySlot& operator=(const EntrySlot&) = default; | ||||||||||||||
1143 | EntrySlot& operator=(EntrySlot&&) = default; | ||||||||||||||
1144 | |||||||||||||||
1145 | bool operator==(const EntrySlot& aRhs) const { return mEntry == aRhs.mEntry; } | ||||||||||||||
1146 | |||||||||||||||
1147 | bool operator<(const EntrySlot& aRhs) const { return mEntry < aRhs.mEntry; } | ||||||||||||||
1148 | |||||||||||||||
1149 | EntrySlot& operator++() { | ||||||||||||||
1150 | ++mEntry; | ||||||||||||||
1151 | ++mKeyHash; | ||||||||||||||
1152 | return *this; | ||||||||||||||
1153 | } | ||||||||||||||
1154 | |||||||||||||||
1155 | void destroy() { mEntry->destroy(); } | ||||||||||||||
1156 | |||||||||||||||
1157 | void swap(EntrySlot& aOther) { | ||||||||||||||
1158 | mEntry->swap(aOther.mEntry, aOther.isLive()); | ||||||||||||||
1159 | std::swap(*mKeyHash, *aOther.mKeyHash); | ||||||||||||||
1160 | } | ||||||||||||||
1161 | |||||||||||||||
1162 | T& get() const { return mEntry->get(); } | ||||||||||||||
1163 | |||||||||||||||
1164 | NonConstT& getMutable() { return mEntry->getMutable(); } | ||||||||||||||
1165 | |||||||||||||||
1166 | bool isFree() const { return *mKeyHash == Entry::sFreeKey; } | ||||||||||||||
1167 | |||||||||||||||
1168 | void clearLive() { | ||||||||||||||
1169 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1169); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { *((volatile int*)__null) = 1169; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1170 | *mKeyHash = Entry::sFreeKey; | ||||||||||||||
1171 | mEntry->destroyStoredT(); | ||||||||||||||
1172 | } | ||||||||||||||
1173 | |||||||||||||||
1174 | void clear() { | ||||||||||||||
1175 | if (isLive()) { | ||||||||||||||
1176 | mEntry->destroyStoredT(); | ||||||||||||||
1177 | } | ||||||||||||||
1178 | MOZ_MAKE_MEM_UNDEFINED(mEntry, sizeof(*mEntry))do { } while (0); | ||||||||||||||
1179 | *mKeyHash = Entry::sFreeKey; | ||||||||||||||
1180 | } | ||||||||||||||
1181 | |||||||||||||||
1182 | bool isRemoved() const { return *mKeyHash == Entry::sRemovedKey; } | ||||||||||||||
1183 | |||||||||||||||
1184 | void removeLive() { | ||||||||||||||
1185 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1185); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { *((volatile int*)__null) = 1185; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1186 | *mKeyHash = Entry::sRemovedKey; | ||||||||||||||
1187 | mEntry->destroyStoredT(); | ||||||||||||||
1188 | } | ||||||||||||||
1189 | |||||||||||||||
1190 | bool isLive() const { return isLiveHash(*mKeyHash); } | ||||||||||||||
1191 | |||||||||||||||
1192 | void setCollision() { | ||||||||||||||
1193 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1193); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { *((volatile int*)__null) = 1193; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1194 | *mKeyHash |= Entry::sCollisionBit; | ||||||||||||||
1195 | } | ||||||||||||||
1196 | void unsetCollision() { *mKeyHash &= ~Entry::sCollisionBit; } | ||||||||||||||
1197 | bool hasCollision() const { return *mKeyHash & Entry::sCollisionBit; } | ||||||||||||||
1198 | bool matchHash(HashNumber hn) { | ||||||||||||||
1199 | return (*mKeyHash & ~Entry::sCollisionBit) == hn; | ||||||||||||||
1200 | } | ||||||||||||||
1201 | HashNumber getKeyHash() const { return *mKeyHash & ~Entry::sCollisionBit; } | ||||||||||||||
1202 | |||||||||||||||
1203 | template <typename... Args> | ||||||||||||||
1204 | void setLive(HashNumber aHashNumber, Args&&... aArgs) { | ||||||||||||||
1205 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1205); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!isLive()" ")" ); do { *((volatile int*)__null) = 1205; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1206 | *mKeyHash = aHashNumber; | ||||||||||||||
1207 | new (KnownNotNull, mEntry->valuePtr()) T(std::forward<Args>(aArgs)...); | ||||||||||||||
1208 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1208); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { *((volatile int*)__null) = 1208; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1209 | } | ||||||||||||||
1210 | |||||||||||||||
1211 | Entry* toEntry() const { return mEntry; } | ||||||||||||||
1212 | }; | ||||||||||||||
1213 | |||||||||||||||
1214 | template <class T, class HashPolicy, class AllocPolicy> | ||||||||||||||
1215 | class HashTable : private AllocPolicy { | ||||||||||||||
1216 | friend class mozilla::ReentrancyGuard; | ||||||||||||||
1217 | |||||||||||||||
1218 | using NonConstT = std::remove_const_t<T>; | ||||||||||||||
1219 | using Key = typename HashPolicy::KeyType; | ||||||||||||||
1220 | using Lookup = typename HashPolicy::Lookup; | ||||||||||||||
1221 | |||||||||||||||
1222 | public: | ||||||||||||||
1223 | using Entry = HashTableEntry<T>; | ||||||||||||||
1224 | using Slot = EntrySlot<T>; | ||||||||||||||
1225 | |||||||||||||||
1226 | template <typename F> | ||||||||||||||
1227 | static void forEachSlot(char* aTable, uint32_t aCapacity, F&& f) { | ||||||||||||||
1228 | auto hashes = reinterpret_cast<HashNumber*>(aTable); | ||||||||||||||
1229 | auto entries = reinterpret_cast<Entry*>(&hashes[aCapacity]); | ||||||||||||||
1230 | Slot slot(entries, hashes); | ||||||||||||||
1231 | for (size_t i = 0; i < size_t(aCapacity); ++i) { | ||||||||||||||
1232 | f(slot); | ||||||||||||||
1233 | ++slot; | ||||||||||||||
1234 | } | ||||||||||||||
1235 | } | ||||||||||||||
1236 | |||||||||||||||
1237 | // A nullable pointer to a hash table element. A Ptr |p| can be tested | ||||||||||||||
1238 | // either explicitly |if (p.found()) p->...| or using boolean conversion | ||||||||||||||
1239 | // |if (p) p->...|. Ptr objects must not be used after any mutating hash | ||||||||||||||
1240 | // table operations unless |generation()| is tested. | ||||||||||||||
1241 | class Ptr { | ||||||||||||||
1242 | friend class HashTable; | ||||||||||||||
1243 | |||||||||||||||
1244 | Slot mSlot; | ||||||||||||||
1245 | #ifdef DEBUG1 | ||||||||||||||
1246 | const HashTable* mTable; | ||||||||||||||
1247 | Generation mGeneration; | ||||||||||||||
1248 | #endif | ||||||||||||||
1249 | |||||||||||||||
1250 | protected: | ||||||||||||||
1251 | Ptr(Slot aSlot, const HashTable& aTable) | ||||||||||||||
1252 | : mSlot(aSlot) | ||||||||||||||
1253 | #ifdef DEBUG1 | ||||||||||||||
1254 | , | ||||||||||||||
1255 | mTable(&aTable), | ||||||||||||||
1256 | mGeneration(aTable.generation()) | ||||||||||||||
1257 | #endif | ||||||||||||||
1258 | { | ||||||||||||||
1259 | } | ||||||||||||||
1260 | |||||||||||||||
1261 | // This constructor is used only by AddPtr() within lookupForAdd(). | ||||||||||||||
1262 | explicit Ptr(const HashTable& aTable) | ||||||||||||||
1263 | : mSlot(nullptr, nullptr) | ||||||||||||||
1264 | #ifdef DEBUG1 | ||||||||||||||
1265 | , | ||||||||||||||
1266 | mTable(&aTable), | ||||||||||||||
1267 | mGeneration(aTable.generation()) | ||||||||||||||
1268 | #endif | ||||||||||||||
1269 | { | ||||||||||||||
1270 | } | ||||||||||||||
1271 | |||||||||||||||
1272 | bool isValid() const { return !!mSlot.toEntry(); } | ||||||||||||||
1273 | |||||||||||||||
1274 | public: | ||||||||||||||
1275 | Ptr() | ||||||||||||||
1276 | : mSlot(nullptr, nullptr) | ||||||||||||||
1277 | #ifdef DEBUG1 | ||||||||||||||
1278 | , | ||||||||||||||
1279 | mTable(nullptr), | ||||||||||||||
1280 | mGeneration(0) | ||||||||||||||
1281 | #endif | ||||||||||||||
1282 | { | ||||||||||||||
1283 | } | ||||||||||||||
1284 | |||||||||||||||
1285 | bool found() const { | ||||||||||||||
1286 | if (!isValid()) { | ||||||||||||||
1287 | return false; | ||||||||||||||
1288 | } | ||||||||||||||
1289 | #ifdef DEBUG1 | ||||||||||||||
1290 | 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()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1290); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()" ")"); do { *((volatile int*)__null) = 1290; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1291 | #endif | ||||||||||||||
1292 | return mSlot.isLive(); | ||||||||||||||
1293 | } | ||||||||||||||
1294 | |||||||||||||||
1295 | explicit operator bool() const { return found(); } | ||||||||||||||
1296 | |||||||||||||||
1297 | bool operator==(const Ptr& aRhs) const { | ||||||||||||||
1298 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1298); AnnotateMozCrashReason("MOZ_ASSERT" "(" "found() && aRhs.found()" ")"); do { *((volatile int*)__null) = 1298; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1299 | return mSlot == aRhs.mSlot; | ||||||||||||||
1300 | } | ||||||||||||||
1301 | |||||||||||||||
1302 | bool operator!=(const Ptr& aRhs) const { | ||||||||||||||
1303 | #ifdef DEBUG1 | ||||||||||||||
1304 | 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()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1304); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()" ")"); do { *((volatile int*)__null) = 1304; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1305 | #endif | ||||||||||||||
1306 | return !(*this == aRhs); | ||||||||||||||
1307 | } | ||||||||||||||
1308 | |||||||||||||||
1309 | T& operator*() const { | ||||||||||||||
1310 | #ifdef DEBUG1 | ||||||||||||||
1311 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1311); AnnotateMozCrashReason("MOZ_ASSERT" "(" "found()" ")" ); do { *((volatile int*)__null) = 1311; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1312 | 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()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1312); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()" ")"); do { *((volatile int*)__null) = 1312; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1313 | #endif | ||||||||||||||
1314 | return mSlot.get(); | ||||||||||||||
1315 | } | ||||||||||||||
1316 | |||||||||||||||
1317 | T* operator->() const { | ||||||||||||||
1318 | #ifdef DEBUG1 | ||||||||||||||
1319 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1319); AnnotateMozCrashReason("MOZ_ASSERT" "(" "found()" ")" ); do { *((volatile int*)__null) = 1319; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1320 | 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()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1320); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable->generation()" ")"); do { *((volatile int*)__null) = 1320; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1321 | #endif | ||||||||||||||
1322 | return &mSlot.get(); | ||||||||||||||
1323 | } | ||||||||||||||
1324 | }; | ||||||||||||||
1325 | |||||||||||||||
1326 | // A Ptr that can be used to add a key after a failed lookup. | ||||||||||||||
1327 | class AddPtr : public Ptr { | ||||||||||||||
1328 | friend class HashTable; | ||||||||||||||
1329 | |||||||||||||||
1330 | HashNumber mKeyHash; | ||||||||||||||
1331 | #ifdef DEBUG1 | ||||||||||||||
1332 | uint64_t mMutationCount; | ||||||||||||||
1333 | #endif | ||||||||||||||
1334 | |||||||||||||||
1335 | AddPtr(Slot aSlot, const HashTable& aTable, HashNumber aHashNumber) | ||||||||||||||
1336 | : Ptr(aSlot, aTable), | ||||||||||||||
1337 | mKeyHash(aHashNumber) | ||||||||||||||
1338 | #ifdef DEBUG1 | ||||||||||||||
1339 | , | ||||||||||||||
1340 | mMutationCount(aTable.mMutationCount) | ||||||||||||||
1341 | #endif | ||||||||||||||
1342 | { | ||||||||||||||
1343 | } | ||||||||||||||
1344 | |||||||||||||||
1345 | // This constructor is used when lookupForAdd() is performed on a table | ||||||||||||||
1346 | // lacking entry storage; it leaves mSlot null but initializes everything | ||||||||||||||
1347 | // else. | ||||||||||||||
1348 | AddPtr(const HashTable& aTable, HashNumber aHashNumber) | ||||||||||||||
1349 | : Ptr(aTable), | ||||||||||||||
1350 | mKeyHash(aHashNumber) | ||||||||||||||
1351 | #ifdef DEBUG1 | ||||||||||||||
1352 | , | ||||||||||||||
1353 | mMutationCount(aTable.mMutationCount) | ||||||||||||||
1354 | #endif | ||||||||||||||
1355 | { | ||||||||||||||
1356 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1356); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLive()" ")" ); do { *((volatile int*)__null) = 1356; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1357 | } | ||||||||||||||
1358 | |||||||||||||||
1359 | bool isLive() const { return isLiveHash(mKeyHash); } | ||||||||||||||
1360 | |||||||||||||||
1361 | public: | ||||||||||||||
1362 | AddPtr() : mKeyHash(0) {} | ||||||||||||||
1363 | }; | ||||||||||||||
1364 | |||||||||||||||
1365 | // A hash table iterator that (mostly) doesn't allow table modifications. | ||||||||||||||
1366 | // As with Ptr/AddPtr, Iterator objects must not be used after any mutating | ||||||||||||||
1367 | // hash table operation unless the |generation()| is tested. | ||||||||||||||
1368 | class Iterator { | ||||||||||||||
1369 | void moveToNextLiveEntry() { | ||||||||||||||
1370 | while (++mCur < mEnd && !mCur.isLive()) { | ||||||||||||||
1371 | continue; | ||||||||||||||
1372 | } | ||||||||||||||
1373 | } | ||||||||||||||
1374 | |||||||||||||||
1375 | protected: | ||||||||||||||
1376 | friend class HashTable; | ||||||||||||||
1377 | |||||||||||||||
1378 | explicit Iterator(const HashTable& aTable) | ||||||||||||||
1379 | : mCur(aTable.slotForIndex(0)), | ||||||||||||||
1380 | mEnd(aTable.slotForIndex(aTable.capacity())) | ||||||||||||||
1381 | #ifdef DEBUG1 | ||||||||||||||
1382 | , | ||||||||||||||
1383 | mTable(aTable), | ||||||||||||||
1384 | mMutationCount(aTable.mMutationCount), | ||||||||||||||
1385 | mGeneration(aTable.generation()), | ||||||||||||||
1386 | mValidEntry(true) | ||||||||||||||
1387 | #endif | ||||||||||||||
1388 | { | ||||||||||||||
1389 | if (!done() && !mCur.isLive()) { | ||||||||||||||
1390 | moveToNextLiveEntry(); | ||||||||||||||
1391 | } | ||||||||||||||
1392 | } | ||||||||||||||
1393 | |||||||||||||||
1394 | Slot mCur; | ||||||||||||||
1395 | Slot mEnd; | ||||||||||||||
1396 | #ifdef DEBUG1 | ||||||||||||||
1397 | const HashTable& mTable; | ||||||||||||||
1398 | uint64_t mMutationCount; | ||||||||||||||
1399 | Generation mGeneration; | ||||||||||||||
1400 | bool mValidEntry; | ||||||||||||||
1401 | #endif | ||||||||||||||
1402 | |||||||||||||||
1403 | public: | ||||||||||||||
1404 | bool done() const { | ||||||||||||||
1405 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1405); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable.generation()" ")"); do { *((volatile int*)__null) = 1405; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1406 | 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" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1406); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mMutationCount == mTable.mMutationCount" ")"); do { *((volatile int*)__null) = 1406; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1407 | return mCur == mEnd; | ||||||||||||||
1408 | } | ||||||||||||||
1409 | |||||||||||||||
1410 | T& get() const { | ||||||||||||||
1411 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1411); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!done()" ")" ); do { *((volatile int*)__null) = 1411; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1412 | 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", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1412); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mValidEntry" ")"); do { *((volatile int*)__null) = 1412; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1413 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1413); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable.generation()" ")"); do { *((volatile int*)__null) = 1413; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1414 | 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" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1414); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mMutationCount == mTable.mMutationCount" ")"); do { *((volatile int*)__null) = 1414; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1415 | return mCur.get(); | ||||||||||||||
1416 | } | ||||||||||||||
1417 | |||||||||||||||
1418 | void next() { | ||||||||||||||
1419 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1419); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!done()" ")" ); do { *((volatile int*)__null) = 1419; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1420 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1420); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mGeneration == mTable.generation()" ")"); do { *((volatile int*)__null) = 1420; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1421 | 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" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1421); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mMutationCount == mTable.mMutationCount" ")"); do { *((volatile int*)__null) = 1421; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1422 | moveToNextLiveEntry(); | ||||||||||||||
1423 | #ifdef DEBUG1 | ||||||||||||||
1424 | mValidEntry = true; | ||||||||||||||
1425 | #endif | ||||||||||||||
1426 | } | ||||||||||||||
1427 | }; | ||||||||||||||
1428 | |||||||||||||||
1429 | // A hash table iterator that permits modification, removal and rekeying. | ||||||||||||||
1430 | // Since rehashing when elements were removed during enumeration would be | ||||||||||||||
1431 | // bad, it is postponed until the ModIterator is destructed. Since the | ||||||||||||||
1432 | // ModIterator's destructor touches the hash table, the user must ensure | ||||||||||||||
1433 | // that the hash table is still alive when the destructor runs. | ||||||||||||||
1434 | class ModIterator : public Iterator { | ||||||||||||||
1435 | friend class HashTable; | ||||||||||||||
1436 | |||||||||||||||
1437 | HashTable& mTable; | ||||||||||||||
1438 | bool mRekeyed; | ||||||||||||||
1439 | bool mRemoved; | ||||||||||||||
1440 | |||||||||||||||
1441 | // ModIterator is movable but not copyable. | ||||||||||||||
1442 | ModIterator(const ModIterator&) = delete; | ||||||||||||||
1443 | void operator=(const ModIterator&) = delete; | ||||||||||||||
1444 | |||||||||||||||
1445 | protected: | ||||||||||||||
1446 | explicit ModIterator(HashTable& aTable) | ||||||||||||||
1447 | : Iterator(aTable), mTable(aTable), mRekeyed(false), mRemoved(false) {} | ||||||||||||||
1448 | |||||||||||||||
1449 | public: | ||||||||||||||
1450 | MOZ_IMPLICIT ModIterator(ModIterator&& aOther) | ||||||||||||||
1451 | : Iterator(aOther), | ||||||||||||||
1452 | mTable(aOther.mTable), | ||||||||||||||
1453 | mRekeyed(aOther.mRekeyed), | ||||||||||||||
1454 | mRemoved(aOther.mRemoved) { | ||||||||||||||
1455 | aOther.mRekeyed = false; | ||||||||||||||
1456 | aOther.mRemoved = false; | ||||||||||||||
1457 | } | ||||||||||||||
1458 | |||||||||||||||
1459 | // Removes the current element from the table, leaving |get()| | ||||||||||||||
1460 | // invalid until the next call to |next()|. | ||||||||||||||
1461 | void remove() { | ||||||||||||||
1462 | mTable.remove(this->mCur); | ||||||||||||||
1463 | mRemoved = true; | ||||||||||||||
1464 | #ifdef DEBUG1 | ||||||||||||||
1465 | this->mValidEntry = false; | ||||||||||||||
1466 | this->mMutationCount = mTable.mMutationCount; | ||||||||||||||
1467 | #endif | ||||||||||||||
1468 | } | ||||||||||||||
1469 | |||||||||||||||
1470 | NonConstT& getMutable() { | ||||||||||||||
1471 | 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()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1471); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!this->done()" ")"); do { *((volatile int*)__null) = 1471; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1472 | 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" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1472); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this->mValidEntry" ")"); do { *((volatile int*)__null) = 1472; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1473 | 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()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1473); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this->mGeneration == this->Iterator::mTable.generation()" ")"); do { *((volatile int*)__null) = 1473; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1474 | 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" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1474); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this->mMutationCount == this->Iterator::mTable.mMutationCount" ")"); do { *((volatile int*)__null) = 1474; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1475 | return this->mCur.getMutable(); | ||||||||||||||
1476 | } | ||||||||||||||
1477 | |||||||||||||||
1478 | // Removes the current element and re-inserts it into the table with | ||||||||||||||
1479 | // a new key at the new Lookup position. |get()| is invalid after | ||||||||||||||
1480 | // this operation until the next call to |next()|. | ||||||||||||||
1481 | void rekey(const Lookup& l, const Key& k) { | ||||||||||||||
1482 | MOZ_ASSERT(&k != &HashPolicy::getKey(this->mCur.get()))do { static_assert( mozilla::detail::AssertionConditionType< decltype(&k != &HashPolicy::getKey(this->mCur.get( )))>::isValid, "invalid assertion condition"); if ((__builtin_expect (!!(!(!!(&k != &HashPolicy::getKey(this->mCur.get( ))))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("&k != &HashPolicy::getKey(this->mCur.get())", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1482); AnnotateMozCrashReason("MOZ_ASSERT" "(" "&k != &HashPolicy::getKey(this->mCur.get())" ")"); do { *((volatile int*)__null) = 1482; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1483 | Ptr p(this->mCur, mTable); | ||||||||||||||
1484 | mTable.rekeyWithoutRehash(p, l, k); | ||||||||||||||
1485 | mRekeyed = true; | ||||||||||||||
1486 | #ifdef DEBUG1 | ||||||||||||||
1487 | this->mValidEntry = false; | ||||||||||||||
1488 | this->mMutationCount = mTable.mMutationCount; | ||||||||||||||
1489 | #endif | ||||||||||||||
1490 | } | ||||||||||||||
1491 | |||||||||||||||
1492 | void rekey(const Key& k) { rekey(k, k); } | ||||||||||||||
1493 | |||||||||||||||
1494 | // Potentially rehashes the table. | ||||||||||||||
1495 | ~ModIterator() { | ||||||||||||||
1496 | if (mRekeyed) { | ||||||||||||||
1497 | mTable.mGen++; | ||||||||||||||
1498 | mTable.infallibleRehashIfOverloaded(); | ||||||||||||||
1499 | } | ||||||||||||||
1500 | |||||||||||||||
1501 | if (mRemoved) { | ||||||||||||||
1502 | mTable.compact(); | ||||||||||||||
1503 | } | ||||||||||||||
1504 | } | ||||||||||||||
1505 | }; | ||||||||||||||
1506 | |||||||||||||||
1507 | // Range is similar to Iterator, but uses different terminology. | ||||||||||||||
1508 | class Range { | ||||||||||||||
1509 | friend class HashTable; | ||||||||||||||
1510 | |||||||||||||||
1511 | Iterator mIter; | ||||||||||||||
1512 | |||||||||||||||
1513 | protected: | ||||||||||||||
1514 | explicit Range(const HashTable& table) : mIter(table) {} | ||||||||||||||
1515 | |||||||||||||||
1516 | public: | ||||||||||||||
1517 | bool empty() const { return mIter.done(); } | ||||||||||||||
1518 | |||||||||||||||
1519 | T& front() const { return mIter.get(); } | ||||||||||||||
1520 | |||||||||||||||
1521 | void popFront() { return mIter.next(); } | ||||||||||||||
1522 | }; | ||||||||||||||
1523 | |||||||||||||||
1524 | // Enum is similar to ModIterator, but uses different terminology. | ||||||||||||||
1525 | class Enum { | ||||||||||||||
1526 | ModIterator mIter; | ||||||||||||||
1527 | |||||||||||||||
1528 | // Enum is movable but not copyable. | ||||||||||||||
1529 | Enum(const Enum&) = delete; | ||||||||||||||
1530 | void operator=(const Enum&) = delete; | ||||||||||||||
1531 | |||||||||||||||
1532 | public: | ||||||||||||||
1533 | template <class Map> | ||||||||||||||
1534 | explicit Enum(Map& map) : mIter(map.mImpl) {} | ||||||||||||||
1535 | |||||||||||||||
1536 | MOZ_IMPLICIT Enum(Enum&& other) : mIter(std::move(other.mIter)) {} | ||||||||||||||
1537 | |||||||||||||||
1538 | bool empty() const { return mIter.done(); } | ||||||||||||||
1539 | |||||||||||||||
1540 | T& front() const { return mIter.get(); } | ||||||||||||||
1541 | |||||||||||||||
1542 | void popFront() { return mIter.next(); } | ||||||||||||||
1543 | |||||||||||||||
1544 | void removeFront() { mIter.remove(); } | ||||||||||||||
1545 | |||||||||||||||
1546 | NonConstT& mutableFront() { return mIter.getMutable(); } | ||||||||||||||
1547 | |||||||||||||||
1548 | void rekeyFront(const Lookup& aLookup, const Key& aKey) { | ||||||||||||||
1549 | mIter.rekey(aLookup, aKey); | ||||||||||||||
1550 | } | ||||||||||||||
1551 | |||||||||||||||
1552 | void rekeyFront(const Key& aKey) { mIter.rekey(aKey); } | ||||||||||||||
1553 | }; | ||||||||||||||
1554 | |||||||||||||||
1555 | // HashTable is movable | ||||||||||||||
1556 | HashTable(HashTable&& aRhs) : AllocPolicy(std::move(aRhs)) { moveFrom(aRhs); } | ||||||||||||||
1557 | HashTable& operator=(HashTable&& aRhs) { | ||||||||||||||
1558 | 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" ")", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1558); AnnotateMozCrashReason("MOZ_ASSERT" "(" "this != &aRhs" ") (" "self-move assignment is prohibited" ")"); do { *((volatile int*)__null) = 1558; __attribute__((nomerge)) ::abort(); } while (false); } } while (false); | ||||||||||||||
1559 | if (mTable) { | ||||||||||||||
1560 | destroyTable(*this, mTable, capacity()); | ||||||||||||||
1561 | } | ||||||||||||||
1562 | AllocPolicy::operator=(std::move(aRhs)); | ||||||||||||||
1563 | moveFrom(aRhs); | ||||||||||||||
1564 | return *this; | ||||||||||||||
1565 | } | ||||||||||||||
1566 | |||||||||||||||
1567 | void swap(HashTable& aOther) { | ||||||||||||||
1568 | ReentrancyGuard g1(*this); | ||||||||||||||
1569 | ReentrancyGuard g2(aOther); | ||||||||||||||
1570 | |||||||||||||||
1571 | // Manual swap of generation because it's a bitfield | ||||||||||||||
1572 | uint64_t generation = mGen; | ||||||||||||||
1573 | mGen = aOther.mGen; | ||||||||||||||
1574 | aOther.mGen = generation; | ||||||||||||||
1575 | |||||||||||||||
1576 | // Manual swap of hashShift because it's a bitfield | ||||||||||||||
1577 | uint64_t hashShift = mHashShift; | ||||||||||||||
1578 | mHashShift = aOther.mHashShift; | ||||||||||||||
1579 | aOther.mHashShift = hashShift; | ||||||||||||||
1580 | |||||||||||||||
1581 | std::swap(mTable, aOther.mTable); | ||||||||||||||
1582 | std::swap(mEntryCount, aOther.mEntryCount); | ||||||||||||||
1583 | std::swap(mRemovedCount, aOther.mRemovedCount); | ||||||||||||||
1584 | #ifdef DEBUG1 | ||||||||||||||
1585 | std::swap(mMutationCount, aOther.mMutationCount); | ||||||||||||||
1586 | std::swap(mEntered, aOther.mEntered); | ||||||||||||||
1587 | #endif | ||||||||||||||
1588 | } | ||||||||||||||
1589 | |||||||||||||||
1590 | private: | ||||||||||||||
1591 | void moveFrom(HashTable& aRhs) { | ||||||||||||||
1592 | mGen = aRhs.mGen; | ||||||||||||||
1593 | mHashShift = aRhs.mHashShift; | ||||||||||||||
1594 | mTable = aRhs.mTable; | ||||||||||||||
1595 | mEntryCount = aRhs.mEntryCount; | ||||||||||||||
1596 | mRemovedCount = aRhs.mRemovedCount; | ||||||||||||||
1597 | #ifdef DEBUG1 | ||||||||||||||
1598 | mMutationCount = aRhs.mMutationCount; | ||||||||||||||
1599 | mEntered = aRhs.mEntered; | ||||||||||||||
1600 | #endif | ||||||||||||||
1601 | aRhs.mTable = nullptr; | ||||||||||||||
1602 | aRhs.clearAndCompact(); | ||||||||||||||
1603 | } | ||||||||||||||
1604 | |||||||||||||||
1605 | // HashTable is not copyable or assignable | ||||||||||||||
1606 | HashTable(const HashTable&) = delete; | ||||||||||||||
1607 | void operator=(const HashTable&) = delete; | ||||||||||||||
1608 | |||||||||||||||
1609 | static const uint32_t CAP_BITS = 30; | ||||||||||||||
1610 | |||||||||||||||
1611 | public: | ||||||||||||||
1612 | uint64_t mGen : 56; // entry storage generation number | ||||||||||||||
1613 | uint64_t mHashShift : 8; // multiplicative hash shift | ||||||||||||||
1614 | char* mTable; // entry storage | ||||||||||||||
1615 | uint32_t mEntryCount; // number of entries in mTable | ||||||||||||||
1616 | uint32_t mRemovedCount; // removed entry sentinels in mTable | ||||||||||||||
1617 | |||||||||||||||
1618 | #ifdef DEBUG1 | ||||||||||||||
1619 | uint64_t mMutationCount; | ||||||||||||||
1620 | mutable bool mEntered; | ||||||||||||||
1621 | #endif | ||||||||||||||
1622 | |||||||||||||||
1623 | // The default initial capacity is 32 (enough to hold 16 elements), but it | ||||||||||||||
1624 | // can be as low as 4. | ||||||||||||||
1625 | static const uint32_t sDefaultLen = 16; | ||||||||||||||
1626 | static const uint32_t sMinCapacity = 4; | ||||||||||||||
1627 | // See the comments in HashTableEntry about this value. | ||||||||||||||
1628 | static_assert(sMinCapacity >= 4, "too-small sMinCapacity breaks assumptions"); | ||||||||||||||
1629 | static const uint32_t sMaxInit = 1u << (CAP_BITS - 1); | ||||||||||||||
1630 | static const uint32_t sMaxCapacity = 1u << CAP_BITS; | ||||||||||||||
1631 | |||||||||||||||
1632 | // Hash-table alpha is conceptually a fraction, but to avoid floating-point | ||||||||||||||
1633 | // math we implement it as a ratio of integers. | ||||||||||||||
1634 | static const uint8_t sAlphaDenominator = 4; | ||||||||||||||
1635 | static const uint8_t sMinAlphaNumerator = 1; // min alpha: 1/4 | ||||||||||||||
1636 | static const uint8_t sMaxAlphaNumerator = 3; // max alpha: 3/4 | ||||||||||||||
1637 | |||||||||||||||
1638 | static const HashNumber sFreeKey = Entry::sFreeKey; | ||||||||||||||
1639 | static const HashNumber sRemovedKey = Entry::sRemovedKey; | ||||||||||||||
1640 | static const HashNumber sCollisionBit = Entry::sCollisionBit; | ||||||||||||||
1641 | |||||||||||||||
1642 | static uint32_t bestCapacity(uint32_t aLen) { | ||||||||||||||
1643 | static_assert( | ||||||||||||||
1644 | (sMaxInit * sAlphaDenominator) / sAlphaDenominator == sMaxInit, | ||||||||||||||
1645 | "multiplication in numerator below could overflow"); | ||||||||||||||
1646 | static_assert( | ||||||||||||||
1647 | sMaxInit * sAlphaDenominator <= UINT32_MAX(4294967295U) - sMaxAlphaNumerator, | ||||||||||||||
1648 | "numerator calculation below could potentially overflow"); | ||||||||||||||
1649 | |||||||||||||||
1650 | // Callers should ensure this is true. | ||||||||||||||
1651 | 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" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1651); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aLen <= sMaxInit" ")"); do { *((volatile int*)__null) = 1651; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1652 | |||||||||||||||
1653 | // Compute the smallest capacity allowing |aLen| elements to be | ||||||||||||||
1654 | // inserted without rehashing: ceil(aLen / max-alpha). (Ceiling | ||||||||||||||
1655 | // integral division: <http://stackoverflow.com/a/2745086>.) | ||||||||||||||
1656 | uint32_t capacity = (aLen * sAlphaDenominator + sMaxAlphaNumerator - 1) / | ||||||||||||||
1657 | sMaxAlphaNumerator; | ||||||||||||||
1658 | capacity = (capacity < sMinCapacity) ? sMinCapacity : RoundUpPow2(capacity); | ||||||||||||||
1659 | |||||||||||||||
1660 | 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" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1660); AnnotateMozCrashReason("MOZ_ASSERT" "(" "capacity >= aLen" ")"); do { *((volatile int*)__null) = 1660; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1661 | 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" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1661); AnnotateMozCrashReason("MOZ_ASSERT" "(" "capacity <= sMaxCapacity" ")"); do { *((volatile int*)__null) = 1661; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1662 | |||||||||||||||
1663 | return capacity; | ||||||||||||||
1664 | } | ||||||||||||||
1665 | |||||||||||||||
1666 | static uint32_t hashShift(uint32_t aLen) { | ||||||||||||||
1667 | // Reject all lengths whose initial computed capacity would exceed | ||||||||||||||
1668 | // sMaxCapacity. Round that maximum aLen down to the nearest power of two | ||||||||||||||
1669 | // for speedier code. | ||||||||||||||
1670 | if (MOZ_UNLIKELY(aLen > sMaxInit)(__builtin_expect(!!(aLen > sMaxInit), 0))) { | ||||||||||||||
1671 | MOZ_CRASH("initial length is too large")do { do { } while (false); MOZ_ReportCrash("" "initial length is too large" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1671); AnnotateMozCrashReason("MOZ_CRASH(" "initial length is too large" ")"); do { *((volatile int*)__null) = 1671; __attribute__((nomerge )) ::abort(); } while (false); } while (false); | ||||||||||||||
1672 | } | ||||||||||||||
1673 | |||||||||||||||
1674 | return kHashNumberBits - mozilla::CeilingLog2(bestCapacity(aLen)); | ||||||||||||||
1675 | } | ||||||||||||||
1676 | |||||||||||||||
1677 | static bool isLiveHash(HashNumber aHash) { return Entry::isLiveHash(aHash); } | ||||||||||||||
1678 | |||||||||||||||
1679 | static HashNumber prepareHash(HashNumber aInputHash) { | ||||||||||||||
1680 | HashNumber keyHash = ScrambleHashCode(aInputHash); | ||||||||||||||
1681 | |||||||||||||||
1682 | // Avoid reserved hash codes. | ||||||||||||||
1683 | if (!isLiveHash(keyHash)) { | ||||||||||||||
1684 | keyHash -= (sRemovedKey + 1); | ||||||||||||||
1685 | } | ||||||||||||||
1686 | return keyHash & ~sCollisionBit; | ||||||||||||||
1687 | } | ||||||||||||||
1688 | |||||||||||||||
1689 | enum FailureBehavior { DontReportFailure = false, ReportFailure = true }; | ||||||||||||||
1690 | |||||||||||||||
1691 | // Fake a struct that we're going to alloc. See the comments in | ||||||||||||||
1692 | // HashTableEntry about how the table is laid out, and why it's safe. | ||||||||||||||
1693 | struct FakeSlot { | ||||||||||||||
1694 | unsigned char c[sizeof(HashNumber) + sizeof(typename Entry::NonConstT)]; | ||||||||||||||
1695 | }; | ||||||||||||||
1696 | |||||||||||||||
1697 | static char* createTable(AllocPolicy& aAllocPolicy, uint32_t aCapacity, | ||||||||||||||
1698 | FailureBehavior aReportFailure = ReportFailure) { | ||||||||||||||
1699 | FakeSlot* fake = | ||||||||||||||
1700 | aReportFailure | ||||||||||||||
1701 | ? aAllocPolicy.template pod_malloc<FakeSlot>(aCapacity) | ||||||||||||||
1702 | : aAllocPolicy.template maybe_pod_malloc<FakeSlot>(aCapacity); | ||||||||||||||
1703 | |||||||||||||||
1704 | 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" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1705); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) == 0" ")"); do { *((volatile int*)__null) = 1705; __attribute__((nomerge )) ::abort(); } while (false); } } while (false) | ||||||||||||||
1705 | 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" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1705); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(reinterpret_cast<uintptr_t>(fake) % Entry::kMinimumAlignment) == 0" ")"); do { *((volatile int*)__null) = 1705; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1706 | |||||||||||||||
1707 | char* table = reinterpret_cast<char*>(fake); | ||||||||||||||
1708 | if (table) { | ||||||||||||||
1709 | forEachSlot(table, aCapacity, [&](Slot& slot) { | ||||||||||||||
1710 | *slot.mKeyHash = sFreeKey; | ||||||||||||||
1711 | new (KnownNotNull, slot.toEntry()) Entry(); | ||||||||||||||
1712 | }); | ||||||||||||||
1713 | } | ||||||||||||||
1714 | return table; | ||||||||||||||
1715 | } | ||||||||||||||
1716 | |||||||||||||||
1717 | static void destroyTable(AllocPolicy& aAllocPolicy, char* aOldTable, | ||||||||||||||
1718 | uint32_t aCapacity) { | ||||||||||||||
1719 | forEachSlot(aOldTable, aCapacity, [&](const Slot& slot) { | ||||||||||||||
1720 | if (slot.isLive()) { | ||||||||||||||
1721 | slot.toEntry()->destroyStoredT(); | ||||||||||||||
1722 | } | ||||||||||||||
1723 | }); | ||||||||||||||
1724 | freeTable(aAllocPolicy, aOldTable, aCapacity); | ||||||||||||||
1725 | } | ||||||||||||||
1726 | |||||||||||||||
1727 | static void freeTable(AllocPolicy& aAllocPolicy, char* aOldTable, | ||||||||||||||
1728 | uint32_t aCapacity) { | ||||||||||||||
1729 | FakeSlot* fake = reinterpret_cast<FakeSlot*>(aOldTable); | ||||||||||||||
1730 | aAllocPolicy.free_(fake, aCapacity); | ||||||||||||||
1731 | } | ||||||||||||||
1732 | |||||||||||||||
1733 | public: | ||||||||||||||
1734 | HashTable(AllocPolicy aAllocPolicy, uint32_t aLen) | ||||||||||||||
1735 | : AllocPolicy(std::move(aAllocPolicy)), | ||||||||||||||
1736 | mGen(0), | ||||||||||||||
1737 | mHashShift(hashShift(aLen)), | ||||||||||||||
1738 | mTable(nullptr), | ||||||||||||||
1739 | mEntryCount(0), | ||||||||||||||
1740 | mRemovedCount(0) | ||||||||||||||
1741 | #ifdef DEBUG1 | ||||||||||||||
1742 | , | ||||||||||||||
1743 | mMutationCount(0), | ||||||||||||||
1744 | mEntered(false) | ||||||||||||||
1745 | #endif | ||||||||||||||
1746 | { | ||||||||||||||
1747 | } | ||||||||||||||
1748 | |||||||||||||||
1749 | explicit HashTable(AllocPolicy aAllocPolicy) | ||||||||||||||
1750 | : HashTable(aAllocPolicy, sDefaultLen) {} | ||||||||||||||
1751 | |||||||||||||||
1752 | ~HashTable() { | ||||||||||||||
1753 | if (mTable
| ||||||||||||||
1754 | destroyTable(*this, mTable, capacity()); | ||||||||||||||
| |||||||||||||||
1755 | } | ||||||||||||||
1756 | } | ||||||||||||||
1757 | |||||||||||||||
1758 | private: | ||||||||||||||
1759 | HashNumber hash1(HashNumber aHash0) const { return aHash0 >> mHashShift; } | ||||||||||||||
1760 | |||||||||||||||
1761 | struct DoubleHash { | ||||||||||||||
1762 | HashNumber mHash2; | ||||||||||||||
1763 | HashNumber mSizeMask; | ||||||||||||||
1764 | }; | ||||||||||||||
1765 | |||||||||||||||
1766 | DoubleHash hash2(HashNumber aCurKeyHash) const { | ||||||||||||||
1767 | uint32_t sizeLog2 = kHashNumberBits - mHashShift; | ||||||||||||||
1768 | DoubleHash dh = {((aCurKeyHash << sizeLog2) >> mHashShift) | 1, | ||||||||||||||
1769 | (HashNumber(1) << sizeLog2) - 1}; | ||||||||||||||
1770 | return dh; | ||||||||||||||
1771 | } | ||||||||||||||
1772 | |||||||||||||||
1773 | static HashNumber applyDoubleHash(HashNumber aHash1, | ||||||||||||||
1774 | const DoubleHash& aDoubleHash) { | ||||||||||||||
1775 | return WrappingSubtract(aHash1, aDoubleHash.mHash2) & aDoubleHash.mSizeMask; | ||||||||||||||
1776 | } | ||||||||||||||
1777 | |||||||||||||||
1778 | static MOZ_ALWAYS_INLINEinline bool match(T& aEntry, const Lookup& aLookup) { | ||||||||||||||
1779 | return HashPolicy::match(HashPolicy::getKey(aEntry), aLookup); | ||||||||||||||
1780 | } | ||||||||||||||
1781 | |||||||||||||||
1782 | enum LookupReason { ForNonAdd, ForAdd }; | ||||||||||||||
1783 | |||||||||||||||
1784 | Slot slotForIndex(HashNumber aIndex) const { | ||||||||||||||
1785 | auto hashes = reinterpret_cast<HashNumber*>(mTable); | ||||||||||||||
1786 | auto entries = reinterpret_cast<Entry*>(&hashes[capacity()]); | ||||||||||||||
1787 | return Slot(&entries[aIndex], &hashes[aIndex]); | ||||||||||||||
1788 | } | ||||||||||||||
1789 | |||||||||||||||
1790 | // Warning: in order for readonlyThreadsafeLookup() to be safe this | ||||||||||||||
1791 | // function must not modify the table in any way when Reason==ForNonAdd. | ||||||||||||||
1792 | template <LookupReason Reason> | ||||||||||||||
1793 | MOZ_ALWAYS_INLINEinline Slot lookup(const Lookup& aLookup, | ||||||||||||||
1794 | HashNumber aKeyHash) const { | ||||||||||||||
1795 | 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)" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1795); AnnotateMozCrashReason("MOZ_ASSERT" "(" "isLiveHash(aKeyHash)" ")"); do { *((volatile int*)__null) = 1795; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1796 | 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)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1796); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!(aKeyHash & sCollisionBit)" ")"); do { *((volatile int*)__null) = 1796; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1797 | 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", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1797); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { *((volatile int*)__null) = 1797; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1798 | |||||||||||||||
1799 | // Compute the primary hash address. | ||||||||||||||
1800 | HashNumber h1 = hash1(aKeyHash); | ||||||||||||||
1801 | Slot slot = slotForIndex(h1); | ||||||||||||||
1802 | |||||||||||||||
1803 | // Miss: return space for a new entry. | ||||||||||||||
1804 | if (slot.isFree()) { | ||||||||||||||
1805 | return slot; | ||||||||||||||
1806 | } | ||||||||||||||
1807 | |||||||||||||||
1808 | // Hit: return entry. | ||||||||||||||
1809 | if (slot.matchHash(aKeyHash) && match(slot.get(), aLookup)) { | ||||||||||||||
1810 | return slot; | ||||||||||||||
1811 | } | ||||||||||||||
1812 | |||||||||||||||
1813 | // Collision: double hash. | ||||||||||||||
1814 | DoubleHash dh = hash2(aKeyHash); | ||||||||||||||
1815 | |||||||||||||||
1816 | // Save the first removed entry pointer so we can recycle later. | ||||||||||||||
1817 | Maybe<Slot> firstRemoved; | ||||||||||||||
1818 | |||||||||||||||
1819 | while (true) { | ||||||||||||||
1820 | if (Reason == ForAdd && !firstRemoved) { | ||||||||||||||
1821 | if (MOZ_UNLIKELY(slot.isRemoved())(__builtin_expect(!!(slot.isRemoved()), 0))) { | ||||||||||||||
1822 | firstRemoved.emplace(slot); | ||||||||||||||
1823 | } else { | ||||||||||||||
1824 | slot.setCollision(); | ||||||||||||||
1825 | } | ||||||||||||||
1826 | } | ||||||||||||||
1827 | |||||||||||||||
1828 | h1 = applyDoubleHash(h1, dh); | ||||||||||||||
1829 | |||||||||||||||
1830 | slot = slotForIndex(h1); | ||||||||||||||
1831 | if (slot.isFree()) { | ||||||||||||||
1832 | return firstRemoved.refOr(slot); | ||||||||||||||
1833 | } | ||||||||||||||
1834 | |||||||||||||||
1835 | if (slot.matchHash(aKeyHash) && match(slot.get(), aLookup)) { | ||||||||||||||
1836 | return slot; | ||||||||||||||
1837 | } | ||||||||||||||
1838 | } | ||||||||||||||
1839 | } | ||||||||||||||
1840 | |||||||||||||||
1841 | // This is a copy of lookup() hardcoded to the assumptions: | ||||||||||||||
1842 | // 1. the lookup is for an add; | ||||||||||||||
1843 | // 2. the key, whose |keyHash| has been passed, is not in the table. | ||||||||||||||
1844 | Slot findNonLiveSlot(HashNumber aKeyHash) { | ||||||||||||||
1845 | 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)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1845); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!(aKeyHash & sCollisionBit)" ")"); do { *((volatile int*)__null) = 1845; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1846 | 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", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1846); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { *((volatile int*)__null) = 1846; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1847 | |||||||||||||||
1848 | // We assume 'aKeyHash' has already been distributed. | ||||||||||||||
1849 | |||||||||||||||
1850 | // Compute the primary hash address. | ||||||||||||||
1851 | HashNumber h1 = hash1(aKeyHash); | ||||||||||||||
1852 | Slot slot = slotForIndex(h1); | ||||||||||||||
1853 | |||||||||||||||
1854 | // Miss: return space for a new entry. | ||||||||||||||
1855 | if (!slot.isLive()) { | ||||||||||||||
1856 | return slot; | ||||||||||||||
1857 | } | ||||||||||||||
1858 | |||||||||||||||
1859 | // Collision: double hash. | ||||||||||||||
1860 | DoubleHash dh = hash2(aKeyHash); | ||||||||||||||
1861 | |||||||||||||||
1862 | while (true) { | ||||||||||||||
1863 | slot.setCollision(); | ||||||||||||||
1864 | |||||||||||||||
1865 | h1 = applyDoubleHash(h1, dh); | ||||||||||||||
1866 | |||||||||||||||
1867 | slot = slotForIndex(h1); | ||||||||||||||
1868 | if (!slot.isLive()) { | ||||||||||||||
1869 | return slot; | ||||||||||||||
1870 | } | ||||||||||||||
1871 | } | ||||||||||||||
1872 | } | ||||||||||||||
1873 | |||||||||||||||
1874 | enum RebuildStatus { NotOverloaded, Rehashed, RehashFailed }; | ||||||||||||||
1875 | |||||||||||||||
1876 | RebuildStatus changeTableSize( | ||||||||||||||
1877 | uint32_t newCapacity, FailureBehavior aReportFailure = ReportFailure) { | ||||||||||||||
1878 | MOZ_ASSERT(IsPowerOfTwo(newCapacity))do { static_assert( mozilla::detail::AssertionConditionType< decltype(IsPowerOfTwo(newCapacity))>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(IsPowerOfTwo(newCapacity)))) , 0))) { do { } while (false); MOZ_ReportAssertionFailure("IsPowerOfTwo(newCapacity)" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1878); AnnotateMozCrashReason("MOZ_ASSERT" "(" "IsPowerOfTwo(newCapacity)" ")"); do { *((volatile int*)__null) = 1878; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1879 | 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()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1879); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!!mTable == !!capacity()" ")"); do { *((volatile int*)__null) = 1879; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1880 | |||||||||||||||
1881 | // Look, but don't touch, until we succeed in getting new entry store. | ||||||||||||||
1882 | char* oldTable = mTable; | ||||||||||||||
1883 | uint32_t oldCapacity = capacity(); | ||||||||||||||
1884 | uint32_t newLog2 = mozilla::CeilingLog2(newCapacity); | ||||||||||||||
1885 | |||||||||||||||
1886 | if (MOZ_UNLIKELY(newCapacity > sMaxCapacity)(__builtin_expect(!!(newCapacity > sMaxCapacity), 0))) { | ||||||||||||||
1887 | if (aReportFailure) { | ||||||||||||||
1888 | this->reportAllocOverflow(); | ||||||||||||||
1889 | } | ||||||||||||||
1890 | return RehashFailed; | ||||||||||||||
1891 | } | ||||||||||||||
1892 | |||||||||||||||
1893 | char* newTable = createTable(*this, newCapacity, aReportFailure); | ||||||||||||||
1894 | if (!newTable) { | ||||||||||||||
1895 | return RehashFailed; | ||||||||||||||
1896 | } | ||||||||||||||
1897 | |||||||||||||||
1898 | // We can't fail from here on, so update table parameters. | ||||||||||||||
1899 | mHashShift = kHashNumberBits - newLog2; | ||||||||||||||
1900 | mRemovedCount = 0; | ||||||||||||||
1901 | mGen++; | ||||||||||||||
1902 | mTable = newTable; | ||||||||||||||
1903 | |||||||||||||||
1904 | // Copy only live entries, leaving removed ones behind. | ||||||||||||||
1905 | forEachSlot(oldTable, oldCapacity, [&](Slot& slot) { | ||||||||||||||
1906 | if (slot.isLive()) { | ||||||||||||||
1907 | HashNumber hn = slot.getKeyHash(); | ||||||||||||||
1908 | findNonLiveSlot(hn).setLive( | ||||||||||||||
1909 | hn, std::move(const_cast<typename Entry::NonConstT&>(slot.get()))); | ||||||||||||||
1910 | } | ||||||||||||||
1911 | |||||||||||||||
1912 | slot.clear(); | ||||||||||||||
1913 | }); | ||||||||||||||
1914 | |||||||||||||||
1915 | // All entries have been destroyed, no need to destroyTable. | ||||||||||||||
1916 | freeTable(*this, oldTable, oldCapacity); | ||||||||||||||
1917 | return Rehashed; | ||||||||||||||
1918 | } | ||||||||||||||
1919 | |||||||||||||||
1920 | RebuildStatus rehashIfOverloaded( | ||||||||||||||
1921 | FailureBehavior aReportFailure = ReportFailure) { | ||||||||||||||
1922 | static_assert(sMaxCapacity <= UINT32_MAX(4294967295U) / sMaxAlphaNumerator, | ||||||||||||||
1923 | "multiplication below could overflow"); | ||||||||||||||
1924 | |||||||||||||||
1925 | // Note: if capacity() is zero, this will always succeed, which is | ||||||||||||||
1926 | // what we want. | ||||||||||||||
1927 | bool overloaded = mEntryCount + mRemovedCount >= | ||||||||||||||
1928 | capacity() * sMaxAlphaNumerator / sAlphaDenominator; | ||||||||||||||
1929 | |||||||||||||||
1930 | if (!overloaded) { | ||||||||||||||
1931 | return NotOverloaded; | ||||||||||||||
1932 | } | ||||||||||||||
1933 | |||||||||||||||
1934 | // Succeed if a quarter or more of all entries are removed. Note that this | ||||||||||||||
1935 | // always succeeds if capacity() == 0 (i.e. entry storage has not been | ||||||||||||||
1936 | // allocated), which is what we want, because it means changeTableSize() | ||||||||||||||
1937 | // will allocate the requested capacity rather than doubling it. | ||||||||||||||
1938 | bool manyRemoved = mRemovedCount >= (capacity() >> 2); | ||||||||||||||
1939 | uint32_t newCapacity = manyRemoved ? rawCapacity() : rawCapacity() * 2; | ||||||||||||||
1940 | return changeTableSize(newCapacity, aReportFailure); | ||||||||||||||
1941 | } | ||||||||||||||
1942 | |||||||||||||||
1943 | void infallibleRehashIfOverloaded() { | ||||||||||||||
1944 | if (rehashIfOverloaded(DontReportFailure) == RehashFailed) { | ||||||||||||||
1945 | rehashTableInPlace(); | ||||||||||||||
1946 | } | ||||||||||||||
1947 | } | ||||||||||||||
1948 | |||||||||||||||
1949 | void remove(Slot& aSlot) { | ||||||||||||||
1950 | 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", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 1950); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { *((volatile int*)__null) = 1950; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
1951 | |||||||||||||||
1952 | if (aSlot.hasCollision()) { | ||||||||||||||
1953 | aSlot.removeLive(); | ||||||||||||||
1954 | mRemovedCount++; | ||||||||||||||
1955 | } else { | ||||||||||||||
1956 | aSlot.clearLive(); | ||||||||||||||
1957 | } | ||||||||||||||
1958 | mEntryCount--; | ||||||||||||||
1959 | #ifdef DEBUG1 | ||||||||||||||
1960 | mMutationCount++; | ||||||||||||||
1961 | #endif | ||||||||||||||
1962 | } | ||||||||||||||
1963 | |||||||||||||||
1964 | void shrinkIfUnderloaded() { | ||||||||||||||
1965 | static_assert(sMaxCapacity <= UINT32_MAX(4294967295U) / sMinAlphaNumerator, | ||||||||||||||
1966 | "multiplication below could overflow"); | ||||||||||||||
1967 | bool underloaded = | ||||||||||||||
1968 | capacity() > sMinCapacity && | ||||||||||||||
1969 | mEntryCount <= capacity() * sMinAlphaNumerator / sAlphaDenominator; | ||||||||||||||
1970 | |||||||||||||||
1971 | if (underloaded) { | ||||||||||||||
1972 | (void)changeTableSize(capacity() / 2, DontReportFailure); | ||||||||||||||
1973 | } | ||||||||||||||
1974 | } | ||||||||||||||
1975 | |||||||||||||||
1976 | // This is identical to changeTableSize(currentSize), but without requiring | ||||||||||||||
1977 | // a second table. We do this by recycling the collision bits to tell us if | ||||||||||||||
1978 | // the element is already inserted or still waiting to be inserted. Since | ||||||||||||||
1979 | // already-inserted elements win any conflicts, we get the same table as we | ||||||||||||||
1980 | // would have gotten through random insertion order. | ||||||||||||||
1981 | void rehashTableInPlace() { | ||||||||||||||
1982 | mRemovedCount = 0; | ||||||||||||||
1983 | mGen++; | ||||||||||||||
1984 | forEachSlot(mTable, capacity(), [&](Slot& slot) { slot.unsetCollision(); }); | ||||||||||||||
1985 | for (uint32_t i = 0; i < capacity();) { | ||||||||||||||
1986 | Slot src = slotForIndex(i); | ||||||||||||||
1987 | |||||||||||||||
1988 | if (!src.isLive() || src.hasCollision()) { | ||||||||||||||
1989 | ++i; | ||||||||||||||
1990 | continue; | ||||||||||||||
1991 | } | ||||||||||||||
1992 | |||||||||||||||
1993 | HashNumber keyHash = src.getKeyHash(); | ||||||||||||||
1994 | HashNumber h1 = hash1(keyHash); | ||||||||||||||
1995 | DoubleHash dh = hash2(keyHash); | ||||||||||||||
1996 | Slot tgt = slotForIndex(h1); | ||||||||||||||
1997 | while (true) { | ||||||||||||||
1998 | if (!tgt.hasCollision()) { | ||||||||||||||
1999 | src.swap(tgt); | ||||||||||||||
2000 | tgt.setCollision(); | ||||||||||||||
2001 | break; | ||||||||||||||
2002 | } | ||||||||||||||
2003 | |||||||||||||||
2004 | h1 = applyDoubleHash(h1, dh); | ||||||||||||||
2005 | tgt = slotForIndex(h1); | ||||||||||||||
2006 | } | ||||||||||||||
2007 | } | ||||||||||||||
2008 | |||||||||||||||
2009 | // TODO: this algorithm leaves collision bits on *all* elements, even if | ||||||||||||||
2010 | // they are on no collision path. We have the option of setting the | ||||||||||||||
2011 | // collision bits correctly on a subsequent pass or skipping the rehash | ||||||||||||||
2012 | // unless we are totally filled with tombstones: benchmark to find out | ||||||||||||||
2013 | // which approach is best. | ||||||||||||||
2014 | } | ||||||||||||||
2015 | |||||||||||||||
2016 | // Prefer to use putNewInfallible; this function does not check | ||||||||||||||
2017 | // invariants. | ||||||||||||||
2018 | template <typename... Args> | ||||||||||||||
2019 | void putNewInfallibleInternal(HashNumber aKeyHash, Args&&... aArgs) { | ||||||||||||||
2020 | 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", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2020); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { *((volatile int*)__null) = 2020; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2021 | |||||||||||||||
2022 | Slot slot = findNonLiveSlot(aKeyHash); | ||||||||||||||
2023 | |||||||||||||||
2024 | if (slot.isRemoved()) { | ||||||||||||||
2025 | mRemovedCount--; | ||||||||||||||
2026 | aKeyHash |= sCollisionBit; | ||||||||||||||
2027 | } | ||||||||||||||
2028 | |||||||||||||||
2029 | slot.setLive(aKeyHash, std::forward<Args>(aArgs)...); | ||||||||||||||
2030 | mEntryCount++; | ||||||||||||||
2031 | #ifdef DEBUG1 | ||||||||||||||
2032 | mMutationCount++; | ||||||||||||||
2033 | #endif | ||||||||||||||
2034 | } | ||||||||||||||
2035 | |||||||||||||||
2036 | public: | ||||||||||||||
2037 | void clear() { | ||||||||||||||
2038 | forEachSlot(mTable, capacity(), [&](Slot& slot) { slot.clear(); }); | ||||||||||||||
2039 | mRemovedCount = 0; | ||||||||||||||
2040 | mEntryCount = 0; | ||||||||||||||
2041 | #ifdef DEBUG1 | ||||||||||||||
2042 | mMutationCount++; | ||||||||||||||
2043 | #endif | ||||||||||||||
2044 | } | ||||||||||||||
2045 | |||||||||||||||
2046 | // Resize the table down to the smallest capacity that doesn't overload the | ||||||||||||||
2047 | // table. Since we call shrinkIfUnderloaded() on every remove, you only need | ||||||||||||||
2048 | // to call this after a bulk removal of items done without calling remove(). | ||||||||||||||
2049 | void compact() { | ||||||||||||||
2050 | if (empty()) { | ||||||||||||||
2051 | // Free the entry storage. | ||||||||||||||
2052 | freeTable(*this, mTable, capacity()); | ||||||||||||||
2053 | mGen++; | ||||||||||||||
2054 | mHashShift = hashShift(0); // gives minimum capacity on regrowth | ||||||||||||||
2055 | mTable = nullptr; | ||||||||||||||
2056 | mRemovedCount = 0; | ||||||||||||||
2057 | return; | ||||||||||||||
2058 | } | ||||||||||||||
2059 | |||||||||||||||
2060 | uint32_t bestCapacity = this->bestCapacity(mEntryCount); | ||||||||||||||
2061 | MOZ_ASSERT(bestCapacity <= capacity())do { static_assert( mozilla::detail::AssertionConditionType< decltype(bestCapacity <= capacity())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(bestCapacity <= capacity( )))), 0))) { do { } while (false); MOZ_ReportAssertionFailure ("bestCapacity <= capacity()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2061); AnnotateMozCrashReason("MOZ_ASSERT" "(" "bestCapacity <= capacity()" ")"); do { *((volatile int*)__null) = 2061; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2062 | |||||||||||||||
2063 | if (bestCapacity < capacity()) { | ||||||||||||||
2064 | (void)changeTableSize(bestCapacity, DontReportFailure); | ||||||||||||||
2065 | } | ||||||||||||||
2066 | } | ||||||||||||||
2067 | |||||||||||||||
2068 | void clearAndCompact() { | ||||||||||||||
2069 | clear(); | ||||||||||||||
2070 | compact(); | ||||||||||||||
2071 | } | ||||||||||||||
2072 | |||||||||||||||
2073 | [[nodiscard]] bool reserve(uint32_t aLen) { | ||||||||||||||
2074 | if (aLen == 0) { | ||||||||||||||
2075 | return true; | ||||||||||||||
2076 | } | ||||||||||||||
2077 | |||||||||||||||
2078 | if (MOZ_UNLIKELY(aLen > sMaxInit)(__builtin_expect(!!(aLen > sMaxInit), 0))) { | ||||||||||||||
2079 | this->reportAllocOverflow(); | ||||||||||||||
2080 | return false; | ||||||||||||||
2081 | } | ||||||||||||||
2082 | |||||||||||||||
2083 | uint32_t bestCapacity = this->bestCapacity(aLen); | ||||||||||||||
2084 | if (bestCapacity <= capacity()) { | ||||||||||||||
2085 | return true; // Capacity is already sufficient. | ||||||||||||||
2086 | } | ||||||||||||||
2087 | |||||||||||||||
2088 | RebuildStatus status = changeTableSize(bestCapacity, ReportFailure); | ||||||||||||||
2089 | 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" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2089); AnnotateMozCrashReason("MOZ_ASSERT" "(" "status != NotOverloaded" ")"); do { *((volatile int*)__null) = 2089; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2090 | return status != RehashFailed; | ||||||||||||||
2091 | } | ||||||||||||||
2092 | |||||||||||||||
2093 | Iterator iter() const { return Iterator(*this); } | ||||||||||||||
2094 | |||||||||||||||
2095 | ModIterator modIter() { return ModIterator(*this); } | ||||||||||||||
2096 | |||||||||||||||
2097 | Range all() const { return Range(*this); } | ||||||||||||||
2098 | |||||||||||||||
2099 | bool empty() const { return mEntryCount == 0; } | ||||||||||||||
2100 | |||||||||||||||
2101 | uint32_t count() const { return mEntryCount; } | ||||||||||||||
2102 | |||||||||||||||
2103 | uint32_t rawCapacity() const { return 1u << (kHashNumberBits - mHashShift); } | ||||||||||||||
2104 | |||||||||||||||
2105 | uint32_t capacity() const { return mTable ? rawCapacity() : 0; } | ||||||||||||||
2106 | |||||||||||||||
2107 | Generation generation() const { return Generation(mGen); } | ||||||||||||||
2108 | |||||||||||||||
2109 | size_t shallowSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { | ||||||||||||||
2110 | return aMallocSizeOf(mTable); | ||||||||||||||
2111 | } | ||||||||||||||
2112 | |||||||||||||||
2113 | size_t shallowSizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { | ||||||||||||||
2114 | return aMallocSizeOf(this) + shallowSizeOfExcludingThis(aMallocSizeOf); | ||||||||||||||
2115 | } | ||||||||||||||
2116 | |||||||||||||||
2117 | MOZ_ALWAYS_INLINEinline Ptr readonlyThreadsafeLookup(const Lookup& aLookup) const { | ||||||||||||||
2118 | if (empty()) { | ||||||||||||||
2119 | return Ptr(); | ||||||||||||||
2120 | } | ||||||||||||||
2121 | |||||||||||||||
2122 | HashNumber inputHash; | ||||||||||||||
2123 | if (!MaybeGetHash<HashPolicy>(aLookup, &inputHash)) { | ||||||||||||||
2124 | return Ptr(); | ||||||||||||||
2125 | } | ||||||||||||||
2126 | |||||||||||||||
2127 | HashNumber keyHash = prepareHash(inputHash); | ||||||||||||||
2128 | return Ptr(lookup<ForNonAdd>(aLookup, keyHash), *this); | ||||||||||||||
2129 | } | ||||||||||||||
2130 | |||||||||||||||
2131 | MOZ_ALWAYS_INLINEinline Ptr lookup(const Lookup& aLookup) const { | ||||||||||||||
2132 | ReentrancyGuard g(*this); | ||||||||||||||
2133 | return readonlyThreadsafeLookup(aLookup); | ||||||||||||||
2134 | } | ||||||||||||||
2135 | |||||||||||||||
2136 | MOZ_ALWAYS_INLINEinline AddPtr lookupForAdd(const Lookup& aLookup) { | ||||||||||||||
2137 | ReentrancyGuard g(*this); | ||||||||||||||
2138 | |||||||||||||||
2139 | HashNumber inputHash; | ||||||||||||||
2140 | if (!EnsureHash<HashPolicy>(aLookup, &inputHash)) { | ||||||||||||||
2141 | return AddPtr(); | ||||||||||||||
2142 | } | ||||||||||||||
2143 | |||||||||||||||
2144 | HashNumber keyHash = prepareHash(inputHash); | ||||||||||||||
2145 | |||||||||||||||
2146 | if (!mTable) { | ||||||||||||||
2147 | return AddPtr(*this, keyHash); | ||||||||||||||
2148 | } | ||||||||||||||
2149 | |||||||||||||||
2150 | // Directly call the constructor in the return statement to avoid | ||||||||||||||
2151 | // excess copying when building with Visual Studio 2017. | ||||||||||||||
2152 | // See bug 1385181. | ||||||||||||||
2153 | return AddPtr(lookup<ForAdd>(aLookup, keyHash), *this, keyHash); | ||||||||||||||
2154 | } | ||||||||||||||
2155 | |||||||||||||||
2156 | template <typename... Args> | ||||||||||||||
2157 | [[nodiscard]] bool add(AddPtr& aPtr, Args&&... aArgs) { | ||||||||||||||
2158 | ReentrancyGuard g(*this); | ||||||||||||||
2159 | 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", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2159); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { *((volatile int*)__null) = 2159; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); } } while ( false); | ||||||||||||||
2160 | 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", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2160); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mTable == this" ")"); do { *((volatile int*)__null) = 2160; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); } } while ( false); | ||||||||||||||
2161 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2161); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!aPtr.found()" ")"); do { *((volatile int*)__null) = 2161; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2162 | 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)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2162); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!(aPtr.mKeyHash & sCollisionBit)" ")"); do { *((volatile int*)__null) = 2162; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2163 | |||||||||||||||
2164 | // Check for error from ensureHash() here. | ||||||||||||||
2165 | if (!aPtr.isLive()) { | ||||||||||||||
2166 | return false; | ||||||||||||||
2167 | } | ||||||||||||||
2168 | |||||||||||||||
2169 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2169); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mGeneration == generation()" ")"); do { *((volatile int*)__null) = 2169; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2170 | #ifdef DEBUG1 | ||||||||||||||
2171 | 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" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2171); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mMutationCount == mMutationCount" ")"); do { *((volatile int*)__null) = 2171; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2172 | #endif | ||||||||||||||
2173 | |||||||||||||||
2174 | if (!aPtr.isValid()) { | ||||||||||||||
2175 | 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", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2175); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!mTable && mEntryCount == 0" ")"); do { *((volatile int*)__null) = 2175; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2176 | uint32_t newCapacity = rawCapacity(); | ||||||||||||||
2177 | RebuildStatus status = changeTableSize(newCapacity, ReportFailure); | ||||||||||||||
2178 | 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" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2178); AnnotateMozCrashReason("MOZ_ASSERT" "(" "status != NotOverloaded" ")"); do { *((volatile int*)__null) = 2178; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2179 | if (status == RehashFailed) { | ||||||||||||||
2180 | return false; | ||||||||||||||
2181 | } | ||||||||||||||
2182 | aPtr.mSlot = findNonLiveSlot(aPtr.mKeyHash); | ||||||||||||||
2183 | |||||||||||||||
2184 | } else if (aPtr.mSlot.isRemoved()) { | ||||||||||||||
2185 | // Changing an entry from removed to live does not affect whether we are | ||||||||||||||
2186 | // overloaded and can be handled separately. | ||||||||||||||
2187 | if (!this->checkSimulatedOOM()) { | ||||||||||||||
2188 | return false; | ||||||||||||||
2189 | } | ||||||||||||||
2190 | mRemovedCount--; | ||||||||||||||
2191 | aPtr.mKeyHash |= sCollisionBit; | ||||||||||||||
2192 | |||||||||||||||
2193 | } else { | ||||||||||||||
2194 | // Preserve the validity of |aPtr.mSlot|. | ||||||||||||||
2195 | RebuildStatus status = rehashIfOverloaded(); | ||||||||||||||
2196 | if (status == RehashFailed) { | ||||||||||||||
2197 | return false; | ||||||||||||||
2198 | } | ||||||||||||||
2199 | if (status == NotOverloaded && !this->checkSimulatedOOM()) { | ||||||||||||||
2200 | return false; | ||||||||||||||
2201 | } | ||||||||||||||
2202 | if (status == Rehashed) { | ||||||||||||||
2203 | aPtr.mSlot = findNonLiveSlot(aPtr.mKeyHash); | ||||||||||||||
2204 | } | ||||||||||||||
2205 | } | ||||||||||||||
2206 | |||||||||||||||
2207 | aPtr.mSlot.setLive(aPtr.mKeyHash, std::forward<Args>(aArgs)...); | ||||||||||||||
2208 | mEntryCount++; | ||||||||||||||
2209 | #ifdef DEBUG1 | ||||||||||||||
2210 | mMutationCount++; | ||||||||||||||
2211 | aPtr.mGeneration = generation(); | ||||||||||||||
2212 | aPtr.mMutationCount = mMutationCount; | ||||||||||||||
2213 | #endif | ||||||||||||||
2214 | return true; | ||||||||||||||
2215 | } | ||||||||||||||
2216 | |||||||||||||||
2217 | // Note: |aLookup| may reference pieces of arguments in |aArgs|, so this | ||||||||||||||
2218 | // function must take care not to use |aLookup| after moving |aArgs|. | ||||||||||||||
2219 | template <typename... Args> | ||||||||||||||
2220 | void putNewInfallible(const Lookup& aLookup, Args&&... aArgs) { | ||||||||||||||
2221 | 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()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2221); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!lookup(aLookup).found()" ")"); do { *((volatile int*)__null) = 2221; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2222 | ReentrancyGuard g(*this); | ||||||||||||||
2223 | HashNumber keyHash = prepareHash(HashPolicy::hash(aLookup)); | ||||||||||||||
2224 | putNewInfallibleInternal(keyHash, std::forward<Args>(aArgs)...); | ||||||||||||||
2225 | } | ||||||||||||||
2226 | |||||||||||||||
2227 | // Note: |aLookup| may alias arguments in |aArgs|, so this function must take | ||||||||||||||
2228 | // care not to use |aLookup| after moving |aArgs|. | ||||||||||||||
2229 | template <typename... Args> | ||||||||||||||
2230 | [[nodiscard]] bool putNew(const Lookup& aLookup, Args&&... aArgs) { | ||||||||||||||
2231 | 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()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2231); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!lookup(aLookup).found()" ")"); do { *((volatile int*)__null) = 2231; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2232 | ReentrancyGuard g(*this); | ||||||||||||||
2233 | if (!this->checkSimulatedOOM()) { | ||||||||||||||
2234 | return false; | ||||||||||||||
2235 | } | ||||||||||||||
2236 | HashNumber inputHash; | ||||||||||||||
2237 | if (!EnsureHash<HashPolicy>(aLookup, &inputHash)) { | ||||||||||||||
2238 | return false; | ||||||||||||||
2239 | } | ||||||||||||||
2240 | HashNumber keyHash = prepareHash(inputHash); | ||||||||||||||
2241 | if (rehashIfOverloaded() == RehashFailed) { | ||||||||||||||
2242 | return false; | ||||||||||||||
2243 | } | ||||||||||||||
2244 | putNewInfallibleInternal(keyHash, std::forward<Args>(aArgs)...); | ||||||||||||||
2245 | return true; | ||||||||||||||
2246 | } | ||||||||||||||
2247 | |||||||||||||||
2248 | // Note: |aLookup| may be a reference pieces of arguments in |aArgs|, so this | ||||||||||||||
2249 | // function must take care not to use |aLookup| after moving |aArgs|. | ||||||||||||||
2250 | template <typename... Args> | ||||||||||||||
2251 | [[nodiscard]] bool relookupOrAdd(AddPtr& aPtr, const Lookup& aLookup, | ||||||||||||||
2252 | Args&&... aArgs) { | ||||||||||||||
2253 | // Check for error from ensureHash() here. | ||||||||||||||
2254 | if (!aPtr.isLive()) { | ||||||||||||||
2255 | return false; | ||||||||||||||
2256 | } | ||||||||||||||
2257 | #ifdef DEBUG1 | ||||||||||||||
2258 | aPtr.mGeneration = generation(); | ||||||||||||||
2259 | aPtr.mMutationCount = mMutationCount; | ||||||||||||||
2260 | #endif | ||||||||||||||
2261 | if (mTable) { | ||||||||||||||
2262 | ReentrancyGuard g(*this); | ||||||||||||||
2263 | // Check that aLookup has not been destroyed. | ||||||||||||||
2264 | 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", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2264); AnnotateMozCrashReason("MOZ_ASSERT" "(" "prepareHash(HashPolicy::hash(aLookup)) == aPtr.mKeyHash" ")"); do { *((volatile int*)__null) = 2264; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2265 | aPtr.mSlot = lookup<ForAdd>(aLookup, aPtr.mKeyHash); | ||||||||||||||
2266 | if (aPtr.found()) { | ||||||||||||||
2267 | return true; | ||||||||||||||
2268 | } | ||||||||||||||
2269 | } else { | ||||||||||||||
2270 | // Clear aPtr so it's invalid; add() will allocate storage and redo the | ||||||||||||||
2271 | // lookup. | ||||||||||||||
2272 | aPtr.mSlot = Slot(nullptr, nullptr); | ||||||||||||||
2273 | } | ||||||||||||||
2274 | return add(aPtr, std::forward<Args>(aArgs)...); | ||||||||||||||
2275 | } | ||||||||||||||
2276 | |||||||||||||||
2277 | void remove(Ptr aPtr) { | ||||||||||||||
2278 | 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", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2278); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { *((volatile int*)__null) = 2278; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2279 | ReentrancyGuard g(*this); | ||||||||||||||
2280 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2280); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.found()" ")"); do { *((volatile int*)__null) = 2280; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2281 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2281); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mGeneration == generation()" ")"); do { *((volatile int*)__null) = 2281; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2282 | remove(aPtr.mSlot); | ||||||||||||||
2283 | shrinkIfUnderloaded(); | ||||||||||||||
2284 | } | ||||||||||||||
2285 | |||||||||||||||
2286 | void rekeyWithoutRehash(Ptr aPtr, const Lookup& aLookup, const Key& aKey) { | ||||||||||||||
2287 | 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", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2287); AnnotateMozCrashReason("MOZ_ASSERT" "(" "mTable" ")" ); do { *((volatile int*)__null) = 2287; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2288 | ReentrancyGuard g(*this); | ||||||||||||||
2289 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2289); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.found()" ")"); do { *((volatile int*)__null) = 2289; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2290 | 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()", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/HashTable.h" , 2290); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aPtr.mGeneration == generation()" ")"); do { *((volatile int*)__null) = 2290; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); | ||||||||||||||
2291 | typename HashTableEntry<T>::NonConstT t(std::move(*aPtr)); | ||||||||||||||
2292 | HashPolicy::setKey(t, const_cast<Key&>(aKey)); | ||||||||||||||
2293 | remove(aPtr.mSlot); | ||||||||||||||
2294 | HashNumber keyHash = prepareHash(HashPolicy::hash(aLookup)); | ||||||||||||||
2295 | putNewInfallibleInternal(keyHash, std::move(t)); | ||||||||||||||
2296 | } | ||||||||||||||
2297 | |||||||||||||||
2298 | void rekeyAndMaybeRehash(Ptr aPtr, const Lookup& aLookup, const Key& aKey) { | ||||||||||||||
2299 | rekeyWithoutRehash(aPtr, aLookup, aKey); | ||||||||||||||
2300 | infallibleRehashIfOverloaded(); | ||||||||||||||
2301 | } | ||||||||||||||
2302 | }; | ||||||||||||||
2303 | |||||||||||||||
2304 | } // namespace detail | ||||||||||||||
2305 | } // namespace mozilla | ||||||||||||||
2306 | |||||||||||||||
2307 | #endif /* mozilla_HashTable_h */ |
1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
2 | * vim: set ts=8 sts=2 et sw=2 tw=80: |
3 | * This Source Code Form is subject to the terms of the Mozilla Public |
4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | |
7 | /* |
8 | * JS allocation policies. |
9 | * |
10 | * The allocators here are for system memory with lifetimes which are not |
11 | * managed by the GC. See the comment at the top of vm/MallocProvider.h. |
12 | */ |
13 | |
14 | #ifndef js_AllocPolicy_h |
15 | #define js_AllocPolicy_h |
16 | |
17 | #include "js/TypeDecls.h" |
18 | #include "js/Utility.h" |
19 | |
20 | extern MOZ_COLD__attribute__((cold)) JS_PUBLIC_API void JS_ReportOutOfMemory(JSContext* cx); |
21 | |
22 | namespace js { |
23 | |
24 | class FrontendContext; |
25 | |
26 | enum class AllocFunction { Malloc, Calloc, Realloc }; |
27 | |
28 | class ArenaAllocPolicyBase { |
29 | public: |
30 | template <typename T> |
31 | T* maybe_pod_arena_malloc(arena_id_t arenaId, size_t numElems) { |
32 | return js_pod_arena_malloc<T>(arenaId, numElems); |
33 | } |
34 | template <typename T> |
35 | T* maybe_pod_arena_calloc(arena_id_t arenaId, size_t numElems) { |
36 | return js_pod_arena_calloc<T>(arenaId, numElems); |
37 | } |
38 | template <typename T> |
39 | T* maybe_pod_arena_realloc(arena_id_t arenaId, T* p, size_t oldSize, |
40 | size_t newSize) { |
41 | return js_pod_arena_realloc<T>(arenaId, p, oldSize, newSize); |
42 | } |
43 | template <typename T> |
44 | T* pod_arena_malloc(arena_id_t arenaId, size_t numElems) { |
45 | return maybe_pod_arena_malloc<T>(arenaId, numElems); |
46 | } |
47 | template <typename T> |
48 | T* pod_arena_calloc(arena_id_t arenaId, size_t numElems) { |
49 | return maybe_pod_arena_calloc<T>(arenaId, numElems); |
50 | } |
51 | template <typename T> |
52 | T* pod_arena_realloc(arena_id_t arenaId, T* p, size_t oldSize, |
53 | size_t newSize) { |
54 | return maybe_pod_arena_realloc<T>(arenaId, p, oldSize, newSize); |
55 | } |
56 | }; |
57 | |
58 | /* Base class allocation policies providing allocation methods. */ |
59 | class AllocPolicyBase : public ArenaAllocPolicyBase { |
60 | public: |
61 | template <typename T> |
62 | T* maybe_pod_malloc(size_t numElems) { |
63 | return maybe_pod_arena_malloc<T>(js::MallocArena, numElems); |
64 | } |
65 | template <typename T> |
66 | T* maybe_pod_calloc(size_t numElems) { |
67 | return maybe_pod_arena_calloc<T>(js::MallocArena, numElems); |
68 | } |
69 | template <typename T> |
70 | T* maybe_pod_realloc(T* p, size_t oldSize, size_t newSize) { |
71 | return maybe_pod_arena_realloc<T>(js::MallocArena, p, oldSize, newSize); |
72 | } |
73 | template <typename T> |
74 | T* pod_malloc(size_t numElems) { |
75 | return pod_arena_malloc<T>(js::MallocArena, numElems); |
76 | } |
77 | template <typename T> |
78 | T* pod_calloc(size_t numElems) { |
79 | return pod_arena_calloc<T>(js::MallocArena, numElems); |
80 | } |
81 | template <typename T> |
82 | T* pod_realloc(T* p, size_t oldSize, size_t newSize) { |
83 | return pod_arena_realloc<T>(js::MallocArena, p, oldSize, newSize); |
84 | } |
85 | |
86 | template <typename T> |
87 | void free_(T* p, size_t numElems = 0) { |
88 | js_free(p); |
89 | } |
90 | }; |
91 | |
92 | /* |
93 | * Base class allocation policies providing allocation methods for allocations |
94 | * off the main thread. |
95 | */ |
96 | class BackgroundAllocPolicyBase : ArenaAllocPolicyBase { |
97 | public: |
98 | template <typename T> |
99 | T* maybe_pod_malloc(size_t numElems) { |
100 | return maybe_pod_arena_malloc<T>(js::BackgroundMallocArena, numElems); |
101 | } |
102 | template <typename T> |
103 | T* maybe_pod_calloc(size_t numElems) { |
104 | return maybe_pod_arena_calloc<T>(js::BackgroundMallocArena, numElems); |
105 | } |
106 | template <typename T> |
107 | T* maybe_pod_realloc(T* p, size_t oldSize, size_t newSize) { |
108 | return maybe_pod_arena_realloc<T>(js::BackgroundMallocArena, p, oldSize, |
109 | newSize); |
110 | } |
111 | template <typename T> |
112 | T* pod_malloc(size_t numElems) { |
113 | return pod_arena_malloc<T>(js::BackgroundMallocArena, numElems); |
114 | } |
115 | template <typename T> |
116 | T* pod_calloc(size_t numElems) { |
117 | return pod_arena_calloc<T>(js::BackgroundMallocArena, numElems); |
118 | } |
119 | template <typename T> |
120 | T* pod_realloc(T* p, size_t oldSize, size_t newSize) { |
121 | return pod_arena_realloc<T>(js::BackgroundMallocArena, p, oldSize, newSize); |
122 | } |
123 | |
124 | template <typename T> |
125 | void free_(T* p, size_t numElems = 0) { |
126 | js_free(p); |
127 | } |
128 | }; |
129 | |
130 | /* Policy for using system memory functions and doing no error reporting. */ |
131 | class SystemAllocPolicy : public AllocPolicyBase { |
132 | public: |
133 | void reportAllocOverflow() const {} |
134 | bool checkSimulatedOOM() const { return !js::oom::ShouldFailWithOOM(); } |
135 | }; |
136 | |
137 | class BackgroundSystemAllocPolicy : public BackgroundAllocPolicyBase { |
138 | public: |
139 | void reportAllocOverflow() const {} |
140 | bool checkSimulatedOOM() const { return !js::oom::ShouldFailWithOOM(); } |
141 | }; |
142 | |
143 | MOZ_COLD__attribute__((cold)) JS_PUBLIC_API void ReportOutOfMemory(JSContext* cx); |
144 | MOZ_COLD__attribute__((cold)) JS_PUBLIC_API void ReportOutOfMemory(FrontendContext* fc); |
145 | |
146 | // An out of memory condition which is easily user generatable and should |
147 | // be specially handled to try and avoid a tab crash. |
148 | MOZ_COLD__attribute__((cold)) JS_PUBLIC_API void ReportLargeOutOfMemory(JSContext* cx); |
149 | |
150 | /* |
151 | * Allocation policy that calls the system memory functions and reports errors |
152 | * to the context. Since the JSContext given on construction is stored for |
153 | * the lifetime of the container, this policy may only be used for containers |
154 | * whose lifetime is a shorter than the given JSContext. |
155 | * |
156 | * FIXME bug 647103 - rewrite this in terms of temporary allocation functions, |
157 | * not the system ones. |
158 | */ |
159 | class JS_PUBLIC_API TempAllocPolicy : public AllocPolicyBase { |
160 | // Type tag for context_bits_ |
161 | static constexpr uintptr_t JsContextTag = 0x1; |
162 | |
163 | // Either a JSContext* (if JsContextTag is set), or FrontendContext* |
164 | uintptr_t const context_bits_; |
165 | |
166 | MOZ_ALWAYS_INLINEinline bool hasJSContext() const { |
167 | return (context_bits_ & JsContextTag) == JsContextTag; |
168 | } |
169 | |
170 | MOZ_ALWAYS_INLINEinline JSContext* cx() const { |
171 | MOZ_ASSERT(hasJSContext())do { static_assert( mozilla::detail::AssertionConditionType< decltype(hasJSContext())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(hasJSContext()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("hasJSContext()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/AllocPolicy.h" , 171); AnnotateMozCrashReason("MOZ_ASSERT" "(" "hasJSContext()" ")"); do { *((volatile int*)__null) = 171; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
172 | return reinterpret_cast<JSContext*>(context_bits_ ^ JsContextTag); |
173 | } |
174 | |
175 | MOZ_ALWAYS_INLINEinline FrontendContext* fc() const { |
176 | MOZ_ASSERT(!hasJSContext())do { static_assert( mozilla::detail::AssertionConditionType< decltype(!hasJSContext())>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(!hasJSContext()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!hasJSContext()" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/AllocPolicy.h" , 176); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!hasJSContext()" ")"); do { *((volatile int*)__null) = 176; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
177 | return reinterpret_cast<FrontendContext*>(context_bits_); |
178 | } |
179 | |
180 | /* |
181 | * Non-inline helper to call JSRuntime::onOutOfMemory with minimal |
182 | * code bloat. |
183 | */ |
184 | void* onOutOfMemory(arena_id_t arenaId, AllocFunction allocFunc, |
185 | size_t nbytes, void* reallocPtr = nullptr); |
186 | |
187 | template <typename T> |
188 | T* onOutOfMemoryTyped(arena_id_t arenaId, AllocFunction allocFunc, |
189 | size_t numElems, void* reallocPtr = nullptr) { |
190 | size_t bytes; |
191 | if (MOZ_UNLIKELY(!CalculateAllocSize<T>(numElems, &bytes))(__builtin_expect(!!(!CalculateAllocSize<T>(numElems, & bytes)), 0))) { |
192 | return nullptr; |
193 | } |
194 | return static_cast<T*>( |
195 | onOutOfMemory(arenaId, allocFunc, bytes, reallocPtr)); |
196 | } |
197 | |
198 | public: |
199 | MOZ_IMPLICIT TempAllocPolicy(JSContext* cx) |
200 | : context_bits_(uintptr_t(cx) | JsContextTag) { |
201 | MOZ_ASSERT((uintptr_t(cx) & JsContextTag) == 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype((uintptr_t(cx) & JsContextTag) == 0)>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!((uintptr_t(cx) & JsContextTag) == 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("(uintptr_t(cx) & JsContextTag) == 0" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/AllocPolicy.h" , 201); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(uintptr_t(cx) & JsContextTag) == 0" ")"); do { *((volatile int*)__null) = 201; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
202 | } |
203 | MOZ_IMPLICIT TempAllocPolicy(FrontendContext* fc) |
204 | : context_bits_(uintptr_t(fc)) { |
205 | MOZ_ASSERT((uintptr_t(fc) & JsContextTag) == 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype((uintptr_t(fc) & JsContextTag) == 0)>::isValid , "invalid assertion condition"); if ((__builtin_expect(!!(!( !!((uintptr_t(fc) & JsContextTag) == 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("(uintptr_t(fc) & JsContextTag) == 0" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/AllocPolicy.h" , 205); AnnotateMozCrashReason("MOZ_ASSERT" "(" "(uintptr_t(fc) & JsContextTag) == 0" ")"); do { *((volatile int*)__null) = 205; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
206 | } |
207 | |
208 | template <typename T> |
209 | T* pod_arena_malloc(arena_id_t arenaId, size_t numElems) { |
210 | T* p = this->maybe_pod_arena_malloc<T>(arenaId, numElems); |
211 | if (MOZ_UNLIKELY(!p)(__builtin_expect(!!(!p), 0))) { |
212 | p = onOutOfMemoryTyped<T>(arenaId, AllocFunction::Malloc, numElems); |
213 | } |
214 | return p; |
215 | } |
216 | |
217 | template <typename T> |
218 | T* pod_arena_calloc(arena_id_t arenaId, size_t numElems) { |
219 | T* p = this->maybe_pod_arena_calloc<T>(arenaId, numElems); |
220 | if (MOZ_UNLIKELY(!p)(__builtin_expect(!!(!p), 0))) { |
221 | p = onOutOfMemoryTyped<T>(arenaId, AllocFunction::Calloc, numElems); |
222 | } |
223 | return p; |
224 | } |
225 | |
226 | template <typename T> |
227 | T* pod_arena_realloc(arena_id_t arenaId, T* prior, size_t oldSize, |
228 | size_t newSize) { |
229 | T* p2 = this->maybe_pod_arena_realloc<T>(arenaId, prior, oldSize, newSize); |
230 | if (MOZ_UNLIKELY(!p2)(__builtin_expect(!!(!p2), 0))) { |
231 | p2 = onOutOfMemoryTyped<T>(arenaId, AllocFunction::Realloc, newSize, |
232 | prior); |
233 | } |
234 | return p2; |
235 | } |
236 | |
237 | template <typename T> |
238 | T* pod_malloc(size_t numElems) { |
239 | return pod_arena_malloc<T>(js::MallocArena, numElems); |
240 | } |
241 | |
242 | template <typename T> |
243 | T* pod_calloc(size_t numElems) { |
244 | return pod_arena_calloc<T>(js::MallocArena, numElems); |
245 | } |
246 | |
247 | template <typename T> |
248 | T* pod_realloc(T* prior, size_t oldSize, size_t newSize) { |
249 | return pod_arena_realloc<T>(js::MallocArena, prior, oldSize, newSize); |
250 | } |
251 | |
252 | template <typename T> |
253 | void free_(T* p, size_t numElems = 0) { |
254 | js_free(p); |
255 | } |
256 | |
257 | void reportAllocOverflow() const; |
258 | |
259 | bool checkSimulatedOOM() const { |
260 | if (js::oom::ShouldFailWithOOM()) { |
261 | if (hasJSContext()) { |
262 | ReportOutOfMemory(cx()); |
263 | } else { |
264 | ReportOutOfMemory(fc()); |
265 | } |
266 | return false; |
267 | } |
268 | |
269 | return true; |
270 | } |
271 | }; |
272 | |
273 | /* |
274 | * A replacement for MallocAllocPolicy that allocates in the JS heap and adds no |
275 | * extra behaviours. |
276 | * |
277 | * This is currently used for allocating source buffers for parsing. Since these |
278 | * are temporary and will not be freed by GC, the memory is not tracked by the |
279 | * usual accounting. |
280 | */ |
281 | class MallocAllocPolicy : public AllocPolicyBase { |
282 | public: |
283 | void reportAllocOverflow() const {} |
284 | |
285 | [[nodiscard]] bool checkSimulatedOOM() const { return true; } |
286 | }; |
287 | |
288 | } /* namespace js */ |
289 | |
290 | #endif /* js_AllocPolicy_h */ |
1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
2 | * vim: set ts=8 sts=2 et sw=2 tw=80: |
3 | * This Source Code Form is subject to the terms of the Mozilla Public |
4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | |
7 | #ifndef js_Utility_h |
8 | #define js_Utility_h |
9 | |
10 | #include "mozilla/Assertions.h" |
11 | #include "mozilla/Atomics.h" |
12 | #include "mozilla/Attributes.h" |
13 | #include "mozilla/Compiler.h" |
14 | #include "mozilla/TemplateLib.h" |
15 | #include "mozilla/UniquePtr.h" |
16 | |
17 | #include <stdlib.h> |
18 | #include <string.h> |
19 | #include <type_traits> |
20 | #include <utility> |
21 | |
22 | #include "jstypes.h" |
23 | #include "mozmemory.h" |
24 | #include "js/TypeDecls.h" |
25 | |
26 | /* The public JS engine namespace. */ |
27 | namespace JS {} |
28 | |
29 | /* The mozilla-shared reusable template/utility namespace. */ |
30 | namespace mozilla {} |
31 | |
32 | /* The private JS engine namespace. */ |
33 | namespace js {} |
34 | |
35 | extern MOZ_NORETURN__attribute__((noreturn)) MOZ_COLD__attribute__((cold)) JS_PUBLIC_API void JS_Assert(const char* s, |
36 | const char* file, |
37 | int ln); |
38 | |
39 | /* |
40 | * Custom allocator support for SpiderMonkey |
41 | */ |
42 | #if defined JS_USE_CUSTOM_ALLOCATOR |
43 | # include "jscustomallocator.h" |
44 | #else |
45 | |
46 | namespace js { |
47 | |
48 | /* |
49 | * Thread types are used to tag threads for certain kinds of testing (see |
50 | * below), and also used to characterize threads in the thread scheduler (see |
51 | * js/src/vm/HelperThreads.cpp). |
52 | * |
53 | * Please update oom::FirstThreadTypeToTest and oom::LastThreadTypeToTest when |
54 | * adding new thread types. |
55 | */ |
56 | enum ThreadType { |
57 | THREAD_TYPE_NONE = 0, // 0 |
58 | THREAD_TYPE_MAIN, // 1 |
59 | THREAD_TYPE_WASM_COMPILE_TIER1, // 2 |
60 | THREAD_TYPE_WASM_COMPILE_TIER2, // 3 |
61 | THREAD_TYPE_BASELINE, // 4 |
62 | THREAD_TYPE_ION, // 5 |
63 | THREAD_TYPE_COMPRESS, // 6 |
64 | THREAD_TYPE_GCPARALLEL, // 7 |
65 | THREAD_TYPE_PROMISE_TASK, // 8 |
66 | THREAD_TYPE_ION_FREE, // 9 |
67 | THREAD_TYPE_WASM_GENERATOR_COMPLETE_TIER2, // 10 |
68 | THREAD_TYPE_WASM_COMPILE_PARTIAL_TIER2, // 11 |
69 | THREAD_TYPE_WORKER, // 12 |
70 | THREAD_TYPE_DELAZIFY, // 13 |
71 | THREAD_TYPE_DELAZIFY_FREE, // 14 |
72 | THREAD_TYPE_MAX // Used to check shell function arguments |
73 | }; |
74 | |
75 | namespace oom { |
76 | |
77 | /* |
78 | * Theads are tagged only in certain debug contexts. Notably, to make testing |
79 | * OOM in certain helper threads more effective, we allow restricting the OOM |
80 | * testing to a certain helper thread type. This allows us to fail e.g. in |
81 | * off-thread script parsing without causing an OOM in the active thread first. |
82 | * |
83 | * Getter/Setter functions to encapsulate mozilla::ThreadLocal, implementation |
84 | * is in util/Utility.cpp. |
85 | */ |
86 | # if defined(DEBUG1) || defined(JS_OOM_BREAKPOINT) |
87 | |
88 | // Define the range of threads tested by simulated OOM testing and the |
89 | // like. Testing worker threads is not supported. |
90 | const ThreadType FirstThreadTypeToTest = THREAD_TYPE_MAIN; |
91 | const ThreadType LastThreadTypeToTest = THREAD_TYPE_WASM_COMPILE_PARTIAL_TIER2; |
92 | |
93 | extern bool InitThreadType(void); |
94 | extern void SetThreadType(ThreadType); |
95 | extern JS_PUBLIC_API uint32_t GetThreadType(void); |
96 | |
97 | # else |
98 | |
99 | inline bool InitThreadType(void) { return true; } |
100 | inline void SetThreadType(ThreadType t) {}; |
101 | inline uint32_t GetThreadType(void) { return 0; } |
102 | inline uint32_t GetAllocationThreadType(void) { return 0; } |
103 | inline uint32_t GetStackCheckThreadType(void) { return 0; } |
104 | inline uint32_t GetInterruptCheckThreadType(void) { return 0; } |
105 | |
106 | # endif |
107 | |
108 | } /* namespace oom */ |
109 | } /* namespace js */ |
110 | |
111 | # if defined(DEBUG1) || defined(JS_OOM_BREAKPOINT) |
112 | |
113 | # ifdef JS_OOM_BREAKPOINT |
114 | # if defined(_MSC_VER) |
115 | static MOZ_NEVER_INLINE__attribute__((noinline)) void js_failedAllocBreakpoint() { |
116 | __asm {} |
117 | ; |
118 | } |
119 | # else |
120 | static MOZ_NEVER_INLINE__attribute__((noinline)) void js_failedAllocBreakpoint() { asm(""); } |
121 | # endif |
122 | # define JS_OOM_CALL_BP_FUNC()do { } while (0) js_failedAllocBreakpoint() |
123 | # else |
124 | # define JS_OOM_CALL_BP_FUNC()do { } while (0) \ |
125 | do { \ |
126 | } while (0) |
127 | # endif |
128 | |
129 | namespace js { |
130 | namespace oom { |
131 | |
132 | /* |
133 | * Out of memory testing support. We provide various testing functions to |
134 | * simulate OOM conditions and so we can test that they are handled correctly. |
135 | */ |
136 | class FailureSimulator { |
137 | public: |
138 | enum class Kind : uint8_t { Nothing, OOM, StackOOM, Interrupt }; |
139 | |
140 | private: |
141 | Kind kind_ = Kind::Nothing; |
142 | uint32_t targetThread_ = 0; |
143 | uint64_t maxChecks_ = UINT64_MAX(18446744073709551615UL); |
144 | uint64_t counter_ = 0; |
145 | bool failAlways_ = true; |
146 | bool inUnsafeRegion_ = false; |
147 | |
148 | public: |
149 | uint64_t maxChecks() const { return maxChecks_; } |
150 | uint64_t counter() const { return counter_; } |
151 | void setInUnsafeRegion(bool b) { |
152 | MOZ_ASSERT(inUnsafeRegion_ != b)do { static_assert( mozilla::detail::AssertionConditionType< decltype(inUnsafeRegion_ != b)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(inUnsafeRegion_ != b))), 0)) ) { do { } while (false); MOZ_ReportAssertionFailure("inUnsafeRegion_ != b" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/Utility.h" , 152); AnnotateMozCrashReason("MOZ_ASSERT" "(" "inUnsafeRegion_ != b" ")"); do { *((volatile int*)__null) = 152; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
153 | inUnsafeRegion_ = b; |
154 | } |
155 | uint32_t targetThread() const { return targetThread_; } |
156 | bool isThreadSimulatingAny() const { |
157 | return targetThread_ && targetThread_ == js::oom::GetThreadType() && |
158 | !inUnsafeRegion_; |
159 | } |
160 | bool isThreadSimulating(Kind kind) const { |
161 | return kind_ == kind && isThreadSimulatingAny(); |
162 | } |
163 | bool isSimulatedFailure(Kind kind) const { |
164 | if (!isThreadSimulating(kind)) { |
165 | return false; |
166 | } |
167 | return counter_ == maxChecks_ || (counter_ > maxChecks_ && failAlways_); |
168 | } |
169 | bool hadFailure(Kind kind) const { |
170 | return kind_ == kind && counter_ >= maxChecks_; |
171 | } |
172 | bool shouldFail(Kind kind) { |
173 | if (!isThreadSimulating(kind)) { |
174 | return false; |
175 | } |
176 | counter_++; |
177 | if (isSimulatedFailure(kind)) { |
178 | JS_OOM_CALL_BP_FUNC()do { } while (0); |
179 | return true; |
180 | } |
181 | return false; |
182 | } |
183 | |
184 | void simulateFailureAfter(Kind kind, uint64_t checks, uint32_t thread, |
185 | bool always); |
186 | void reset(); |
187 | }; |
188 | extern JS_PUBLIC_DATA FailureSimulator simulator; |
189 | |
190 | inline bool IsSimulatedOOMAllocation() { |
191 | return simulator.isSimulatedFailure(FailureSimulator::Kind::OOM); |
192 | } |
193 | |
194 | inline bool ShouldFailWithOOM() { |
195 | return simulator.shouldFail(FailureSimulator::Kind::OOM); |
196 | } |
197 | |
198 | inline bool HadSimulatedOOM() { |
199 | return simulator.hadFailure(FailureSimulator::Kind::OOM); |
200 | } |
201 | |
202 | /* |
203 | * Out of stack space testing support, similar to OOM testing functions. |
204 | */ |
205 | |
206 | inline bool IsSimulatedStackOOMCheck() { |
207 | return simulator.isSimulatedFailure(FailureSimulator::Kind::StackOOM); |
208 | } |
209 | |
210 | inline bool ShouldFailWithStackOOM() { |
211 | return simulator.shouldFail(FailureSimulator::Kind::StackOOM); |
212 | } |
213 | |
214 | inline bool HadSimulatedStackOOM() { |
215 | return simulator.hadFailure(FailureSimulator::Kind::StackOOM); |
216 | } |
217 | |
218 | /* |
219 | * Interrupt testing support, similar to OOM testing functions. |
220 | */ |
221 | |
222 | inline bool IsSimulatedInterruptCheck() { |
223 | return simulator.isSimulatedFailure(FailureSimulator::Kind::Interrupt); |
224 | } |
225 | |
226 | inline bool ShouldFailWithInterrupt() { |
227 | return simulator.shouldFail(FailureSimulator::Kind::Interrupt); |
228 | } |
229 | |
230 | inline bool HadSimulatedInterrupt() { |
231 | return simulator.hadFailure(FailureSimulator::Kind::Interrupt); |
232 | } |
233 | |
234 | } /* namespace oom */ |
235 | } /* namespace js */ |
236 | |
237 | # define JS_OOM_POSSIBLY_FAIL()do { if (js::oom::ShouldFailWithOOM()) return nullptr; } while (0) \ |
238 | do { \ |
239 | if (js::oom::ShouldFailWithOOM()) return nullptr; \ |
240 | } while (0) |
241 | |
242 | # define JS_OOM_POSSIBLY_FAIL_BOOL()do { if (js::oom::ShouldFailWithOOM()) return false; } while ( 0) \ |
243 | do { \ |
244 | if (js::oom::ShouldFailWithOOM()) return false; \ |
245 | } while (0) |
246 | |
247 | # define JS_STACK_OOM_POSSIBLY_FAIL()do { if (js::oom::ShouldFailWithStackOOM()) return false; } while (0) \ |
248 | do { \ |
249 | if (js::oom::ShouldFailWithStackOOM()) return false; \ |
250 | } while (0) |
251 | |
252 | # define JS_INTERRUPT_POSSIBLY_FAIL()do { if ((__builtin_expect(!!(js::oom::ShouldFailWithInterrupt ()), 0))) { cx->requestInterrupt(js::InterruptReason::CallbackUrgent ); return cx->handleInterrupt(); } } while (0) \ |
253 | do { \ |
254 | if (MOZ_UNLIKELY(js::oom::ShouldFailWithInterrupt())(__builtin_expect(!!(js::oom::ShouldFailWithInterrupt()), 0))) { \ |
255 | cx->requestInterrupt(js::InterruptReason::CallbackUrgent); \ |
256 | return cx->handleInterrupt(); \ |
257 | } \ |
258 | } while (0) |
259 | |
260 | # else |
261 | |
262 | # define JS_OOM_POSSIBLY_FAIL()do { if (js::oom::ShouldFailWithOOM()) return nullptr; } while (0) \ |
263 | do { \ |
264 | } while (0) |
265 | # define JS_OOM_POSSIBLY_FAIL_BOOL()do { if (js::oom::ShouldFailWithOOM()) return false; } while ( 0) \ |
266 | do { \ |
267 | } while (0) |
268 | # define JS_STACK_OOM_POSSIBLY_FAIL()do { if (js::oom::ShouldFailWithStackOOM()) return false; } while (0) \ |
269 | do { \ |
270 | } while (0) |
271 | # define JS_INTERRUPT_POSSIBLY_FAIL()do { if ((__builtin_expect(!!(js::oom::ShouldFailWithInterrupt ()), 0))) { cx->requestInterrupt(js::InterruptReason::CallbackUrgent ); return cx->handleInterrupt(); } } while (0) \ |
272 | do { \ |
273 | } while (0) |
274 | namespace js { |
275 | namespace oom { |
276 | static inline bool IsSimulatedOOMAllocation() { return false; } |
277 | static inline bool ShouldFailWithOOM() { return false; } |
278 | } /* namespace oom */ |
279 | } /* namespace js */ |
280 | |
281 | # endif /* DEBUG || JS_OOM_BREAKPOINT */ |
282 | |
283 | # ifdef FUZZING |
284 | namespace js { |
285 | namespace oom { |
286 | extern JS_PUBLIC_DATA size_t largeAllocLimit; |
287 | extern void InitLargeAllocLimit(); |
288 | } /* namespace oom */ |
289 | } /* namespace js */ |
290 | |
291 | # define JS_CHECK_LARGE_ALLOC(x)do { } while (0) \ |
292 | do { \ |
293 | if (js::oom::largeAllocLimit && x > js::oom::largeAllocLimit) { \ |
294 | if (getenv("MOZ_FUZZ_CRASH_ON_LARGE_ALLOC")) { \ |
295 | MOZ_CRASH("Large allocation")do { do { } while (false); MOZ_ReportCrash("" "Large allocation" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/Utility.h" , 295); AnnotateMozCrashReason("MOZ_CRASH(" "Large allocation" ")"); do { *((volatile int*)__null) = 295; __attribute__((nomerge )) ::abort(); } while (false); } while (false); \ |
296 | } else { \ |
297 | return nullptr; \ |
298 | } \ |
299 | } \ |
300 | } while (0) |
301 | # else |
302 | # define JS_CHECK_LARGE_ALLOC(x)do { } while (0) \ |
303 | do { \ |
304 | } while (0) |
305 | # endif |
306 | |
307 | namespace js { |
308 | |
309 | /* Disable OOM testing in sections which are not OOM safe. */ |
310 | struct MOZ_RAII JS_PUBLIC_DATA AutoEnterOOMUnsafeRegion { |
311 | MOZ_NORETURN__attribute__((noreturn)) MOZ_COLD__attribute__((cold)) void crash(const char* reason) { crash_impl(reason); } |
312 | MOZ_NORETURN__attribute__((noreturn)) MOZ_COLD__attribute__((cold)) void crash(size_t size, const char* reason) { |
313 | crash_impl(reason); |
314 | } |
315 | |
316 | using AnnotateOOMAllocationSizeCallback = void (*)(size_t); |
317 | static mozilla::Atomic<AnnotateOOMAllocationSizeCallback, mozilla::Relaxed> |
318 | annotateOOMSizeCallback; |
319 | static void setAnnotateOOMAllocationSizeCallback( |
320 | AnnotateOOMAllocationSizeCallback callback) { |
321 | annotateOOMSizeCallback = callback; |
322 | } |
323 | |
324 | # if defined(DEBUG1) || defined(JS_OOM_BREAKPOINT) |
325 | AutoEnterOOMUnsafeRegion() |
326 | : oomEnabled_(oom::simulator.isThreadSimulatingAny()) { |
327 | if (oomEnabled_) { |
328 | MOZ_ALWAYS_TRUE(owner_.compareExchange(nullptr, this))do { if ((__builtin_expect(!!(owner_.compareExchange(nullptr, this)), 1))) { } else { do { do { } while (false); MOZ_ReportCrash ("" "owner_.compareExchange(nullptr, this)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/Utility.h" , 328); AnnotateMozCrashReason("MOZ_CRASH(" "owner_.compareExchange(nullptr, this)" ")"); do { *((volatile int*)__null) = 328; __attribute__((nomerge )) ::abort(); } while (false); } while (false); } } while (false ); |
329 | oom::simulator.setInUnsafeRegion(true); |
330 | } |
331 | } |
332 | |
333 | ~AutoEnterOOMUnsafeRegion() { |
334 | if (oomEnabled_) { |
335 | oom::simulator.setInUnsafeRegion(false); |
336 | MOZ_ALWAYS_TRUE(owner_.compareExchange(this, nullptr))do { if ((__builtin_expect(!!(owner_.compareExchange(this, nullptr )), 1))) { } else { do { do { } while (false); MOZ_ReportCrash ("" "owner_.compareExchange(this, nullptr)", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/Utility.h" , 336); AnnotateMozCrashReason("MOZ_CRASH(" "owner_.compareExchange(this, nullptr)" ")"); do { *((volatile int*)__null) = 336; __attribute__((nomerge )) ::abort(); } while (false); } while (false); } } while (false ); |
337 | } |
338 | } |
339 | |
340 | private: |
341 | // Used to catch concurrent use from other threads. |
342 | static mozilla::Atomic<AutoEnterOOMUnsafeRegion*> owner_; |
343 | |
344 | bool oomEnabled_; |
345 | # endif |
346 | private: |
347 | static MOZ_NORETURN__attribute__((noreturn)) MOZ_COLD__attribute__((cold)) void crash_impl(const char* reason); |
348 | static MOZ_NORETURN__attribute__((noreturn)) MOZ_COLD__attribute__((cold)) void crash_impl(size_t size, const char* reason); |
349 | }; |
350 | |
351 | } /* namespace js */ |
352 | |
353 | // Malloc allocation. |
354 | |
355 | namespace js { |
356 | |
357 | // The following two arenas require a little bit of clarification. We have |
358 | // observed that, particularly on devices with heterogeneous CPU architectures |
359 | // where background work can run on significantly slower cores than main thread |
360 | // work, the lock contention in the allocator can be a big problem for the |
361 | // main thread. So we introduced an arena for background allocations which can |
362 | // reduce that contention. |
363 | // |
364 | // The general rule for these is: if it's easy to determine at the time of |
365 | // authorship that an allocation will be *off* the main thread, use the |
366 | // BackgroundMallocArena, and vice versa. If it is hard to determine, just make |
367 | // a guess, and that will be fine. Do not spend too much time on this, and |
368 | // don't do anything fancy at runtime to try to determine which to use. |
369 | extern JS_PUBLIC_DATA arena_id_t MallocArena; |
370 | extern JS_PUBLIC_DATA arena_id_t BackgroundMallocArena; |
371 | |
372 | extern JS_PUBLIC_DATA arena_id_t ArrayBufferContentsArena; |
373 | extern JS_PUBLIC_DATA arena_id_t StringBufferArena; |
374 | |
375 | extern void InitMallocAllocator(); |
376 | extern void ShutDownMallocAllocator(); |
377 | |
378 | // This is a no-op if built without MOZ_MEMORY and MOZ_DEBUG. |
379 | extern void AssertJSStringBufferInCorrectArena(const void* ptr); |
380 | |
381 | } /* namespace js */ |
382 | |
383 | static inline void* js_arena_malloc(arena_id_t arena, size_t bytes) { |
384 | JS_OOM_POSSIBLY_FAIL()do { if (js::oom::ShouldFailWithOOM()) return nullptr; } while (0); |
385 | JS_CHECK_LARGE_ALLOC(bytes)do { } while (0); |
386 | return moz_arena_malloc(arena, bytes); |
387 | } |
388 | |
389 | static inline void* js_malloc(size_t bytes) { |
390 | return js_arena_malloc(js::MallocArena, bytes); |
391 | } |
392 | |
393 | static inline void* js_arena_calloc(arena_id_t arena, size_t bytes) { |
394 | JS_OOM_POSSIBLY_FAIL()do { if (js::oom::ShouldFailWithOOM()) return nullptr; } while (0); |
395 | JS_CHECK_LARGE_ALLOC(bytes)do { } while (0); |
396 | return moz_arena_calloc(arena, bytes, 1); |
397 | } |
398 | |
399 | static inline void* js_arena_calloc(arena_id_t arena, size_t nmemb, |
400 | size_t size) { |
401 | JS_OOM_POSSIBLY_FAIL()do { if (js::oom::ShouldFailWithOOM()) return nullptr; } while (0); |
402 | JS_CHECK_LARGE_ALLOC(nmemb * size)do { } while (0); |
403 | return moz_arena_calloc(arena, nmemb, size); |
404 | } |
405 | |
406 | static inline void* js_calloc(size_t bytes) { |
407 | return js_arena_calloc(js::MallocArena, bytes); |
408 | } |
409 | |
410 | static inline void* js_calloc(size_t nmemb, size_t size) { |
411 | return js_arena_calloc(js::MallocArena, nmemb, size); |
412 | } |
413 | |
414 | static inline void* js_arena_realloc(arena_id_t arena, void* p, size_t bytes) { |
415 | // realloc() with zero size is not portable, as some implementations may |
416 | // return nullptr on success and free |p| for this. We assume nullptr |
417 | // indicates failure and that |p| is still valid. |
418 | MOZ_ASSERT(bytes != 0)do { static_assert( mozilla::detail::AssertionConditionType< decltype(bytes != 0)>::isValid, "invalid assertion condition" ); if ((__builtin_expect(!!(!(!!(bytes != 0))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("bytes != 0", "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/Utility.h" , 418); AnnotateMozCrashReason("MOZ_ASSERT" "(" "bytes != 0" ")" ); do { *((volatile int*)__null) = 418; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
419 | |
420 | JS_OOM_POSSIBLY_FAIL()do { if (js::oom::ShouldFailWithOOM()) return nullptr; } while (0); |
421 | JS_CHECK_LARGE_ALLOC(bytes)do { } while (0); |
422 | return moz_arena_realloc(arena, p, bytes); |
423 | } |
424 | |
425 | static inline void* js_realloc(void* p, size_t bytes) { |
426 | return js_arena_realloc(js::MallocArena, p, bytes); |
427 | } |
428 | |
429 | static inline void js_free(void* p) { |
430 | // Bug 1784164: This should call |moz_arena_free(js::MallocArena, p)| but we |
431 | // currently can't enforce that all memory freed here was allocated by |
432 | // js_malloc(). All other memory should go through a different allocator and |
433 | // deallocator. |
434 | free(p); |
435 | } |
436 | #endif /* JS_USE_CUSTOM_ALLOCATOR */ |
437 | |
438 | #include <new> |
439 | |
440 | /* |
441 | * [SMDOC] Low-level memory management in SpiderMonkey |
442 | * |
443 | * ** Do not use the standard malloc/free/realloc: SpiderMonkey allows these |
444 | * to be redefined (via JS_USE_CUSTOM_ALLOCATOR) and Gecko even #define's |
445 | * these symbols. |
446 | * |
447 | * ** Do not use the builtin C++ operator new and delete: these throw on |
448 | * error and we cannot override them not to. |
449 | * |
450 | * Allocation: |
451 | * |
452 | * - If the lifetime of the allocation is tied to the lifetime of a GC-thing |
453 | * (that is, finalizing the GC-thing will free the allocation), call one of |
454 | * the following functions: |
455 | * |
456 | * JSContext::{pod_malloc,pod_calloc,pod_realloc} |
457 | * Zone::{pod_malloc,pod_calloc,pod_realloc} |
458 | * |
459 | * These functions accumulate the number of bytes allocated which is used as |
460 | * part of the GC-triggering heuristics. |
461 | * |
462 | * The difference between the JSContext and Zone versions is that the |
463 | * cx version report an out-of-memory error on OOM. (This follows from the |
464 | * general SpiderMonkey idiom that a JSContext-taking function reports its |
465 | * own errors.) |
466 | * |
467 | * If you don't want to report an error on failure, there are maybe_ versions |
468 | * of these methods available too, e.g. maybe_pod_malloc. |
469 | * |
470 | * The methods above use templates to allow allocating memory suitable for an |
471 | * array of a given type and number of elements. There are _with_extra |
472 | * versions to allow allocating an area of memory which is larger by a |
473 | * specified number of bytes, e.g. pod_malloc_with_extra. |
474 | * |
475 | * These methods are available on a JSRuntime, but calling them is |
476 | * discouraged. Memory attributed to a runtime can only be reclaimed by full |
477 | * GCs, and we try to avoid those where possible. |
478 | * |
479 | * - Otherwise, use js_malloc/js_realloc/js_calloc/js_new |
480 | * |
481 | * Deallocation: |
482 | * |
483 | * - Ordinarily, use js_free/js_delete. |
484 | * |
485 | * - For deallocations during GC finalization, use one of the following |
486 | * operations on the JS::GCContext provided to the finalizer: |
487 | * |
488 | * JS::GCContext::{free_,delete_} |
489 | */ |
490 | |
491 | /* |
492 | * Given a class which should provide a 'new' method, add |
493 | * JS_DECLARE_NEW_METHODS (see js::MallocProvider for an example). |
494 | * |
495 | * Note: Do not add a ; at the end of a use of JS_DECLARE_NEW_METHODS, |
496 | * or the build will break. |
497 | */ |
498 | #define JS_DECLARE_NEW_METHODS(NEWNAME, ALLOCATOR, QUALIFIERS)template <class T, typename... Args> QUALIFIERS T* NEWNAME (Args&&... args) { static_assert( alignof(T) <= alignof (max_align_t), "over-aligned type is not supported by JS_DECLARE_NEW_METHODS" ); void* memory = ALLOCATOR(sizeof(T)); return (__builtin_expect (!!(memory), 1)) ? new (memory) T(std::forward<Args>(args )...) : nullptr; } \ |
499 | template <class T, typename... Args> \ |
500 | QUALIFIERS T* MOZ_HEAP_ALLOCATOR NEWNAME(Args&&... args) { \ |
501 | static_assert( \ |
502 | alignof(T) <= alignof(max_align_t), \ |
503 | "over-aligned type is not supported by JS_DECLARE_NEW_METHODS"); \ |
504 | void* memory = ALLOCATOR(sizeof(T)); \ |
505 | return MOZ_LIKELY(memory)(__builtin_expect(!!(memory), 1)) ? new (memory) T(std::forward<Args>(args)...) \ |
506 | : nullptr; \ |
507 | } |
508 | |
509 | /* |
510 | * Given a class which should provide a 'new' method that takes an arena as |
511 | * its first argument, add JS_DECLARE_NEW_ARENA_METHODS |
512 | * (see js::MallocProvider for an example). |
513 | * |
514 | * Note: Do not add a ; at the end of a use of JS_DECLARE_NEW_ARENA_METHODS, |
515 | * or the build will break. |
516 | */ |
517 | #define JS_DECLARE_NEW_ARENA_METHODS(NEWNAME, ALLOCATOR, QUALIFIERS)template <class T, typename... Args> QUALIFIERS T* NEWNAME (arena_id_t arena, Args&&... args) { static_assert( alignof (T) <= alignof(max_align_t), "over-aligned type is not supported by JS_DECLARE_NEW_ARENA_METHODS" ); void* memory = ALLOCATOR(arena, sizeof(T)); return (__builtin_expect (!!(memory), 1)) ? new (memory) T(std::forward<Args>(args )...) : nullptr; } \ |
518 | template <class T, typename... Args> \ |
519 | QUALIFIERS T* MOZ_HEAP_ALLOCATOR NEWNAME(arena_id_t arena, Args&&... args) { \ |
520 | static_assert( \ |
521 | alignof(T) <= alignof(max_align_t), \ |
522 | "over-aligned type is not supported by JS_DECLARE_NEW_ARENA_METHODS"); \ |
523 | void* memory = ALLOCATOR(arena, sizeof(T)); \ |
524 | return MOZ_LIKELY(memory)(__builtin_expect(!!(memory), 1)) ? new (memory) T(std::forward<Args>(args)...) \ |
525 | : nullptr; \ |
526 | } |
527 | |
528 | /* |
529 | * Given a class which should provide 'make' methods, add |
530 | * JS_DECLARE_MAKE_METHODS (see js::MallocProvider for an example). This |
531 | * method is functionally the same as JS_DECLARE_NEW_METHODS: it just declares |
532 | * methods that return mozilla::UniquePtr instances that will singly-manage |
533 | * ownership of the created object. |
534 | * |
535 | * Note: Do not add a ; at the end of a use of JS_DECLARE_MAKE_METHODS, |
536 | * or the build will break. |
537 | */ |
538 | #define JS_DECLARE_MAKE_METHODS(MAKENAME, NEWNAME, QUALIFIERS)template <class T, typename... Args> QUALIFIERS mozilla ::UniquePtr<T, JS::DeletePolicy<T>> MAKENAME(Args &&... args) { T* ptr = NEWNAME<T>(std::forward< Args>(args)...); return mozilla::UniquePtr<T, JS::DeletePolicy <T>>(ptr); } \ |
539 | template <class T, typename... Args> \ |
540 | QUALIFIERS mozilla::UniquePtr<T, JS::DeletePolicy<T>> MOZ_HEAP_ALLOCATOR \ |
541 | MAKENAME(Args&&... args) { \ |
542 | T* ptr = NEWNAME<T>(std::forward<Args>(args)...); \ |
543 | return mozilla::UniquePtr<T, JS::DeletePolicy<T>>(ptr); \ |
544 | } |
545 | |
546 | JS_DECLARE_NEW_METHODS(js_new, js_malloc, static MOZ_ALWAYS_INLINE)template <class T, typename... Args> static inline T* js_new (Args&&... args) { static_assert( alignof(T) <= alignof (max_align_t), "over-aligned type is not supported by JS_DECLARE_NEW_METHODS" ); void* memory = js_malloc(sizeof(T)); return (__builtin_expect (!!(memory), 1)) ? new (memory) T(std::forward<Args>(args )...) : nullptr; } |
547 | JS_DECLARE_NEW_ARENA_METHODS(js_arena_new, js_arena_malloc,template <class T, typename... Args> static inline T* js_arena_new (arena_id_t arena, Args&&... args) { static_assert( alignof (T) <= alignof(max_align_t), "over-aligned type is not supported by JS_DECLARE_NEW_ARENA_METHODS" ); void* memory = js_arena_malloc(arena, sizeof(T)); return ( __builtin_expect(!!(memory), 1)) ? new (memory) T(std::forward <Args>(args)...) : nullptr; } |
548 | static MOZ_ALWAYS_INLINE)template <class T, typename... Args> static inline T* js_arena_new (arena_id_t arena, Args&&... args) { static_assert( alignof (T) <= alignof(max_align_t), "over-aligned type is not supported by JS_DECLARE_NEW_ARENA_METHODS" ); void* memory = js_arena_malloc(arena, sizeof(T)); return ( __builtin_expect(!!(memory), 1)) ? new (memory) T(std::forward <Args>(args)...) : nullptr; } |
549 | |
550 | namespace js { |
551 | |
552 | /* |
553 | * Calculate the number of bytes needed to allocate |numElems| contiguous |
554 | * instances of type |T|. Return false if the calculation overflowed. |
555 | */ |
556 | template <typename T> |
557 | [[nodiscard]] inline bool CalculateAllocSize(size_t numElems, |
558 | size_t* bytesOut) { |
559 | *bytesOut = numElems * sizeof(T); |
560 | return (numElems & mozilla::tl::MulOverflowMask<sizeof(T)>::value) == 0; |
561 | } |
562 | |
563 | /* |
564 | * Calculate the number of bytes needed to allocate a single instance of type |
565 | * |T| followed by |numExtra| contiguous instances of type |Extra|. Return |
566 | * false if the calculation overflowed. |
567 | */ |
568 | template <typename T, typename Extra> |
569 | [[nodiscard]] inline bool CalculateAllocSizeWithExtra(size_t numExtra, |
570 | size_t* bytesOut) { |
571 | *bytesOut = sizeof(T) + numExtra * sizeof(Extra); |
572 | return (numExtra & mozilla::tl::MulOverflowMask<sizeof(Extra)>::value) == 0 && |
573 | *bytesOut >= sizeof(T); |
574 | } |
575 | |
576 | } /* namespace js */ |
577 | |
578 | template <class T> |
579 | static MOZ_ALWAYS_INLINEinline void js_delete(const T* p) { |
580 | if (p) { |
581 | p->~T(); |
582 | js_free(const_cast<T*>(p)); |
583 | } |
584 | } |
585 | |
586 | template <class T> |
587 | static MOZ_ALWAYS_INLINEinline void js_delete_poison(const T* p) { |
588 | if (p) { |
589 | p->~T(); |
590 | memset(static_cast<void*>(const_cast<T*>(p)), 0x3B, sizeof(T)); |
591 | js_free(const_cast<T*>(p)); |
592 | } |
593 | } |
594 | |
595 | template <class T> |
596 | static MOZ_ALWAYS_INLINEinline T* js_pod_arena_malloc(arena_id_t arena, |
597 | size_t numElems) { |
598 | size_t bytes; |
599 | if (MOZ_UNLIKELY(!js::CalculateAllocSize<T>(numElems, &bytes))(__builtin_expect(!!(!js::CalculateAllocSize<T>(numElems , &bytes)), 0))) { |
600 | return nullptr; |
601 | } |
602 | return static_cast<T*>(js_arena_malloc(arena, bytes)); |
603 | } |
604 | |
605 | template <class T> |
606 | static MOZ_ALWAYS_INLINEinline T* js_pod_malloc(size_t numElems) { |
607 | return js_pod_arena_malloc<T>(js::MallocArena, numElems); |
608 | } |
609 | |
610 | template <class T> |
611 | static MOZ_ALWAYS_INLINEinline T* js_pod_arena_calloc(arena_id_t arena, |
612 | size_t numElems) { |
613 | size_t bytes; |
614 | if (MOZ_UNLIKELY(!js::CalculateAllocSize<T>(numElems, &bytes))(__builtin_expect(!!(!js::CalculateAllocSize<T>(numElems , &bytes)), 0))) { |
615 | return nullptr; |
616 | } |
617 | return static_cast<T*>(js_arena_calloc(arena, bytes, 1)); |
618 | } |
619 | |
620 | template <class T> |
621 | static MOZ_ALWAYS_INLINEinline T* js_pod_calloc(size_t numElems) { |
622 | return js_pod_arena_calloc<T>(js::MallocArena, numElems); |
623 | } |
624 | |
625 | template <class T> |
626 | static MOZ_ALWAYS_INLINEinline T* js_pod_arena_realloc(arena_id_t arena, T* prior, |
627 | size_t oldSize, |
628 | size_t newSize) { |
629 | MOZ_ASSERT(!(oldSize & mozilla::tl::MulOverflowMask<sizeof(T)>::value))do { static_assert( mozilla::detail::AssertionConditionType< decltype(!(oldSize & mozilla::tl::MulOverflowMask<sizeof (T)>::value))>::isValid, "invalid assertion condition") ; if ((__builtin_expect(!!(!(!!(!(oldSize & mozilla::tl:: MulOverflowMask<sizeof(T)>::value)))), 0))) { do { } while (false); MOZ_ReportAssertionFailure("!(oldSize & mozilla::tl::MulOverflowMask<sizeof(T)>::value)" , "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/js/Utility.h" , 629); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!(oldSize & mozilla::tl::MulOverflowMask<sizeof(T)>::value)" ")"); do { *((volatile int*)__null) = 629; __attribute__((nomerge )) ::abort(); } while (false); } } while (false); |
630 | size_t bytes; |
631 | if (MOZ_UNLIKELY(!js::CalculateAllocSize<T>(newSize, &bytes))(__builtin_expect(!!(!js::CalculateAllocSize<T>(newSize , &bytes)), 0))) { |
632 | return nullptr; |
633 | } |
634 | return static_cast<T*>(js_arena_realloc(arena, prior, bytes)); |
635 | } |
636 | |
637 | template <class T> |
638 | static MOZ_ALWAYS_INLINEinline T* js_pod_realloc(T* prior, size_t oldSize, |
639 | size_t newSize) { |
640 | return js_pod_arena_realloc<T>(js::MallocArena, prior, oldSize, newSize); |
641 | } |
642 | |
643 | namespace JS { |
644 | |
645 | template <typename T> |
646 | struct DeletePolicy { |
647 | constexpr DeletePolicy() = default; |
648 | |
649 | template <typename U> |
650 | MOZ_IMPLICIT DeletePolicy( |
651 | DeletePolicy<U> other, |
652 | std::enable_if_t<std::is_convertible_v<U*, T*>, int> dummy = 0) {} |
653 | |
654 | void operator()(const T* ptr) { js_delete(const_cast<T*>(ptr)); } |
655 | }; |
656 | |
657 | struct FreePolicy { |
658 | void operator()(const void* ptr) { js_free(const_cast<void*>(ptr)); } |
659 | }; |
660 | |
661 | using UniqueChars = mozilla::UniquePtr<char[], JS::FreePolicy>; |
662 | using UniqueTwoByteChars = mozilla::UniquePtr<char16_t[], JS::FreePolicy>; |
663 | using UniqueLatin1Chars = mozilla::UniquePtr<JS::Latin1Char[], JS::FreePolicy>; |
664 | using UniqueWideChars = mozilla::UniquePtr<wchar_t[], JS::FreePolicy>; |
665 | |
666 | } // namespace JS |
667 | |
668 | /* sixgill annotation defines */ |
669 | #ifndef HAVE_STATIC_ANNOTATIONS |
670 | # define HAVE_STATIC_ANNOTATIONS |
671 | # ifdef XGILL_PLUGIN |
672 | # define STATIC_PRECONDITION(COND) __attribute__((precondition(#COND))) |
673 | # define STATIC_PRECONDITION_ASSUME(COND) \ |
674 | __attribute__((precondition_assume(#COND))) |
675 | # define STATIC_POSTCONDITION(COND) __attribute__((postcondition(#COND))) |
676 | # define STATIC_POSTCONDITION_ASSUME(COND) \ |
677 | __attribute__((postcondition_assume(#COND))) |
678 | # define STATIC_INVARIANT(COND) __attribute__((invariant(#COND))) |
679 | # define STATIC_INVARIANT_ASSUME(COND) \ |
680 | __attribute__((invariant_assume(#COND))) |
681 | # define STATIC_ASSUME(COND)do { } while (0) \ |
682 | JS_BEGIN_MACROdo { \ |
683 | __attribute__((assume_static(#COND), unused)) int STATIC_PASTE1( \ |
684 | assume_static_, __COUNTER__9); \ |
685 | JS_END_MACRO} while (0) |
686 | # else /* XGILL_PLUGIN */ |
687 | # define STATIC_PRECONDITION(COND) /* nothing */ |
688 | # define STATIC_PRECONDITION_ASSUME(COND) /* nothing */ |
689 | # define STATIC_POSTCONDITION(COND) /* nothing */ |
690 | # define STATIC_POSTCONDITION_ASSUME(COND) /* nothing */ |
691 | # define STATIC_INVARIANT(COND) /* nothing */ |
692 | # define STATIC_INVARIANT_ASSUME(COND) /* nothing */ |
693 | # define STATIC_ASSUME(COND)do { } while (0) \ |
694 | JS_BEGIN_MACROdo { /* nothing */ \ |
695 | JS_END_MACRO} while (0) |
696 | # endif /* XGILL_PLUGIN */ |
697 | # define STATIC_SKIP_INFERENCE STATIC_INVARIANT(skip_inference()) |
698 | #endif /* HAVE_STATIC_ANNOTATIONS */ |
699 | |
700 | #endif /* js_Utility_h */ |
1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | |
7 | /* Internal storage class used e.g. by Maybe and Result. This file doesn't |
8 | * contain any public declarations. */ |
9 | |
10 | #ifndef mfbt_MaybeStorageBase_h |
11 | #define mfbt_MaybeStorageBase_h |
12 | |
13 | #include <type_traits> |
14 | #include <utility> |
15 | |
16 | namespace mozilla::detail { |
17 | |
18 | template <typename T> |
19 | constexpr bool IsTriviallyDestructibleAndCopyable = |
20 | std::is_trivially_destructible_v<T> && |
21 | (std::is_trivially_copy_constructible_v<T> || |
22 | !std::is_copy_constructible_v<T>); |
23 | |
24 | template <typename T, bool TriviallyDestructibleAndCopyable = |
25 | IsTriviallyDestructibleAndCopyable<T>> |
26 | struct MaybeStorageBase; |
27 | |
28 | template <typename T> |
29 | struct MaybeStorageBase<T, false> { |
30 | protected: |
31 | using NonConstT = std::remove_const_t<T>; |
32 | |
33 | union Union { |
34 | Union() {} |
35 | explicit Union(const T& aVal) : val{aVal} {} |
36 | template <typename U, |
37 | typename = std::enable_if_t<std::is_move_constructible_v<U>>> |
38 | explicit Union(U&& aVal) : val{std::forward<U>(aVal)} {} |
39 | template <typename... Args> |
40 | explicit Union(std::in_place_t, Args&&... aArgs) |
41 | : val{std::forward<Args>(aArgs)...} {} |
42 | |
43 | ~Union() {} |
44 | |
45 | NonConstT val; |
46 | } mStorage; |
47 | |
48 | public: |
49 | constexpr MaybeStorageBase() = default; |
50 | explicit MaybeStorageBase(const T& aVal) : mStorage{aVal} {} |
51 | explicit MaybeStorageBase(T&& aVal) : mStorage{std::move(aVal)} {} |
52 | template <typename... Args> |
53 | explicit MaybeStorageBase(std::in_place_t, Args&&... aArgs) |
54 | : mStorage{std::in_place, std::forward<Args>(aArgs)...} {} |
55 | |
56 | const T* addr() const { return &mStorage.val; } |
57 | T* addr() { return &mStorage.val; } |
58 | }; |
59 | |
60 | template <typename T> |
61 | struct MaybeStorageBase<T, true> { |
62 | protected: |
63 | using NonConstT = std::remove_const_t<T>; |
64 | |
65 | union Union { |
66 | constexpr Union() : empty() {} |
67 | constexpr explicit Union(const T& aVal) : val{aVal} {} |
68 | constexpr explicit Union(T&& aVal) : val{std::move(aVal)} {} |
69 | template <typename... Args> |
70 | constexpr explicit Union(std::in_place_t, Args&&... aArgs) |
71 | : val{std::forward<Args>(aArgs)...} {} |
72 | |
73 | NonConstT val; |
74 | char empty; |
75 | } mStorage; |
76 | |
77 | public: |
78 | constexpr MaybeStorageBase() = default; |
79 | constexpr explicit MaybeStorageBase(const T& aVal) : mStorage{aVal} {} |
80 | constexpr explicit MaybeStorageBase(T&& aVal) : mStorage{std::move(aVal)} {} |
81 | |
82 | template <typename... Args> |
83 | constexpr explicit MaybeStorageBase(std::in_place_t, Args&&... aArgs) |
84 | : mStorage{std::in_place, std::forward<Args>(aArgs)...} {} |
85 | |
86 | constexpr const T* addr() const { return &mStorage.val; } |
87 | constexpr T* addr() { return &mStorage.val; } |
88 | }; |
89 | |
90 | } // namespace mozilla::detail |
91 | |
92 | #endif |