Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jni/JNIreferences.cpp
41162 views
/*1* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22#include "jni.h"23#include <stdlib.h>24#include "nsk_tools.h"2526extern "C" {272829static jobject* globalReferences = NULL;30static jweak* weakReferences = NULL;3132JNIEXPORT jint JNICALL33Java_nsk_share_ReferringObject_createJNIGlobalReferenceNative(JNIEnv *env,34jobject thisObject, jobject object, jint maxJNIGlobalReferences)35{36jint i;37jint result = -1;3839if (globalReferences == NULL)40{41globalReferences = (jobject*)malloc(sizeof(jobject) * maxJNIGlobalReferences);4243if (globalReferences == NULL)44{45NSK_COMPLAIN0("malloc return NULL\n");46return -1;47}4849for (i = 0; i < maxJNIGlobalReferences; i++)50{51globalReferences[i] = NULL;52}53}5455for (i = 0; i < maxJNIGlobalReferences; i++)56{57jobject reference = globalReferences[i];5859if (reference == NULL)60{61reference = env->NewGlobalRef(object);6263if (reference == NULL)64{65NSK_COMPLAIN0("NewGlobalRef return NULL\n");6667env->ThrowNew(68env->FindClass("nsk/share/TestJNIError"),69"NewGlobalRef return NULL");70}7172globalReferences[i] = reference;7374result = i;7576break;77}78}7980return result;81}8283JNIEXPORT void JNICALL84Java_nsk_share_ReferringObject_deleteJNIGlobalReferenceNative(JNIEnv *env,85jobject thisObject, jint index)86{87jobject reference = globalReferences[index];8889if (reference == NULL)90{91NSK_COMPLAIN1("globalReferences[%d] = NULL, possible wrong index is passed\n", index);9293env->ThrowNew(94env->FindClass("nsk/share/TestBug"),95"Requested globalReferences[] element is NULL, possible wrong index is passed");96}9798env->DeleteGlobalRef(reference);99100globalReferences[index] = NULL;101}102103104JNIEXPORT void JNICALL105Java_nsk_share_ReferringObject_createJNILocalReferenceNative(JNIEnv *env,106jobject thisObject, jobject object, jobject createWicket, jobject deleteWicket)107{108jobject reference = env->NewLocalRef(object);109jclass klass;110111if (reference == NULL)112{113NSK_COMPLAIN0("NewLocalRef return NULL\n");114115env->ThrowNew(116env->FindClass("nsk/share/TestJNIError"),117"NewLocalRef return NULL");118}119120klass = env->GetObjectClass(createWicket);121122// notify another thread that JNI local reference has been created123env->CallVoidMethod(createWicket,124env->GetMethodID(klass, "unlock", "()V"));125126// wait till JNI local reference can be released (it will heppen then we will leave the method)127env->CallVoidMethod(deleteWicket,128env->GetMethodID(klass, "waitFor", "()V"));129}130131JNIEXPORT jint JNICALL132Java_nsk_share_ReferringObject_createJNIWeakReferenceNative(JNIEnv *env,133jobject thisObject, jobject object, jint maxJNIWeakReferences)134{135jint i;136jint result = -1;137138if (weakReferences == NULL)139{140weakReferences = (jweak*)malloc(sizeof(jweak) * maxJNIWeakReferences);141142if (weakReferences == NULL)143{144NSK_COMPLAIN0("malloc return NULL\n");145146return -1;147}148149for (i = 0; i < maxJNIWeakReferences; i++)150{151weakReferences[i] = NULL;152}153}154155for (i = 0; i < maxJNIWeakReferences; i++)156{157jobject reference = weakReferences[i];158159if (reference == NULL)160{161reference = env->NewWeakGlobalRef(object);162163if (reference == NULL)164{165NSK_COMPLAIN0("NewWeakGlobalRef return NULL\n");166167env->ThrowNew(168env->FindClass("nsk/share/TestJNIError"),169"NewWeakGlobalRef return NULL");170}171172weakReferences[i] = reference;173174result = i;175176break;177}178}179180return result;181}182183JNIEXPORT void JNICALL184Java_nsk_share_ReferringObject_deleteJNIWeakReferenceNative(JNIEnv *env,185jobject thisObject, jint index)186{187jweak reference = weakReferences[index];188189if (reference == NULL)190{191NSK_COMPLAIN1("weakReferences[%d] = NULL, possible wrong index is passed\n", index);192193env->ThrowNew(194env->FindClass("nsk/share/TestBug"),195"Requested weakReferences[] element is NULL, possible wrong index is passed");196}197198if (env->IsSameObject(reference, NULL) == JNI_TRUE)199{200NSK_COMPLAIN0("TEST BUG: Weak reference was collected\n");201202env->ThrowNew(203env->FindClass("nsk/share/TestBug"),204"TEST BUG: Weak reference was collected");205}206207env->DeleteWeakGlobalRef(reference);208209weakReferences[index] = NULL;210}211212}213214215