Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jni/JNIreferences.cpp
41162 views
1
/*
2
* Copyright (c) 2006, 2018, 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
#include "jni.h"
24
#include <stdlib.h>
25
#include "nsk_tools.h"
26
27
extern "C" {
28
29
30
static jobject* globalReferences = NULL;
31
static jweak* weakReferences = NULL;
32
33
JNIEXPORT jint JNICALL
34
Java_nsk_share_ReferringObject_createJNIGlobalReferenceNative(JNIEnv *env,
35
jobject thisObject, jobject object, jint maxJNIGlobalReferences)
36
{
37
jint i;
38
jint result = -1;
39
40
if (globalReferences == NULL)
41
{
42
globalReferences = (jobject*)malloc(sizeof(jobject) * maxJNIGlobalReferences);
43
44
if (globalReferences == NULL)
45
{
46
NSK_COMPLAIN0("malloc return NULL\n");
47
return -1;
48
}
49
50
for (i = 0; i < maxJNIGlobalReferences; i++)
51
{
52
globalReferences[i] = NULL;
53
}
54
}
55
56
for (i = 0; i < maxJNIGlobalReferences; i++)
57
{
58
jobject reference = globalReferences[i];
59
60
if (reference == NULL)
61
{
62
reference = env->NewGlobalRef(object);
63
64
if (reference == NULL)
65
{
66
NSK_COMPLAIN0("NewGlobalRef return NULL\n");
67
68
env->ThrowNew(
69
env->FindClass("nsk/share/TestJNIError"),
70
"NewGlobalRef return NULL");
71
}
72
73
globalReferences[i] = reference;
74
75
result = i;
76
77
break;
78
}
79
}
80
81
return result;
82
}
83
84
JNIEXPORT void JNICALL
85
Java_nsk_share_ReferringObject_deleteJNIGlobalReferenceNative(JNIEnv *env,
86
jobject thisObject, jint index)
87
{
88
jobject reference = globalReferences[index];
89
90
if (reference == NULL)
91
{
92
NSK_COMPLAIN1("globalReferences[%d] = NULL, possible wrong index is passed\n", index);
93
94
env->ThrowNew(
95
env->FindClass("nsk/share/TestBug"),
96
"Requested globalReferences[] element is NULL, possible wrong index is passed");
97
}
98
99
env->DeleteGlobalRef(reference);
100
101
globalReferences[index] = NULL;
102
}
103
104
105
JNIEXPORT void JNICALL
106
Java_nsk_share_ReferringObject_createJNILocalReferenceNative(JNIEnv *env,
107
jobject thisObject, jobject object, jobject createWicket, jobject deleteWicket)
108
{
109
jobject reference = env->NewLocalRef(object);
110
jclass klass;
111
112
if (reference == NULL)
113
{
114
NSK_COMPLAIN0("NewLocalRef return NULL\n");
115
116
env->ThrowNew(
117
env->FindClass("nsk/share/TestJNIError"),
118
"NewLocalRef return NULL");
119
}
120
121
klass = env->GetObjectClass(createWicket);
122
123
// notify another thread that JNI local reference has been created
124
env->CallVoidMethod(createWicket,
125
env->GetMethodID(klass, "unlock", "()V"));
126
127
// wait till JNI local reference can be released (it will heppen then we will leave the method)
128
env->CallVoidMethod(deleteWicket,
129
env->GetMethodID(klass, "waitFor", "()V"));
130
}
131
132
JNIEXPORT jint JNICALL
133
Java_nsk_share_ReferringObject_createJNIWeakReferenceNative(JNIEnv *env,
134
jobject thisObject, jobject object, jint maxJNIWeakReferences)
135
{
136
jint i;
137
jint result = -1;
138
139
if (weakReferences == NULL)
140
{
141
weakReferences = (jweak*)malloc(sizeof(jweak) * maxJNIWeakReferences);
142
143
if (weakReferences == NULL)
144
{
145
NSK_COMPLAIN0("malloc return NULL\n");
146
147
return -1;
148
}
149
150
for (i = 0; i < maxJNIWeakReferences; i++)
151
{
152
weakReferences[i] = NULL;
153
}
154
}
155
156
for (i = 0; i < maxJNIWeakReferences; i++)
157
{
158
jobject reference = weakReferences[i];
159
160
if (reference == NULL)
161
{
162
reference = env->NewWeakGlobalRef(object);
163
164
if (reference == NULL)
165
{
166
NSK_COMPLAIN0("NewWeakGlobalRef return NULL\n");
167
168
env->ThrowNew(
169
env->FindClass("nsk/share/TestJNIError"),
170
"NewWeakGlobalRef return NULL");
171
}
172
173
weakReferences[i] = reference;
174
175
result = i;
176
177
break;
178
}
179
}
180
181
return result;
182
}
183
184
JNIEXPORT void JNICALL
185
Java_nsk_share_ReferringObject_deleteJNIWeakReferenceNative(JNIEnv *env,
186
jobject thisObject, jint index)
187
{
188
jweak reference = weakReferences[index];
189
190
if (reference == NULL)
191
{
192
NSK_COMPLAIN1("weakReferences[%d] = NULL, possible wrong index is passed\n", index);
193
194
env->ThrowNew(
195
env->FindClass("nsk/share/TestBug"),
196
"Requested weakReferences[] element is NULL, possible wrong index is passed");
197
}
198
199
if (env->IsSameObject(reference, NULL) == JNI_TRUE)
200
{
201
NSK_COMPLAIN0("TEST BUG: Weak reference was collected\n");
202
203
env->ThrowNew(
204
env->FindClass("nsk/share/TestBug"),
205
"TEST BUG: Weak reference was collected");
206
}
207
208
env->DeleteWeakGlobalRef(reference);
209
210
weakReferences[index] = NULL;
211
}
212
213
}
214
215