77import java .lang .invoke .MethodHandle ;
88import java .nio .ByteBuffer ;
99
10+ @ SuppressWarnings ("unused" )
1011public 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 ;
0 commit comments