1 /*
 2  * Copyright (c) 2024, 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.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package hat.backend;
26 
27 import java.lang.foreign.FunctionDescriptor;
28 import java.lang.foreign.Linker;
29 import java.lang.foreign.MemoryLayout;
30 import java.lang.foreign.SymbolLookup;
31 import java.lang.invoke.MethodHandle;
32 
33 import static java.lang.foreign.ValueLayout.JAVA_BOOLEAN;
34 import static java.lang.foreign.ValueLayout.JAVA_INT;
35 import static java.lang.foreign.ValueLayout.JAVA_LONG;
36 
37 public class NativeLib {
38     final public String name;
39     public final boolean available;
40 
41 
42     final public Linker nativeLinker;
43 
44     final public SymbolLookup loaderLookup;
45 
46     NativeLib(String name) {
47         this.name = name;
48 
49         boolean nonFinalAvailable = true;
50         try {
51             Runtime.getRuntime().loadLibrary(name);
52         } catch (UnsatisfiedLinkError e) {
53             nonFinalAvailable = false;
54         }
55         this.available = nonFinalAvailable;
56 
57         this.nativeLinker = Linker.nativeLinker();
58 
59         this.loaderLookup = SymbolLookup.loaderLookup();
60     }
61 
62 
63     MethodHandle voidFunc(String name, MemoryLayout... args) {
64         return loaderLookup.find(name)
65                 .map(symbolSegment -> nativeLinker.downcallHandle(symbolSegment,
66                         FunctionDescriptor.ofVoid(args)))
67                 .orElse(null);
68     }
69 
70     MethodHandle typedFunc(String name, MemoryLayout returnLayout, MemoryLayout... args) {
71         return loaderLookup.find(name)
72                 .map(symbolSegment -> nativeLinker.downcallHandle(symbolSegment,
73                         FunctionDescriptor.of(returnLayout, args)))
74                 .orElse(null);
75     }
76 
77     MethodHandle longFunc(String name, MemoryLayout... args) {
78         return typedFunc(name, JAVA_LONG, args);
79     }
80 
81     MethodHandle booleanFunc(String name, MemoryLayout... args) {
82         return typedFunc(name, JAVA_BOOLEAN, args);
83     }
84 
85     MethodHandle intFunc(String name, MemoryLayout... args) {
86         return typedFunc(name, JAVA_INT, args);
87     }
88 }