Skip to content

Commit f418fab

Browse files
committed
Fix bugs
1 parent 9231fc2 commit f418fab

File tree

6 files changed

+72
-11
lines changed

6 files changed

+72
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ cbuild
44
!gradle/wrapper/gradle-wrapper.jar
55
!**/src/main/**/build/
66
!**/src/test/**/build/
7+
*.log
78

89
### IntelliJ IDEA ###
910
.idea

build.gradle

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ plugins {
66
group 'cn.apisium.shm'
77
version '1.0.0'
88

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

1212
repositories {
1313
mavenCentral()
@@ -28,3 +28,13 @@ publishing {
2828
}
2929
}
3030
}
31+
32+
dependencies {
33+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
34+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
35+
}
36+
37+
test {
38+
useJUnitPlatform()
39+
jvmArgs(['--enable-preview', '--enable-native-access=ALL-UNNAMED'])
40+
}
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-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.1-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

include/jshm.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ namespace jshm {
4141

4242
static shared_memory* init(const char* name, int size, bool isCreate) {
4343
#ifdef _WIN32
44-
auto hMapFile = isCreate ? CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, name) : OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, name);
44+
auto hMapFile = isCreate ? CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT, 0, size, name)
45+
: OpenFileMapping(SECTION_MAP_WRITE | SECTION_MAP_READ, FALSE, name);
4546
if (hMapFile == NULL) return nullptr;
46-
auto pBuf = (LPTSTR)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, size);
47+
auto pBuf = (LPTSTR)MapViewOfFile(hMapFile, SECTION_MAP_WRITE | SECTION_MAP_READ, 0, 0, size);
4748
if (pBuf == NULL) {
4849
CloseHandle(hMapFile);
4950
throw nullptr;

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

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

10+
@SuppressWarnings("unused")
1011
public class WindowsSharedMemory implements SharedMemory {
1112
private static MemorySession session;
1213
private static MethodHandle createFileMapping, openFileMapping, closeHandle, mapViewOfFile, unmapViewOfFile;
14+
private static final int SECTION_QUERY = 0x0001, SECTION_MAP_WRITE = 0x0002, SECTION_MAP_READ = 0x0004,
15+
SECTION_MAP_EXECUTE = 0x0008, SECTION_EXTEND_SIZE = 0x0010, SECTION_MAP_EXECUTE_EXPLICIT = 0x0020,
16+
SECTION_ALL_ACCESS = SECTION_QUERY | SECTION_MAP_WRITE | SECTION_MAP_READ | SECTION_MAP_EXECUTE | SECTION_EXTEND_SIZE | SECTION_MAP_EXECUTE_EXPLICIT;
17+
private static final int PAGE_NOACCESS = 0x01, PAGE_READONLY = 0x02, PAGE_READWRITE = 0x04, PAGE_WRITECOPY = 0x08,
18+
PAGE_EXECUTE = 0x10, PAGE_EXECUTE_READ = 0x20, PAGE_EXECUTE_READWRITE = 0x40, PAGE_EXECUTE_WRITECOPY = 0x80,
19+
PAGE_GUARD = 0x100, PAGE_NOCACHE = 0x200, PAGE_WRITECOMBINE = 0x400;
20+
private static final int SEC_COMMIT = 0x08000000, SEC_LARGE_PAGES = 0x80000000, FILE_MAP_LARGE_PAGES = 0x20000000;
1321

1422
static {
1523
if (CABI.SYSTEM_TYPE == CABI.SystemType.Windows) {
@@ -53,7 +61,6 @@ public class WindowsSharedMemory implements SharedMemory {
5361
private final MemoryAddress hMapFile, pBuf;
5462
private final ByteBuffer buffer;
5563

56-
@SuppressWarnings("unused")
5764
public WindowsSharedMemory(String name, int size) throws Throwable { this(name, size, true); }
5865
public WindowsSharedMemory(String name, int size, boolean isCreate) throws Throwable {
5966
if (CABI.SYSTEM_TYPE != CABI.SystemType.Windows) throw new UnsupportedOperationException("Only Windows is supported");
@@ -62,15 +69,15 @@ public WindowsSharedMemory(String name, int size, boolean isCreate) throws Throw
6269
hMapFile = isCreate ? (MemoryAddress) createFileMapping.invokeExact(
6370
(Addressable) MemoryAddress.ofLong(-1),
6471
(Addressable) MemoryAddress.NULL,
65-
0x04,
72+
PAGE_READWRITE | SEC_COMMIT,
6673
0,
6774
size,
6875
(Addressable) session.allocateUtf8String(name)
69-
) : (MemoryAddress) openFileMapping.invokeExact(0x02, 0, name);
70-
if (hMapFile.toRawLongValue() == 0) throw new IllegalStateException("CreateFileMapping failed");
76+
) : (MemoryAddress) openFileMapping.invokeExact(SECTION_MAP_WRITE | SECTION_MAP_READ, 0, name);
77+
if (hMapFile.toRawLongValue() == 0) throw new IllegalStateException("CreateFileMapping failed.");
7178
try {
72-
pBuf = (MemoryAddress) mapViewOfFile.invokeExact((Addressable) hMapFile, 0x02, 0, 0, 1024);
73-
if (pBuf.toRawLongValue() == 0) throw new IllegalStateException("CreateFileMapping failed");
79+
pBuf = (MemoryAddress) mapViewOfFile.invokeExact((Addressable) hMapFile, SECTION_MAP_WRITE | SECTION_MAP_READ, 0, 0, size);
80+
if (pBuf.toRawLongValue() == 0) throw new IllegalStateException("MapViewOfFile failed.");
7481
} catch (Throwable th) {
7582
closeHandle.invokeExact((Addressable) hMapFile);
7683
throw th;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package cn.apisium.shm;
2+
3+
import org.junit.jupiter.api.AfterAll;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class SharedMemoryTest {
9+
private static SharedMemory shm;
10+
@BeforeAll
11+
public static void init() {
12+
shm = SharedMemory.create("jshm-test", 8192);
13+
}
14+
15+
@Test
16+
public void test() {
17+
var buf = shm.toByteBuffer();
18+
var array = "Hello, World!".getBytes();
19+
buf.put(array);
20+
var array2 = new byte[array.length];
21+
buf.rewind();
22+
buf.get(array2);
23+
Assertions.assertEquals("Hello, World!", new String(array2));
24+
}
25+
26+
@Test
27+
public void testFull() {
28+
var buf = shm.toByteBuffer();
29+
var array = new byte[shm.size()];
30+
for (int i = 0; i < shm.size(); i++) array[i] = (byte)i;
31+
buf.put(array);
32+
var array2 = new byte[shm.size()];
33+
buf.rewind();
34+
buf.get(array2);
35+
Assertions.assertArrayEquals(array, array2);
36+
}
37+
38+
@AfterAll
39+
public static void close() throws Exception {
40+
if (shm != null) shm.close();
41+
}
42+
}

0 commit comments

Comments
 (0)