michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef jsweakcache_h michael@0: #define jsweakcache_h michael@0: michael@0: #include "jscntxt.h" michael@0: #include "gc/Marking.h" michael@0: #include "js/HashTable.h" michael@0: #include "vm/Runtime.h" michael@0: michael@0: namespace js { michael@0: michael@0: // A WeakCache is used to map a key to a value similar to an HashMap except michael@0: // that its entries are garbage collected. An entry is kept as long as michael@0: // both the key and value are marked. michael@0: // michael@0: // No mark function is provided with this weak container. However, this weak michael@0: // container should take part in the sweep phase. michael@0: template , michael@0: class AllocPolicy = RuntimeAllocPolicy> michael@0: class WeakCache : public HashMap { michael@0: private: michael@0: typedef HashMap Base; michael@0: typedef typename Base::Range Range; michael@0: typedef typename Base::Enum Enum; michael@0: michael@0: public: michael@0: explicit WeakCache(JSRuntime *rt) : Base(rt) { } michael@0: explicit WeakCache(JSContext *cx) : Base(cx->runtime()) { } michael@0: michael@0: public: michael@0: // Sweep all entries which have unmarked key or value. michael@0: void sweep(FreeOp *fop) { michael@0: // Remove all entries whose keys/values remain unmarked. michael@0: for (Enum e(*this); !e.empty(); e.popFront()) { michael@0: // Checking IsMarked() may update the location of the Key (or Value). michael@0: // Pass in a stack local, then manually update the backing heap store. michael@0: Key k(e.front().key); michael@0: bool isKeyDying = gc::IsAboutToBeFinalized(&k); michael@0: michael@0: if (isKeyDying || gc::IsAboutToBeFinalized(e.front().value)) { michael@0: e.removeFront(); michael@0: } else { michael@0: // Potentially update the location of the Key. michael@0: // The Value had its heap addresses correctly passed to IsMarked(), michael@0: // and therefore has already been updated if necessary. michael@0: // e.rekeyFront(k); michael@0: } michael@0: } michael@0: michael@0: #if DEBUG michael@0: // Once we've swept, all remaining edges should stay within the michael@0: // known-live part of the graph. michael@0: for (Range r = Base::all(); !r.empty(); r.popFront()) { michael@0: Key k(r.front().key); michael@0: michael@0: JS_ASSERT(!gc::IsAboutToBeFinalized(&k)); michael@0: JS_ASSERT(!gc::IsAboutToBeFinalized(r.front().value)); michael@0: michael@0: // Assert that IsMarked() did not perform relocation. michael@0: JS_ASSERT(k == r.front().key); michael@0: } michael@0: #endif michael@0: } michael@0: }; michael@0: michael@0: // A WeakValueCache is similar to a WeakCache, except keys are never marked. michael@0: // This is useful for weak maps where the keys are primitive values such as uint32_t. michael@0: template , michael@0: class AllocPolicy = RuntimeAllocPolicy> michael@0: class WeakValueCache : public HashMap michael@0: { michael@0: public: michael@0: typedef HashMap Base; michael@0: typedef typename Base::Range Range; michael@0: typedef typename Base::Enum Enum; michael@0: michael@0: explicit WeakValueCache(JSRuntime *rt) : Base(rt) { } michael@0: explicit WeakValueCache(JSContext *cx) : Base(cx->runtime()) { } michael@0: michael@0: public: michael@0: // Sweep all entries which have unmarked key or value. michael@0: void sweep(FreeOp *fop) { michael@0: // Remove all entries whose values remain unmarked. michael@0: for (Enum e(*this); !e.empty(); e.popFront()) { michael@0: if (gc::IsAboutToBeFinalized(e.front().value())) michael@0: e.removeFront(); michael@0: } michael@0: michael@0: #if DEBUG michael@0: // Once we've swept, all remaining edges should stay within the michael@0: // known-live part of the graph. michael@0: for (Range r = Base::all(); !r.empty(); r.popFront()) michael@0: JS_ASSERT(!gc::IsAboutToBeFinalized(r.front().value())); michael@0: #endif michael@0: } michael@0: }; michael@0: michael@0: } // namespace js michael@0: michael@0: #endif /* jsweakcache_h */