Skip to content

Commit 9dbd8f2

Browse files
committed
Foreign (Function) Memory API
1 parent 41e8101 commit 9dbd8f2

File tree

6 files changed

+145
-0
lines changed

6 files changed

+145
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Introducing memory segment view VarHandle:
2+
Exemplify the usage of MethodHandles.memorySegmentViewVarHandle(ValueLayout layout) for creating a VarHandle that can be used to access a memory segment.
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>P162_IntroMemorySegmentViewVarHandle</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>P162_IntroMemorySegmentViewVarHandle</name>
14+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package modern.challenge;
2+
3+
import java.lang.foreign.Arena;
4+
import java.lang.foreign.MemorySegment;
5+
import java.lang.foreign.ValueLayout;
6+
import java.lang.invoke.MethodHandles;
7+
import java.lang.invoke.VarHandle;
8+
9+
public class Main {
10+
11+
public static void main(String[] args) {
12+
13+
try (Arena arena = Arena.ofConfined()) {
14+
15+
MemorySegment segment = arena.allocate(ValueLayout.JAVA_INT);
16+
17+
System.out.println("Segment size: " + segment.byteSize());
18+
19+
// VarHandle[varType=int, coord=[interface java.lang.foreign.MemorySegment]]
20+
VarHandle pathhandle = ValueLayout.JAVA_INT.varHandle();
21+
pathhandle.set(segment, 0L, 25);
22+
System.out.println("Value: " + pathhandle.get(segment, 0L));
23+
24+
// VarHandle[varType=int, coord=[interface java.lang.foreign.MemorySegment, long]]
25+
VarHandle arrhandle = ValueLayout.JAVA_INT.arrayElementVarHandle();
26+
arrhandle.set(segment,0L, 0, 50);
27+
System.out.println("Value: " + arrhandle.get(segment, 0L, 0L));
28+
29+
// NO LOGER AVAILABLE IN JDK 22
30+
// VarHandle[varType=int, coord=[interface java.lang.foreign.MemorySegment, long]]
31+
/* VarHandle viewhandle = MethodHandles.memorySegmentViewVarHandle(ValueLayout.JAVA_INT);
32+
33+
// insert the coordinates
34+
viewhandle = MethodHandles.insertCoordinates(viewhandle, 1, 0);
35+
36+
viewhandle.set(segment, 75);
37+
System.out.println("Value: " + viewhandle.get(segment));
38+
*/
39+
}
40+
}
41+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Streaming memory segments:
2+
Write several snippets of code for combining memory segments with the Java Stream API.
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>P163_StreamingMemorySegment</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>P163_StreamingMemorySegment</name>
14+
</project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package modern.challenge;
2+
3+
import java.lang.foreign.Arena;
4+
import java.lang.foreign.MemoryLayout;
5+
import java.lang.foreign.MemoryLayout.PathElement;
6+
import java.lang.foreign.MemorySegment;
7+
import java.lang.foreign.SequenceLayout;
8+
import java.lang.foreign.ValueLayout;
9+
import java.lang.invoke.MethodHandle;
10+
import java.lang.invoke.VarHandle;
11+
import java.util.Arrays;
12+
13+
public class Main {
14+
15+
public static void main(String[] args) throws Throwable {
16+
17+
SequenceLayout xy = MemoryLayout.sequenceLayout(2, MemoryLayout.structLayout(
18+
ValueLayout.JAVA_INT.withName("x"),
19+
ValueLayout.JAVA_INT.withName("y")
20+
));
21+
22+
VarHandle xHandle = xy.varHandle(
23+
PathElement.sequenceElement(), PathElement.groupElement("x"));
24+
VarHandle yHandle = xy.varHandle(
25+
PathElement.sequenceElement(), PathElement.groupElement("y"));
26+
27+
try (Arena arena = Arena.ofShared()) {
28+
29+
MemorySegment segment = arena.allocate(xy);
30+
31+
xHandle.set(segment, 0L, 0, 5);
32+
yHandle.set(segment, 0L, 0, 9);
33+
xHandle.set(segment, 0L, 1, 6);
34+
yHandle.set(segment, 0L, 1, 8);
35+
36+
// sum everything
37+
int sum1 = segment.elements(xy)
38+
.map(t -> t.toArray(ValueLayout.JAVA_INT))
39+
.flatMapToInt(t -> Arrays.stream(t))
40+
.sum();
41+
42+
int sum2 = segment.elements(ValueLayout.JAVA_INT).parallel()
43+
.mapToInt(s -> s.get(ValueLayout.JAVA_INT, 0))
44+
.sum();
45+
46+
System.out.println("Sum everything: " + sum1 + " " + sum2);
47+
48+
// sum only the frist pair of x, y
49+
MethodHandle xyHandle = xy.sliceHandle(PathElement.sequenceElement());
50+
51+
MemorySegment subsegment = (MemorySegment) xyHandle.invoke(segment, 0L, 0);
52+
53+
int sum3 = subsegment.elements(ValueLayout.JAVA_INT).parallel()
54+
.mapToInt(s -> s.get(ValueLayout.JAVA_INT, 0))
55+
.sum();
56+
57+
System.out.println("Sum the first pair of (x, y): " + sum3);
58+
59+
// sum y from the first pair with the second pair (x, y)
60+
var sum4 = segment.elements(xy).parallel()
61+
.map(t -> t.asSlice(4).toArray(ValueLayout.JAVA_INT)) // by offset 4 we skip the first x
62+
.flatMapToInt(t -> Arrays.stream(t))
63+
.sum();
64+
65+
System.out.println("Sum y from the first pair with the second pair (x, y): " + sum4);
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)