HashTable.h 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * vim: set ts=8 sts=4 et sw=4 tw=99:
  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. #ifndef js_HashTable_h
  7. #define js_HashTable_h
  8. #include "mozilla/Alignment.h"
  9. #include "mozilla/Assertions.h"
  10. #include "mozilla/Attributes.h"
  11. #include "mozilla/Casting.h"
  12. #include "mozilla/HashFunctions.h"
  13. #include "mozilla/MemoryReporting.h"
  14. #include "mozilla/Move.h"
  15. #include "mozilla/Opaque.h"
  16. #include "mozilla/PodOperations.h"
  17. #include "mozilla/ReentrancyGuard.h"
  18. #include "mozilla/TemplateLib.h"
  19. #include "mozilla/TypeTraits.h"
  20. #include "mozilla/UniquePtr.h"
  21. #include "js/Utility.h"
  22. namespace js {
  23. class TempAllocPolicy;
  24. template <class> struct DefaultHasher;
  25. template <class, class> class HashMapEntry;
  26. namespace detail {
  27. template <class T> class HashTableEntry;
  28. template <class T, class HashPolicy, class AllocPolicy> class HashTable;
  29. } // namespace detail
  30. /*****************************************************************************/
  31. // The "generation" of a hash table is an opaque value indicating the state of
  32. // modification of the hash table through its lifetime. If the generation of
  33. // a hash table compares equal at times T1 and T2, then lookups in the hash
  34. // table, pointers to (or into) hash table entries, etc. at time T1 are valid
  35. // at time T2. If the generation compares unequal, these computations are all
  36. // invalid and must be performed again to be used.
  37. //
  38. // Generations are meaningfully comparable only with respect to a single hash
  39. // table. It's always nonsensical to compare the generation of distinct hash
  40. // tables H1 and H2.
  41. using Generation = mozilla::Opaque<uint64_t>;
  42. // A JS-friendly, STL-like container providing a hash-based map from keys to
  43. // values. In particular, HashMap calls constructors and destructors of all
  44. // objects added so non-PODs may be used safely.
  45. //
  46. // Key/Value requirements:
  47. // - movable, destructible, assignable
  48. // HashPolicy requirements:
  49. // - see Hash Policy section below
  50. // AllocPolicy:
  51. // - see jsalloc.h
  52. //
  53. // Note:
  54. // - HashMap is not reentrant: Key/Value/HashPolicy/AllocPolicy members
  55. // called by HashMap must not call back into the same HashMap object.
  56. // - Due to the lack of exception handling, the user must call |init()|.
  57. template <class Key,
  58. class Value,
  59. class HashPolicy = DefaultHasher<Key>,
  60. class AllocPolicy = TempAllocPolicy>
  61. class HashMap
  62. {
  63. typedef HashMapEntry<Key, Value> TableEntry;
  64. struct MapHashPolicy : HashPolicy
  65. {
  66. using Base = HashPolicy;
  67. typedef Key KeyType;
  68. static const Key& getKey(TableEntry& e) { return e.key(); }
  69. static void setKey(TableEntry& e, Key& k) { HashPolicy::rekey(e.mutableKey(), k); }
  70. };
  71. typedef detail::HashTable<TableEntry, MapHashPolicy, AllocPolicy> Impl;
  72. Impl impl;
  73. public:
  74. typedef typename HashPolicy::Lookup Lookup;
  75. typedef TableEntry Entry;
  76. // HashMap construction is fallible (due to OOM); thus the user must call
  77. // init after constructing a HashMap and check the return value.
  78. explicit HashMap(AllocPolicy a = AllocPolicy()) : impl(a) {}
  79. MOZ_MUST_USE bool init(uint32_t len = 16) { return impl.init(len); }
  80. bool initialized() const { return impl.initialized(); }
  81. // Return whether the given lookup value is present in the map. E.g.:
  82. //
  83. // typedef HashMap<int,char> HM;
  84. // HM h;
  85. // if (HM::Ptr p = h.lookup(3)) {
  86. // const HM::Entry& e = *p; // p acts like a pointer to Entry
  87. // assert(p->key == 3); // Entry contains the key
  88. // char val = p->value; // and value
  89. // }
  90. //
  91. // Also see the definition of Ptr in HashTable above (with T = Entry).
  92. typedef typename Impl::Ptr Ptr;
  93. Ptr lookup(const Lookup& l) const { return impl.lookup(l); }
  94. // Like lookup, but does not assert if two threads call lookup at the same
  95. // time. Only use this method when none of the threads will modify the map.
  96. Ptr readonlyThreadsafeLookup(const Lookup& l) const { return impl.readonlyThreadsafeLookup(l); }
  97. // Assuming |p.found()|, remove |*p|.
  98. void remove(Ptr p) { impl.remove(p); }
  99. // Like |lookup(l)|, but on miss, |p = lookupForAdd(l)| allows efficient
  100. // insertion of Key |k| (where |HashPolicy::match(k,l) == true|) using
  101. // |add(p,k,v)|. After |add(p,k,v)|, |p| points to the new Entry. E.g.:
  102. //
  103. // typedef HashMap<int,char> HM;
  104. // HM h;
  105. // HM::AddPtr p = h.lookupForAdd(3);
  106. // if (!p) {
  107. // if (!h.add(p, 3, 'a'))
  108. // return false;
  109. // }
  110. // const HM::Entry& e = *p; // p acts like a pointer to Entry
  111. // assert(p->key == 3); // Entry contains the key
  112. // char val = p->value; // and value
  113. //
  114. // Also see the definition of AddPtr in HashTable above (with T = Entry).
  115. //
  116. // N.B. The caller must ensure that no mutating hash table operations
  117. // occur between a pair of |lookupForAdd| and |add| calls. To avoid
  118. // looking up the key a second time, the caller may use the more efficient
  119. // relookupOrAdd method. This method reuses part of the hashing computation
  120. // to more efficiently insert the key if it has not been added. For
  121. // example, a mutation-handling version of the previous example:
  122. //
  123. // HM::AddPtr p = h.lookupForAdd(3);
  124. // if (!p) {
  125. // call_that_may_mutate_h();
  126. // if (!h.relookupOrAdd(p, 3, 'a'))
  127. // return false;
  128. // }
  129. // const HM::Entry& e = *p;
  130. // assert(p->key == 3);
  131. // char val = p->value;
  132. typedef typename Impl::AddPtr AddPtr;
  133. AddPtr lookupForAdd(const Lookup& l) const {
  134. return impl.lookupForAdd(l);
  135. }
  136. template<typename KeyInput, typename ValueInput>
  137. MOZ_MUST_USE bool add(AddPtr& p, KeyInput&& k, ValueInput&& v) {
  138. return impl.add(p,
  139. mozilla::Forward<KeyInput>(k),
  140. mozilla::Forward<ValueInput>(v));
  141. }
  142. template<typename KeyInput>
  143. MOZ_MUST_USE bool add(AddPtr& p, KeyInput&& k) {
  144. return impl.add(p, mozilla::Forward<KeyInput>(k), Value());
  145. }
  146. template<typename KeyInput, typename ValueInput>
  147. MOZ_MUST_USE bool relookupOrAdd(AddPtr& p, KeyInput&& k, ValueInput&& v) {
  148. return impl.relookupOrAdd(p, k,
  149. mozilla::Forward<KeyInput>(k),
  150. mozilla::Forward<ValueInput>(v));
  151. }
  152. // |all()| returns a Range containing |count()| elements. E.g.:
  153. //
  154. // typedef HashMap<int,char> HM;
  155. // HM h;
  156. // for (HM::Range r = h.all(); !r.empty(); r.popFront())
  157. // char c = r.front().value();
  158. //
  159. // Also see the definition of Range in HashTable above (with T = Entry).
  160. typedef typename Impl::Range Range;
  161. Range all() const { return impl.all(); }
  162. // Typedef for the enumeration class. An Enum may be used to examine and
  163. // remove table entries:
  164. //
  165. // typedef HashMap<int,char> HM;
  166. // HM s;
  167. // for (HM::Enum e(s); !e.empty(); e.popFront())
  168. // if (e.front().value() == 'l')
  169. // e.removeFront();
  170. //
  171. // Table resize may occur in Enum's destructor. Also see the definition of
  172. // Enum in HashTable above (with T = Entry).
  173. typedef typename Impl::Enum Enum;
  174. // Remove all entries. This does not shrink the table. For that consider
  175. // using the finish() method.
  176. void clear() { impl.clear(); }
  177. // Remove all the entries and release all internal buffers. The map must
  178. // be initialized again before any use.
  179. void finish() { impl.finish(); }
  180. // Does the table contain any entries?
  181. bool empty() const { return impl.empty(); }
  182. // Number of live elements in the map.
  183. uint32_t count() const { return impl.count(); }
  184. // Total number of allocation in the dynamic table. Note: resize will
  185. // happen well before count() == capacity().
  186. size_t capacity() const { return impl.capacity(); }
  187. // Don't just call |impl.sizeOfExcludingThis()| because there's no
  188. // guarantee that |impl| is the first field in HashMap.
  189. size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
  190. return impl.sizeOfExcludingThis(mallocSizeOf);
  191. }
  192. size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
  193. return mallocSizeOf(this) + impl.sizeOfExcludingThis(mallocSizeOf);
  194. }
  195. Generation generation() const {
  196. return impl.generation();
  197. }
  198. /************************************************** Shorthand operations */
  199. bool has(const Lookup& l) const {
  200. return impl.lookup(l).found();
  201. }
  202. // Overwrite existing value with v. Return false on oom.
  203. template<typename KeyInput, typename ValueInput>
  204. MOZ_MUST_USE bool put(KeyInput&& k, ValueInput&& v) {
  205. AddPtr p = lookupForAdd(k);
  206. if (p) {
  207. p->value() = mozilla::Forward<ValueInput>(v);
  208. return true;
  209. }
  210. return add(p, mozilla::Forward<KeyInput>(k), mozilla::Forward<ValueInput>(v));
  211. }
  212. // Like put, but assert that the given key is not already present.
  213. template<typename KeyInput, typename ValueInput>
  214. MOZ_MUST_USE bool putNew(KeyInput&& k, ValueInput&& v) {
  215. return impl.putNew(k, mozilla::Forward<KeyInput>(k), mozilla::Forward<ValueInput>(v));
  216. }
  217. // Only call this to populate an empty map after reserving space with init().
  218. template<typename KeyInput, typename ValueInput>
  219. void putNewInfallible(KeyInput&& k, ValueInput&& v) {
  220. impl.putNewInfallible(k, mozilla::Forward<KeyInput>(k), mozilla::Forward<ValueInput>(v));
  221. }
  222. // Add (k,defaultValue) if |k| is not found. Return a false-y Ptr on oom.
  223. Ptr lookupWithDefault(const Key& k, const Value& defaultValue) {
  224. AddPtr p = lookupForAdd(k);
  225. if (p)
  226. return p;
  227. bool ok = add(p, k, defaultValue);
  228. MOZ_ASSERT_IF(!ok, !p); // p is left false-y on oom.
  229. (void)ok;
  230. return p;
  231. }
  232. // Remove if present.
  233. void remove(const Lookup& l) {
  234. if (Ptr p = lookup(l))
  235. remove(p);
  236. }
  237. // Infallibly rekey one entry, if necessary.
  238. // Requires template parameters Key and HashPolicy::Lookup to be the same type.
  239. void rekeyIfMoved(const Key& old_key, const Key& new_key) {
  240. if (old_key != new_key)
  241. rekeyAs(old_key, new_key, new_key);
  242. }
  243. // Infallibly rekey one entry if present, and return whether that happened.
  244. bool rekeyAs(const Lookup& old_lookup, const Lookup& new_lookup, const Key& new_key) {
  245. if (Ptr p = lookup(old_lookup)) {
  246. impl.rekeyAndMaybeRehash(p, new_lookup, new_key);
  247. return true;
  248. }
  249. return false;
  250. }
  251. // HashMap is movable
  252. HashMap(HashMap&& rhs) : impl(mozilla::Move(rhs.impl)) {}
  253. void operator=(HashMap&& rhs) {
  254. MOZ_ASSERT(this != &rhs, "self-move assignment is prohibited");
  255. impl = mozilla::Move(rhs.impl);
  256. }
  257. private:
  258. // HashMap is not copyable or assignable
  259. HashMap(const HashMap& hm) = delete;
  260. HashMap& operator=(const HashMap& hm) = delete;
  261. friend class Impl::Enum;
  262. };
  263. /*****************************************************************************/
  264. // A JS-friendly, STL-like container providing a hash-based set of values. In
  265. // particular, HashSet calls constructors and destructors of all objects added
  266. // so non-PODs may be used safely.
  267. //
  268. // T requirements:
  269. // - movable, destructible, assignable
  270. // HashPolicy requirements:
  271. // - see Hash Policy section below
  272. // AllocPolicy:
  273. // - see jsalloc.h
  274. //
  275. // Note:
  276. // - HashSet is not reentrant: T/HashPolicy/AllocPolicy members called by
  277. // HashSet must not call back into the same HashSet object.
  278. // - Due to the lack of exception handling, the user must call |init()|.
  279. template <class T,
  280. class HashPolicy = DefaultHasher<T>,
  281. class AllocPolicy = TempAllocPolicy>
  282. class HashSet
  283. {
  284. struct SetOps : HashPolicy
  285. {
  286. using Base = HashPolicy;
  287. typedef T KeyType;
  288. static const KeyType& getKey(const T& t) { return t; }
  289. static void setKey(T& t, KeyType& k) { HashPolicy::rekey(t, k); }
  290. };
  291. typedef detail::HashTable<const T, SetOps, AllocPolicy> Impl;
  292. Impl impl;
  293. public:
  294. typedef typename HashPolicy::Lookup Lookup;
  295. typedef T Entry;
  296. // HashSet construction is fallible (due to OOM); thus the user must call
  297. // init after constructing a HashSet and check the return value.
  298. explicit HashSet(AllocPolicy a = AllocPolicy()) : impl(a) {}
  299. MOZ_MUST_USE bool init(uint32_t len = 16) { return impl.init(len); }
  300. bool initialized() const { return impl.initialized(); }
  301. // Return whether the given lookup value is present in the map. E.g.:
  302. //
  303. // typedef HashSet<int> HS;
  304. // HS h;
  305. // if (HS::Ptr p = h.lookup(3)) {
  306. // assert(*p == 3); // p acts like a pointer to int
  307. // }
  308. //
  309. // Also see the definition of Ptr in HashTable above.
  310. typedef typename Impl::Ptr Ptr;
  311. Ptr lookup(const Lookup& l) const { return impl.lookup(l); }
  312. // Like lookup, but does not assert if two threads call lookup at the same
  313. // time. Only use this method when none of the threads will modify the map.
  314. Ptr readonlyThreadsafeLookup(const Lookup& l) const { return impl.readonlyThreadsafeLookup(l); }
  315. // Assuming |p.found()|, remove |*p|.
  316. void remove(Ptr p) { impl.remove(p); }
  317. // Like |lookup(l)|, but on miss, |p = lookupForAdd(l)| allows efficient
  318. // insertion of T value |t| (where |HashPolicy::match(t,l) == true|) using
  319. // |add(p,t)|. After |add(p,t)|, |p| points to the new element. E.g.:
  320. //
  321. // typedef HashSet<int> HS;
  322. // HS h;
  323. // HS::AddPtr p = h.lookupForAdd(3);
  324. // if (!p) {
  325. // if (!h.add(p, 3))
  326. // return false;
  327. // }
  328. // assert(*p == 3); // p acts like a pointer to int
  329. //
  330. // Also see the definition of AddPtr in HashTable above.
  331. //
  332. // N.B. The caller must ensure that no mutating hash table operations
  333. // occur between a pair of |lookupForAdd| and |add| calls. To avoid
  334. // looking up the key a second time, the caller may use the more efficient
  335. // relookupOrAdd method. This method reuses part of the hashing computation
  336. // to more efficiently insert the key if it has not been added. For
  337. // example, a mutation-handling version of the previous example:
  338. //
  339. // HS::AddPtr p = h.lookupForAdd(3);
  340. // if (!p) {
  341. // call_that_may_mutate_h();
  342. // if (!h.relookupOrAdd(p, 3, 3))
  343. // return false;
  344. // }
  345. // assert(*p == 3);
  346. //
  347. // Note that relookupOrAdd(p,l,t) performs Lookup using |l| and adds the
  348. // entry |t|, where the caller ensures match(l,t).
  349. typedef typename Impl::AddPtr AddPtr;
  350. AddPtr lookupForAdd(const Lookup& l) const { return impl.lookupForAdd(l); }
  351. template <typename U>
  352. MOZ_MUST_USE bool add(AddPtr& p, U&& u) {
  353. return impl.add(p, mozilla::Forward<U>(u));
  354. }
  355. template <typename U>
  356. MOZ_MUST_USE bool relookupOrAdd(AddPtr& p, const Lookup& l, U&& u) {
  357. return impl.relookupOrAdd(p, l, mozilla::Forward<U>(u));
  358. }
  359. // |all()| returns a Range containing |count()| elements:
  360. //
  361. // typedef HashSet<int> HS;
  362. // HS h;
  363. // for (HS::Range r = h.all(); !r.empty(); r.popFront())
  364. // int i = r.front();
  365. //
  366. // Also see the definition of Range in HashTable above.
  367. typedef typename Impl::Range Range;
  368. Range all() const { return impl.all(); }
  369. // Typedef for the enumeration class. An Enum may be used to examine and
  370. // remove table entries:
  371. //
  372. // typedef HashSet<int> HS;
  373. // HS s;
  374. // for (HS::Enum e(s); !e.empty(); e.popFront())
  375. // if (e.front() == 42)
  376. // e.removeFront();
  377. //
  378. // Table resize may occur in Enum's destructor. Also see the definition of
  379. // Enum in HashTable above.
  380. typedef typename Impl::Enum Enum;
  381. // Remove all entries. This does not shrink the table. For that consider
  382. // using the finish() method.
  383. void clear() { impl.clear(); }
  384. // Remove all the entries and release all internal buffers. The set must
  385. // be initialized again before any use.
  386. void finish() { impl.finish(); }
  387. // Does the table contain any entries?
  388. bool empty() const { return impl.empty(); }
  389. // Number of live elements in the map.
  390. uint32_t count() const { return impl.count(); }
  391. // Total number of allocation in the dynamic table. Note: resize will
  392. // happen well before count() == capacity().
  393. size_t capacity() const { return impl.capacity(); }
  394. // Don't just call |impl.sizeOfExcludingThis()| because there's no
  395. // guarantee that |impl| is the first field in HashSet.
  396. size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
  397. return impl.sizeOfExcludingThis(mallocSizeOf);
  398. }
  399. size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
  400. return mallocSizeOf(this) + impl.sizeOfExcludingThis(mallocSizeOf);
  401. }
  402. Generation generation() const {
  403. return impl.generation();
  404. }
  405. /************************************************** Shorthand operations */
  406. bool has(const Lookup& l) const {
  407. return impl.lookup(l).found();
  408. }
  409. // Add |u| if it is not present already. Return false on oom.
  410. template <typename U>
  411. MOZ_MUST_USE bool put(U&& u) {
  412. AddPtr p = lookupForAdd(u);
  413. return p ? true : add(p, mozilla::Forward<U>(u));
  414. }
  415. // Like put, but assert that the given key is not already present.
  416. template <typename U>
  417. MOZ_MUST_USE bool putNew(U&& u) {
  418. return impl.putNew(u, mozilla::Forward<U>(u));
  419. }
  420. template <typename U>
  421. MOZ_MUST_USE bool putNew(const Lookup& l, U&& u) {
  422. return impl.putNew(l, mozilla::Forward<U>(u));
  423. }
  424. // Only call this to populate an empty set after reserving space with init().
  425. template <typename U>
  426. void putNewInfallible(const Lookup& l, U&& u) {
  427. impl.putNewInfallible(l, mozilla::Forward<U>(u));
  428. }
  429. void remove(const Lookup& l) {
  430. if (Ptr p = lookup(l))
  431. remove(p);
  432. }
  433. // Infallibly rekey one entry, if present.
  434. // Requires template parameters T and HashPolicy::Lookup to be the same type.
  435. void rekeyIfMoved(const Lookup& old_value, const T& new_value) {
  436. if (old_value != new_value)
  437. rekeyAs(old_value, new_value, new_value);
  438. }
  439. // Infallibly rekey one entry if present, and return whether that happened.
  440. bool rekeyAs(const Lookup& old_lookup, const Lookup& new_lookup, const T& new_value) {
  441. if (Ptr p = lookup(old_lookup)) {
  442. impl.rekeyAndMaybeRehash(p, new_lookup, new_value);
  443. return true;
  444. }
  445. return false;
  446. }
  447. // Infallibly replace the current key at |p| with an equivalent key.
  448. // Specifically, both HashPolicy::hash and HashPolicy::match must return
  449. // identical results for the new and old key when applied against all
  450. // possible matching values.
  451. void replaceKey(Ptr p, const T& new_value) {
  452. MOZ_ASSERT(p.found());
  453. MOZ_ASSERT(*p != new_value);
  454. MOZ_ASSERT(HashPolicy::hash(*p) == HashPolicy::hash(new_value));
  455. MOZ_ASSERT(HashPolicy::match(*p, new_value));
  456. const_cast<T&>(*p) = new_value;
  457. }
  458. // HashSet is movable
  459. HashSet(HashSet&& rhs) : impl(mozilla::Move(rhs.impl)) {}
  460. void operator=(HashSet&& rhs) {
  461. MOZ_ASSERT(this != &rhs, "self-move assignment is prohibited");
  462. impl = mozilla::Move(rhs.impl);
  463. }
  464. private:
  465. // HashSet is not copyable or assignable
  466. HashSet(const HashSet& hs) = delete;
  467. HashSet& operator=(const HashSet& hs) = delete;
  468. friend class Impl::Enum;
  469. };
  470. /*****************************************************************************/
  471. // Hash Policy
  472. //
  473. // A hash policy P for a hash table with key-type Key must provide:
  474. // - a type |P::Lookup| to use to lookup table entries;
  475. // - a static member function |P::hash| with signature
  476. //
  477. // static js::HashNumber hash(Lookup)
  478. //
  479. // to use to hash the lookup type; and
  480. // - a static member function |P::match| with signature
  481. //
  482. // static bool match(Key, Lookup)
  483. //
  484. // to use to test equality of key and lookup values.
  485. //
  486. // Normally, Lookup = Key. In general, though, different values and types of
  487. // values can be used to lookup and store. If a Lookup value |l| is != to the
  488. // added Key value |k|, the user must ensure that |P::match(k,l)|. E.g.:
  489. //
  490. // js::HashSet<Key, P>::AddPtr p = h.lookup(l);
  491. // if (!p) {
  492. // assert(P::match(k, l)); // must hold
  493. // h.add(p, k);
  494. // }
  495. // Pointer hashing policy that strips the lowest zeroBits when calculating the
  496. // hash to improve key distribution.
  497. template <typename Key, size_t zeroBits>
  498. struct PointerHasher
  499. {
  500. typedef Key Lookup;
  501. static HashNumber hash(const Lookup& l) {
  502. size_t word = reinterpret_cast<size_t>(l) >> zeroBits;
  503. static_assert(sizeof(HashNumber) == 4,
  504. "subsequent code assumes a four-byte hash");
  505. #if JS_BITS_PER_WORD == 32
  506. return HashNumber(word);
  507. #else
  508. static_assert(sizeof(word) == 8,
  509. "unexpected word size, new hashing strategy required to "
  510. "properly incorporate all bits");
  511. return HashNumber((word >> 32) ^ word);
  512. #endif
  513. }
  514. static bool match(const Key& k, const Lookup& l) {
  515. return k == l;
  516. }
  517. static void rekey(Key& k, const Key& newKey) {
  518. k = newKey;
  519. }
  520. };
  521. // Default hash policy: just use the 'lookup' value. This of course only
  522. // works if the lookup value is integral. HashTable applies ScrambleHashCode to
  523. // the result of the 'hash' which means that it is 'ok' if the lookup value is
  524. // not well distributed over the HashNumber domain.
  525. template <class Key>
  526. struct DefaultHasher
  527. {
  528. typedef Key Lookup;
  529. static HashNumber hash(const Lookup& l) {
  530. // Hash if can implicitly cast to hash number type.
  531. return l;
  532. }
  533. static bool match(const Key& k, const Lookup& l) {
  534. // Use builtin or overloaded operator==.
  535. return k == l;
  536. }
  537. static void rekey(Key& k, const Key& newKey) {
  538. k = newKey;
  539. }
  540. };
  541. // Specialize hashing policy for pointer types. It assumes that the type is
  542. // at least word-aligned. For types with smaller size use PointerHasher.
  543. template <class T>
  544. struct DefaultHasher<T*> : PointerHasher<T*, mozilla::tl::FloorLog2<sizeof(void*)>::value>
  545. {};
  546. // Specialize hashing policy for mozilla::UniquePtr to proxy the UniquePtr's
  547. // raw pointer to PointerHasher.
  548. template <class T, class D>
  549. struct DefaultHasher<mozilla::UniquePtr<T, D>>
  550. {
  551. using Lookup = mozilla::UniquePtr<T, D>;
  552. using PtrHasher = PointerHasher<T*, mozilla::tl::FloorLog2<sizeof(void*)>::value>;
  553. static HashNumber hash(const Lookup& l) {
  554. return PtrHasher::hash(l.get());
  555. }
  556. static bool match(const mozilla::UniquePtr<T, D>& k, const Lookup& l) {
  557. return PtrHasher::match(k.get(), l.get());
  558. }
  559. static void rekey(mozilla::UniquePtr<T, D>& k, mozilla::UniquePtr<T, D>&& newKey) {
  560. k = mozilla::Move(newKey);
  561. }
  562. };
  563. // For doubles, we can xor the two uint32s.
  564. template <>
  565. struct DefaultHasher<double>
  566. {
  567. typedef double Lookup;
  568. static HashNumber hash(double d) {
  569. static_assert(sizeof(HashNumber) == 4,
  570. "subsequent code assumes a four-byte hash");
  571. uint64_t u = mozilla::BitwiseCast<uint64_t>(d);
  572. return HashNumber(u ^ (u >> 32));
  573. }
  574. static bool match(double lhs, double rhs) {
  575. return mozilla::BitwiseCast<uint64_t>(lhs) == mozilla::BitwiseCast<uint64_t>(rhs);
  576. }
  577. };
  578. template <>
  579. struct DefaultHasher<float>
  580. {
  581. typedef float Lookup;
  582. static HashNumber hash(float f) {
  583. static_assert(sizeof(HashNumber) == 4,
  584. "subsequent code assumes a four-byte hash");
  585. return HashNumber(mozilla::BitwiseCast<uint32_t>(f));
  586. }
  587. static bool match(float lhs, float rhs) {
  588. return mozilla::BitwiseCast<uint32_t>(lhs) == mozilla::BitwiseCast<uint32_t>(rhs);
  589. }
  590. };
  591. // A hash policy that compares C strings.
  592. struct CStringHasher
  593. {
  594. typedef const char* Lookup;
  595. static js::HashNumber hash(Lookup l) {
  596. return mozilla::HashString(l);
  597. }
  598. static bool match(const char* key, Lookup lookup) {
  599. return strcmp(key, lookup) == 0;
  600. }
  601. };
  602. // Fallible hashing interface.
  603. //
  604. // Most of the time generating a hash code is infallible so this class provides
  605. // default methods that always succeed. Specialize this class for your own hash
  606. // policy to provide fallible hashing.
  607. //
  608. // This is used by MovableCellHasher to handle the fact that generating a unique
  609. // ID for cell pointer may fail due to OOM.
  610. template <typename HashPolicy>
  611. struct FallibleHashMethods
  612. {
  613. // Return true if a hashcode is already available for its argument. Once
  614. // this returns true for a specific argument it must continue to do so.
  615. template <typename Lookup> static bool hasHash(Lookup&& l) { return true; }
  616. // Fallible method to ensure a hashcode exists for its argument and create
  617. // one if not. Returns false on error, e.g. out of memory.
  618. template <typename Lookup> static bool ensureHash(Lookup&& l) { return true; }
  619. };
  620. template <typename HashPolicy, typename Lookup>
  621. static bool
  622. HasHash(Lookup&& l) {
  623. return FallibleHashMethods<typename HashPolicy::Base>::hasHash(mozilla::Forward<Lookup>(l));
  624. }
  625. template <typename HashPolicy, typename Lookup>
  626. static bool
  627. EnsureHash(Lookup&& l) {
  628. return FallibleHashMethods<typename HashPolicy::Base>::ensureHash(mozilla::Forward<Lookup>(l));
  629. }
  630. /*****************************************************************************/
  631. // Both HashMap and HashSet are implemented by a single HashTable that is even
  632. // more heavily parameterized than the other two. This leaves HashTable gnarly
  633. // and extremely coupled to HashMap and HashSet; thus code should not use
  634. // HashTable directly.
  635. template <class Key, class Value>
  636. class HashMapEntry
  637. {
  638. Key key_;
  639. Value value_;
  640. template <class, class, class> friend class detail::HashTable;
  641. template <class> friend class detail::HashTableEntry;
  642. template <class, class, class, class> friend class HashMap;
  643. public:
  644. template<typename KeyInput, typename ValueInput>
  645. HashMapEntry(KeyInput&& k, ValueInput&& v)
  646. : key_(mozilla::Forward<KeyInput>(k)),
  647. value_(mozilla::Forward<ValueInput>(v))
  648. {}
  649. HashMapEntry(HashMapEntry&& rhs)
  650. : key_(mozilla::Move(rhs.key_)),
  651. value_(mozilla::Move(rhs.value_))
  652. {}
  653. void operator=(HashMapEntry&& rhs) {
  654. key_ = mozilla::Move(rhs.key_);
  655. value_ = mozilla::Move(rhs.value_);
  656. }
  657. typedef Key KeyType;
  658. typedef Value ValueType;
  659. const Key& key() const { return key_; }
  660. Key& mutableKey() { return key_; }
  661. const Value& value() const { return value_; }
  662. Value& value() { return value_; }
  663. private:
  664. HashMapEntry(const HashMapEntry&) = delete;
  665. void operator=(const HashMapEntry&) = delete;
  666. };
  667. } // namespace js
  668. namespace mozilla {
  669. template <typename T>
  670. struct IsPod<js::detail::HashTableEntry<T> > : IsPod<T> {};
  671. template <typename K, typename V>
  672. struct IsPod<js::HashMapEntry<K, V> >
  673. : IntegralConstant<bool, IsPod<K>::value && IsPod<V>::value>
  674. {};
  675. } // namespace mozilla
  676. namespace js {
  677. namespace detail {
  678. template <class T, class HashPolicy, class AllocPolicy>
  679. class HashTable;
  680. template <class T>
  681. class HashTableEntry
  682. {
  683. template <class, class, class> friend class HashTable;
  684. typedef typename mozilla::RemoveConst<T>::Type NonConstT;
  685. HashNumber keyHash;
  686. mozilla::AlignedStorage2<NonConstT> mem;
  687. static const HashNumber sFreeKey = 0;
  688. static const HashNumber sRemovedKey = 1;
  689. static const HashNumber sCollisionBit = 1;
  690. static bool isLiveHash(HashNumber hash)
  691. {
  692. return hash > sRemovedKey;
  693. }
  694. HashTableEntry(const HashTableEntry&) = delete;
  695. void operator=(const HashTableEntry&) = delete;
  696. ~HashTableEntry() = delete;
  697. public:
  698. // NB: HashTableEntry is treated as a POD: no constructor or destructor calls.
  699. void destroyIfLive() {
  700. if (isLive())
  701. mem.addr()->~T();
  702. }
  703. void destroy() {
  704. MOZ_ASSERT(isLive());
  705. mem.addr()->~T();
  706. }
  707. void swap(HashTableEntry* other) {
  708. if (this == other)
  709. return;
  710. MOZ_ASSERT(isLive());
  711. if (other->isLive()) {
  712. mozilla::Swap(*mem.addr(), *other->mem.addr());
  713. } else {
  714. *other->mem.addr() = mozilla::Move(*mem.addr());
  715. destroy();
  716. }
  717. mozilla::Swap(keyHash, other->keyHash);
  718. }
  719. T& get() { MOZ_ASSERT(isLive()); return *mem.addr(); }
  720. NonConstT& getMutable() { MOZ_ASSERT(isLive()); return *mem.addr(); }
  721. bool isFree() const { return keyHash == sFreeKey; }
  722. void clearLive() { MOZ_ASSERT(isLive()); keyHash = sFreeKey; mem.addr()->~T(); }
  723. void clear() { if (isLive()) mem.addr()->~T(); keyHash = sFreeKey; }
  724. bool isRemoved() const { return keyHash == sRemovedKey; }
  725. void removeLive() { MOZ_ASSERT(isLive()); keyHash = sRemovedKey; mem.addr()->~T(); }
  726. bool isLive() const { return isLiveHash(keyHash); }
  727. void setCollision() { MOZ_ASSERT(isLive()); keyHash |= sCollisionBit; }
  728. void unsetCollision() { keyHash &= ~sCollisionBit; }
  729. bool hasCollision() const { return keyHash & sCollisionBit; }
  730. bool matchHash(HashNumber hn) { return (keyHash & ~sCollisionBit) == hn; }
  731. HashNumber getKeyHash() const { return keyHash & ~sCollisionBit; }
  732. template <typename... Args>
  733. void setLive(HashNumber hn, Args&&... args)
  734. {
  735. MOZ_ASSERT(!isLive());
  736. keyHash = hn;
  737. new(mem.addr()) T(mozilla::Forward<Args>(args)...);
  738. MOZ_ASSERT(isLive());
  739. }
  740. };
  741. template <class T, class HashPolicy, class AllocPolicy>
  742. class HashTable : private AllocPolicy
  743. {
  744. friend class mozilla::ReentrancyGuard;
  745. typedef typename mozilla::RemoveConst<T>::Type NonConstT;
  746. typedef typename HashPolicy::KeyType Key;
  747. typedef typename HashPolicy::Lookup Lookup;
  748. public:
  749. typedef HashTableEntry<T> Entry;
  750. // A nullable pointer to a hash table element. A Ptr |p| can be tested
  751. // either explicitly |if (p.found()) p->...| or using boolean conversion
  752. // |if (p) p->...|. Ptr objects must not be used after any mutating hash
  753. // table operations unless |generation()| is tested.
  754. class Ptr
  755. {
  756. friend class HashTable;
  757. Entry* entry_;
  758. #ifdef JS_DEBUG
  759. const HashTable* table_;
  760. Generation generation;
  761. #endif
  762. protected:
  763. Ptr(Entry& entry, const HashTable& tableArg)
  764. : entry_(&entry)
  765. #ifdef JS_DEBUG
  766. , table_(&tableArg)
  767. , generation(tableArg.generation())
  768. #endif
  769. {}
  770. public:
  771. Ptr()
  772. : entry_(nullptr)
  773. #ifdef JS_DEBUG
  774. , table_(nullptr)
  775. , generation(0)
  776. #endif
  777. {}
  778. bool isValid() const {
  779. return !entry_;
  780. }
  781. bool found() const {
  782. if (isValid())
  783. return false;
  784. #ifdef JS_DEBUG
  785. MOZ_ASSERT(generation == table_->generation());
  786. #endif
  787. return entry_->isLive();
  788. }
  789. explicit operator bool() const {
  790. return found();
  791. }
  792. bool operator==(const Ptr& rhs) const {
  793. MOZ_ASSERT(found() && rhs.found());
  794. return entry_ == rhs.entry_;
  795. }
  796. bool operator!=(const Ptr& rhs) const {
  797. #ifdef JS_DEBUG
  798. MOZ_ASSERT(generation == table_->generation());
  799. #endif
  800. return !(*this == rhs);
  801. }
  802. T& operator*() const {
  803. #ifdef JS_DEBUG
  804. MOZ_ASSERT(found());
  805. MOZ_ASSERT(generation == table_->generation());
  806. #endif
  807. return entry_->get();
  808. }
  809. T* operator->() const {
  810. #ifdef JS_DEBUG
  811. MOZ_ASSERT(found());
  812. MOZ_ASSERT(generation == table_->generation());
  813. #endif
  814. return &entry_->get();
  815. }
  816. };
  817. // A Ptr that can be used to add a key after a failed lookup.
  818. class AddPtr : public Ptr
  819. {
  820. friend class HashTable;
  821. HashNumber keyHash;
  822. #ifdef JS_DEBUG
  823. uint64_t mutationCount;
  824. #endif
  825. AddPtr(Entry& entry, const HashTable& tableArg, HashNumber hn)
  826. : Ptr(entry, tableArg)
  827. , keyHash(hn)
  828. #ifdef JS_DEBUG
  829. , mutationCount(tableArg.mutationCount)
  830. #endif
  831. {}
  832. public:
  833. AddPtr() : keyHash(0) {}
  834. };
  835. // A collection of hash table entries. The collection is enumerated by
  836. // calling |front()| followed by |popFront()| as long as |!empty()|. As
  837. // with Ptr/AddPtr, Range objects must not be used after any mutating hash
  838. // table operation unless the |generation()| is tested.
  839. class Range
  840. {
  841. protected:
  842. friend class HashTable;
  843. Range(const HashTable& tableArg, Entry* c, Entry* e)
  844. : cur(c)
  845. , end(e)
  846. #ifdef JS_DEBUG
  847. , table_(&tableArg)
  848. , mutationCount(tableArg.mutationCount)
  849. , generation(tableArg.generation())
  850. , validEntry(true)
  851. #endif
  852. {
  853. while (cur < end && !cur->isLive())
  854. ++cur;
  855. }
  856. Entry* cur;
  857. Entry* end;
  858. #ifdef JS_DEBUG
  859. const HashTable* table_;
  860. uint64_t mutationCount;
  861. Generation generation;
  862. bool validEntry;
  863. #endif
  864. public:
  865. Range()
  866. : cur(nullptr)
  867. , end(nullptr)
  868. #ifdef JS_DEBUG
  869. , table_(nullptr)
  870. , mutationCount(0)
  871. , generation(0)
  872. , validEntry(false)
  873. #endif
  874. {}
  875. bool empty() const {
  876. #ifdef JS_DEBUG
  877. MOZ_ASSERT(generation == table_->generation());
  878. MOZ_ASSERT(mutationCount == table_->mutationCount);
  879. #endif
  880. return cur == end;
  881. }
  882. T& front() const {
  883. MOZ_ASSERT(!empty());
  884. #ifdef JS_DEBUG
  885. MOZ_ASSERT(validEntry);
  886. MOZ_ASSERT(generation == table_->generation());
  887. MOZ_ASSERT(mutationCount == table_->mutationCount);
  888. #endif
  889. return cur->get();
  890. }
  891. void popFront() {
  892. MOZ_ASSERT(!empty());
  893. #ifdef JS_DEBUG
  894. MOZ_ASSERT(generation == table_->generation());
  895. MOZ_ASSERT(mutationCount == table_->mutationCount);
  896. #endif
  897. while (++cur < end && !cur->isLive())
  898. continue;
  899. #ifdef JS_DEBUG
  900. validEntry = true;
  901. #endif
  902. }
  903. };
  904. // A Range whose lifetime delimits a mutating enumeration of a hash table.
  905. // Since rehashing when elements were removed during enumeration would be
  906. // bad, it is postponed until the Enum is destructed. Since the Enum's
  907. // destructor touches the hash table, the user must ensure that the hash
  908. // table is still alive when the destructor runs.
  909. class Enum : public Range
  910. {
  911. friend class HashTable;
  912. HashTable& table_;
  913. bool rekeyed;
  914. bool removed;
  915. /* Not copyable. */
  916. Enum(const Enum&) = delete;
  917. void operator=(const Enum&) = delete;
  918. public:
  919. template<class Map> explicit
  920. Enum(Map& map) : Range(map.all()), table_(map.impl), rekeyed(false), removed(false) {}
  921. // Removes the |front()| element from the table, leaving |front()|
  922. // invalid until the next call to |popFront()|. For example:
  923. //
  924. // HashSet<int> s;
  925. // for (HashSet<int>::Enum e(s); !e.empty(); e.popFront())
  926. // if (e.front() == 42)
  927. // e.removeFront();
  928. void removeFront() {
  929. table_.remove(*this->cur);
  930. removed = true;
  931. #ifdef JS_DEBUG
  932. this->validEntry = false;
  933. this->mutationCount = table_.mutationCount;
  934. #endif
  935. }
  936. NonConstT& mutableFront() {
  937. MOZ_ASSERT(!this->empty());
  938. #ifdef JS_DEBUG
  939. MOZ_ASSERT(this->validEntry);
  940. MOZ_ASSERT(this->generation == this->Range::table_->generation());
  941. MOZ_ASSERT(this->mutationCount == this->Range::table_->mutationCount);
  942. #endif
  943. return this->cur->getMutable();
  944. }
  945. // Removes the |front()| element and re-inserts it into the table with
  946. // a new key at the new Lookup position. |front()| is invalid after
  947. // this operation until the next call to |popFront()|.
  948. void rekeyFront(const Lookup& l, const Key& k) {
  949. MOZ_ASSERT(&k != &HashPolicy::getKey(this->cur->get()));
  950. Ptr p(*this->cur, table_);
  951. table_.rekeyWithoutRehash(p, l, k);
  952. rekeyed = true;
  953. #ifdef JS_DEBUG
  954. this->validEntry = false;
  955. this->mutationCount = table_.mutationCount;
  956. #endif
  957. }
  958. void rekeyFront(const Key& k) {
  959. rekeyFront(k, k);
  960. }
  961. // Potentially rehashes the table.
  962. ~Enum() {
  963. if (rekeyed) {
  964. table_.gen++;
  965. table_.checkOverRemoved();
  966. }
  967. if (removed)
  968. table_.compactIfUnderloaded();
  969. }
  970. };
  971. // HashTable is movable
  972. HashTable(HashTable&& rhs)
  973. : AllocPolicy(rhs)
  974. {
  975. mozilla::PodAssign(this, &rhs);
  976. rhs.table = nullptr;
  977. }
  978. void operator=(HashTable&& rhs) {
  979. MOZ_ASSERT(this != &rhs, "self-move assignment is prohibited");
  980. if (table)
  981. destroyTable(*this, table, capacity());
  982. mozilla::PodAssign(this, &rhs);
  983. rhs.table = nullptr;
  984. }
  985. private:
  986. // HashTable is not copyable or assignable
  987. HashTable(const HashTable&) = delete;
  988. void operator=(const HashTable&) = delete;
  989. private:
  990. static const size_t CAP_BITS = 30;
  991. public:
  992. uint64_t gen:56; // entry storage generation number
  993. uint64_t hashShift:8; // multiplicative hash shift
  994. Entry* table; // entry storage
  995. uint32_t entryCount; // number of entries in table
  996. uint32_t removedCount; // removed entry sentinels in table
  997. #ifdef JS_DEBUG
  998. uint64_t mutationCount;
  999. mutable bool mEntered;
  1000. // Note that some updates to these stats are not thread-safe. See the
  1001. // comment on the three-argument overloading of HashTable::lookup().
  1002. mutable struct Stats
  1003. {
  1004. uint32_t searches; // total number of table searches
  1005. uint32_t steps; // hash chain links traversed
  1006. uint32_t hits; // searches that found key
  1007. uint32_t misses; // searches that didn't find key
  1008. uint32_t addOverRemoved; // adds that recycled a removed entry
  1009. uint32_t removes; // calls to remove
  1010. uint32_t removeFrees; // calls to remove that freed the entry
  1011. uint32_t grows; // table expansions
  1012. uint32_t shrinks; // table contractions
  1013. uint32_t compresses; // table compressions
  1014. uint32_t rehashes; // tombstone decontaminations
  1015. } stats;
  1016. # define METER(x) x
  1017. #else
  1018. # define METER(x)
  1019. #endif
  1020. // The default initial capacity is 32 (enough to hold 16 elements), but it
  1021. // can be as low as 4.
  1022. static const unsigned sMinCapacityLog2 = 2;
  1023. static const unsigned sMinCapacity = 1 << sMinCapacityLog2;
  1024. static const unsigned sMaxInit = JS_BIT(CAP_BITS - 1);
  1025. static const unsigned sMaxCapacity = JS_BIT(CAP_BITS);
  1026. static const unsigned sHashBits = mozilla::tl::BitSize<HashNumber>::value;
  1027. // Hash-table alpha is conceptually a fraction, but to avoid floating-point
  1028. // math we implement it as a ratio of integers.
  1029. static const uint8_t sAlphaDenominator = 4;
  1030. static const uint8_t sMinAlphaNumerator = 1; // min alpha: 1/4
  1031. static const uint8_t sMaxAlphaNumerator = 3; // max alpha: 3/4
  1032. static const HashNumber sFreeKey = Entry::sFreeKey;
  1033. static const HashNumber sRemovedKey = Entry::sRemovedKey;
  1034. static const HashNumber sCollisionBit = Entry::sCollisionBit;
  1035. void setTableSizeLog2(unsigned sizeLog2)
  1036. {
  1037. hashShift = sHashBits - sizeLog2;
  1038. }
  1039. static bool isLiveHash(HashNumber hash)
  1040. {
  1041. return Entry::isLiveHash(hash);
  1042. }
  1043. static HashNumber prepareHash(const Lookup& l)
  1044. {
  1045. HashNumber keyHash = ScrambleHashCode(HashPolicy::hash(l));
  1046. // Avoid reserved hash codes.
  1047. if (!isLiveHash(keyHash))
  1048. keyHash -= (sRemovedKey + 1);
  1049. return keyHash & ~sCollisionBit;
  1050. }
  1051. enum FailureBehavior { DontReportFailure = false, ReportFailure = true };
  1052. static Entry* createTable(AllocPolicy& alloc, uint32_t capacity,
  1053. FailureBehavior reportFailure = ReportFailure)
  1054. {
  1055. static_assert(sFreeKey == 0,
  1056. "newly-calloc'd tables have to be considered empty");
  1057. if (reportFailure)
  1058. return alloc.template pod_calloc<Entry>(capacity);
  1059. return alloc.template maybe_pod_calloc<Entry>(capacity);
  1060. }
  1061. static Entry* maybeCreateTable(AllocPolicy& alloc, uint32_t capacity)
  1062. {
  1063. static_assert(sFreeKey == 0,
  1064. "newly-calloc'd tables have to be considered empty");
  1065. return alloc.template maybe_pod_calloc<Entry>(capacity);
  1066. }
  1067. static void destroyTable(AllocPolicy& alloc, Entry* oldTable, uint32_t capacity)
  1068. {
  1069. Entry* end = oldTable + capacity;
  1070. for (Entry* e = oldTable; e < end; ++e)
  1071. e->destroyIfLive();
  1072. alloc.free_(oldTable);
  1073. }
  1074. public:
  1075. explicit HashTable(AllocPolicy ap)
  1076. : AllocPolicy(ap)
  1077. , gen(0)
  1078. , hashShift(sHashBits)
  1079. , table(nullptr)
  1080. , entryCount(0)
  1081. , removedCount(0)
  1082. #ifdef JS_DEBUG
  1083. , mutationCount(0)
  1084. , mEntered(false)
  1085. #endif
  1086. {}
  1087. MOZ_MUST_USE bool init(uint32_t length)
  1088. {
  1089. MOZ_ASSERT(!initialized());
  1090. // Reject all lengths whose initial computed capacity would exceed
  1091. // sMaxCapacity. Round that maximum length down to the nearest power
  1092. // of two for speedier code.
  1093. if (MOZ_UNLIKELY(length > sMaxInit)) {
  1094. this->reportAllocOverflow();
  1095. return false;
  1096. }
  1097. static_assert((sMaxInit * sAlphaDenominator) / sAlphaDenominator == sMaxInit,
  1098. "multiplication in numerator below could overflow");
  1099. static_assert(sMaxInit * sAlphaDenominator <= UINT32_MAX - sMaxAlphaNumerator,
  1100. "numerator calculation below could potentially overflow");
  1101. // Compute the smallest capacity allowing |length| elements to be
  1102. // inserted without rehashing: ceil(length / max-alpha). (Ceiling
  1103. // integral division: <http://stackoverflow.com/a/2745086>.)
  1104. uint32_t newCapacity =
  1105. (length * sAlphaDenominator + sMaxAlphaNumerator - 1) / sMaxAlphaNumerator;
  1106. if (newCapacity < sMinCapacity)
  1107. newCapacity = sMinCapacity;
  1108. // FIXME: use JS_CEILING_LOG2 when PGO stops crashing (bug 543034).
  1109. uint32_t roundUp = sMinCapacity, roundUpLog2 = sMinCapacityLog2;
  1110. while (roundUp < newCapacity) {
  1111. roundUp <<= 1;
  1112. ++roundUpLog2;
  1113. }
  1114. newCapacity = roundUp;
  1115. MOZ_ASSERT(newCapacity >= length);
  1116. MOZ_ASSERT(newCapacity <= sMaxCapacity);
  1117. table = createTable(*this, newCapacity);
  1118. if (!table)
  1119. return false;
  1120. setTableSizeLog2(roundUpLog2);
  1121. METER(memset(&stats, 0, sizeof(stats)));
  1122. return true;
  1123. }
  1124. bool initialized() const
  1125. {
  1126. return !!table;
  1127. }
  1128. ~HashTable()
  1129. {
  1130. if (table)
  1131. destroyTable(*this, table, capacity());
  1132. }
  1133. private:
  1134. HashNumber hash1(HashNumber hash0) const
  1135. {
  1136. return hash0 >> hashShift;
  1137. }
  1138. struct DoubleHash
  1139. {
  1140. HashNumber h2;
  1141. HashNumber sizeMask;
  1142. };
  1143. DoubleHash hash2(HashNumber curKeyHash) const
  1144. {
  1145. unsigned sizeLog2 = sHashBits - hashShift;
  1146. DoubleHash dh = {
  1147. ((curKeyHash << sizeLog2) >> hashShift) | 1,
  1148. (HashNumber(1) << sizeLog2) - 1
  1149. };
  1150. return dh;
  1151. }
  1152. static HashNumber applyDoubleHash(HashNumber h1, const DoubleHash& dh)
  1153. {
  1154. return (h1 - dh.h2) & dh.sizeMask;
  1155. }
  1156. bool overloaded()
  1157. {
  1158. static_assert(sMaxCapacity <= UINT32_MAX / sMaxAlphaNumerator,
  1159. "multiplication below could overflow");
  1160. return entryCount + removedCount >=
  1161. capacity() * sMaxAlphaNumerator / sAlphaDenominator;
  1162. }
  1163. // Would the table be underloaded if it had the given capacity and entryCount?
  1164. static bool wouldBeUnderloaded(uint32_t capacity, uint32_t entryCount)
  1165. {
  1166. static_assert(sMaxCapacity <= UINT32_MAX / sMinAlphaNumerator,
  1167. "multiplication below could overflow");
  1168. return capacity > sMinCapacity &&
  1169. entryCount <= capacity * sMinAlphaNumerator / sAlphaDenominator;
  1170. }
  1171. bool underloaded()
  1172. {
  1173. return wouldBeUnderloaded(capacity(), entryCount);
  1174. }
  1175. static bool match(Entry& e, const Lookup& l)
  1176. {
  1177. return HashPolicy::match(HashPolicy::getKey(e.get()), l);
  1178. }
  1179. // Warning: in order for readonlyThreadsafeLookup() to be safe this
  1180. // function must not modify the table in any way when |collisionBit| is 0.
  1181. // (The use of the METER() macro to increment stats violates this
  1182. // restriction but we will live with that for now because it's enabled so
  1183. // rarely.)
  1184. Entry& lookup(const Lookup& l, HashNumber keyHash, unsigned collisionBit) const
  1185. {
  1186. MOZ_ASSERT(isLiveHash(keyHash));
  1187. MOZ_ASSERT(!(keyHash & sCollisionBit));
  1188. MOZ_ASSERT(collisionBit == 0 || collisionBit == sCollisionBit);
  1189. MOZ_ASSERT(table);
  1190. METER(stats.searches++);
  1191. // Compute the primary hash address.
  1192. HashNumber h1 = hash1(keyHash);
  1193. Entry* entry = &table[h1];
  1194. // Miss: return space for a new entry.
  1195. if (entry->isFree()) {
  1196. METER(stats.misses++);
  1197. return *entry;
  1198. }
  1199. // Hit: return entry.
  1200. if (entry->matchHash(keyHash) && match(*entry, l)) {
  1201. METER(stats.hits++);
  1202. return *entry;
  1203. }
  1204. // Collision: double hash.
  1205. DoubleHash dh = hash2(keyHash);
  1206. // Save the first removed entry pointer so we can recycle later.
  1207. Entry* firstRemoved = nullptr;
  1208. while (true) {
  1209. if (MOZ_UNLIKELY(entry->isRemoved())) {
  1210. if (!firstRemoved)
  1211. firstRemoved = entry;
  1212. } else {
  1213. if (collisionBit == sCollisionBit)
  1214. entry->setCollision();
  1215. }
  1216. METER(stats.steps++);
  1217. h1 = applyDoubleHash(h1, dh);
  1218. entry = &table[h1];
  1219. if (entry->isFree()) {
  1220. METER(stats.misses++);
  1221. return firstRemoved ? *firstRemoved : *entry;
  1222. }
  1223. if (entry->matchHash(keyHash) && match(*entry, l)) {
  1224. METER(stats.hits++);
  1225. return *entry;
  1226. }
  1227. }
  1228. }
  1229. // This is a copy of lookup hardcoded to the assumptions:
  1230. // 1. the lookup is a lookupForAdd
  1231. // 2. the key, whose |keyHash| has been passed is not in the table,
  1232. // 3. no entries have been removed from the table.
  1233. // This specialized search avoids the need for recovering lookup values
  1234. // from entries, which allows more flexible Lookup/Key types.
  1235. Entry& findFreeEntry(HashNumber keyHash)
  1236. {
  1237. MOZ_ASSERT(!(keyHash & sCollisionBit));
  1238. MOZ_ASSERT(table);
  1239. METER(stats.searches++);
  1240. // We assume 'keyHash' has already been distributed.
  1241. // Compute the primary hash address.
  1242. HashNumber h1 = hash1(keyHash);
  1243. Entry* entry = &table[h1];
  1244. // Miss: return space for a new entry.
  1245. if (!entry->isLive()) {
  1246. METER(stats.misses++);
  1247. return *entry;
  1248. }
  1249. // Collision: double hash.
  1250. DoubleHash dh = hash2(keyHash);
  1251. while (true) {
  1252. MOZ_ASSERT(!entry->isRemoved());
  1253. entry->setCollision();
  1254. METER(stats.steps++);
  1255. h1 = applyDoubleHash(h1, dh);
  1256. entry = &table[h1];
  1257. if (!entry->isLive()) {
  1258. METER(stats.misses++);
  1259. return *entry;
  1260. }
  1261. }
  1262. }
  1263. enum RebuildStatus { NotOverloaded, Rehashed, RehashFailed };
  1264. RebuildStatus changeTableSize(int deltaLog2, FailureBehavior reportFailure = ReportFailure)
  1265. {
  1266. // Look, but don't touch, until we succeed in getting new entry store.
  1267. Entry* oldTable = table;
  1268. uint32_t oldCap = capacity();
  1269. uint32_t newLog2 = sHashBits - hashShift + deltaLog2;
  1270. uint32_t newCapacity = JS_BIT(newLog2);
  1271. if (MOZ_UNLIKELY(newCapacity > sMaxCapacity)) {
  1272. if (reportFailure)
  1273. this->reportAllocOverflow();
  1274. return RehashFailed;
  1275. }
  1276. Entry* newTable = createTable(*this, newCapacity, reportFailure);
  1277. if (!newTable)
  1278. return RehashFailed;
  1279. // We can't fail from here on, so update table parameters.
  1280. setTableSizeLog2(newLog2);
  1281. removedCount = 0;
  1282. gen++;
  1283. table = newTable;
  1284. // Copy only live entries, leaving removed ones behind.
  1285. Entry* end = oldTable + oldCap;
  1286. for (Entry* src = oldTable; src < end; ++src) {
  1287. if (src->isLive()) {
  1288. HashNumber hn = src->getKeyHash();
  1289. findFreeEntry(hn).setLive(
  1290. hn, mozilla::Move(const_cast<typename Entry::NonConstT&>(src->get())));
  1291. src->destroy();
  1292. }
  1293. }
  1294. // All entries have been destroyed, no need to destroyTable.
  1295. this->free_(oldTable);
  1296. return Rehashed;
  1297. }
  1298. bool shouldCompressTable()
  1299. {
  1300. // Compress if a quarter or more of all entries are removed.
  1301. return removedCount >= (capacity() >> 2);
  1302. }
  1303. RebuildStatus checkOverloaded(FailureBehavior reportFailure = ReportFailure)
  1304. {
  1305. if (!overloaded())
  1306. return NotOverloaded;
  1307. int deltaLog2;
  1308. if (shouldCompressTable()) {
  1309. METER(stats.compresses++);
  1310. deltaLog2 = 0;
  1311. } else {
  1312. METER(stats.grows++);
  1313. deltaLog2 = 1;
  1314. }
  1315. return changeTableSize(deltaLog2, reportFailure);
  1316. }
  1317. // Infallibly rehash the table if we are overloaded with removals.
  1318. void checkOverRemoved()
  1319. {
  1320. if (overloaded()) {
  1321. if (checkOverloaded(DontReportFailure) == RehashFailed)
  1322. rehashTableInPlace();
  1323. }
  1324. }
  1325. void remove(Entry& e)
  1326. {
  1327. MOZ_ASSERT(table);
  1328. METER(stats.removes++);
  1329. if (e.hasCollision()) {
  1330. e.removeLive();
  1331. removedCount++;
  1332. } else {
  1333. METER(stats.removeFrees++);
  1334. e.clearLive();
  1335. }
  1336. entryCount--;
  1337. #ifdef JS_DEBUG
  1338. mutationCount++;
  1339. #endif
  1340. }
  1341. void checkUnderloaded()
  1342. {
  1343. if (underloaded()) {
  1344. METER(stats.shrinks++);
  1345. (void) changeTableSize(-1, DontReportFailure);
  1346. }
  1347. }
  1348. // Resize the table down to the largest capacity which doesn't underload the
  1349. // table. Since we call checkUnderloaded() on every remove, you only need
  1350. // to call this after a bulk removal of items done without calling remove().
  1351. void compactIfUnderloaded()
  1352. {
  1353. int32_t resizeLog2 = 0;
  1354. uint32_t newCapacity = capacity();
  1355. while (wouldBeUnderloaded(newCapacity, entryCount)) {
  1356. newCapacity = newCapacity >> 1;
  1357. resizeLog2--;
  1358. }
  1359. if (resizeLog2 != 0)
  1360. (void) changeTableSize(resizeLog2, DontReportFailure);
  1361. }
  1362. // This is identical to changeTableSize(currentSize), but without requiring
  1363. // a second table. We do this by recycling the collision bits to tell us if
  1364. // the element is already inserted or still waiting to be inserted. Since
  1365. // already-inserted elements win any conflicts, we get the same table as we
  1366. // would have gotten through random insertion order.
  1367. void rehashTableInPlace()
  1368. {
  1369. METER(stats.rehashes++);
  1370. removedCount = 0;
  1371. for (size_t i = 0; i < capacity(); ++i)
  1372. table[i].unsetCollision();
  1373. for (size_t i = 0; i < capacity();) {
  1374. Entry* src = &table[i];
  1375. if (!src->isLive() || src->hasCollision()) {
  1376. ++i;
  1377. continue;
  1378. }
  1379. HashNumber keyHash = src->getKeyHash();
  1380. HashNumber h1 = hash1(keyHash);
  1381. DoubleHash dh = hash2(keyHash);
  1382. Entry* tgt = &table[h1];
  1383. while (true) {
  1384. if (!tgt->hasCollision()) {
  1385. src->swap(tgt);
  1386. tgt->setCollision();
  1387. break;
  1388. }
  1389. h1 = applyDoubleHash(h1, dh);
  1390. tgt = &table[h1];
  1391. }
  1392. }
  1393. // TODO: this algorithm leaves collision bits on *all* elements, even if
  1394. // they are on no collision path. We have the option of setting the
  1395. // collision bits correctly on a subsequent pass or skipping the rehash
  1396. // unless we are totally filled with tombstones: benchmark to find out
  1397. // which approach is best.
  1398. }
  1399. // Note: |l| may be a reference to a piece of |u|, so this function
  1400. // must take care not to use |l| after moving |u|.
  1401. //
  1402. // Prefer to use putNewInfallible; this function does not check
  1403. // invariants.
  1404. template <typename... Args>
  1405. void putNewInfallibleInternal(const Lookup& l, Args&&... args)
  1406. {
  1407. MOZ_ASSERT(table);
  1408. HashNumber keyHash = prepareHash(l);
  1409. Entry* entry = &findFreeEntry(keyHash);
  1410. MOZ_ASSERT(entry);
  1411. if (entry->isRemoved()) {
  1412. METER(stats.addOverRemoved++);
  1413. removedCount--;
  1414. keyHash |= sCollisionBit;
  1415. }
  1416. entry->setLive(keyHash, mozilla::Forward<Args>(args)...);
  1417. entryCount++;
  1418. #ifdef JS_DEBUG
  1419. mutationCount++;
  1420. #endif
  1421. }
  1422. public:
  1423. void clear()
  1424. {
  1425. if (mozilla::IsPod<Entry>::value) {
  1426. memset(table, 0, sizeof(*table) * capacity());
  1427. } else {
  1428. uint32_t tableCapacity = capacity();
  1429. Entry* end = table + tableCapacity;
  1430. for (Entry* e = table; e < end; ++e)
  1431. e->clear();
  1432. }
  1433. removedCount = 0;
  1434. entryCount = 0;
  1435. #ifdef JS_DEBUG
  1436. mutationCount++;
  1437. #endif
  1438. }
  1439. void finish()
  1440. {
  1441. #ifdef JS_DEBUG
  1442. MOZ_ASSERT(!mEntered);
  1443. #endif
  1444. if (!table)
  1445. return;
  1446. destroyTable(*this, table, capacity());
  1447. table = nullptr;
  1448. gen++;
  1449. entryCount = 0;
  1450. removedCount = 0;
  1451. #ifdef JS_DEBUG
  1452. mutationCount++;
  1453. #endif
  1454. }
  1455. Range all() const
  1456. {
  1457. MOZ_ASSERT(table);
  1458. return Range(*this, table, table + capacity());
  1459. }
  1460. bool empty() const
  1461. {
  1462. MOZ_ASSERT(table);
  1463. return !entryCount;
  1464. }
  1465. uint32_t count() const
  1466. {
  1467. MOZ_ASSERT(table);
  1468. return entryCount;
  1469. }
  1470. uint32_t capacity() const
  1471. {
  1472. MOZ_ASSERT(table);
  1473. return JS_BIT(sHashBits - hashShift);
  1474. }
  1475. Generation generation() const
  1476. {
  1477. MOZ_ASSERT(table);
  1478. return Generation(gen);
  1479. }
  1480. size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const
  1481. {
  1482. return mallocSizeOf(table);
  1483. }
  1484. size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const
  1485. {
  1486. return mallocSizeOf(this) + sizeOfExcludingThis(mallocSizeOf);
  1487. }
  1488. Ptr lookup(const Lookup& l) const
  1489. {
  1490. mozilla::ReentrancyGuard g(*this);
  1491. if (!HasHash<HashPolicy>(l))
  1492. return Ptr();
  1493. HashNumber keyHash = prepareHash(l);
  1494. return Ptr(lookup(l, keyHash, 0), *this);
  1495. }
  1496. Ptr readonlyThreadsafeLookup(const Lookup& l) const
  1497. {
  1498. if (!HasHash<HashPolicy>(l))
  1499. return Ptr();
  1500. HashNumber keyHash = prepareHash(l);
  1501. return Ptr(lookup(l, keyHash, 0), *this);
  1502. }
  1503. AddPtr lookupForAdd(const Lookup& l) const
  1504. {
  1505. mozilla::ReentrancyGuard g(*this);
  1506. if (!EnsureHash<HashPolicy>(l))
  1507. return AddPtr();
  1508. HashNumber keyHash = prepareHash(l);
  1509. Entry& entry = lookup(l, keyHash, sCollisionBit);
  1510. AddPtr p(entry, *this, keyHash);
  1511. return p;
  1512. }
  1513. template <typename... Args>
  1514. MOZ_MUST_USE bool add(AddPtr& p, Args&&... args)
  1515. {
  1516. mozilla::ReentrancyGuard g(*this);
  1517. MOZ_ASSERT(table);
  1518. MOZ_ASSERT(!p.found());
  1519. MOZ_ASSERT(!(p.keyHash & sCollisionBit));
  1520. // Check for error from ensureHash() here.
  1521. if (p.isValid())
  1522. return false;
  1523. // Changing an entry from removed to live does not affect whether we
  1524. // are overloaded and can be handled separately.
  1525. if (p.entry_->isRemoved()) {
  1526. if (!this->checkSimulatedOOM())
  1527. return false;
  1528. METER(stats.addOverRemoved++);
  1529. removedCount--;
  1530. p.keyHash |= sCollisionBit;
  1531. } else {
  1532. // Preserve the validity of |p.entry_|.
  1533. RebuildStatus status = checkOverloaded();
  1534. if (status == RehashFailed)
  1535. return false;
  1536. if (status == NotOverloaded && !this->checkSimulatedOOM())
  1537. return false;
  1538. if (status == Rehashed)
  1539. p.entry_ = &findFreeEntry(p.keyHash);
  1540. }
  1541. p.entry_->setLive(p.keyHash, mozilla::Forward<Args>(args)...);
  1542. entryCount++;
  1543. #ifdef JS_DEBUG
  1544. mutationCount++;
  1545. p.generation = generation();
  1546. p.mutationCount = mutationCount;
  1547. #endif
  1548. return true;
  1549. }
  1550. // Note: |l| may be a reference to a piece of |u|, so this function
  1551. // must take care not to use |l| after moving |u|.
  1552. template <typename... Args>
  1553. void putNewInfallible(const Lookup& l, Args&&... args)
  1554. {
  1555. MOZ_ASSERT(!lookup(l).found());
  1556. mozilla::ReentrancyGuard g(*this);
  1557. putNewInfallibleInternal(l, mozilla::Forward<Args>(args)...);
  1558. }
  1559. // Note: |l| may be alias arguments in |args|, so this function must take
  1560. // care not to use |l| after moving |args|.
  1561. template <typename... Args>
  1562. MOZ_MUST_USE bool putNew(const Lookup& l, Args&&... args)
  1563. {
  1564. if (!this->checkSimulatedOOM())
  1565. return false;
  1566. if (!EnsureHash<HashPolicy>(l))
  1567. return false;
  1568. if (checkOverloaded() == RehashFailed)
  1569. return false;
  1570. putNewInfallible(l, mozilla::Forward<Args>(args)...);
  1571. return true;
  1572. }
  1573. // Note: |l| may be a reference to a piece of |u|, so this function
  1574. // must take care not to use |l| after moving |u|.
  1575. template <typename... Args>
  1576. MOZ_MUST_USE bool relookupOrAdd(AddPtr& p, const Lookup& l, Args&&... args)
  1577. {
  1578. // Check for error from ensureHash() here.
  1579. if (p.isValid())
  1580. return false;
  1581. #ifdef JS_DEBUG
  1582. p.generation = generation();
  1583. p.mutationCount = mutationCount;
  1584. #endif
  1585. {
  1586. mozilla::ReentrancyGuard g(*this);
  1587. MOZ_ASSERT(prepareHash(l) == p.keyHash); // l has not been destroyed
  1588. p.entry_ = &lookup(l, p.keyHash, sCollisionBit);
  1589. }
  1590. return p.found() || add(p, mozilla::Forward<Args>(args)...);
  1591. }
  1592. void remove(Ptr p)
  1593. {
  1594. MOZ_ASSERT(table);
  1595. mozilla::ReentrancyGuard g(*this);
  1596. MOZ_ASSERT(p.found());
  1597. remove(*p.entry_);
  1598. checkUnderloaded();
  1599. }
  1600. void rekeyWithoutRehash(Ptr p, const Lookup& l, const Key& k)
  1601. {
  1602. MOZ_ASSERT(table);
  1603. mozilla::ReentrancyGuard g(*this);
  1604. MOZ_ASSERT(p.found());
  1605. typename HashTableEntry<T>::NonConstT t(mozilla::Move(*p));
  1606. HashPolicy::setKey(t, const_cast<Key&>(k));
  1607. remove(*p.entry_);
  1608. putNewInfallibleInternal(l, mozilla::Move(t));
  1609. }
  1610. void rekeyAndMaybeRehash(Ptr p, const Lookup& l, const Key& k)
  1611. {
  1612. rekeyWithoutRehash(p, l, k);
  1613. checkOverRemoved();
  1614. }
  1615. #undef METER
  1616. };
  1617. } // namespace detail
  1618. } // namespace js
  1619. #endif /* js_HashTable_h */