Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef jsweakcache_h |
michael@0 | 8 | #define jsweakcache_h |
michael@0 | 9 | |
michael@0 | 10 | #include "jscntxt.h" |
michael@0 | 11 | #include "gc/Marking.h" |
michael@0 | 12 | #include "js/HashTable.h" |
michael@0 | 13 | #include "vm/Runtime.h" |
michael@0 | 14 | |
michael@0 | 15 | namespace js { |
michael@0 | 16 | |
michael@0 | 17 | // A WeakCache is used to map a key to a value similar to an HashMap except |
michael@0 | 18 | // that its entries are garbage collected. An entry is kept as long as |
michael@0 | 19 | // both the key and value are marked. |
michael@0 | 20 | // |
michael@0 | 21 | // No mark function is provided with this weak container. However, this weak |
michael@0 | 22 | // container should take part in the sweep phase. |
michael@0 | 23 | template <class Key, class Value, |
michael@0 | 24 | class HashPolicy = DefaultHasher<Key>, |
michael@0 | 25 | class AllocPolicy = RuntimeAllocPolicy> |
michael@0 | 26 | class WeakCache : public HashMap<Key, Value, HashPolicy, AllocPolicy> { |
michael@0 | 27 | private: |
michael@0 | 28 | typedef HashMap<Key, Value, HashPolicy, AllocPolicy> Base; |
michael@0 | 29 | typedef typename Base::Range Range; |
michael@0 | 30 | typedef typename Base::Enum Enum; |
michael@0 | 31 | |
michael@0 | 32 | public: |
michael@0 | 33 | explicit WeakCache(JSRuntime *rt) : Base(rt) { } |
michael@0 | 34 | explicit WeakCache(JSContext *cx) : Base(cx->runtime()) { } |
michael@0 | 35 | |
michael@0 | 36 | public: |
michael@0 | 37 | // Sweep all entries which have unmarked key or value. |
michael@0 | 38 | void sweep(FreeOp *fop) { |
michael@0 | 39 | // Remove all entries whose keys/values remain unmarked. |
michael@0 | 40 | for (Enum e(*this); !e.empty(); e.popFront()) { |
michael@0 | 41 | // Checking IsMarked() may update the location of the Key (or Value). |
michael@0 | 42 | // Pass in a stack local, then manually update the backing heap store. |
michael@0 | 43 | Key k(e.front().key); |
michael@0 | 44 | bool isKeyDying = gc::IsAboutToBeFinalized(&k); |
michael@0 | 45 | |
michael@0 | 46 | if (isKeyDying || gc::IsAboutToBeFinalized(e.front().value)) { |
michael@0 | 47 | e.removeFront(); |
michael@0 | 48 | } else { |
michael@0 | 49 | // Potentially update the location of the Key. |
michael@0 | 50 | // The Value had its heap addresses correctly passed to IsMarked(), |
michael@0 | 51 | // and therefore has already been updated if necessary. |
michael@0 | 52 | // e.rekeyFront(k); |
michael@0 | 53 | } |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | #if DEBUG |
michael@0 | 57 | // Once we've swept, all remaining edges should stay within the |
michael@0 | 58 | // known-live part of the graph. |
michael@0 | 59 | for (Range r = Base::all(); !r.empty(); r.popFront()) { |
michael@0 | 60 | Key k(r.front().key); |
michael@0 | 61 | |
michael@0 | 62 | JS_ASSERT(!gc::IsAboutToBeFinalized(&k)); |
michael@0 | 63 | JS_ASSERT(!gc::IsAboutToBeFinalized(r.front().value)); |
michael@0 | 64 | |
michael@0 | 65 | // Assert that IsMarked() did not perform relocation. |
michael@0 | 66 | JS_ASSERT(k == r.front().key); |
michael@0 | 67 | } |
michael@0 | 68 | #endif |
michael@0 | 69 | } |
michael@0 | 70 | }; |
michael@0 | 71 | |
michael@0 | 72 | // A WeakValueCache is similar to a WeakCache, except keys are never marked. |
michael@0 | 73 | // This is useful for weak maps where the keys are primitive values such as uint32_t. |
michael@0 | 74 | template <class Key, class Value, |
michael@0 | 75 | class HashPolicy = DefaultHasher<Key>, |
michael@0 | 76 | class AllocPolicy = RuntimeAllocPolicy> |
michael@0 | 77 | class WeakValueCache : public HashMap<Key, Value, HashPolicy, AllocPolicy> |
michael@0 | 78 | { |
michael@0 | 79 | public: |
michael@0 | 80 | typedef HashMap<Key, Value, HashPolicy, AllocPolicy> Base; |
michael@0 | 81 | typedef typename Base::Range Range; |
michael@0 | 82 | typedef typename Base::Enum Enum; |
michael@0 | 83 | |
michael@0 | 84 | explicit WeakValueCache(JSRuntime *rt) : Base(rt) { } |
michael@0 | 85 | explicit WeakValueCache(JSContext *cx) : Base(cx->runtime()) { } |
michael@0 | 86 | |
michael@0 | 87 | public: |
michael@0 | 88 | // Sweep all entries which have unmarked key or value. |
michael@0 | 89 | void sweep(FreeOp *fop) { |
michael@0 | 90 | // Remove all entries whose values remain unmarked. |
michael@0 | 91 | for (Enum e(*this); !e.empty(); e.popFront()) { |
michael@0 | 92 | if (gc::IsAboutToBeFinalized(e.front().value())) |
michael@0 | 93 | e.removeFront(); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | #if DEBUG |
michael@0 | 97 | // Once we've swept, all remaining edges should stay within the |
michael@0 | 98 | // known-live part of the graph. |
michael@0 | 99 | for (Range r = Base::all(); !r.empty(); r.popFront()) |
michael@0 | 100 | JS_ASSERT(!gc::IsAboutToBeFinalized(r.front().value())); |
michael@0 | 101 | #endif |
michael@0 | 102 | } |
michael@0 | 103 | }; |
michael@0 | 104 | |
michael@0 | 105 | } // namespace js |
michael@0 | 106 | |
michael@0 | 107 | #endif /* jsweakcache_h */ |