-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathHashTab.hpp
311 lines (255 loc) · 10.4 KB
/
HashTab.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*******************************************************************************
* Copyright IBM Corp. and others 2000
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#ifndef HASHTAB_INCL
#define HASHTAB_INCL
#include <stdint.h>
#include <string.h>
#include "env/TRMemory.hpp"
#include "env/jittypes.h"
#include "infra/Assert.hpp"
#include "infra/Bit.hpp"
class TR_HashTab;
namespace TR { class Compilation; }
/**
* general purpose hash table that assume a key which fits in 8 bytes
* and stores a 'void *'. Less space efficient than a template,
* but templates have their own headaches.
* User can overwrite two routines, and they must be explicitly
* cast when being used. See HashTab.cpp for examples
*
* uint32_t calculateHash(const void *key) const;
*/
// assume sizeof(long) >= sizeof(uint32_t)
typedef unsigned long TR_HashId;
class TR_HashTabIterator
{
public:
TR_ALLOC(TR_Memory::HashTab)
TR_HashTabIterator() : _curHashIndex(0) {}
TR_HashTabIterator(TR_HashTab *h) : _baseHashTab(h),_curHashIndex(0){}
void *getCurrent ();
void *getFirst() { _curHashIndex = 0; return getCurrent(); }
void *getNext() { ++_curHashIndex; return getCurrent(); }
void reset() { _curHashIndex = 0; }
bool atEnd();
private:
TR_HashId _curHashIndex;
TR_HashTab *_baseHashTab;
};
// hash table with chaining. Chained buckets are in a separate area
class TR_HashTab
{
protected:
enum { kMinimumSize=16,
kDefaultSize=64};
public:
TR_ALLOC(TR_Memory::HashTab)
// inherit and then overwrite these two methods as appropriate
// assume a pointer
virtual TR_HashId calculateHash(const void *key) const { return (TR_HashId((uintptr_t)key) >> 2) %_closedAreaSize;}
virtual bool isEqual(const void * key1, const void *key2) const { return key1 == key2; }
TR_HashTab(TR_Memory *mem,
TR_AllocationKind a=heapAlloc,
uint32_t initialSize=kDefaultSize,
bool g=true):
_tableSize(0), _allocType(a),_table(NULL),_closedAreaSize(0),_allowGrowth(g),_trMemory(mem)
{
init(initialSize);
_trace=false;
}
TR_Memory * trMemory() { return _trMemory; }
TR_StackMemory trStackMemory() { return _trMemory; }
TR_HeapMemory trHeapMemory() { return _trMemory; }
void init(uint32_t size, bool allowGrowth=true);
bool locate(const void* key,TR_HashId &hashIndex);
bool growTo(uint32_t newSize);
void reset()
{
_tableSize=0;
if ((_allocType == persistentAlloc) &&
(_table != NULL))
{
for (TR_HashId i=0; i < _tableSize ;++i)
jitPersistentFree(_table[i]);
jitPersistentFree(_table);
}
_table = NULL; // TODO if heap, notify vm that it's free
}
void clear() { memset(_table,0,sizeof(TR_HashTableEntry*) * _tableSize); }
void * getData(TR_HashId id);
void updateData(TR_HashId id, void *data)
{
_table[id]->_data = data;
return;
}
void setKey(TR_HashId id, void* key) { _table[id]->_key = key;return; } ;
/**
* return true if successfully added
* search table for matching record. If
* a collision, chain in a new entry.
* if we run out of free space, die--in future grow and rehash
*/
bool add(void *key,TR_HashId hashIndex,void * data)
{
TR_HashTableEntry *entry = new (trMemory(), _allocType) TR_HashTableEntry(key,data,0);
return addElement(key,hashIndex,entry);
}
void replace(void *key, TR_HashId hashIndex, void *data)
{
TR_HashTableEntry *entry = new (trMemory(), _allocType) TR_HashTableEntry(key,data,0);
TR_ASSERT(_table && _table[hashIndex], "entry in hashIndex is NULL\n");
entry->_chain = _table[hashIndex]->_chain;
_table[hashIndex] = entry;
}
private:
void growAndRehash(uint32_t);
class TR_HashTableEntry
{
public:
TR_ALLOC(TR_Memory::HashTableEntry)
TR_HashTableEntry():
_chain(0),
_data(NULL){}
TR_HashTableEntry(void *key,void * data,uint32_t chain)
{
_key = key;
_chain = chain;
_data = data;
}
TR_HashTableEntry(const TR_HashTableEntry & entry):
_key(entry._key),
_chain(entry._chain),
_data(entry._data){}
void * _key;
void * _data;
uint32_t _chain;
};
private:
bool addElement(void * key,TR_HashId &hashIndex,TR_HashTableEntry *entry);
uint32_t _tableSize; // total table size (closed + open)
TR::Compilation * _comp;
TR_Memory * _trMemory;
TR_AllocationKind _allocType;
TR_HashId _nextFree; // next free slot in open area
uint32_t _mask;
TR_HashTableEntry **_table;
bool _allowGrowth;
bool _trace;
protected:
uint32_t _closedAreaSize; // range of hashes
friend bool TR_HashTabIterator::atEnd();
friend void *TR_HashTabIterator::getCurrent();
};
class TR_HashTabInt : public TR_HashTab
{
public:
TR_HashTabInt(TR_Memory *mem, TR_AllocationKind allocType=heapAlloc, uint32_t initialSize=kDefaultSize, bool allowGrowth=true):
TR_HashTab(mem, allocType, initialSize, allowGrowth) { }
TR_HashId calculateHash(const void *key) const { return (TR_HashId((intptr_t)key) %_closedAreaSize);}
bool locate(int32_t key,TR_HashId &hashIndex){ return TR_HashTab::locate((const void*)(uintptr_t)key,hashIndex);}
bool add(int32_t key,TR_HashId hashIndex,void * data){ return TR_HashTab::add((void *)(uintptr_t)key,hashIndex,data);}
};
class TR_HashTabString : public TR_HashTab
{
public:
TR_HashTabString(TR_Memory *mem, TR_AllocationKind allocType=heapAlloc, uint32_t initialSize=kDefaultSize, bool allowGrowth=true):
TR_HashTab(mem, allocType, initialSize, allowGrowth) { }
TR_HashId calculateHash(const void *key) const // Bernstein's string hash
{
uint64_t hash = 5381;
char * str = (char *) key;
int32_t c;
while((c = *str++))
hash = ((hash <<5) + hash) + c; // hash * 33 + c
return (TR_HashId(hash) %_closedAreaSize);
}
bool isEqual(const void * key1, const void *key2) const { return 0 == strcmp((char*)key1,(char *) key2); }
bool locate(const char * key,TR_HashId &hashIndex){ return TR_HashTab::locate((const void*)key,hashIndex);}
bool add(const char * key,TR_HashId hashIndex,void * data){ return TR_HashTab::add((void *)key,hashIndex,data);}
};
class TR_HashTabDouble : public TR_HashTab
{
public:
TR_HashTabDouble(TR_Memory *mem, TR_AllocationKind allocType=heapAlloc, uint32_t initialSize=kDefaultSize, bool allowGrowth=true):
TR_HashTab(mem, allocType, initialSize, allowGrowth) { }
TR_HashId calculateHash(const void *key) const // TODO: research better hash fns for double
{
union
{
double dbl;
struct {
int32_t upperInt;
int32_t lowerInt;
}intVal;
} x;
x.dbl = *(double *)key;
int32_t hash = x.intVal.lowerInt | x.intVal.upperInt;
return (TR_HashId(hash) %_closedAreaSize);
}
bool isEqual(const void * key1, const void *key2) const { double a = *(double*)key1; double b = *(double*)key2; return a == b; }
bool locate(const double * key,TR_HashId &hashIndex){ return TR_HashTab::locate((const void*)key,hashIndex);}
bool add(double * key,TR_HashId hashIndex,void * data){ return TR_HashTab::add((void *)key,hashIndex,data);}
};
class TR_HashTabLong : public TR_HashTab
{
public:
TR_HashTabLong(TR_Memory *mem, TR_AllocationKind allocType=heapAlloc, uint32_t initialSize=kDefaultSize, bool allowGrowth=true):
TR_HashTab(mem, allocType, initialSize, allowGrowth) { }
TR_HashId calculateHash(const void *key) const // Bernstein's string hash
{
union
{
int64_t longVal;
struct {
int32_t upperInt;
int32_t lowerInt;
}intVal;
} x;
x.longVal = *(int64_t *)key;
int32_t hash = x.intVal.lowerInt | x.intVal.upperInt;
return (TR_HashId(hash) %_closedAreaSize);
}
bool isEqual(const void * key1, const void *key2) const { return *(int64_t *)key1 == *(int64_t*)key2; }
bool locate(const int64_t * key,TR_HashId &hashIndex){ return TR_HashTab::locate((const void*)key,hashIndex);}
bool add(int64_t * key,TR_HashId hashIndex,void * data){ return TR_HashTab::add((void *)key,hashIndex,data);}
};
class TR_HashTabFloat : public TR_HashTab
{
public:
TR_HashTabFloat(TR_Memory *mem, TR_AllocationKind allocType=heapAlloc,
uint32_t initialSize=kDefaultSize, bool allowGrowth=true):
TR_HashTab(mem, allocType, initialSize, allowGrowth) { }
TR_HashId calculateHash(const void *key) const
{
union
{
float flt;
int32_t intVal;
} x;
x.flt = *(float*)key;
int32_t hash = x.intVal;
return (TR_HashId(hash) %_closedAreaSize);
}
bool isEqual(const void * key1, const void *key2) const { return *(float*)key1 == *(float*)key2;}
bool locate(const float *key,TR_HashId &hashIndex){ return TR_HashTab::locate((const void*)key,hashIndex);}
bool add(float *key,TR_HashId hashIndex,void * data){ return TR_HashTab::add((void *)key,hashIndex,data);}
};
#endif