js/src/jit/PcScriptCache.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jit/PcScriptCache.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99:
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef jit_PcScriptCache_h
    1.11 +#define jit_PcScriptCache_h
    1.12 +
    1.13 +// Defines a fixed-size hash table solely for the purpose of caching jit::GetPcScript().
    1.14 +// One cache is attached to each JSRuntime; it functions as if cleared on GC.
    1.15 +
    1.16 +struct JSRuntime;
    1.17 +
    1.18 +namespace js {
    1.19 +namespace jit {
    1.20 +
    1.21 +struct PcScriptCacheEntry
    1.22 +{
    1.23 +    uint8_t *returnAddress; // Key into the hash table.
    1.24 +    jsbytecode *pc;         // Cached PC.
    1.25 +    JSScript *script;       // Cached script.
    1.26 +};
    1.27 +
    1.28 +struct PcScriptCache
    1.29 +{
    1.30 +    static const uint32_t Length = 73;
    1.31 +
    1.32 +    // GC number at the time the cache was filled or created.
    1.33 +    // Storing and checking against this number allows us to not bother
    1.34 +    // clearing this cache on every GC -- only when actually necessary.
    1.35 +    uint64_t gcNumber;
    1.36 +
    1.37 +    // List of cache entries.
    1.38 +    mozilla::Array<PcScriptCacheEntry, Length> entries;
    1.39 +
    1.40 +    void clear(uint64_t gcNumber) {
    1.41 +        for (uint32_t i = 0; i < Length; i++)
    1.42 +            entries[i].returnAddress = nullptr;
    1.43 +        this->gcNumber = gcNumber;
    1.44 +    }
    1.45 +
    1.46 +    // Get a value from the cache. May perform lazy allocation.
    1.47 +    bool get(JSRuntime *rt, uint32_t hash, uint8_t *addr,
    1.48 +             JSScript **scriptRes, jsbytecode **pcRes)
    1.49 +    {
    1.50 +        // If a GC occurred, lazily clear the cache now.
    1.51 +        if (gcNumber != rt->gcNumber) {
    1.52 +            clear(rt->gcNumber);
    1.53 +            return false;
    1.54 +        }
    1.55 +
    1.56 +        if (entries[hash].returnAddress != addr)
    1.57 +            return false;
    1.58 +
    1.59 +        *scriptRes = entries[hash].script;
    1.60 +        if (pcRes)
    1.61 +            *pcRes = entries[hash].pc;
    1.62 +
    1.63 +        return true;
    1.64 +    }
    1.65 +
    1.66 +    void add(uint32_t hash, uint8_t *addr, jsbytecode *pc, JSScript *script) {
    1.67 +        entries[hash].returnAddress = addr;
    1.68 +        entries[hash].pc = pc;
    1.69 +        entries[hash].script = script;
    1.70 +    }
    1.71 +
    1.72 +    static uint32_t Hash(uint8_t *addr) {
    1.73 +        uint32_t key = (uint32_t)((uintptr_t)addr);
    1.74 +        return ((key >> 3) * 2654435761u) % Length;
    1.75 +    }
    1.76 +};
    1.77 +
    1.78 +} // namespace jit
    1.79 +} // namespace js
    1.80 +
    1.81 +#endif /* jit_PcScriptCache_h */

mercurial