|
1 /* |
|
2 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 package sun.jvm.hotspot.utilities; |
|
26 |
|
27 import java.util.*; |
|
28 import sun.jvm.hotspot.debugger.*; |
|
29 import sun.jvm.hotspot.oops.*; |
|
30 import sun.jvm.hotspot.types.*; |
|
31 import sun.jvm.hotspot.runtime.*; |
|
32 import sun.jvm.hotspot.utilities.*; |
|
33 |
|
34 public class CompactHashTable extends VMObject { |
|
35 static { |
|
36 VM.registerVMInitializedObserver(new Observer() { |
|
37 public void update(Observable o, Object data) { |
|
38 initialize(VM.getVM().getTypeDataBase()); |
|
39 } |
|
40 }); |
|
41 } |
|
42 |
|
43 private static synchronized void initialize(TypeDataBase db) throws WrongTypeException { |
|
44 Type type = db.lookupType("SymbolCompactHashTable"); |
|
45 baseAddressField = type.getAddressField("_base_address"); |
|
46 bucketCountField = type.getCIntegerField("_bucket_count"); |
|
47 tableEndOffsetField = type.getCIntegerField("_table_end_offset"); |
|
48 bucketsField = type.getAddressField("_buckets"); |
|
49 uintSize = db.lookupType("juint").getSize(); |
|
50 } |
|
51 |
|
52 // Fields |
|
53 private static CIntegerField bucketCountField; |
|
54 private static CIntegerField tableEndOffsetField; |
|
55 private static AddressField baseAddressField; |
|
56 private static AddressField bucketsField; |
|
57 private static long uintSize; |
|
58 |
|
59 private static int BUCKET_OFFSET_MASK = 0x3FFFFFFF; |
|
60 private static int BUCKET_TYPE_SHIFT = 30; |
|
61 private static int COMPACT_BUCKET_TYPE = 1; |
|
62 |
|
63 public CompactHashTable(Address addr) { |
|
64 super(addr); |
|
65 } |
|
66 |
|
67 private int bucketCount() { |
|
68 return (int)bucketCountField.getValue(addr); |
|
69 } |
|
70 |
|
71 private int tableEndOffset() { |
|
72 return (int)tableEndOffsetField.getValue(addr); |
|
73 } |
|
74 |
|
75 private boolean isCompactBucket(int bucket_info) { |
|
76 return (bucket_info >> BUCKET_TYPE_SHIFT) == COMPACT_BUCKET_TYPE; |
|
77 } |
|
78 |
|
79 private int bucketOffset(int bucket_info) { |
|
80 return bucket_info & BUCKET_OFFSET_MASK; |
|
81 } |
|
82 |
|
83 public Symbol probe(byte[] name, long hash) { |
|
84 long symOffset; |
|
85 Symbol sym; |
|
86 Address baseAddress = baseAddressField.getValue(addr); |
|
87 Address bucket = bucketsField.getValue(addr); |
|
88 Address bucketEnd = bucket; |
|
89 long index = hash % bucketCount(); |
|
90 int bucketInfo = (int)bucket.getCIntegerAt(index * uintSize, uintSize, true); |
|
91 int bucketOffset = bucketOffset(bucketInfo); |
|
92 int nextBucketInfo = (int)bucket.getCIntegerAt((index+1) * uintSize, uintSize, true); |
|
93 int nextBucketOffset = bucketOffset(nextBucketInfo); |
|
94 |
|
95 bucket = bucket.addOffsetTo(bucketOffset * uintSize); |
|
96 |
|
97 if (isCompactBucket(bucketInfo)) { |
|
98 symOffset = bucket.getCIntegerAt(0, uintSize, true); |
|
99 sym = Symbol.create(baseAddress.addOffsetTo(symOffset)); |
|
100 if (sym.equals(name)) { |
|
101 return sym; |
|
102 } |
|
103 } else { |
|
104 bucketEnd = bucket.addOffsetTo(nextBucketOffset * uintSize); |
|
105 while (bucket.lessThan(bucketEnd)) { |
|
106 long symHash = bucket.getCIntegerAt(0, uintSize, true); |
|
107 if (symHash == hash) { |
|
108 symOffset = bucket.getCIntegerAt(uintSize, uintSize, true); |
|
109 Address symAddr = baseAddress.addOffsetTo(symOffset); |
|
110 sym = Symbol.create(symAddr); |
|
111 if (sym.equals(name)) { |
|
112 return sym; |
|
113 } |
|
114 } |
|
115 bucket = bucket.addOffsetTo(2 * uintSize); |
|
116 } |
|
117 } |
|
118 return null; |
|
119 } |
|
120 } |