Skip to content

Commit f3acb64

Browse files
committed
Foreign (Function) Memory API
1 parent 0aa7810 commit f3acb64

File tree

16 files changed

+225
-0
lines changed

16 files changed

+225
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Introducing Foreign Linker API:
2+
Provide a brief description of Foreign Linker API including Linker, SymbolLookup, downcall, and upcall.
3+
4+
## Using JDK 21
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>java.modern.challenge</groupId>
5+
<artifactId>P165_CallingGetpid</artifactId>
6+
<version>1.0</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>22</maven.compiler.source>
11+
<maven.compiler.target>22</maven.compiler.target>
12+
</properties>
13+
<name>P165_CallingGetpid</name>
14+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package modern.challenge;
2+
3+
import java.lang.foreign.FunctionDescriptor;
4+
import java.lang.foreign.Linker;
5+
import java.lang.foreign.MemorySegment;
6+
import java.lang.foreign.SymbolLookup;
7+
import java.lang.foreign.ValueLayout;
8+
import java.lang.invoke.MethodHandle;
9+
10+
public class Main {
11+
12+
public static void main(String[] args) throws Throwable {
13+
14+
// get the Linker of the underlying native platform
15+
// (operating system + processor that runs the JVM)
16+
Linker linker = Linker.nativeLinker();
17+
18+
// "_getpid" is part of the Universal C Runtime (UCRT) Library
19+
SymbolLookup libLookup = linker.defaultLookup();
20+
21+
// find the "_getpid" foreign function
22+
MemorySegment segmentGetpid = libLookup.find("_getpid").get();
23+
24+
// create a method handle for "_getpid"
25+
MethodHandle func = linker.downcallHandle(segmentGetpid,
26+
FunctionDescriptor.of(ValueLayout.JAVA_INT));
27+
28+
// invoke the foreign function, "_getpid" and get the result
29+
int result = (int) func.invokeExact();
30+
System.out.println(result);
31+
}
32+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Calling the sumTwoInt() foreign function:
2+
Write a Java application that calls the sumTwoInt() method (the long sumTwoInt(int x, int y) implemented in Problem 144) via the Foreign Linker API.
3+
4+
## Using JDK 21
2.48 KB
Binary file not shown.
2.28 MB
Binary file not shown.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>java.modern.challenge</groupId>
5+
<artifactId>P166_CallingSumTwoInt</artifactId>
6+
<version>1.0</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>22</maven.compiler.source>
11+
<maven.compiler.target>22</maven.compiler.target>
12+
</properties>
13+
<name>P166_CallingSumTwoInt</name>
14+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package modern.challenge;
2+
3+
import java.lang.foreign.Arena;
4+
import java.lang.foreign.FunctionDescriptor;
5+
import java.lang.foreign.Linker;
6+
import java.lang.foreign.MemorySegment;
7+
import java.lang.foreign.SymbolLookup;
8+
import java.lang.foreign.ValueLayout;
9+
import java.lang.invoke.MethodHandle;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
13+
public class Main {
14+
15+
public static void main(String[] args) throws Throwable {
16+
17+
Linker linker = Linker.nativeLinker();
18+
Path path = Paths.get("lib/cpp/math.dll");
19+
20+
try (Arena arena = Arena.ofConfined()) {
21+
22+
SymbolLookup libLookup = SymbolLookup.libraryLookup(path, arena);
23+
MemorySegment segmentSumTwoInt = libLookup.find("_Z9sumTwoIntii").get();
24+
25+
MethodHandle func = linker.downcallHandle(segmentSumTwoInt,
26+
FunctionDescriptor.of(ValueLayout.JAVA_LONG, ValueLayout.JAVA_INT, ValueLayout.JAVA_INT));
27+
28+
long result = (long) func.invokeExact(3, 9);
29+
30+
System.out.println(result);
31+
}
32+
}
33+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <iostream>
2+
#include "Arithmetic.h"
3+
4+
long sumTwoInt(int x, int y) {
5+
std::cout << "C++: The received arguments are : " << x << " and " << y << std::endl;
6+
return (long)x + (long)y;
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef FUNCTIONS_H_INCLUDED
2+
#define FUNCTIONS_H_INCLUDED
3+
4+
long sumTwoInt(int x, int y); // Function prototype, its declaration
5+
6+
#endif

0 commit comments

Comments
 (0)