Skip to content

Commit afc3308

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

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Tackling mapped memory segments:
2+
Provide a brief introduction of mapped memory segments and exemplify them in Java code.
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>P164_ArenaMappedMemorySegment</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>P164_ArenaMappedMemorySegment</name>
14+
</project>
Binary file not shown.
Binary file not shown.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package modern.challenge;
2+
3+
import java.io.IOException;
4+
import java.lang.foreign.Arena;
5+
import java.lang.foreign.MemorySegment;
6+
7+
public class Main {
8+
9+
public static void main(String[] args) throws IOException {
10+
11+
try (Arena arena = new MappedArena("readme")) {
12+
13+
MemorySegment segment1 = arena.allocate(100);
14+
MemorySegment segment2 = arena.allocate(50);
15+
16+
segment1.setString(0, "Hello");
17+
segment2.setString(0, "World");
18+
19+
System.out.println(segment1.getString(0)
20+
+ " " + segment2.getString(0));
21+
}
22+
23+
System.out.println("\n\nYou should find the files on disk in the project's root");
24+
}
25+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package modern.challenge;
2+
3+
import java.io.IOException;
4+
import java.lang.foreign.Arena;
5+
import java.lang.foreign.MemorySegment;
6+
import java.nio.channels.FileChannel;
7+
import static java.nio.channels.FileChannel.MapMode.READ_WRITE;
8+
import java.nio.file.Path;
9+
import static java.nio.file.StandardOpenOption.CREATE_NEW;
10+
import static java.nio.file.StandardOpenOption.READ;
11+
import static java.nio.file.StandardOpenOption.SPARSE;
12+
import static java.nio.file.StandardOpenOption.WRITE;
13+
14+
public class MappedArena implements Arena {
15+
16+
private final String fileName;
17+
private final Arena shared;
18+
19+
public MappedArena(String fileName) {
20+
21+
if (fileName == null) {
22+
throw new IllegalArgumentException("The file name cannot be null");
23+
}
24+
25+
this.fileName = fileName;
26+
this.shared = Arena.ofShared();
27+
}
28+
29+
@Override
30+
public MemorySegment allocate(long byteSize, long byteAlignment) {
31+
32+
try (FileChannel file = FileChannel.open(
33+
Path.of(fileName + System.currentTimeMillis() + ".txt"),
34+
CREATE_NEW, SPARSE, READ, WRITE)) {
35+
36+
return file.map(READ_WRITE, 0, byteSize, shared);
37+
38+
} catch (IOException e) {
39+
throw new RuntimeException(e);
40+
}
41+
}
42+
43+
@Override
44+
public MemorySegment.Scope scope() {
45+
return shared.scope();
46+
}
47+
48+
@Override
49+
public void close() {
50+
shared.close();
51+
}
52+
}

0 commit comments

Comments
 (0)