DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (fddffdeab170)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef jsweakcache_h
#define jsweakcache_h

#include "jscntxt.h"
#include "gc/Marking.h"
#include "js/HashTable.h"
#include "vm/Runtime.h"

namespace js {

// A WeakCache is used to map a key to a value similar to an HashMap except
// that its entries are garbage collected.  An entry is kept as long as
// both the key and value are marked.
//
// No mark function is provided with this weak container.  However, this weak
// container should take part in the sweep phase.
template <class Key, class Value,
          class HashPolicy = DefaultHasher<Key>,
          class AllocPolicy = RuntimeAllocPolicy>
class WeakCache : public HashMap<Key, Value, HashPolicy, AllocPolicy> {
  private:
    typedef HashMap<Key, Value, HashPolicy, AllocPolicy> Base;
    typedef typename Base::Range Range;
    typedef typename Base::Enum Enum;

  public:
    explicit WeakCache(JSRuntime* rt) : Base(rt) { }
    explicit WeakCache(JSContext* cx) : Base(cx->runtime()) { }

  public:
    // Sweep all entries which have unmarked key or value.
    void sweep(FreeOp* fop) {
        // Remove all entries whose keys/values remain unmarked.
        for (Enum e(*this); !e.empty(); e.popFront()) {
            // IsAboutToBeFinalized() may update the location of the Key (or Value).
            // Pass in a stack local, then manually update the backing heap store.
            Key key(e.front().key);
            MOZ_ASSERT(key);
            MOZ_ASSERT(e.front().value());
            if (gc::IsAboutToBeFinalized(&key) || gc::IsAboutToBeFinalized(e.front().value))
                e.removeFront();
            else if (key != e.front().key)
                e.rekeyFront(key);
        }

#ifdef DEBUG
        // Once we've swept, all remaining edges should stay within the
        // known-live part of the graph.
        for (Range r = Base::all(); !r.empty(); r.popFront()) {
            Key key(r.front().key);
            Value value(r.front().value);
            MOZ_ASSERT(key);
            MOZ_ASSERT(value);
            MOZ_ASSERT(!gc::IsAboutToBeFinalized(&key));
            MOZ_ASSERT(!gc::IsAboutToBeFinalized(&value));
            CheckGCThingAfterMovingGC(key);
            CheckGCThingAfterMovingGC(value);
            auto ptr = this->lookup(key);
            MOZ_ASSERT(ptr.found() && &*ptr == &r.front());
        }
#endif
    }
};

// A WeakValueCache is similar to a WeakCache, except keys are never marked.
// This is useful for weak maps where the keys are primitive values such as uint32_t.
template <class Key, class Value,
          class HashPolicy = DefaultHasher<Key>,
          class AllocPolicy = RuntimeAllocPolicy>
class WeakValueCache : public HashMap<Key, Value, HashPolicy, AllocPolicy>
{
  public:
    typedef HashMap<Key, Value, HashPolicy, AllocPolicy> Base;
    typedef typename Base::Range Range;
    typedef typename Base::Enum Enum;

    explicit WeakValueCache(JSRuntime* rt) : Base(rt) { }
    explicit WeakValueCache(JSContext* cx) : Base(cx->runtime()) { }

  public:
    // Sweep all entries which have unmarked key or value.
    void sweep(FreeOp* fop) {
        // Remove all entries whose values remain unmarked.
        for (Enum e(*this); !e.empty(); e.popFront()) {
            MOZ_ASSERT(e.front().value());
            if (gc::IsAboutToBeFinalized(&e.front().value()))
                e.removeFront();
        }

#ifdef DEBUG
        // Once we've swept, all remaining edges should stay within the
        // known-live part of the graph.
        for (Range r = Base::all(); !r.empty(); r.popFront()) {
            Value value(r.front().value());
            MOZ_ASSERT(value);
            MOZ_ASSERT(!gc::IsAboutToBeFinalized(&value));
            CheckGCThingAfterMovingGC(value);
            auto ptr = this->lookup(r.front().key());
            MOZ_ASSERT(ptr.found() && &*ptr == &r.front());
        }
#endif
    }
};

} // namespace js

#endif /* jsweakcache_h */