GCHashTable.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 GCHashTable_h
  7. #define GCHashTable_h
  8. #include "js/GCPolicyAPI.h"
  9. #include "js/HashTable.h"
  10. #include "js/RootingAPI.h"
  11. #include "js/SweepingAPI.h"
  12. #include "js/TracingAPI.h"
  13. namespace JS {
  14. // Define a reasonable default GC policy for GC-aware Maps.
  15. template <typename Key, typename Value>
  16. struct DefaultMapSweepPolicy {
  17. static bool needsSweep(Key* key, Value* value) {
  18. return GCPolicy<Key>::needsSweep(key) || GCPolicy<Value>::needsSweep(value);
  19. }
  20. };
  21. // A GCHashMap is a GC-aware HashMap, meaning that it has additional trace and
  22. // sweep methods that know how to visit all keys and values in the table.
  23. // HashMaps that contain GC pointers will generally want to use this GCHashMap
  24. // specialization instead of HashMap, because this conveniently supports tracing
  25. // keys and values, and cleaning up weak entries.
  26. //
  27. // GCHashMap::trace applies GCPolicy<T>::trace to each entry's key and value.
  28. // Most types of GC pointers already have appropriate specializations of
  29. // GCPolicy, so they should just work as keys and values. Any struct type with a
  30. // default constructor and trace and sweep functions should work as well. If you
  31. // need to define your own GCPolicy specialization, generic helpers can be found
  32. // in js/public/TracingAPI.h.
  33. //
  34. // The MapSweepPolicy template parameter controls how the table drops entries
  35. // when swept. GCHashMap::sweep applies MapSweepPolicy::needsSweep to each table
  36. // entry; if it returns true, the entry is dropped. The default MapSweepPolicy
  37. // drops the entry if either the key or value is about to be finalized,
  38. // according to its GCPolicy<T>::needsSweep method. (This default is almost
  39. // always fine: it's hard to imagine keeping such an entry around anyway.)
  40. //
  41. // Note that this HashMap only knows *how* to trace and sweep, but it does not
  42. // itself cause tracing or sweeping to be invoked. For tracing, it must be used
  43. // with Rooted or PersistentRooted, or barriered and traced manually. For
  44. // sweeping, currently it requires an explicit call to <map>.sweep().
  45. template <typename Key,
  46. typename Value,
  47. typename HashPolicy = js::DefaultHasher<Key>,
  48. typename AllocPolicy = js::TempAllocPolicy,
  49. typename MapSweepPolicy = DefaultMapSweepPolicy<Key, Value>>
  50. class GCHashMap : public js::HashMap<Key, Value, HashPolicy, AllocPolicy>
  51. {
  52. using Base = js::HashMap<Key, Value, HashPolicy, AllocPolicy>;
  53. public:
  54. explicit GCHashMap(AllocPolicy a = AllocPolicy()) : Base(a) {}
  55. static void trace(GCHashMap* map, JSTracer* trc) { map->trace(trc); }
  56. void trace(JSTracer* trc) {
  57. if (!this->initialized())
  58. return;
  59. for (typename Base::Enum e(*this); !e.empty(); e.popFront()) {
  60. GCPolicy<Value>::trace(trc, &e.front().value(), "hashmap value");
  61. GCPolicy<Key>::trace(trc, &e.front().mutableKey(), "hashmap key");
  62. }
  63. }
  64. void sweep() {
  65. if (!this->initialized())
  66. return;
  67. for (typename Base::Enum e(*this); !e.empty(); e.popFront()) {
  68. if (MapSweepPolicy::needsSweep(&e.front().mutableKey(), &e.front().value()))
  69. e.removeFront();
  70. }
  71. }
  72. // GCHashMap is movable
  73. GCHashMap(GCHashMap&& rhs) : Base(mozilla::Move(rhs)) {}
  74. void operator=(GCHashMap&& rhs) {
  75. MOZ_ASSERT(this != &rhs, "self-move assignment is prohibited");
  76. Base::operator=(mozilla::Move(rhs));
  77. }
  78. private:
  79. // GCHashMap is not copyable or assignable
  80. GCHashMap(const GCHashMap& hm) = delete;
  81. GCHashMap& operator=(const GCHashMap& hm) = delete;
  82. };
  83. } // namespace JS
  84. namespace js {
  85. // HashMap that supports rekeying.
  86. //
  87. // If your keys are pointers to something like JSObject that can be tenured or
  88. // compacted, prefer to use GCHashMap with MovableCellHasher, which takes
  89. // advantage of the Zone's stable id table to make rekeying unnecessary.
  90. template <typename Key,
  91. typename Value,
  92. typename HashPolicy = DefaultHasher<Key>,
  93. typename AllocPolicy = TempAllocPolicy,
  94. typename MapSweepPolicy = JS::DefaultMapSweepPolicy<Key, Value>>
  95. class GCRekeyableHashMap : public JS::GCHashMap<Key, Value, HashPolicy, AllocPolicy, MapSweepPolicy>
  96. {
  97. using Base = JS::GCHashMap<Key, Value, HashPolicy, AllocPolicy>;
  98. public:
  99. explicit GCRekeyableHashMap(AllocPolicy a = AllocPolicy()) : Base(a) {}
  100. void sweep() {
  101. if (!this->initialized())
  102. return;
  103. for (typename Base::Enum e(*this); !e.empty(); e.popFront()) {
  104. Key key(e.front().key());
  105. if (MapSweepPolicy::needsSweep(&key, &e.front().value()))
  106. e.removeFront();
  107. else if (!HashPolicy::match(key, e.front().key()))
  108. e.rekeyFront(key);
  109. }
  110. }
  111. // GCRekeyableHashMap is movable
  112. GCRekeyableHashMap(GCRekeyableHashMap&& rhs) : Base(mozilla::Move(rhs)) {}
  113. void operator=(GCRekeyableHashMap&& rhs) {
  114. MOZ_ASSERT(this != &rhs, "self-move assignment is prohibited");
  115. Base::operator=(mozilla::Move(rhs));
  116. }
  117. };
  118. template <typename Outer, typename... Args>
  119. class GCHashMapOperations
  120. {
  121. using Map = JS::GCHashMap<Args...>;
  122. using Lookup = typename Map::Lookup;
  123. const Map& map() const { return static_cast<const Outer*>(this)->get(); }
  124. public:
  125. using AddPtr = typename Map::AddPtr;
  126. using Ptr = typename Map::Ptr;
  127. using Range = typename Map::Range;
  128. bool initialized() const { return map().initialized(); }
  129. Ptr lookup(const Lookup& l) const { return map().lookup(l); }
  130. AddPtr lookupForAdd(const Lookup& l) const { return map().lookupForAdd(l); }
  131. Range all() const { return map().all(); }
  132. bool empty() const { return map().empty(); }
  133. uint32_t count() const { return map().count(); }
  134. size_t capacity() const { return map().capacity(); }
  135. bool has(const Lookup& l) const { return map().lookup(l).found(); }
  136. size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
  137. return map().sizeOfExcludingThis(mallocSizeOf);
  138. }
  139. size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
  140. return mallocSizeOf(this) + map().sizeOfExcludingThis(mallocSizeOf);
  141. }
  142. };
  143. template <typename Outer, typename... Args>
  144. class MutableGCHashMapOperations
  145. : public GCHashMapOperations<Outer, Args...>
  146. {
  147. using Map = JS::GCHashMap<Args...>;
  148. using Lookup = typename Map::Lookup;
  149. Map& map() { return static_cast<Outer*>(this)->get(); }
  150. public:
  151. using AddPtr = typename Map::AddPtr;
  152. struct Enum : public Map::Enum { explicit Enum(Outer& o) : Map::Enum(o.map()) {} };
  153. using Ptr = typename Map::Ptr;
  154. using Range = typename Map::Range;
  155. bool init(uint32_t len = 16) { return map().init(len); }
  156. void clear() { map().clear(); }
  157. void finish() { map().finish(); }
  158. void remove(Ptr p) { map().remove(p); }
  159. template<typename KeyInput, typename ValueInput>
  160. bool add(AddPtr& p, KeyInput&& k, ValueInput&& v) {
  161. return map().add(p, mozilla::Forward<KeyInput>(k), mozilla::Forward<ValueInput>(v));
  162. }
  163. template<typename KeyInput>
  164. bool add(AddPtr& p, KeyInput&& k) {
  165. return map().add(p, mozilla::Forward<KeyInput>(k), Map::Value());
  166. }
  167. template<typename KeyInput, typename ValueInput>
  168. bool relookupOrAdd(AddPtr& p, KeyInput&& k, ValueInput&& v) {
  169. return map().relookupOrAdd(p, k,
  170. mozilla::Forward<KeyInput>(k),
  171. mozilla::Forward<ValueInput>(v));
  172. }
  173. template<typename KeyInput, typename ValueInput>
  174. bool put(KeyInput&& k, ValueInput&& v) {
  175. return map().put(mozilla::Forward<KeyInput>(k), mozilla::Forward<ValueInput>(v));
  176. }
  177. template<typename KeyInput, typename ValueInput>
  178. bool putNew(KeyInput&& k, ValueInput&& v) {
  179. return map().putNew(mozilla::Forward<KeyInput>(k), mozilla::Forward<ValueInput>(v));
  180. }
  181. };
  182. template <typename A, typename B, typename C, typename D, typename E>
  183. class RootedBase<JS::GCHashMap<A,B,C,D,E>>
  184. : public MutableGCHashMapOperations<JS::Rooted<JS::GCHashMap<A,B,C,D,E>>, A,B,C,D,E>
  185. {};
  186. template <typename A, typename B, typename C, typename D, typename E>
  187. class MutableHandleBase<JS::GCHashMap<A,B,C,D,E>>
  188. : public MutableGCHashMapOperations<JS::MutableHandle<JS::GCHashMap<A,B,C,D,E>>, A,B,C,D,E>
  189. {};
  190. template <typename A, typename B, typename C, typename D, typename E>
  191. class HandleBase<JS::GCHashMap<A,B,C,D,E>>
  192. : public GCHashMapOperations<JS::Handle<JS::GCHashMap<A,B,C,D,E>>, A,B,C,D,E>
  193. {};
  194. template <typename A, typename B, typename C, typename D, typename E>
  195. class WeakCacheBase<JS::GCHashMap<A,B,C,D,E>>
  196. : public MutableGCHashMapOperations<JS::WeakCache<JS::GCHashMap<A,B,C,D,E>>, A,B,C,D,E>
  197. {};
  198. } // namespace js
  199. namespace JS {
  200. // A GCHashSet is a HashSet with an additional trace method that knows
  201. // be traced to be kept alive will generally want to use this GCHashSet
  202. // specialization in lieu of HashSet.
  203. //
  204. // Most types of GC pointers can be traced with no extra infrastructure. For
  205. // structs and non-gc-pointer members, ensure that there is a specialization of
  206. // GCPolicy<T> with an appropriate trace method available to handle the custom
  207. // type. Generic helpers can be found in js/public/TracingAPI.h.
  208. //
  209. // Note that although this HashSet's trace will deal correctly with moved
  210. // elements, it does not itself know when to barrier or trace elements. To
  211. // function properly it must either be used with Rooted or barriered and traced
  212. // manually.
  213. template <typename T,
  214. typename HashPolicy = js::DefaultHasher<T>,
  215. typename AllocPolicy = js::TempAllocPolicy>
  216. class GCHashSet : public js::HashSet<T, HashPolicy, AllocPolicy>
  217. {
  218. using Base = js::HashSet<T, HashPolicy, AllocPolicy>;
  219. public:
  220. explicit GCHashSet(AllocPolicy a = AllocPolicy()) : Base(a) {}
  221. static void trace(GCHashSet* set, JSTracer* trc) { set->trace(trc); }
  222. void trace(JSTracer* trc) {
  223. if (!this->initialized())
  224. return;
  225. for (typename Base::Enum e(*this); !e.empty(); e.popFront())
  226. GCPolicy<T>::trace(trc, &e.mutableFront(), "hashset element");
  227. }
  228. void sweep() {
  229. if (!this->initialized())
  230. return;
  231. for (typename Base::Enum e(*this); !e.empty(); e.popFront()) {
  232. if (GCPolicy<T>::needsSweep(&e.mutableFront()))
  233. e.removeFront();
  234. }
  235. }
  236. // GCHashSet is movable
  237. GCHashSet(GCHashSet&& rhs) : Base(mozilla::Move(rhs)) {}
  238. void operator=(GCHashSet&& rhs) {
  239. MOZ_ASSERT(this != &rhs, "self-move assignment is prohibited");
  240. Base::operator=(mozilla::Move(rhs));
  241. }
  242. private:
  243. // GCHashSet is not copyable or assignable
  244. GCHashSet(const GCHashSet& hs) = delete;
  245. GCHashSet& operator=(const GCHashSet& hs) = delete;
  246. };
  247. } // namespace JS
  248. namespace js {
  249. template <typename Outer, typename... Args>
  250. class GCHashSetOperations
  251. {
  252. using Set = JS::GCHashSet<Args...>;
  253. using Lookup = typename Set::Lookup;
  254. const Set& set() const { return static_cast<const Outer*>(this)->get(); }
  255. public:
  256. using AddPtr = typename Set::AddPtr;
  257. using Entry = typename Set::Entry;
  258. using Ptr = typename Set::Ptr;
  259. using Range = typename Set::Range;
  260. bool initialized() const { return set().initialized(); }
  261. Ptr lookup(const Lookup& l) const { return set().lookup(l); }
  262. AddPtr lookupForAdd(const Lookup& l) const { return set().lookupForAdd(l); }
  263. Range all() const { return set().all(); }
  264. bool empty() const { return set().empty(); }
  265. uint32_t count() const { return set().count(); }
  266. size_t capacity() const { return set().capacity(); }
  267. bool has(const Lookup& l) const { return set().lookup(l).found(); }
  268. size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
  269. return set().sizeOfExcludingThis(mallocSizeOf);
  270. }
  271. size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
  272. return mallocSizeOf(this) + set().sizeOfExcludingThis(mallocSizeOf);
  273. }
  274. };
  275. template <typename Outer, typename... Args>
  276. class MutableGCHashSetOperations
  277. : public GCHashSetOperations<Outer, Args...>
  278. {
  279. using Set = JS::GCHashSet<Args...>;
  280. using Lookup = typename Set::Lookup;
  281. Set& set() { return static_cast<Outer*>(this)->get(); }
  282. public:
  283. using AddPtr = typename Set::AddPtr;
  284. using Entry = typename Set::Entry;
  285. struct Enum : public Set::Enum { explicit Enum(Outer& o) : Set::Enum(o.set()) {} };
  286. using Ptr = typename Set::Ptr;
  287. using Range = typename Set::Range;
  288. bool init(uint32_t len = 16) { return set().init(len); }
  289. void clear() { set().clear(); }
  290. void finish() { set().finish(); }
  291. void remove(Ptr p) { set().remove(p); }
  292. void remove(const Lookup& l) { set().remove(l); }
  293. template<typename TInput>
  294. bool add(AddPtr& p, TInput&& t) {
  295. return set().add(p, mozilla::Forward<TInput>(t));
  296. }
  297. template<typename TInput>
  298. bool relookupOrAdd(AddPtr& p, const Lookup& l, TInput&& t) {
  299. return set().relookupOrAdd(p, l, mozilla::Forward<TInput>(t));
  300. }
  301. template<typename TInput>
  302. bool put(TInput&& t) {
  303. return set().put(mozilla::Forward<TInput>(t));
  304. }
  305. template<typename TInput>
  306. bool putNew(TInput&& t) {
  307. return set().putNew(mozilla::Forward<TInput>(t));
  308. }
  309. template<typename TInput>
  310. bool putNew(const Lookup& l, TInput&& t) {
  311. return set().putNew(l, mozilla::Forward<TInput>(t));
  312. }
  313. };
  314. template <typename T, typename HP, typename AP>
  315. class RootedBase<JS::GCHashSet<T, HP, AP>>
  316. : public MutableGCHashSetOperations<JS::Rooted<JS::GCHashSet<T, HP, AP>>, T, HP, AP>
  317. {
  318. };
  319. template <typename T, typename HP, typename AP>
  320. class MutableHandleBase<JS::GCHashSet<T, HP, AP>>
  321. : public MutableGCHashSetOperations<JS::MutableHandle<JS::GCHashSet<T, HP, AP>>, T, HP, AP>
  322. {
  323. };
  324. template <typename T, typename HP, typename AP>
  325. class HandleBase<JS::GCHashSet<T, HP, AP>>
  326. : public GCHashSetOperations<JS::Handle<JS::GCHashSet<T, HP, AP>>, T, HP, AP>
  327. {
  328. };
  329. template <typename T, typename HP, typename AP>
  330. class WeakCacheBase<JS::GCHashSet<T, HP, AP>>
  331. : public MutableGCHashSetOperations<JS::WeakCache<JS::GCHashSet<T, HP, AP>>, T, HP, AP>
  332. {
  333. };
  334. } /* namespace js */
  335. #endif /* GCHashTable_h */