View Javadoc
1   /*
2    * junixsocket
3    *
4    * Copyright 2009-2026 Christian Kohlschütter
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.newsclub.net.unix;
19  
20  import java.io.FileDescriptor;
21  import java.io.IOException;
22  import java.nio.ByteBuffer;
23  
24  /**
25   * Internal class, to be used by junixsocket-memory only.
26   *
27   * @author Christian Kohlschütter
28   */
29  @SuppressWarnings("doclint")
30  public final class MemoryImplUtilInternal {
31    public static final int S_IRUSR = NativeUnixSocket.S_IRUSR;
32    public static final int S_IWUSR = NativeUnixSocket.S_IWUSR;
33    public static final int S_IRGRP = NativeUnixSocket.S_IRGRP;
34    public static final int S_IWGRP = NativeUnixSocket.S_IWGRP;
35    public static final int S_IROTH = NativeUnixSocket.S_IROTH;
36    public static final int S_IWOTH = NativeUnixSocket.S_IWOTH;
37  
38    public static final int MOPT_RDONLY = NativeUnixSocket.MOPT_RDONLY;
39    public static final int MOPT_CREAT = NativeUnixSocket.MOPT_CREAT;
40    public static final int MOPT_EXCL = NativeUnixSocket.MOPT_EXCL;
41  
42    private static final long SHM_ALLOC_SIZE = NativeUnixSocket.sharedMemoryAllocationSize();
43  
44    // FIXME: POSIX leaves the behavior of the combination of O_RDONLY and
45    // O_TRUNC unspecified. On Linux, this will successfully truncate
46    // an existing shared memory object—this may not be so on other UNIX
47    // systems.
48    public static final int MOPT_TRUNC = NativeUnixSocket.MOPT_TRUNC;
49    public static final int MOPT_SEALABLE = NativeUnixSocket.MOPT_SEALABLE;
50    public static final int MOPT_SECRET = NativeUnixSocket.MOPT_SECRET;
51    public static final int MOPT_UNLINK_UPON_CLOSE = NativeUnixSocket.MOPT_UNLINK_UPON_CLOSE;
52  
53    public static final int MMODE_READ = NativeUnixSocket.MMODE_READ;
54    public static final int MMODE_WRITE = NativeUnixSocket.MMODE_WRITE;
55    public static final int MMODE_READ_WRITE = NativeUnixSocket.MMODE_READ
56        | NativeUnixSocket.MMODE_WRITE;
57    public static final int MMODE_COPY_ON_WRITE = NativeUnixSocket.MMODE_COPY_ON_WRITE;
58    public static final int MMODE_SYNC = NativeUnixSocket.MMODE_SYNC;
59    public static final int MMODE_FIXED = NativeUnixSocket.MMODE_FIXED;
60    public static final int MMODE_ANONYMOUS = NativeUnixSocket.MMODE_ANONYMOUS;
61    public static final int MMODE_PLACEHOLDER = NativeUnixSocket.MMODE_PLACEHOLDER;
62  
63    public static final int MADV_NORMAL = NativeUnixSocket.MADV_NORMAL;
64    public static final int MADV_FREE = NativeUnixSocket.MADV_FREE;
65    public static final int MADV_FREE_NOW = NativeUnixSocket.MADV_FREE_NOW;
66    public static final int MADV_WILLNEED = NativeUnixSocket.MADV_WILLNEED;
67    public static final int MADV_DONTNEED = NativeUnixSocket.MADV_DONTNEED;
68  
69    private static final boolean NEED_TO_TRACK_SHM = NativeUnixSocket.needToTrackSharedMemory();
70    private static final boolean FUTEX_INTER_PROCESS = NativeUnixSocket.futexIsInterProcess();
71  
72    MemoryImplUtilInternal() {
73    }
74  
75    public void shmUnlink(String name) throws IOException {
76      if (name != null) {
77        NativeUnixSocket.shmUnlink(name);
78      }
79    }
80  
81    public long shmOpen(FileDescriptor fdOut, String name, long truncateLength, int mode, int options)
82        throws IOException {
83      return NativeUnixSocket.shmOpen(fdOut, name, truncateLength, mode, options);
84    }
85  
86    public void close(FileDescriptor fd) throws IOException {
87      NativeUnixSocket.close(fd);
88    }
89  
90    public long getSharedMemoryAllocationSize() {
91      return SHM_ALLOC_SIZE;
92    }
93  
94    public ByteBuffer mmapShm(Object arenaSegment, FileDescriptor fd, long offset, long length,
95        int mmode, int duplicates) throws IOException {
96      if (offset < 0) {
97        throw new IllegalArgumentException("offset");
98      }
99      if (length < 0) {
100       throw new IllegalArgumentException("length");
101     }
102     if (duplicates < 0) {
103       throw new IllegalArgumentException("duplicates");
104     }
105 
106     return NativeUnixSocket.mmapShm(arenaSegment, fd, offset, length, mmode, duplicates);
107   }
108 
109   public ByteBuffer mappedBuffer(long address, long length, FileDescriptor fdRef, int mmode,
110       Object arenaSegment) throws IOException {
111     return NativeUnixSocket.mappedBuffer(address, length, fdRef, mmode, arenaSegment);
112   }
113 
114   public long mmap(long address, FileDescriptor fdObj, long offset, long length, int mmode,
115       FileDescriptor extraFd) throws IOException {
116     return NativeUnixSocket.mmap(address, fdObj, offset, length, mmode, extraFd);
117   }
118 
119   public void unmap(long address, long byteSize, int duplicates, boolean ignoreError)
120       throws IOException {
121     NativeUnixSocket.unmap(address, byteSize, duplicates, ignoreError);
122   }
123 
124   public void madvise(long address, long byteSize, int madv, boolean ignoreError)
125       throws IOException {
126     NativeUnixSocket.madvise(address, byteSize, madv, ignoreError);
127   }
128 
129   public boolean futexWait(long address, int ifValue, int timeoutMillis) throws IOException {
130     try {
131       return NativeUnixSocket.futexWait(address, ifValue, timeoutMillis);
132     } catch (OperationNotSupportedIOException e) {
133       throw new UnsupportedOperationException("futexWait", e);
134     }
135   }
136 
137   public boolean futexWake(long address, boolean wakeAll) throws IOException {
138     try {
139       return NativeUnixSocket.futexWake(address, wakeAll);
140     } catch (OperationNotSupportedIOException e) {
141       throw new UnsupportedOperationException("futexWake", e);
142     }
143   }
144 
145   public boolean needToTrackSharedMemory() {
146     return NEED_TO_TRACK_SHM;
147   }
148 
149   public long sizeOfSharedMemory(FileDescriptor fd) throws IOException {
150     return NativeUnixSocket.sizeOfSharedMemory(fd);
151   }
152 
153   public boolean futexIsInterProcess() {
154     return FUTEX_INTER_PROCESS;
155   }
156 }