js/src/jsweakcache.h

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:91033e604afa
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
7 #ifndef jsweakcache_h
8 #define jsweakcache_h
9
10 #include "jscntxt.h"
11 #include "gc/Marking.h"
12 #include "js/HashTable.h"
13 #include "vm/Runtime.h"
14
15 namespace js {
16
17 // A WeakCache is used to map a key to a value similar to an HashMap except
18 // that its entries are garbage collected. An entry is kept as long as
19 // both the key and value are marked.
20 //
21 // No mark function is provided with this weak container. However, this weak
22 // container should take part in the sweep phase.
23 template <class Key, class Value,
24 class HashPolicy = DefaultHasher<Key>,
25 class AllocPolicy = RuntimeAllocPolicy>
26 class WeakCache : public HashMap<Key, Value, HashPolicy, AllocPolicy> {
27 private:
28 typedef HashMap<Key, Value, HashPolicy, AllocPolicy> Base;
29 typedef typename Base::Range Range;
30 typedef typename Base::Enum Enum;
31
32 public:
33 explicit WeakCache(JSRuntime *rt) : Base(rt) { }
34 explicit WeakCache(JSContext *cx) : Base(cx->runtime()) { }
35
36 public:
37 // Sweep all entries which have unmarked key or value.
38 void sweep(FreeOp *fop) {
39 // Remove all entries whose keys/values remain unmarked.
40 for (Enum e(*this); !e.empty(); e.popFront()) {
41 // Checking IsMarked() may update the location of the Key (or Value).
42 // Pass in a stack local, then manually update the backing heap store.
43 Key k(e.front().key);
44 bool isKeyDying = gc::IsAboutToBeFinalized(&k);
45
46 if (isKeyDying || gc::IsAboutToBeFinalized(e.front().value)) {
47 e.removeFront();
48 } else {
49 // Potentially update the location of the Key.
50 // The Value had its heap addresses correctly passed to IsMarked(),
51 // and therefore has already been updated if necessary.
52 // e.rekeyFront(k);
53 }
54 }
55
56 #if DEBUG
57 // Once we've swept, all remaining edges should stay within the
58 // known-live part of the graph.
59 for (Range r = Base::all(); !r.empty(); r.popFront()) {
60 Key k(r.front().key);
61
62 JS_ASSERT(!gc::IsAboutToBeFinalized(&k));
63 JS_ASSERT(!gc::IsAboutToBeFinalized(r.front().value));
64
65 // Assert that IsMarked() did not perform relocation.
66 JS_ASSERT(k == r.front().key);
67 }
68 #endif
69 }
70 };
71
72 // A WeakValueCache is similar to a WeakCache, except keys are never marked.
73 // This is useful for weak maps where the keys are primitive values such as uint32_t.
74 template <class Key, class Value,
75 class HashPolicy = DefaultHasher<Key>,
76 class AllocPolicy = RuntimeAllocPolicy>
77 class WeakValueCache : public HashMap<Key, Value, HashPolicy, AllocPolicy>
78 {
79 public:
80 typedef HashMap<Key, Value, HashPolicy, AllocPolicy> Base;
81 typedef typename Base::Range Range;
82 typedef typename Base::Enum Enum;
83
84 explicit WeakValueCache(JSRuntime *rt) : Base(rt) { }
85 explicit WeakValueCache(JSContext *cx) : Base(cx->runtime()) { }
86
87 public:
88 // Sweep all entries which have unmarked key or value.
89 void sweep(FreeOp *fop) {
90 // Remove all entries whose values remain unmarked.
91 for (Enum e(*this); !e.empty(); e.popFront()) {
92 if (gc::IsAboutToBeFinalized(e.front().value()))
93 e.removeFront();
94 }
95
96 #if DEBUG
97 // Once we've swept, all remaining edges should stay within the
98 // known-live part of the graph.
99 for (Range r = Base::all(); !r.empty(); r.popFront())
100 JS_ASSERT(!gc::IsAboutToBeFinalized(r.front().value()));
101 #endif
102 }
103 };
104
105 } // namespace js
106
107 #endif /* jsweakcache_h */

mercurial