Mercurial > hg > graal-jvmci-8
comparison src/share/vm/jvmci/jvmciHashtable.hpp @ 21562:47bebae7454f
Merge.
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Thu, 28 May 2015 21:58:33 +0200 |
parents | src/share/vm/graal/graalHashtable.hpp@cecb4e39521c |
children | 59c3f921e454 |
comparison
equal
deleted
inserted
replaced
21561:ce2113326bc8 | 21562:47bebae7454f |
---|---|
1 /* | |
2 * Copyright (c) 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 #ifndef SHARE_VM_JVMCI_JVMCI_HASHTABLE_HPP | |
25 #define SHARE_VM_JVMCI_JVMCI_HASHTABLE_HPP | |
26 | |
27 #include "memory/allocation.hpp" | |
28 #include "memory/allocation.inline.hpp" | |
29 | |
30 // based on hashtable.hpp | |
31 | |
32 template <class T> class JVMCIHashtableEntry : public CHeapObj<mtCompiler> { | |
33 friend class VMStructs; | |
34 private: | |
35 T _literal; // ref to item in table. | |
36 JVMCIHashtableEntry* _next; // Link to next element in the linked list for this bucket | |
37 | |
38 public: | |
39 JVMCIHashtableEntry(T literal) : _literal(literal), _next(NULL) {} | |
40 | |
41 T literal() { | |
42 return _literal; | |
43 } | |
44 | |
45 void set_literal(T value) { | |
46 _literal = value; | |
47 } | |
48 | |
49 T* literal_addr() { | |
50 return &_literal; | |
51 } | |
52 | |
53 JVMCIHashtableEntry* next() const { | |
54 return _next; | |
55 } | |
56 | |
57 void set_next(JVMCIHashtableEntry* next) { | |
58 _next = next; | |
59 } | |
60 }; | |
61 | |
62 template <class V> | |
63 class ValueClosure : public StackObj { | |
64 bool _abort; | |
65 protected: | |
66 void abort() { _abort = true; } | |
67 public: | |
68 ValueClosure() : _abort(false) {} | |
69 virtual void do_value(V* value) = 0; | |
70 bool is_aborted() { return _abort; } | |
71 }; | |
72 | |
73 template <class K, class V> class JVMCIHashtable : public CHeapObj<mtCompiler> { | |
74 friend class VMStructs; | |
75 private: | |
76 // Instance variables | |
77 unsigned int _table_size; | |
78 JVMCIHashtableEntry<V>** _buckets; | |
79 unsigned int _number_of_entries; | |
80 | |
81 public: | |
82 JVMCIHashtable(size_t size) : _table_size(size), _number_of_entries(0) { | |
83 _buckets = NEW_C_HEAP_ARRAY(JVMCIHashtableEntry<V>*, table_size(), mtCompiler); | |
84 for (size_t i = 0; i < table_size(); ++i) { | |
85 _buckets[i] = NULL; | |
86 } | |
87 } | |
88 virtual ~JVMCIHashtable(); | |
89 | |
90 private: | |
91 // Bucket handling | |
92 unsigned int hash_to_index(unsigned int full_hash) { | |
93 unsigned int h = full_hash % _table_size; | |
94 assert(h >= 0 && h < _table_size, "Illegal hash value"); | |
95 return h; | |
96 } | |
97 | |
98 unsigned int index_for(K key) { | |
99 return hash_to_index(compute_hash(key)); | |
100 } | |
101 | |
102 size_t entry_size() { | |
103 return sizeof(V); | |
104 } | |
105 | |
106 size_t table_size() { return _table_size; } | |
107 | |
108 JVMCIHashtableEntry<V>* bucket(unsigned int index) { | |
109 return _buckets[index]; | |
110 } | |
111 | |
112 bool add(V v, bool replace); | |
113 | |
114 protected: | |
115 virtual unsigned int compute_hash(K key) = 0; | |
116 virtual bool key_equals(K k1, K k2) = 0; | |
117 virtual K get_key(V value) = 0; | |
118 virtual K get_key(V* value) = 0; | |
119 | |
120 public: | |
121 /** | |
122 * Tries to insert the value in the hash table. Returns false if an entry with the same key already exists. | |
123 * In this case it does *not* replace the existing entry. | |
124 */ | |
125 bool add(V v) { return add(v, false); } | |
126 /** | |
127 * Inserts the value in the hash table. Returns false if an entry with the same key already exists. | |
128 * In this case it replaces the existing entry. | |
129 */ | |
130 bool put(V v) { return add(v, true); } | |
131 V* get(K k); | |
132 void for_each(ValueClosure<V>* closure); | |
133 int number_of_entries() { return _number_of_entries; } | |
134 | |
135 }; | |
136 | |
137 #endif // SHARE_VM_JVMCI_JVMCI_HASHTABLE_HPP |