Skip to content

Commit afbefda

Browse files
committed
Expose MemorySegment
1 parent 50b7e8f commit afbefda

File tree

6 files changed

+43
-16
lines changed

6 files changed

+43
-16
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,10 @@ main.cpp:
6161
#include <jshm.h>
6262

6363
int main() {
64-
auto shm = jshm::shared_memory::create("test", 1024);
65-
// auto shm = jshm::shared_memory::open("test", 1024);
66-
auto buf = (char*)shm;
67-
strcpy(buf, "Hello, World!");
68-
delete shm;
64+
auto shm = jshm::shared_memory::create("test", 1024);
65+
// auto shm = jshm::shared_memory::open("test", 1024);
66+
strcpy((char*)shm->address(), "Hello, World!");
67+
delete shm;
6968
}
7069
```
7170

build.gradle

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

66
group 'cn.apisium.shm'
7-
version '1.0.0'
7+
version '0.0.1'
88

99
sourceCompatibility = JavaVersion.VERSION_19
1010
targetCompatibility = JavaVersion.VERSION_19
@@ -30,6 +30,7 @@ publishing {
3030
}
3131

3232
dependencies {
33+
implementation 'org.jetbrains:annotations:24.0.0'
3334
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
3435
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
3536
}

include/jshm.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
namespace jshm {
1010
class shared_memory {
1111
public:
12-
int size() { return _size; }
13-
const char* name() { return _name; }
12+
int size() const noexcept { return _size; }
13+
const char* name() const noexcept { return _name; }
1414

1515
~shared_memory() {
1616
#ifdef _WIN32
@@ -19,7 +19,7 @@ namespace jshm {
1919
#endif
2020
}
2121

22-
operator void* () {
22+
void* address() const noexcept {
2323
#ifdef _WIN32
2424
return pBuf;
2525
#endif

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cn.apisium.shm;
22

3+
import org.jetbrains.annotations.NotNull;
4+
35
import static java.lang.foreign.ValueLayout.ADDRESS;
46

57
public class CABI {
@@ -11,6 +13,7 @@ public enum SystemType { Unknown, Windows, Linux, MacOS }
1113
/**
1214
* The current system type.
1315
*/
16+
@NotNull
1417
public static final SystemType SYSTEM_TYPE;
1518

1619
static {

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package cn.apisium.shm;
22

33
import cn.apisium.shm.impl.WindowsSharedMemory;
4+
import org.jetbrains.annotations.Contract;
5+
import org.jetbrains.annotations.NotNull;
46

7+
import java.lang.foreign.MemorySegment;
58
import java.nio.ByteBuffer;
69

710
/**
@@ -13,18 +16,31 @@ public interface SharedMemory extends AutoCloseable {
1316
* Get the shared memory as a {@link ByteBuffer}.
1417
* @return The shared memory as a {@link ByteBuffer}.
1518
*/
19+
@NotNull
20+
@Contract(pure = true)
1621
ByteBuffer toByteBuffer();
1722

23+
/**
24+
* Get the shared memory as a {@link MemorySegment}.
25+
* @return The shared memory as a {@link MemorySegment}.
26+
*/
27+
@NotNull
28+
@Contract(pure = true)
29+
MemorySegment getMemorySegment();
30+
1831
/**
1932
* Get the size of the shared memory.
2033
* @return The size of the shared memory.
2134
*/
35+
@Contract(pure = true)
2236
int size();
2337

2438
/**
2539
* Get the name of the shared memory.
2640
* @return The name of the shared memory.
2741
*/
42+
@NotNull
43+
@Contract(pure = true)
2844
String getName();
2945

3046
/**
@@ -33,7 +49,8 @@ public interface SharedMemory extends AutoCloseable {
3349
* @param size The size of the shared memory.
3450
* @return The shared memory.
3551
*/
36-
static SharedMemory open(String name, int size) {
52+
@NotNull
53+
static SharedMemory open(@NotNull String name, int size) {
3754
return init(name, size, false);
3855
}
3956

@@ -43,19 +60,22 @@ static SharedMemory open(String name, int size) {
4360
* @param size The size of the shared memory.
4461
* @return The shared memory.
4562
*/
46-
static SharedMemory create(String name, int size) {
63+
@NotNull
64+
static SharedMemory create(@NotNull String name, int size) {
4765
return init(name, size, true);
4866
}
4967

5068
/**
5169
* Check if the current system is supported.
5270
* @return If the current system is supported.
5371
*/
72+
@Contract(pure = true)
5473
static boolean isSupported() {
5574
return CABI.SYSTEM_TYPE == CABI.SystemType.Windows;
5675
}
5776

58-
private static SharedMemory init(String name, int size, boolean isCreate) {
77+
@NotNull
78+
private static SharedMemory init(@NotNull String name, int size, boolean isCreate) {
5979
try {
6080
return switch (CABI.SYSTEM_TYPE) {
6181
case Windows -> new WindowsSharedMemory(name, size, isCreate);

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cn.apisium.shm.CABI;
44
import cn.apisium.shm.SharedMemory;
5+
import org.jetbrains.annotations.NotNull;
56

67
import java.lang.foreign.*;
78
import java.lang.invoke.MethodHandle;
@@ -59,7 +60,7 @@ public class WindowsSharedMemory implements SharedMemory {
5960
private final int size;
6061
private final String name;
6162
private final MemoryAddress hMapFile, pBuf;
62-
private final ByteBuffer buffer;
63+
private final MemorySegment segment;
6364

6465
public WindowsSharedMemory(String name, int size) throws Throwable { this(name, size, true); }
6566
public WindowsSharedMemory(String name, int size, boolean isCreate) throws Throwable {
@@ -82,17 +83,20 @@ public WindowsSharedMemory(String name, int size, boolean isCreate) throws Throw
8283
closeHandle.invokeExact((Addressable) hMapFile);
8384
throw th;
8485
}
85-
buffer = MemorySegment.ofAddress(pBuf, size, session).asByteBuffer();
86+
segment = MemorySegment.ofAddress(pBuf, size, session);
8687
}
8788

8889
@Override
89-
public ByteBuffer toByteBuffer() { return buffer.duplicate(); }
90+
public @NotNull MemorySegment getMemorySegment() { return segment; }
91+
92+
@Override
93+
public @NotNull ByteBuffer toByteBuffer() { return segment.asByteBuffer(); }
9094

9195
@Override
9296
public int size() { return size; }
9397

9498
@Override
95-
public String getName() { return name; }
99+
public @NotNull String getName() { return name; }
96100

97101
@Override
98102
public void close() throws Exception {

0 commit comments

Comments
 (0)