Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/compiler/ras/HashTable.hpp
6000 views
1
/*******************************************************************************
2
* Copyright (c) 2000, 2021 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
23
#ifndef HASHTABLE_INCL
24
#define HASHTABLE_INCL
25
26
#include <stddef.h>
27
#include <stdint.h>
28
#include "env/jittypes.h"
29
#include "infra/Assert.hpp"
30
31
class TR_Memory;
32
33
#define MINIMUM_HASHTABLE_SIZE 16
34
35
typedef uint32_t TR_HashIndex;
36
typedef uintptr_t TR_HashCode;
37
38
class TR_HashTableEntry
39
{
40
public:
41
42
TR_HashTableEntry() {}
43
44
TR_HashTableEntry(void *key, void *data, TR_HashCode hashCode)
45
: _key(key),
46
_data(data),
47
_hashCode(hashCode),
48
_chain(0) {}
49
50
TR_HashTableEntry(const TR_HashTableEntry &entry)
51
: _key(entry._key),
52
_data(entry._data),
53
_hashCode(entry._hashCode),
54
_chain(entry._chain) {}
55
56
void *operator new[](size_t size, TR_Memory *m);
57
void *operator new(size_t size, void *cell) {return cell;}
58
void operator delete[] (void *p, TR_Memory *m) {}
59
60
void *getKey() const {return _key;}
61
void *setKey(void *key) {return (_key = key);}
62
63
void *getData() const {return _data;}
64
void *setData(void *data) {return (_data = data);}
65
66
TR_HashCode getHashCode() const {return _hashCode;}
67
TR_HashCode setHashCode(TR_HashCode hashCode) {return (_hashCode = hashCode);}
68
69
TR_HashIndex getCollisionChain() const {return _chain;}
70
TR_HashIndex setCollisionChain(TR_HashIndex chain) {return (_chain = chain);}
71
72
bool isValid() const {return (_hashCode != 0);}
73
void invalidate() {_hashCode = 0;}
74
75
private:
76
77
void * _key;
78
void * _data;
79
TR_HashCode _hashCode; // unmasked hash value
80
TR_HashIndex _chain; // collision chain
81
};
82
83
// TR_HashTable
84
//
85
// An extensible hash table with arbitrary key and data types.
86
// Note that keys and data must be explicitly cast when adding
87
// to, and retrieving from, the hash table, for efficiency sake.
88
// The following methods can be overridden to suit the desired
89
// key type.
90
//
91
// TR_HashCode calculateHashCode(void *key)
92
// Computes a hash function for the given key
93
//
94
// bool isEqual (void *key1, void *key2)
95
// Determines if a pair of keys are equal
96
//
97
class TR_HashTable
98
{
99
public:
100
101
TR_HashTable(TR_Memory *mem, TR_HashIndex numElements = 64);
102
103
TR_HashTable(const TR_HashTable &table, TR_Memory *mem); // copy constructor
104
105
void *operator new(size_t size, TR_Memory *mem);
106
107
// Locates a record with the given key. Returns true iff a record is
108
// found. If the record is found, set the index reference parameter.
109
// If a hash code is given, use it instead of computing a new code.
110
//
111
bool locate(void *key, TR_HashIndex &index, TR_HashCode hashCode = 0);
112
113
// Adds a record given a (key, data) pair. Returns true iff the record
114
// is successfully added. If a record with the given key already exists,
115
// the add will fail. If the add succeeds, the index reference parameter
116
// is set to the index at which the record was added. If a hash code is
117
// given, it is used instead of recomputing the code based on the key.
118
//
119
bool add(void *key, void *data, TR_HashCode hashCode = 0);
120
121
void *getData(TR_HashIndex index)
122
{
123
TR_ASSERT(_table[index].isValid(), "cannot retrieve invalid data from hash table\n");
124
return _table[index].getData();
125
}
126
127
void remove(TR_HashIndex index);
128
void removeAll();
129
130
bool isEmpty() const;
131
132
void grow(uint32_t newSize);
133
134
// Assuming key is a pointer to an object , divide its
135
// address by 4, and return the quotient as the hash code
136
//
137
virtual TR_HashCode calculateHashCode(void *key) const {return (TR_HashCode) key >> 2;}
138
139
virtual bool isEqual (void *key1, void *key2) const {return (key1 == key2);}
140
141
protected:
142
143
void grow();
144
void growAndRehash(TR_HashTableEntry *, TR_HashIndex, TR_HashIndex, TR_HashIndex);
145
146
TR_Memory *_mem;
147
TR_HashIndex _tableSize; // Total table size (closed + open)
148
TR_HashIndex _mask; // Mask to compute modulus for closed area
149
TR_HashIndex _nextFree; // Next free slot in the open area
150
TR_HashIndex _highestIndex; // Highest allocated index
151
TR_HashTableEntry *_table;
152
};
153
#endif
154
155