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