1 /*
 2  * Copyright (c) 2020, 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 "jlong.h"
26 
27 typedef struct {
28     jclass holder;
29     jmethodID mid;
30 } *JNICB;
31 
32 #define CHECK_NULL(thing, message) \
33     if (thing == NULL) { \
34         jclass cls = (*env)->FindClass(env, "java/lang/Exception"); \
35         (*env)->ThrowNew(env, cls, message); \
36         return 0; \
37     }
38 
39 JNIEXPORT jlong JNICALL Java_org_openjdk_bench_jdk_incubator_foreign_Upcalls_makeCB
40   (JNIEnv *env, jclass cls, jstring holderName, jstring methodName, jstring descriptor) {
41 
42   const char* holderNameC = (*env)->GetStringUTFChars(env, holderName, NULL);
43   const char* methodNameC = (*env)->GetStringUTFChars(env, methodName, NULL);
44   const char* descriptorC = (*env)->GetStringUTFChars(env, descriptor, NULL);
45 
46   JNICB cb = malloc(sizeof *cb);
47   CHECK_NULL(cb, "Can not allocate cb");
48 
49   jclass holder = (*env)->FindClass(env, holderNameC);
50   CHECK_NULL(holder, "Can not find class");
51   holder = (jclass) (*env)->NewGlobalRef(env, holder);
52   cb->holder = holder;
53 
54   jmethodID methodID = (*env)->GetStaticMethodID(env, holder, methodNameC, descriptorC);
55   CHECK_NULL(methodID, "Can not find method");
56   //methodID = (jmethodID) (*env)->NewGlobalRef(env, methodID); // DON'T DO THIS! -> Crashes GC
57   cb->mid = methodID;
58 
59   (*env)->ReleaseStringUTFChars(env, holderName, holderNameC);
60   (*env)->ReleaseStringUTFChars(env, methodName, methodNameC);
61   (*env)->ReleaseStringUTFChars(env, descriptor, descriptorC);
62 
63   return ptr_to_jlong(cb);
64 }
65 
66 JNIEXPORT void JNICALL Java_org_openjdk_bench_jdk_incubator_foreign_Upcalls_blank
67   (JNIEnv *env, jclass cls, jlong cb) {
68     JNICB jniCb = jlong_to_ptr(cb);
69     (*env)->CallStaticVoidMethod(env, jniCb->holder, jniCb->mid);
70 }
71 
72 JNIEXPORT jint JNICALL Java_org_openjdk_bench_jdk_incubator_foreign_Upcalls_identity
73   (JNIEnv *env, jclass cls, jint x, jlong cb) {
74     JNICB jniCb = jlong_to_ptr(cb);
75     return (*env)->CallStaticIntMethod(env, jniCb->holder, jniCb->mid, x);
76 }
77 
78 JNIEXPORT jint JNICALL Java_org_openjdk_bench_jdk_incubator_foreign_Upcalls_args5
79   (JNIEnv *env, jclass cls,
80       jlong a0, jdouble a1, jlong a2, jdouble a3, jlong a4,
81       jlong cb) {
82     JNICB jniCb = jlong_to_ptr(cb);
83     return (*env)->CallStaticIntMethod(env, jniCb->holder, jniCb->mid, a0, a1, a2, a3, a4);
84 }
85 
86 JNIEXPORT jint JNICALL Java_org_openjdk_bench_jdk_incubator_foreign_Upcalls_args10
87   (JNIEnv *env, jclass cls,
88       jlong a0, jdouble a1, jlong a2, jdouble a3, jlong a4,
89       jdouble a5, jlong a6, jdouble a7, jlong a8, jdouble a9,
90       jlong cb) {
91     JNICB jniCb = jlong_to_ptr(cb);
92     return (*env)->CallStaticIntMethod(env, jniCb->holder, jniCb->mid, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
93 }