1 /*
 2  * Copyright (c) 2021, 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 
24 #include <jni.h>
25 
26 #include <stdlib.h>
27 
28 #include "jlong.h"
29 #include "JNICB.h"
30 
31 #ifdef _WIN64
32 #define THREAD_LOCAL __declspec(thread)
33 #else
34 #define THREAD_LOCAL __thread
35 #endif
36 
37 THREAD_LOCAL struct {
38   JNICB cb;
39   JNIEnv* env;
40 } ctx_opt;
41 
42 static int comparator(const void* e0, const void* e1) {
43     JNICB jniCb = ctx_opt.cb;
44     JNIEnv* env = ctx_opt.env;
45     jint j0 = *((jint*) e0);
46     jint j1 = *((jint*) e1);
47     return (*env)->CallStaticIntMethod(env, jniCb->holder, jniCb->mid, j0, j1);
48 }
49 
50 JNIEXPORT void JNICALL Java_org_openjdk_bench_jdk_incubator_foreign_QSort_jni_1qsort_1optimized
51         (JNIEnv *env, jclass cls, jintArray arr, jlong cb) {
52 
53     ctx_opt.cb = jlong_to_ptr(cb);
54     ctx_opt.env = env;
55 
56     jint* ints = (*env)->GetIntArrayElements(env, arr, NULL);
57     jsize length = (*env)->GetArrayLength(env, arr);
58 
59     qsort(ints, length, sizeof(jint), &comparator);
60 
61     (*env)->ReleaseIntArrayElements(env, arr, ints, 0);
62 }
63 
64 JavaVM* VM = NULL;
65 
66 int java_cmp(const void *a, const void *b) {
67    int v1 = *((int*)a);
68    int v2 = *((int*)b);
69 
70    JNIEnv* env;
71    (*VM)->GetEnv(VM, (void**) &env, JNI_VERSION_10);
72 
73    jclass qsortClass = (*env)->FindClass(env, "org/openjdk/bench/jdk/incubator/foreign/QSort");
74    jmethodID methodId = (*env)->GetStaticMethodID(env, qsortClass, "jni_upcall_compar", "(II)I");
75 
76    return (*env)->CallStaticIntMethod(env, qsortClass, methodId, v1, v2);
77 }
78 
79 JNIEXPORT void JNICALL Java_org_openjdk_bench_jdk_incubator_foreign_QSort_jni_1qsort_1naive
80         (JNIEnv *env, jclass cls, jintArray arr) {
81     if (VM == NULL) {
82         (*env)->GetJavaVM(env, &VM);
83     }
84 
85     jint* carr = (*env)->GetIntArrayElements(env, arr, 0);
86     jsize length = (*env)->GetArrayLength(env, arr);
87     qsort(carr, length, sizeof(jint), java_cmp);
88     (*env)->ReleaseIntArrayElements(env, arr, carr, 0);
89 }