-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathomrExampleVM.cpp
123 lines (111 loc) · 3.79 KB
/
omrExampleVM.cpp
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
/*******************************************************************************
* Copyright IBM Corp. and others 2015
*
* 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 http://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
*******************************************************************************/
#include <string.h>
#include "omrExampleVM.hpp"
/**
* Hash table callback for retrieving hash value of an entry
*
* @param[in] entry Entry to hash
* @param[in] userData Data that can be passed along, unused in this callback
*/
uintptr_t
rootTableHashFn(void *entry, void *userData)
{
const char *name = ((RootEntry *)entry)->name;
uintptr_t length = strlen(name);
uintptr_t hash = 0;
uintptr_t i;
for (i = 0; i < length; i++) {
hash = (hash << 5) - hash + name[i];
}
return hash;
}
/**
* Hash table callback for entry comparison
*
* @param[in] leftEntry Left entry to compare
* @param[in] rightEntry Right entry to compare
* @param[in] userData Data that can be passed along, unused in this callback
*
* @return True if the entries are deemed equal, that is if the string representation of their
* keys are identical.
*
*/
uintptr_t
rootTableHashEqualFn(void *leftEntry, void *rightEntry, void *userData)
{
RootEntry *loe = (RootEntry *)leftEntry;
RootEntry *roe = (RootEntry *)rightEntry;
return (0 == strcmp(loe->name, roe->name));
}
/**
* Hash table callback for retrieving hash value of an entry
*
* @param[in] entry Entry to hash
* @param[in] userData Data that can be passed along, unused in this callback
*/
uintptr_t
objectTableHashFn(void *entry, void *userData)
{
const char *name = ((ObjectEntry *)entry)->name;
uintptr_t length = strlen(name);
uintptr_t hash = 0;
uintptr_t i;
for (i = 0; i < length; i++) {
hash = (hash << 5) - hash + name[i];
}
return hash;
}
/**
* Hash table callback for entry comparison
*
* @param[in] leftEntry Left entry to compare
* @param[in] rightEntry Right entry to compare
* @param[in] userData Data that can be passed along, unused in this callback
*
* @return True if the entries are deemed equal, that is if the string representation of their
* keys are identical.
*
*/
uintptr_t
objectTableHashEqualFn(void *leftEntry, void *rightEntry, void *userData)
{
ObjectEntry *loe = (ObjectEntry *)leftEntry;
ObjectEntry *roe = (ObjectEntry *)rightEntry;
return (0 == strcmp(loe->name, roe->name));
}
/**
* Hash table callback used when about to free the hash table, this ensures the memory
* used by the ObjectEntry is freed up (and not just the hash table itself).
*
* @param[in] entry The entry to free
* @param[in] userData Data that can be passed along, unused in this callback
*/
uintptr_t
objectTableFreeFn(void *entry, void *userData)
{
OMR_VM_Example *exampleVM = (OMR_VM_Example *)userData;
OMRPORT_ACCESS_FROM_OMRVM(exampleVM->_omrVM);
ObjectEntry *objEntry = (ObjectEntry *)entry;
omrmem_free_memory((void *)objEntry->name);
objEntry->name = NULL;
return 0;
}