comparison src/share/vm/graal/graalHashtable.hpp @ 21519:cecb4e39521c

Use files in lib/graal/options to define Graal options (-G:...) instead of generating code for them
author Gilles Duboscq <gilles.m.duboscq@oracle.com>
date Wed, 27 May 2015 17:40:26 +0200
parents
children
comparison
equal deleted inserted replaced
21518:c2e58b2a2a76 21519:cecb4e39521c
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_GRAAL_GRAAL_HASHTABLE_HPP
25 #define SHARE_VM_GRAAL_GRAAL_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 GraalHashtableEntry : public CHeapObj<mtCompiler> {
33 friend class VMStructs;
34 private:
35 T _literal; // ref to item in table.
36 GraalHashtableEntry* _next; // Link to next element in the linked list for this bucket
37
38 public:
39 GraalHashtableEntry(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 GraalHashtableEntry* next() const {
54 return _next;
55 }
56
57 void set_next(GraalHashtableEntry* 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 GraalHashtable : public CHeapObj<mtCompiler> {
74 friend class VMStructs;
75 private:
76 // Instance variables
77 unsigned int _table_size;
78 GraalHashtableEntry<V>** _buckets;
79 unsigned int _number_of_entries;
80
81 public:
82 GraalHashtable(size_t size) : _table_size(size), _number_of_entries(0) {
83 _buckets = NEW_C_HEAP_ARRAY(GraalHashtableEntry<V>*, table_size(), mtCompiler);
84 for (size_t i = 0; i < table_size(); ++i) {
85 _buckets[i] = NULL;
86 }
87 }
88 virtual ~GraalHashtable();
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 GraalHashtableEntry<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_GRAAL_GRAAL_HASHTABLE_HPP