77import java .lang .invoke .MethodHandle ;
88
99public 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