Skip to content

Commit 350c740

Browse files
committed
Update to java 21
1 parent c0b36b1 commit 350c740

File tree

7 files changed

+50
-56
lines changed

7 files changed

+50
-56
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ plugins {
66
group 'cn.apisium.shm'
77
version = System.getenv('VERSION') ?: '0.0.0'
88

9-
sourceCompatibility = JavaVersion.VERSION_19
10-
targetCompatibility = JavaVersion.VERSION_19
9+
sourceCompatibility = JavaVersion.VERSION_21
10+
targetCompatibility = JavaVersion.VERSION_21
1111

1212
repositories {
1313
mavenCentral()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

src/main/java/cn/apisium/shm/AbstractSharedMemory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import org.jetbrains.annotations.NotNull;
44

5+
import java.lang.foreign.Arena;
56
import java.lang.foreign.MemorySegment;
67
import java.nio.ByteBuffer;
78
import java.nio.ByteOrder;
89

910
@SuppressWarnings("unused")
1011
public abstract class AbstractSharedMemory implements SharedMemory {
12+
private final Arena arena = Arena.ofShared();
1113
private final int size;
1214
private final String name;
1315
protected final boolean isCreate;

src/main/java/cn/apisium/shm/CABI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public enum SystemType { Unknown, Windows, Unix }
1919
static {
2020
var OS = System.getProperty("os.name");
2121
var ARCH = System.getProperty("os.arch");
22-
var ADDRESS_SIZE = ADDRESS.bitSize();
22+
var ADDRESS_SIZE = ADDRESS.byteSize();
2323
if ((ARCH.equals("amd64") || ARCH.equals("x86_64")) && ADDRESS_SIZE == 64) {
2424
SYSTEM_TYPE = OS.startsWith("Windows") ? SystemType.Windows : SystemType.Unknown;
2525
} else if (ARCH.equals("aarch64")) {

src/main/java/cn/apisium/shm/impl/MmapSharedMemory.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.lang.invoke.MethodHandle;
88

99
public final class MmapSharedMemory extends AbstractSharedMemory {
10-
private static MemorySession session;
1110
private static MethodHandle shm_open, ftruncate, mmap, munmap, shm_unlink;
1211
private static final int O_CREAT = 0x00000200, O_EXCL = 0x00000800, O_RDWR = 0x0002,
1312
PROT_READ = 0x01, PROT_WRITE = 0x02, MAP_SHARED = 0x01;
@@ -18,18 +17,18 @@ public final class MmapSharedMemory extends AbstractSharedMemory {
1817
if (CABI.SYSTEM_TYPE == CABI.SystemType.Unix) {
1918
var linker = Linker.nativeLinker();
2019
var lookup = linker.defaultLookup();
21-
session = MemorySession.openImplicit();
22-
shm_open = linker.downcallHandle(lookup.lookup("shm_open").orElseThrow(), FunctionDescriptor.of(
20+
shm_open = linker.downcallHandle(lookup.find("shm_open").orElseThrow(), FunctionDescriptor.of(
2321
ValueLayout.JAVA_INT,
2422
ValueLayout.ADDRESS,
23+
ValueLayout.JAVA_INT,
2524
ValueLayout.JAVA_INT
26-
).asVariadic(ValueLayout.JAVA_SHORT));
27-
ftruncate = linker.downcallHandle(lookup.lookup("ftruncate").orElseThrow(), FunctionDescriptor.of(
25+
), Linker.Option.firstVariadicArg(2));
26+
ftruncate = linker.downcallHandle(lookup.find("ftruncate").orElseThrow(), FunctionDescriptor.of(
2827
ValueLayout.JAVA_INT,
2928
ValueLayout.JAVA_INT,
3029
ValueLayout.JAVA_INT
3130
));
32-
mmap = linker.downcallHandle(lookup.lookup("mmap").orElseThrow(), FunctionDescriptor.of(
31+
mmap = linker.downcallHandle(lookup.find("mmap").orElseThrow(), FunctionDescriptor.of(
3332
ValueLayout.ADDRESS,
3433
ValueLayout.ADDRESS,
3534
ValueLayout.JAVA_INT,
@@ -38,43 +37,41 @@ public final class MmapSharedMemory extends AbstractSharedMemory {
3837
ValueLayout.JAVA_INT,
3938
ValueLayout.JAVA_INT
4039
));
41-
munmap = linker.downcallHandle(lookup.lookup("munmap").orElseThrow(), FunctionDescriptor.ofVoid(
40+
munmap = linker.downcallHandle(lookup.find("munmap").orElseThrow(), FunctionDescriptor.ofVoid(
4241
ValueLayout.ADDRESS,
4342
ValueLayout.JAVA_INT
4443
));
45-
shm_unlink = linker.downcallHandle(lookup.lookup("shm_unlink").orElseThrow(), FunctionDescriptor.ofVoid(
44+
shm_unlink = linker.downcallHandle(lookup.find("shm_unlink").orElseThrow(), FunctionDescriptor.ofVoid(
4645
ValueLayout.ADDRESS
4746
));
4847
}
4948
}
5049

51-
private final MemoryAddress shmAddress;
52-
5350
public MmapSharedMemory(String name, int size, boolean isCreate) throws Throwable {
5451
super(name, size, isCreate);
5552
if (CABI.SYSTEM_TYPE != CABI.SystemType.Unix) throw new UnsupportedOperationException("Only Unix is supported");
5653
int mode = O_RDWR;
5754
if (isCreate) mode |= O_CREAT | O_EXCL;
58-
int fd = (int) shm_open.invokeExact((Addressable) session.allocateUtf8String(name), mode, (short) (S_IRUSR | S_IWUSR));
55+
int fd = (int) shm_open.invokeExact(Arena.ofAuto().allocateUtf8String(name), mode, (int) (S_IRUSR | S_IWUSR));
5956
if (fd == -1) throw new IllegalStateException("shm_open failed.");
6057
try {
6158
if (isCreate && (int) ftruncate.invokeExact(fd, size) == -1) throw new IllegalStateException("ftruncate failed.");
62-
shmAddress = (MemoryAddress) mmap.invokeExact(
63-
(Addressable) MemoryAddress.NULL,
59+
segment = (MemorySegment) mmap.invokeExact(
60+
MemorySegment.NULL,
6461
size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
65-
if (shmAddress.toRawLongValue() == -1) throw new IllegalStateException("mmap failed.");
62+
if (segment.address() == -1) throw new IllegalStateException("mmap failed.");
6663
} catch (Throwable th) {
6764
close();
6865
throw th;
6966
}
70-
segment = MemorySegment.ofAddress(shmAddress, size, session);
67+
segment = segment.reinterpret(size);
7168
}
7269

7370
@Override
7471
public void close() throws Exception {
7572
try {
76-
if (shmAddress != null) munmap.invokeExact((Addressable) shmAddress, getSize());
77-
if (isCreate) shm_unlink.invokeExact((Addressable) session.allocateUtf8String(getName()));
73+
if (segment != null) munmap.invokeExact(segment, getSize());
74+
if (isCreate) shm_unlink.invokeExact(Arena.ofAuto().allocateUtf8String(getName()));
7875
} catch (Throwable throwable) {
7976
throw new Exception(throwable);
8077
}

src/main/java/cn/apisium/shm/impl/SystemVSharedMemory.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
@SuppressWarnings("unused")
1010
public final class SystemVSharedMemory extends AbstractSharedMemory {
11-
private static MemorySession session;
1211
private static MethodHandle ftok, shmget, shmat, shmdt, shmctl;
1312
@SuppressWarnings("OctalInteger")
1413
private static final int IPC_CREAT = 001000, IPC_RMID = 0, IPC_R = 000400, IPC_W = 000200;
@@ -17,61 +16,59 @@ public final class SystemVSharedMemory extends AbstractSharedMemory {
1716
if (CABI.SYSTEM_TYPE == CABI.SystemType.Unix) {
1817
var linker = Linker.nativeLinker();
1918
var lookup = linker.defaultLookup();
20-
session = MemorySession.openImplicit();
21-
ftok = linker.downcallHandle(lookup.lookup("ftok").orElseThrow(), FunctionDescriptor.of(
19+
ftok = linker.downcallHandle(lookup.find("ftok").orElseThrow(), FunctionDescriptor.of(
2220
ValueLayout.JAVA_INT,
2321
ValueLayout.ADDRESS,
2422
ValueLayout.JAVA_INT
2523
));
26-
shmget = linker.downcallHandle(lookup.lookup("shmget").orElseThrow(), FunctionDescriptor.of(
24+
shmget = linker.downcallHandle(lookup.find("shmget").orElseThrow(), FunctionDescriptor.of(
2725
ValueLayout.JAVA_INT,
2826
ValueLayout.JAVA_INT,
2927
ValueLayout.JAVA_INT,
3028
ValueLayout.JAVA_INT
3129
));
32-
shmat = linker.downcallHandle(lookup.lookup("shmat").orElseThrow(), FunctionDescriptor.of(
30+
shmat = linker.downcallHandle(lookup.find("shmat").orElseThrow(), FunctionDescriptor.of(
3331
ValueLayout.ADDRESS,
3432
ValueLayout.JAVA_INT,
3533
ValueLayout.ADDRESS,
3634
ValueLayout.JAVA_INT
3735
));
38-
shmdt = linker.downcallHandle(lookup.lookup("shmdt").orElseThrow(), FunctionDescriptor.ofVoid(
36+
shmdt = linker.downcallHandle(lookup.find("shmdt").orElseThrow(), FunctionDescriptor.ofVoid(
3937
ValueLayout.ADDRESS
4038
));
41-
shmctl = linker.downcallHandle(lookup.lookup("shmctl").orElseThrow(), FunctionDescriptor.ofVoid(
39+
shmctl = linker.downcallHandle(lookup.find("shmctl").orElseThrow(), FunctionDescriptor.ofVoid(
4240
ValueLayout.JAVA_INT,
4341
ValueLayout.JAVA_INT,
4442
ValueLayout.ADDRESS
4543
));
4644
}
4745
}
4846
private final int shmId;
49-
private final MemoryAddress shmAddress;
5047

5148
public SystemVSharedMemory(String name, int size, boolean isCreate) throws Throwable {
5249
super(name, size, isCreate);
5350
if (CABI.SYSTEM_TYPE != CABI.SystemType.Unix) throw new UnsupportedOperationException("Only Unix is supported");
54-
int shmKey = (int) ftok.invokeExact((Addressable) session.allocateUtf8String(name), 0);
51+
int shmKey = (int) ftok.invokeExact(Arena.ofAuto().allocateUtf8String(name), 0);
5552
if (shmKey == -1) throw new IllegalStateException("ftok failed.");
5653
try {
5754
int shmflg = IPC_R | IPC_W;
5855
if (isCreate) shmflg |= IPC_CREAT;
5956
shmId = (int) shmget.invokeExact(shmKey, size, shmflg);
6057
if (shmId == -1) throw new IllegalStateException("shmget failed.");
61-
shmAddress = (MemoryAddress) shmat.invokeExact(shmId, (Addressable) MemoryAddress.NULL, 0);
62-
if (shmAddress.toRawLongValue() == -1) throw new IllegalStateException("shmat failed.");
58+
segment = (MemorySegment) shmat.invokeExact(shmId, MemorySegment.NULL, 0);
59+
if (segment.address() == -1) throw new IllegalStateException("shmat failed.");
6360
} catch (Throwable th) {
6461
close();
6562
throw th;
6663
}
67-
segment = MemorySegment.ofAddress(shmAddress, size, session);
64+
segment = segment.reinterpret(size);
6865
}
6966

7067
@Override
7168
public void close() throws Exception {
7269
try {
73-
if (shmAddress != null) shmdt.invokeExact((Addressable) shmAddress);
74-
if (shmId != -1 && isCreate) shmctl.invokeExact(shmId, IPC_RMID, (Addressable) MemoryAddress.NULL);
70+
if (segment != null) shmdt.invokeExact(segment);
71+
if (shmId != -1 && isCreate) shmctl.invokeExact(shmId, IPC_RMID, MemorySegment.NULL);
7572
} catch (Throwable throwable) {
7673
throw new Exception(throwable);
7774
}

src/main/java/cn/apisium/shm/impl/WindowsSharedMemory.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.lang.invoke.MethodHandle;
88

99
public final class WindowsSharedMemory extends AbstractSharedMemory {
10-
private static MemorySession session;
1110
private static MethodHandle createFileMapping, openFileMapping, closeHandle, mapViewOfFile, unmapViewOfFile;
1211
@SuppressWarnings("unused")
1312
private static final int SECTION_QUERY = 0x0001, SECTION_MAP_WRITE = 0x0002, SECTION_MAP_READ = 0x0004,
@@ -23,9 +22,8 @@ public final class WindowsSharedMemory extends AbstractSharedMemory {
2322
static {
2423
if (CABI.SYSTEM_TYPE == CABI.SystemType.Windows) {
2524
var linker = Linker.nativeLinker();
26-
session = MemorySession.openImplicit();
27-
var kernel = SymbolLookup.libraryLookup("kernel32.dll", session);
28-
createFileMapping = linker.downcallHandle(kernel.lookup("CreateFileMappingA").orElseThrow(), FunctionDescriptor.of(
25+
var kernel = SymbolLookup.libraryLookup("kernel32.dll", Arena.global());
26+
createFileMapping = linker.downcallHandle(kernel.find("CreateFileMappingA").orElseThrow(), FunctionDescriptor.of(
2927
ValueLayout.ADDRESS,
3028
ValueLayout.ADDRESS,
3129
ValueLayout.ADDRESS,
@@ -34,57 +32,57 @@ public final class WindowsSharedMemory extends AbstractSharedMemory {
3432
ValueLayout.JAVA_INT,
3533
ValueLayout.ADDRESS
3634
));
37-
openFileMapping = linker.downcallHandle(kernel.lookup("OpenFileMappingA").orElseThrow(), FunctionDescriptor.of(
35+
openFileMapping = linker.downcallHandle(kernel.find("OpenFileMappingA").orElseThrow(), FunctionDescriptor.of(
3836
ValueLayout.ADDRESS,
3937
ValueLayout.JAVA_INT,
4038
ValueLayout.JAVA_INT,
4139
ValueLayout.ADDRESS
4240
));
43-
closeHandle = linker.downcallHandle(kernel.lookup("CloseHandle").orElseThrow(), FunctionDescriptor.ofVoid(
41+
closeHandle = linker.downcallHandle(kernel.find("CloseHandle").orElseThrow(), FunctionDescriptor.ofVoid(
4442
ValueLayout.ADDRESS
4543
));
46-
mapViewOfFile = linker.downcallHandle(kernel.lookup("MapViewOfFile").orElseThrow(), FunctionDescriptor.of(
44+
mapViewOfFile = linker.downcallHandle(kernel.find("MapViewOfFile").orElseThrow(), FunctionDescriptor.of(
4745
ValueLayout.ADDRESS,
4846
ValueLayout.ADDRESS,
4947
ValueLayout.JAVA_INT,
5048
ValueLayout.JAVA_INT,
5149
ValueLayout.JAVA_INT,
5250
ValueLayout.JAVA_INT
5351
));
54-
unmapViewOfFile = linker.downcallHandle(kernel.lookup("UnmapViewOfFile").orElseThrow(), FunctionDescriptor.ofVoid(
52+
unmapViewOfFile = linker.downcallHandle(kernel.find("UnmapViewOfFile").orElseThrow(), FunctionDescriptor.ofVoid(
5553
ValueLayout.ADDRESS
5654
));
5755
}
5856
}
59-
private final MemoryAddress hMapFile, pBuf;
57+
private final MemorySegment hMapFile;
6058

6159
public WindowsSharedMemory(String name, int size, boolean isCreate) throws Throwable {
6260
super(name, size, isCreate);
6361
if (CABI.SYSTEM_TYPE != CABI.SystemType.Windows) throw new UnsupportedOperationException("Only Windows is supported");
64-
hMapFile = isCreate ? (MemoryAddress) createFileMapping.invokeExact(
65-
(Addressable) MemoryAddress.ofLong(-1),
66-
(Addressable) MemoryAddress.NULL,
62+
hMapFile = isCreate ? (MemorySegment) createFileMapping.invokeExact(
63+
(MemorySegment) MemorySegment.ofAddress(-1),
64+
MemorySegment.NULL,
6765
PAGE_READWRITE | SEC_COMMIT,
6866
0,
6967
size,
70-
(Addressable) session.allocateUtf8String(name)
71-
) : (MemoryAddress) openFileMapping.invokeExact(SECTION_MAP_WRITE | SECTION_MAP_READ, 0, name);
72-
if (hMapFile.toRawLongValue() == 0) throw new IllegalStateException("CreateFileMapping failed.");
68+
(MemorySegment) Arena.ofAuto().allocateUtf8String(name)
69+
) : (MemorySegment) openFileMapping.invokeExact(SECTION_MAP_WRITE | SECTION_MAP_READ, 0, name);
70+
if (hMapFile.address() == 0) throw new IllegalStateException("CreateFileMapping failed.");
7371
try {
74-
pBuf = (MemoryAddress) mapViewOfFile.invokeExact((Addressable) hMapFile, SECTION_MAP_WRITE | SECTION_MAP_READ, 0, 0, size);
75-
if (pBuf.toRawLongValue() == 0) throw new IllegalStateException("MapViewOfFile failed.");
72+
segment = (MemorySegment) mapViewOfFile.invokeExact(hMapFile, SECTION_MAP_WRITE | SECTION_MAP_READ, 0, 0, size);
73+
if (segment.address() == 0) throw new IllegalStateException("MapViewOfFile failed.");
7674
} catch (Throwable th) {
77-
closeHandle.invokeExact((Addressable) hMapFile);
75+
closeHandle.invokeExact(hMapFile);
7876
throw th;
7977
}
80-
segment = MemorySegment.ofAddress(pBuf, size, session);
78+
segment = segment.reinterpret(size);
8179
}
8280

8381
@Override
8482
public void close() throws Exception {
8583
try {
86-
unmapViewOfFile.invokeExact((Addressable) pBuf);
87-
closeHandle.invokeExact((Addressable) hMapFile);
84+
if (segment != null) unmapViewOfFile.invokeExact(segment);
85+
if (hMapFile != null) closeHandle.invokeExact(hMapFile);
8886
} catch (Throwable throwable) {
8987
throw new Exception(throwable);
9088
}

0 commit comments

Comments
 (0)