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 package org.openjdk.bench.jdk.incubator.foreign;
 24 
 25 import jdk.incubator.foreign.CLinker;
 26 import jdk.incubator.foreign.FunctionDescriptor;
 27 import jdk.incubator.foreign.MemoryLayout;
 28 import jdk.incubator.foreign.MemorySegment;
 29 import jdk.incubator.foreign.NativeSymbol;
 30 import jdk.incubator.foreign.ResourceScope;
 31 import jdk.incubator.foreign.SegmentAllocator;
 32 import jdk.incubator.foreign.SymbolLookup;
 33 
 34 import java.lang.invoke.MethodHandle;
 35 import java.lang.invoke.MethodType;
 36 
 37 import static java.lang.invoke.MethodHandles.insertArguments;
 38 
 39 public class CallOverheadHelper extends CLayouts {
 40 
 41     static final CLinker abi = CLinker.systemCLinker();
 42 
 43     static final MethodHandle func;
 44     static final MethodHandle func_v;
 45     static NativeSymbol func_addr;
 46     static final MethodHandle identity;
 47     static final MethodHandle identity_v;
 48     static NativeSymbol identity_addr;
 49     static final MethodHandle identity_struct;
 50     static final MethodHandle identity_struct_v;
 51     static NativeSymbol identity_struct_addr;
 52     static final MethodHandle identity_struct_3;
 53     static final MethodHandle identity_struct_3_v;
 54     static NativeSymbol identity_struct_3_addr;
 55     static final MethodHandle identity_memory_address;
 56     static final MethodHandle identity_memory_address_v;
 57     static NativeSymbol identity_memory_address_addr;
 58     static final MethodHandle identity_memory_address_3;
 59     static final MethodHandle identity_memory_address_3_v;
 60     static NativeSymbol identity_memory_address_3_addr;
 61     static final MethodHandle args1;
 62     static final MethodHandle args1_v;
 63     static NativeSymbol args1_addr;
 64     static final MethodHandle args2;
 65     static final MethodHandle args2_v;
 66     static NativeSymbol args2_addr;
 67     static final MethodHandle args3;
 68     static final MethodHandle args3_v;
 69     static NativeSymbol args3_addr;
 70     static final MethodHandle args4;
 71     static final MethodHandle args4_v;
 72     static NativeSymbol args4_addr;
 73     static final MethodHandle args5;
 74     static final MethodHandle args5_v;
 75     static NativeSymbol args5_addr;
 76     static final MethodHandle args10;
 77     static final MethodHandle args10_v;
 78     static NativeSymbol args10_addr;
 79 
 80     static final MemoryLayout POINT_LAYOUT = MemoryLayout.structLayout(
 81             C_INT, C_INT
 82     );
 83 
 84     static final MemorySegment sharedPoint = MemorySegment.allocateNative(POINT_LAYOUT, ResourceScope.newSharedScope());
 85     static final MemorySegment confinedPoint = MemorySegment.allocateNative(POINT_LAYOUT, ResourceScope.newConfinedScope());
 86 
 87     static final MemorySegment point = MemorySegment.allocateNative(POINT_LAYOUT, ResourceScope.newImplicitScope());
 88 
 89     static final SegmentAllocator recycling_allocator = SegmentAllocator.prefixAllocator(MemorySegment.allocateNative(POINT_LAYOUT, ResourceScope.newImplicitScope()));
 90 
 91     static {
 92         System.loadLibrary("CallOverheadJNI");
 93 
 94         System.loadLibrary("CallOverhead");
 95         SymbolLookup lookup = SymbolLookup.loaderLookup();
 96         {
 97             func_addr = lookup.lookup("func").orElseThrow();
 98             MethodType mt = MethodType.methodType(void.class);
 99             FunctionDescriptor fd = FunctionDescriptor.ofVoid();
100             func_v = abi.downcallHandle(fd);
101             func = insertArguments(func_v, 0, func_addr);
102         }
103         {
104             identity_addr = lookup.lookup("identity").orElseThrow();
105             FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT);
106             identity_v = abi.downcallHandle(fd);
107             identity = insertArguments(identity_v, 0, identity_addr);
108         }
109         identity_struct_addr = lookup.lookup("identity_struct").orElseThrow();
110         identity_struct_v = abi.downcallHandle(
111                 FunctionDescriptor.of(POINT_LAYOUT, POINT_LAYOUT));
112         identity_struct = insertArguments(identity_struct_v, 0, identity_struct_addr);
113 
114         identity_struct_3_addr = lookup.lookup("identity_struct_3").orElseThrow();
115         identity_struct_3_v = abi.downcallHandle(
116                 FunctionDescriptor.of(POINT_LAYOUT, POINT_LAYOUT, POINT_LAYOUT, POINT_LAYOUT));
117         identity_struct_3 = insertArguments(identity_struct_3_v, 0, identity_struct_3_addr);
118 
119         identity_memory_address_addr = lookup.lookup("identity_memory_address").orElseThrow();
120         identity_memory_address_v = abi.downcallHandle(
121                 FunctionDescriptor.of(C_POINTER, C_POINTER));
122         identity_memory_address = insertArguments(identity_memory_address_v, 0, identity_memory_address_addr);
123 
124         identity_memory_address_3_addr = lookup.lookup("identity_memory_address_3").orElseThrow();
125         identity_memory_address_3_v = abi.downcallHandle(
126                 FunctionDescriptor.of(C_POINTER, C_POINTER, C_POINTER, C_POINTER));
127         identity_memory_address_3 = insertArguments(identity_memory_address_3_v, 0, identity_memory_address_3_addr);
128 
129         args1_addr = lookup.lookup("args1").orElseThrow();
130         args1_v = abi.downcallHandle(
131                 FunctionDescriptor.ofVoid(C_LONG_LONG));
132         args1 = insertArguments(args1_v, 0, args1_addr);
133 
134         args2_addr = lookup.lookup("args2").orElseThrow();
135         args2_v = abi.downcallHandle(
136                 FunctionDescriptor.ofVoid(C_LONG_LONG, C_DOUBLE));
137         args2 = insertArguments(args2_v, 0, args2_addr);
138 
139         args3_addr = lookup.lookup("args3").orElseThrow();
140         args3_v = abi.downcallHandle(
141                 FunctionDescriptor.ofVoid(C_LONG_LONG, C_DOUBLE, C_LONG_LONG));
142         args3 = insertArguments(args3_v, 0, args3_addr);
143 
144         args4_addr = lookup.lookup("args4").orElseThrow();
145         args4_v = abi.downcallHandle(
146                 FunctionDescriptor.ofVoid(C_LONG_LONG, C_DOUBLE, C_LONG_LONG, C_DOUBLE));
147         args4 = insertArguments(args4_v, 0, args4_addr);
148 
149         args5_addr = lookup.lookup("args5").orElseThrow();
150         args5_v = abi.downcallHandle(
151                 FunctionDescriptor.ofVoid(C_LONG_LONG, C_DOUBLE, C_LONG_LONG, C_DOUBLE, C_LONG_LONG));
152         args5 = insertArguments(args5_v, 0, args5_addr);
153 
154         args10_addr = lookup.lookup("args10").orElseThrow();
155         args10_v = abi.downcallHandle(
156                 FunctionDescriptor.ofVoid(C_LONG_LONG, C_DOUBLE, C_LONG_LONG, C_DOUBLE, C_LONG_LONG,
157                                           C_DOUBLE, C_LONG_LONG, C_DOUBLE, C_LONG_LONG, C_DOUBLE));
158         args10 = insertArguments(args10_v, 0, args10_addr);
159     }
160 
161     static native void blank();
162     static native int identity(int x);
163 }