Merge pull request #13449 from exaucae/BAEL-2407_project-panama
Bael 6060: Project Panama
This commit is contained in:
commit
19b08b7806
|
@ -0,0 +1,49 @@
|
|||
<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">
|
||||
<modelVersion>${project.model.version}</modelVersion>
|
||||
|
||||
<groupId>com.baeldung.java.panama</groupId>
|
||||
<artifactId>java-panama</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>java-panama</name>
|
||||
<url>https://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.model.version>4.0.0</project.model.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.version>1.0</project.version>
|
||||
<maven.compiler.source>19</maven.compiler.source>
|
||||
<maven.compiler.target>19</maven.compiler.target>
|
||||
<maven.compiler.version>3.10.1</maven.compiler.version>
|
||||
<junit.jupiter.version>5.9.0</junit.jupiter.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit.jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven.compiler.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
<compilerArgs>
|
||||
<arg>--add-opens=java.base/java.lang.foreign=ALL-UNNAMED</arg>
|
||||
<arg>--enable-preview</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.java.panama.core;
|
||||
|
||||
import static java.lang.foreign.ValueLayout.ADDRESS;
|
||||
import static java.lang.foreign.ValueLayout.JAVA_INT;
|
||||
|
||||
import java.lang.foreign.FunctionDescriptor;
|
||||
import java.lang.foreign.Linker;
|
||||
import java.lang.foreign.MemorySegment;
|
||||
import java.lang.foreign.MemorySession;
|
||||
import java.lang.foreign.SymbolLookup;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
|
||||
public class Greetings {
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
|
||||
String symbolName = "printf";
|
||||
String greeting = "Hello World from Project Panama Baeldung Article";
|
||||
|
||||
Linker nativeLinker = Linker.nativeLinker();
|
||||
SymbolLookup stdlibLookup = nativeLinker.defaultLookup();
|
||||
SymbolLookup loaderLookup = SymbolLookup.loaderLookup();
|
||||
|
||||
FunctionDescriptor descriptor = FunctionDescriptor.of(JAVA_INT, ADDRESS);
|
||||
|
||||
MethodHandle methodHandle = loaderLookup.lookup(symbolName)
|
||||
.or(() -> stdlibLookup.lookup(symbolName))
|
||||
.map(symbolSegment -> nativeLinker.downcallHandle(symbolSegment, descriptor))
|
||||
.orElse(null);
|
||||
|
||||
if (methodHandle == null) {
|
||||
throw new NoSuchMethodError("Method Handle was not found");
|
||||
}
|
||||
|
||||
try (MemorySession memorySession = MemorySession.openConfined()) {
|
||||
MemorySegment greetingSegment = memorySession.allocateUtf8String(greeting);
|
||||
methodHandle.invoke(greetingSegment);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.java.panama.core;
|
||||
|
||||
import java.lang.foreign.MemorySegment;
|
||||
import java.lang.foreign.MemorySession;
|
||||
import java.lang.foreign.SegmentAllocator;
|
||||
import java.lang.foreign.ValueLayout;
|
||||
|
||||
public class MemoryAllocation {
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
|
||||
try (MemorySession session = MemorySession.openConfined()) {
|
||||
String[] greetingStrings = { "hello", "world", "panama", "baeldung" };
|
||||
SegmentAllocator allocator = SegmentAllocator.implicitAllocator();
|
||||
MemorySegment offHeapSegment = allocator.allocateArray(ValueLayout.ADDRESS, greetingStrings.length);
|
||||
for (int i = 0; i < greetingStrings.length; i++) {
|
||||
// Allocate a string off-heap, then store a pointer to it
|
||||
MemorySegment cString = allocator.allocateUtf8String(greetingStrings[i]);
|
||||
offHeapSegment.setAtIndex(ValueLayout.ADDRESS, i, cString);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package com.baeldung.java.panama.core;
|
||||
|
||||
import static java.lang.foreign.MemoryLayout.sequenceLayout;
|
||||
import static java.lang.foreign.MemoryLayout.structLayout;
|
||||
import static java.lang.foreign.ValueLayout.JAVA_DOUBLE;
|
||||
import static java.lang.foreign.ValueLayout.JAVA_FLOAT;
|
||||
import static java.lang.foreign.ValueLayout.PathElement;
|
||||
|
||||
import java.lang.foreign.GroupLayout;
|
||||
import java.lang.foreign.MemorySegment;
|
||||
import java.lang.foreign.MemorySession;
|
||||
import java.lang.foreign.SequenceLayout;
|
||||
import java.lang.invoke.VarHandle;
|
||||
|
||||
public class MemoryLayout {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
GroupLayout pointLayout = structLayout(JAVA_DOUBLE.withName("x"), JAVA_DOUBLE.withName("y"));
|
||||
|
||||
SequenceLayout ptsLayout = sequenceLayout(10, pointLayout);
|
||||
|
||||
VarHandle xvarHandle = pointLayout.varHandle(PathElement.groupElement("x"));
|
||||
VarHandle yvarHandle = pointLayout.varHandle(PathElement.groupElement("y"));
|
||||
|
||||
try (MemorySession memorySession = MemorySession.openConfined()) {
|
||||
|
||||
MemorySegment pointSegment = memorySession.allocate(pointLayout);
|
||||
xvarHandle.set(pointSegment, 3d);
|
||||
yvarHandle.set(pointSegment, 4d);
|
||||
|
||||
System.out.println(pointSegment.toString());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class ValueLayout {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try (MemorySession memorySession = MemorySession.openConfined()) {
|
||||
int byteSize = 5;
|
||||
int index = 3;
|
||||
float value = 6;
|
||||
MemorySegment segment = MemorySegment.allocateNative(byteSize, memorySession);
|
||||
segment.setAtIndex(JAVA_FLOAT, index, value);
|
||||
float result = segment.getAtIndex(JAVA_FLOAT, index);
|
||||
System.out.println("Float value is:" + result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.java.panama.jextract;
|
||||
|
||||
import java.lang.foreign.MemorySegment;
|
||||
import java.lang.foreign.MemorySession;
|
||||
// Generate JExtract bindings before uncommenting
|
||||
// import static foreign.c.stdio_h.printf;
|
||||
|
||||
public class Greetings {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String greeting = "Hello World from Project Panama Baeldung Article, using JExtract!";
|
||||
|
||||
try (MemorySession memorySession = MemorySession.openConfined()) {
|
||||
MemorySegment greetingSegment = memorySession.allocateUtf8String(greeting);
|
||||
// Generate JExtract bingings before uncommenting
|
||||
// printf(greetingSegment);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
#include <stdio.h>
|
||||
int main() {
|
||||
printf("Hello World from Project Panama Baeldung Article");
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue