@@ -24,7 +24,7 @@ class ClientConnectionPosix implements ClientConnection {
2424 interface LibCExt extends LibC {
2525 LibCExt INSTANCE = Native .load (LibCExt .class );
2626
27- Pointer mmap (Pointer addr , int length , int prot , int flags , int fd , int offset ) throws LastErrorException ;
27+ Pointer mmap (Pointer addr , long length , int prot , int flags , int fd , long offset ) throws LastErrorException ;
2828 }
2929
3030 interface PosixShm extends Library {
@@ -57,17 +57,26 @@ public void disconnect() {
5757 public WrappedBuffer getGameTable () {
5858 int fd = PosixShm .INSTANCE .shm_open ("/bwapi_shared_memory_game_list" , Fcntl .O_RDWR , 0 );
5959 if (fd < 0 ) throw new IllegalStateException ("SHM not found" );
60- Pointer gameTableView = LibCExt .INSTANCE .mmap (Pointer .NULL , GameTable .SIZE , PROT_READ | PROT_WRITE , MAP_SHARED , fd , 0 );
61- return new WrappedBuffer (gameTableView , GameTable .SIZE );
60+ try {
61+ Pointer gameTableView = LibCExt .INSTANCE .mmap (Pointer .NULL , GameTable .SIZE , PROT_READ | PROT_WRITE , MAP_SHARED , fd , 0 );
62+ return new WrappedBuffer (gameTableView , GameTable .SIZE );
63+ } finally {
64+ LibCExt .INSTANCE .close (fd );
65+ }
6266 }
6367
6468 @ Override
6569 public WrappedBuffer getSharedMemory (int serverProcID ) {
6670 String sharedMemoryName = "/bwapi_shared_memory_" + serverProcID ;
6771 try {
68- Pointer mapFileView = LibCExt .INSTANCE .mmap (Pointer .NULL , ClientData .GameData .SIZE , PROT_READ | PROT_WRITE , MAP_SHARED , LibRT .INSTANCE .shm_open (sharedMemoryName , Fcntl .O_RDWR , 0 ),
69- 0 );
70- return new WrappedBuffer (mapFileView , ClientData .GameData .SIZE );
72+ int fd = PosixShm .INSTANCE .shm_open (sharedMemoryName , Fcntl .O_RDWR , 0 );
73+ if (fd < 0 ) throw new IllegalStateException ("SHM not found" );
74+ try {
75+ Pointer mapFileView = LibCExt .INSTANCE .mmap (Pointer .NULL , ClientData .GameData .SIZE , PROT_READ | PROT_WRITE , MAP_SHARED , fd , 0 );
76+ return new WrappedBuffer (mapFileView , ClientData .GameData .SIZE );
77+ } finally {
78+ LibCExt .INSTANCE .close (fd );
79+ }
7180 } catch (Exception e ) {
7281 throw new SharedMemoryConnectionException (sharedMemoryName , e );
7382 }
0 commit comments