Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 jit_PcScriptCache_h |
michael@0 | 8 | #define jit_PcScriptCache_h |
michael@0 | 9 | |
michael@0 | 10 | // Defines a fixed-size hash table solely for the purpose of caching jit::GetPcScript(). |
michael@0 | 11 | // One cache is attached to each JSRuntime; it functions as if cleared on GC. |
michael@0 | 12 | |
michael@0 | 13 | struct JSRuntime; |
michael@0 | 14 | |
michael@0 | 15 | namespace js { |
michael@0 | 16 | namespace jit { |
michael@0 | 17 | |
michael@0 | 18 | struct PcScriptCacheEntry |
michael@0 | 19 | { |
michael@0 | 20 | uint8_t *returnAddress; // Key into the hash table. |
michael@0 | 21 | jsbytecode *pc; // Cached PC. |
michael@0 | 22 | JSScript *script; // Cached script. |
michael@0 | 23 | }; |
michael@0 | 24 | |
michael@0 | 25 | struct PcScriptCache |
michael@0 | 26 | { |
michael@0 | 27 | static const uint32_t Length = 73; |
michael@0 | 28 | |
michael@0 | 29 | // GC number at the time the cache was filled or created. |
michael@0 | 30 | // Storing and checking against this number allows us to not bother |
michael@0 | 31 | // clearing this cache on every GC -- only when actually necessary. |
michael@0 | 32 | uint64_t gcNumber; |
michael@0 | 33 | |
michael@0 | 34 | // List of cache entries. |
michael@0 | 35 | mozilla::Array<PcScriptCacheEntry, Length> entries; |
michael@0 | 36 | |
michael@0 | 37 | void clear(uint64_t gcNumber) { |
michael@0 | 38 | for (uint32_t i = 0; i < Length; i++) |
michael@0 | 39 | entries[i].returnAddress = nullptr; |
michael@0 | 40 | this->gcNumber = gcNumber; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | // Get a value from the cache. May perform lazy allocation. |
michael@0 | 44 | bool get(JSRuntime *rt, uint32_t hash, uint8_t *addr, |
michael@0 | 45 | JSScript **scriptRes, jsbytecode **pcRes) |
michael@0 | 46 | { |
michael@0 | 47 | // If a GC occurred, lazily clear the cache now. |
michael@0 | 48 | if (gcNumber != rt->gcNumber) { |
michael@0 | 49 | clear(rt->gcNumber); |
michael@0 | 50 | return false; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | if (entries[hash].returnAddress != addr) |
michael@0 | 54 | return false; |
michael@0 | 55 | |
michael@0 | 56 | *scriptRes = entries[hash].script; |
michael@0 | 57 | if (pcRes) |
michael@0 | 58 | *pcRes = entries[hash].pc; |
michael@0 | 59 | |
michael@0 | 60 | return true; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | void add(uint32_t hash, uint8_t *addr, jsbytecode *pc, JSScript *script) { |
michael@0 | 64 | entries[hash].returnAddress = addr; |
michael@0 | 65 | entries[hash].pc = pc; |
michael@0 | 66 | entries[hash].script = script; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | static uint32_t Hash(uint8_t *addr) { |
michael@0 | 70 | uint32_t key = (uint32_t)((uintptr_t)addr); |
michael@0 | 71 | return ((key >> 3) * 2654435761u) % Length; |
michael@0 | 72 | } |
michael@0 | 73 | }; |
michael@0 | 74 | |
michael@0 | 75 | } // namespace jit |
michael@0 | 76 | } // namespace js |
michael@0 | 77 | |
michael@0 | 78 | #endif /* jit_PcScriptCache_h */ |