Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/bcutil/ROMClassHashTable.c
5985 views
1
/*******************************************************************************
2
* Copyright (c) 1991, 2014 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
#include "j9.h"
24
#include "j9port.h"
25
#include "j9protos.h"
26
#include "j9consts.h"
27
#include "bcutil_internal.h"
28
29
typedef struct romClassTableQueryEntry {
30
J9ROMClass *romClass;
31
U_8 *charData;
32
UDATA length;
33
} romClassTableQueryEntry;
34
35
typedef union romClassTableEntry {
36
UDATA tag;
37
J9ROMClass* romClass;
38
} romClassTableEntry;
39
40
static void
41
romClassHashGetName(void *key, const U_8 **name, UDATA *nameLength)
42
{
43
romClassTableEntry *entry = (romClassTableEntry*)key;
44
45
/* We can distinguish between the different types of entries by the first slot.
46
* Query entries always have a NULL romClass field, romClasses have a non-NULL field.
47
*/
48
if (entry->tag == 0) {
49
romClassTableQueryEntry *queryEntry = (romClassTableQueryEntry*)entry;
50
51
*name = queryEntry->charData;
52
*nameLength = queryEntry->length;
53
} else {
54
J9UTF8 *className = J9ROMCLASS_CLASSNAME(entry->romClass);
55
56
*name = J9UTF8_DATA(className);
57
*nameLength = J9UTF8_LENGTH(className);
58
}
59
}
60
61
static UDATA
62
romClassHashEqualFn(void *leftKey, void *rightKey, void *userData)
63
{
64
const U_8 *leftName;
65
UDATA leftLength;
66
const U_8 *rightName;
67
UDATA rightLength;
68
69
romClassHashGetName(leftKey, &leftName, &leftLength);
70
romClassHashGetName(rightKey, &rightName, &rightLength);
71
72
return J9UTF8_DATA_EQUALS(leftName, leftLength, rightName, rightLength);
73
}
74
75
static UDATA
76
romClassHashFn(void *key, void *userData)
77
{
78
const U_8 *name;
79
UDATA length;
80
UDATA hash;
81
UDATA i;
82
83
romClassHashGetName(key, &name, &length);
84
85
hash = 0;
86
for (i = 0; i < length; ++i) {
87
hash = (hash << 5) - hash + name[i];
88
}
89
90
return hash;
91
}
92
93
J9HashTable*
94
romClassHashTableNew(J9JavaVM *vm, U_32 initialSize)
95
{
96
J9HashTable *table;
97
98
table = hashTableNew(OMRPORT_FROM_J9PORT(vm->portLibrary), J9_GET_CALLSITE(), initialSize,
99
sizeof(romClassTableEntry), sizeof(char *), J9HASH_TABLE_ALLOW_SIZE_OPTIMIZATION,
100
J9MEM_CATEGORY_CLASSES, romClassHashFn, romClassHashEqualFn, NULL, vm);
101
return table;
102
}
103
104
void
105
romClassHashTableFree(J9HashTable *hashTable)
106
{
107
hashTableFree(hashTable);
108
}
109
110
UDATA
111
romClassHashTableAdd(J9HashTable *hashTable, J9ROMClass *value)
112
{
113
UDATA result = 1; /* failure */
114
romClassTableEntry entry;
115
116
entry.romClass = value;
117
if (NULL != hashTableAdd(hashTable, &entry)) {
118
result = 0; /* success */
119
}
120
121
return result;
122
}
123
124
J9ROMClass*
125
romClassHashTableFind(J9HashTable *hashTable, U_8 *className, UDATA classNameLength)
126
{
127
romClassTableQueryEntry entry;
128
romClassTableEntry *result;
129
130
entry.romClass = NULL;
131
entry.charData = className;
132
entry.length = classNameLength;
133
result = (romClassTableEntry*)hashTableFind(hashTable, &entry);
134
if (NULL != result) {
135
return result->romClass;
136
} else {
137
return NULL;
138
}
139
}
140
141
void
142
romClassHashTableReplace(J9HashTable *hashTable, J9ROMClass *originalClass, J9ROMClass *replacementClass)
143
{
144
romClassTableEntry *result;
145
romClassTableEntry original;
146
147
original.romClass = originalClass;
148
149
result = hashTableFind(hashTable, &original);
150
if ( (result != NULL) && (result->romClass == originalClass)) {
151
result->romClass = replacementClass;
152
}
153
}
154
155
UDATA
156
romClassHashTableDelete(J9HashTable *hashTable, J9ROMClass *romClass)
157
{
158
romClassTableEntry entry;
159
160
entry.romClass = romClass;
161
return hashTableRemove(hashTable, &entry);
162
}
163
164