Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/jni/libjnistress006.cpp
41155 views
/*1* Copyright (c) 2007, 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*/2223#include <jni.h>24#include <stdio.h>25#include <stdlib.h>26#include "jnihelper.h"2728extern "C" {2930JNIEXPORT jboolean JNICALL31Java_nsk_stress_jni_JNIter006_refs (JNIEnv *env, jobject jobj, jobject tobj, jint LIMIT) {3233static jobject *globRefsArray = 0;34static int upper = 0;3536jclass clazz;37jmethodID jmethod;38jboolean res=JNI_FALSE;39const char *classname = "nsk/stress/jni/JNIter006";40const char *getmethodname="get_i";41const char *setmethodname="set_i";42const char *getsig="()I";43const char *setsig="(I)V";44const char *setdone = "halt";45const char *setdonesig = "()V";46int i = 0;4748if (upper >= LIMIT) return JNI_TRUE;4950if (upper == 0) {51globRefsArray=(jobject*)(malloc(LIMIT*sizeof(jobject)));52}5354globRefsArray[upper]=env->NewGlobalRef(tobj); CE55if (env->IsSameObject(tobj, globRefsArray[upper])) {56env->DeleteLocalRef(tobj); CE57clazz=env->GetObjectClass(globRefsArray[upper]); CE58}59else {60fprintf(stderr,"Objects are different\n");61env->MonitorExit(jobj); CE62return res;63}64jmethod=env->GetStaticMethodID(clazz, setmethodname, setsig); CE65env->CallStaticVoidMethod(clazz, jmethod, (jint)upper); CE66env->MonitorEnter(jobj); CE67++upper;68res=JNI_TRUE;69env->MonitorExit(jobj); CE70/* If upper == LIMIT than flush ref's array and set */71/* 'done' flag in JNIter006 class to JNI_TRUE */72if (upper == LIMIT) {73fprintf(stderr,"\n\tTotal memory allocated: %zd bytes\n",74LIMIT*sizeof(jobject));75clazz=env->FindClass(classname); CE76jmethod=env->GetMethodID(clazz, setdone, setdonesig); CE77env->CallVoidMethod(jobj, jmethod); CE7879for (i=0;i<LIMIT;i++) {80env->DeleteGlobalRef(globRefsArray[i]); CE81}82free(globRefsArray);83}84return res;85}8687}888990