Skip to content

Commit 9260d4e

Browse files
committed
Fix bugs
1 parent fd40cca commit 9260d4e

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'cn.apisium.shm'
7-
version '0.1.3'
7+
version '0.1.4'
88

99
sourceCompatibility = JavaVersion.VERSION_19
1010
targetCompatibility = JavaVersion.VERSION_19

include/jshm.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ namespace jshm {
5959
}
6060
return new shared_memory(hMapFile, pBuf, size, name);
6161
#elif __linux__ || __APPLE__
62-
auto fd = isCreate ? shm_open(name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR) : shm_open(name, O_RDWR, S_IRUSR | S_IWUSR);
62+
auto mode = O_RDWR;
63+
if (isCreate) mode |= O_CREAT | O_EXCL;
64+
auto fd = shm_open(name, mode, S_IRUSR | S_IWUSR);
6365
if (fd == -1) return nullptr;
6466
if (isCreate && ftruncate(fd, size) == -1) {
6567
close(fd);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
public final class MmapSharedMemory extends AbstractSharedMemory {
1010
private static MemorySession session;
1111
private static MethodHandle shm_open, ftruncate, mmap, munmap, shm_unlink;
12+
private static final int O_CREAT = 0x00000200, O_EXCL = 0x00000800, O_RDWR = 0x0002,
13+
PROT_READ = 0x01, PROT_WRITE = 0x02, MAP_SHARED = 0x01;
1214
@SuppressWarnings("OctalInteger")
13-
private static final int O_CREAT = 0x00000200, O_RDWR = 0x0002, S_IRUSR = 0000400, S_IWUSR = 0000200,
14-
PROT_READ = 0x01, PROT_WRITE = 0x02, MAP_SHARED = 0x01;
15+
private static final short S_IRUSR = 00400, S_IWUSR = 00200;
1516

1617
static {
1718
if (CABI.SYSTEM_TYPE == CABI.SystemType.Unix) {
@@ -21,9 +22,8 @@ public final class MmapSharedMemory extends AbstractSharedMemory {
2122
shm_open = linker.downcallHandle(lookup.lookup("shm_open").orElseThrow(), FunctionDescriptor.of(
2223
ValueLayout.JAVA_INT,
2324
ValueLayout.ADDRESS,
24-
ValueLayout.JAVA_INT,
2525
ValueLayout.JAVA_INT
26-
));
26+
).asVariadic(ValueLayout.JAVA_SHORT));
2727
ftruncate = linker.downcallHandle(lookup.lookup("ftruncate").orElseThrow(), FunctionDescriptor.of(
2828
ValueLayout.JAVA_INT,
2929
ValueLayout.JAVA_INT,
@@ -54,8 +54,8 @@ public MmapSharedMemory(String name, int size, boolean isCreate) throws Throwabl
5454
super(name, size, isCreate);
5555
if (CABI.SYSTEM_TYPE != CABI.SystemType.Unix) throw new UnsupportedOperationException("Only Unix is supported");
5656
int mode = O_RDWR;
57-
if (isCreate) mode |= O_CREAT;
58-
int fd = (int) shm_open.invokeExact((Addressable) session.allocateUtf8String(name), mode, S_IRUSR | S_IWUSR);
57+
if (isCreate) mode |= O_CREAT | O_EXCL;
58+
int fd = (int) shm_open.invokeExact((Addressable) session.allocateUtf8String(name), mode, (short) (S_IRUSR | S_IWUSR));
5959
if (fd == -1) throw new IllegalStateException("shm_open failed.");
6060
try {
6161
if (isCreate && (int) ftruncate.invokeExact(fd, size) == -1) throw new IllegalStateException("ftruncate failed.");

0 commit comments

Comments
 (0)