Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ShirasawaSama committed Oct 25, 2023
1 parent fd40cca commit 9260d4e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'cn.apisium.shm'
version '0.1.3'
version '0.1.4'

sourceCompatibility = JavaVersion.VERSION_19
targetCompatibility = JavaVersion.VERSION_19
Expand Down
4 changes: 3 additions & 1 deletion include/jshm.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ namespace jshm {
}
return new shared_memory(hMapFile, pBuf, size, name);
#elif __linux__ || __APPLE__
auto fd = isCreate ? shm_open(name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR) : shm_open(name, O_RDWR, S_IRUSR | S_IWUSR);
auto mode = O_RDWR;
if (isCreate) mode |= O_CREAT | O_EXCL;
auto fd = shm_open(name, mode, S_IRUSR | S_IWUSR);
if (fd == -1) return nullptr;
if (isCreate && ftruncate(fd, size) == -1) {
close(fd);
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/cn/apisium/shm/impl/MmapSharedMemory.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
public final class MmapSharedMemory extends AbstractSharedMemory {
private static MemorySession session;
private static MethodHandle shm_open, ftruncate, mmap, munmap, shm_unlink;
private static final int O_CREAT = 0x00000200, O_EXCL = 0x00000800, O_RDWR = 0x0002,
PROT_READ = 0x01, PROT_WRITE = 0x02, MAP_SHARED = 0x01;
@SuppressWarnings("OctalInteger")
private static final int O_CREAT = 0x00000200, O_RDWR = 0x0002, S_IRUSR = 0000400, S_IWUSR = 0000200,
PROT_READ = 0x01, PROT_WRITE = 0x02, MAP_SHARED = 0x01;
private static final short S_IRUSR = 00400, S_IWUSR = 00200;

static {
if (CABI.SYSTEM_TYPE == CABI.SystemType.Unix) {
Expand All @@ -21,9 +22,8 @@ public final class MmapSharedMemory extends AbstractSharedMemory {
shm_open = linker.downcallHandle(lookup.lookup("shm_open").orElseThrow(), FunctionDescriptor.of(
ValueLayout.JAVA_INT,
ValueLayout.ADDRESS,
ValueLayout.JAVA_INT,
ValueLayout.JAVA_INT
));
).asVariadic(ValueLayout.JAVA_SHORT));
ftruncate = linker.downcallHandle(lookup.lookup("ftruncate").orElseThrow(), FunctionDescriptor.of(
ValueLayout.JAVA_INT,
ValueLayout.JAVA_INT,
Expand Down Expand Up @@ -54,8 +54,8 @@ public MmapSharedMemory(String name, int size, boolean isCreate) throws Throwabl
super(name, size, isCreate);
if (CABI.SYSTEM_TYPE != CABI.SystemType.Unix) throw new UnsupportedOperationException("Only Unix is supported");
int mode = O_RDWR;
if (isCreate) mode |= O_CREAT;
int fd = (int) shm_open.invokeExact((Addressable) session.allocateUtf8String(name), mode, S_IRUSR | S_IWUSR);
if (isCreate) mode |= O_CREAT | O_EXCL;
int fd = (int) shm_open.invokeExact((Addressable) session.allocateUtf8String(name), mode, (short) (S_IRUSR | S_IWUSR));
if (fd == -1) throw new IllegalStateException("shm_open failed.");
try {
if (isCreate && (int) ftruncate.invokeExact(fd, size) == -1) throw new IllegalStateException("ftruncate failed.");
Expand Down

0 comments on commit 9260d4e

Please sign in to comment.