author | simonis |
Mon, 20 Jan 2014 09:24:25 +0100 | |
changeset 22604 | 9b394795e216 |
parent 19607 | bee007586d06 |
child 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
14025
diff
changeset
|
2 |
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.nio.ch; |
|
27 |
||
28 |
import java.lang.ref.SoftReference; |
|
29 |
import java.lang.reflect.*; |
|
30 |
import java.io.IOException; |
|
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6301
diff
changeset
|
31 |
import java.io.FileDescriptor; |
2 | 32 |
import java.nio.ByteBuffer; |
33 |
import java.nio.MappedByteBuffer; |
|
34 |
import java.nio.channels.*; |
|
35 |
import java.security.AccessController; |
|
36 |
import java.security.PrivilegedAction; |
|
37 |
import java.util.*; |
|
38 |
import sun.misc.Unsafe; |
|
39 |
import sun.misc.Cleaner; |
|
40 |
import sun.security.action.GetPropertyAction; |
|
41 |
||
42 |
||
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10882
diff
changeset
|
43 |
public class Util { |
2 | 44 |
|
45 |
// -- Caches -- |
|
46 |
||
47 |
// The number of temp buffers in our pool |
|
13024
ada1a7c54e84
7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents:
12559
diff
changeset
|
48 |
private static final int TEMP_BUF_POOL_SIZE = IOUtil.IOV_MAX; |
2 | 49 |
|
6301 | 50 |
// Per-thread cache of temporary direct buffers |
51 |
private static ThreadLocal<BufferCache> bufferCache = |
|
52 |
new ThreadLocal<BufferCache>() |
|
53 |
{ |
|
54 |
@Override |
|
55 |
protected BufferCache initialValue() { |
|
56 |
return new BufferCache(); |
|
57 |
} |
|
58 |
}; |
|
59 |
||
60 |
/** |
|
61 |
* A simple cache of direct buffers. |
|
62 |
*/ |
|
63 |
private static class BufferCache { |
|
64 |
// the array of buffers |
|
65 |
private ByteBuffer[] buffers; |
|
2 | 66 |
|
6301 | 67 |
// the number of buffers in the cache |
68 |
private int count; |
|
69 |
||
70 |
// the index of the first valid buffer (undefined if count == 0) |
|
71 |
private int start; |
|
72 |
||
73 |
private int next(int i) { |
|
74 |
return (i + 1) % TEMP_BUF_POOL_SIZE; |
|
75 |
} |
|
76 |
||
77 |
BufferCache() { |
|
78 |
buffers = new ByteBuffer[TEMP_BUF_POOL_SIZE]; |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* Removes and returns a buffer from the cache of at least the given |
|
83 |
* size (or null if no suitable buffer is found). |
|
84 |
*/ |
|
85 |
ByteBuffer get(int size) { |
|
86 |
if (count == 0) |
|
87 |
return null; // cache is empty |
|
895 | 88 |
|
6301 | 89 |
ByteBuffer[] buffers = this.buffers; |
2 | 90 |
|
6301 | 91 |
// search for suitable buffer (often the first buffer will do) |
92 |
ByteBuffer buf = buffers[start]; |
|
93 |
if (buf.capacity() < size) { |
|
94 |
buf = null; |
|
95 |
int i = start; |
|
96 |
while ((i = next(i)) != start) { |
|
97 |
ByteBuffer bb = buffers[i]; |
|
98 |
if (bb == null) |
|
99 |
break; |
|
100 |
if (bb.capacity() >= size) { |
|
101 |
buf = bb; |
|
102 |
break; |
|
103 |
} |
|
104 |
} |
|
105 |
if (buf == null) |
|
106 |
return null; |
|
107 |
// move first element to here to avoid re-packing |
|
108 |
buffers[i] = buffers[start]; |
|
109 |
} |
|
110 |
||
111 |
// remove first element |
|
112 |
buffers[start] = null; |
|
113 |
start = next(start); |
|
114 |
count--; |
|
115 |
||
116 |
// prepare the buffer and return it |
|
117 |
buf.rewind(); |
|
118 |
buf.limit(size); |
|
119 |
return buf; |
|
120 |
} |
|
121 |
||
122 |
boolean offerFirst(ByteBuffer buf) { |
|
123 |
if (count >= TEMP_BUF_POOL_SIZE) { |
|
124 |
return false; |
|
125 |
} else { |
|
126 |
start = (start + TEMP_BUF_POOL_SIZE - 1) % TEMP_BUF_POOL_SIZE; |
|
127 |
buffers[start] = buf; |
|
128 |
count++; |
|
129 |
return true; |
|
2 | 130 |
} |
131 |
} |
|
132 |
||
6301 | 133 |
boolean offerLast(ByteBuffer buf) { |
134 |
if (count >= TEMP_BUF_POOL_SIZE) { |
|
135 |
return false; |
|
136 |
} else { |
|
137 |
int next = (start + count) % TEMP_BUF_POOL_SIZE; |
|
138 |
buffers[next] = buf; |
|
139 |
count++; |
|
140 |
return true; |
|
2 | 141 |
} |
142 |
} |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
895
diff
changeset
|
143 |
|
6301 | 144 |
boolean isEmpty() { |
145 |
return count == 0; |
|
146 |
} |
|
147 |
||
148 |
ByteBuffer removeFirst() { |
|
149 |
assert count > 0; |
|
150 |
ByteBuffer buf = buffers[start]; |
|
151 |
buffers[start] = null; |
|
152 |
start = next(start); |
|
153 |
count--; |
|
154 |
return buf; |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
/** |
|
159 |
* Returns a temporary buffer of at least the given size |
|
160 |
*/ |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10882
diff
changeset
|
161 |
public static ByteBuffer getTemporaryDirectBuffer(int size) { |
6301 | 162 |
BufferCache cache = bufferCache.get(); |
163 |
ByteBuffer buf = cache.get(size); |
|
164 |
if (buf != null) { |
|
165 |
return buf; |
|
166 |
} else { |
|
167 |
// No suitable buffer in the cache so we need to allocate a new |
|
168 |
// one. To avoid the cache growing then we remove the first |
|
169 |
// buffer from the cache and free it. |
|
170 |
if (!cache.isEmpty()) { |
|
171 |
buf = cache.removeFirst(); |
|
172 |
free(buf); |
|
173 |
} |
|
174 |
return ByteBuffer.allocateDirect(size); |
|
175 |
} |
|
176 |
} |
|
177 |
||
178 |
/** |
|
179 |
* Releases a temporary buffer by returning to the cache or freeing it. |
|
180 |
*/ |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10882
diff
changeset
|
181 |
public static void releaseTemporaryDirectBuffer(ByteBuffer buf) { |
6301 | 182 |
offerFirstTemporaryDirectBuffer(buf); |
183 |
} |
|
184 |
||
185 |
/** |
|
186 |
* Releases a temporary buffer by returning to the cache or freeing it. If |
|
187 |
* returning to the cache then insert it at the start so that it is |
|
188 |
* likely to be returned by a subsequent call to getTemporaryDirectBuffer. |
|
189 |
*/ |
|
190 |
static void offerFirstTemporaryDirectBuffer(ByteBuffer buf) { |
|
191 |
assert buf != null; |
|
192 |
BufferCache cache = bufferCache.get(); |
|
193 |
if (!cache.offerFirst(buf)) { |
|
194 |
// cache is full |
|
195 |
free(buf); |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
/** |
|
200 |
* Releases a temporary buffer by returning to the cache or freeing it. If |
|
201 |
* returning to the cache then insert it at the end. This makes it |
|
202 |
* suitable for scatter/gather operations where the buffers are returned to |
|
203 |
* cache in same order that they were obtained. |
|
204 |
*/ |
|
205 |
static void offerLastTemporaryDirectBuffer(ByteBuffer buf) { |
|
206 |
assert buf != null; |
|
207 |
BufferCache cache = bufferCache.get(); |
|
208 |
if (!cache.offerLast(buf)) { |
|
209 |
// cache is full |
|
210 |
free(buf); |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
/** |
|
215 |
* Frees the memory for the given direct buffer |
|
216 |
*/ |
|
217 |
private static void free(ByteBuffer buf) { |
|
218 |
((DirectBuffer)buf).cleaner().clean(); |
|
2 | 219 |
} |
220 |
||
221 |
||
222 |
// -- Random stuff -- |
|
223 |
||
224 |
static ByteBuffer[] subsequence(ByteBuffer[] bs, int offset, int length) { |
|
225 |
if ((offset == 0) && (length == bs.length)) |
|
226 |
return bs; |
|
227 |
int n = length; |
|
228 |
ByteBuffer[] bs2 = new ByteBuffer[n]; |
|
229 |
for (int i = 0; i < n; i++) |
|
230 |
bs2[i] = bs[offset + i]; |
|
231 |
return bs2; |
|
232 |
} |
|
233 |
||
234 |
static <E> Set<E> ungrowableSet(final Set<E> s) { |
|
235 |
return new Set<E>() { |
|
236 |
||
237 |
public int size() { return s.size(); } |
|
238 |
public boolean isEmpty() { return s.isEmpty(); } |
|
239 |
public boolean contains(Object o) { return s.contains(o); } |
|
240 |
public Object[] toArray() { return s.toArray(); } |
|
241 |
public <T> T[] toArray(T[] a) { return s.toArray(a); } |
|
242 |
public String toString() { return s.toString(); } |
|
243 |
public Iterator<E> iterator() { return s.iterator(); } |
|
244 |
public boolean equals(Object o) { return s.equals(o); } |
|
245 |
public int hashCode() { return s.hashCode(); } |
|
246 |
public void clear() { s.clear(); } |
|
247 |
public boolean remove(Object o) { return s.remove(o); } |
|
248 |
||
249 |
public boolean containsAll(Collection<?> coll) { |
|
250 |
return s.containsAll(coll); |
|
251 |
} |
|
252 |
public boolean removeAll(Collection<?> coll) { |
|
253 |
return s.removeAll(coll); |
|
254 |
} |
|
255 |
public boolean retainAll(Collection<?> coll) { |
|
256 |
return s.retainAll(coll); |
|
257 |
} |
|
258 |
||
259 |
public boolean add(E o){ |
|
260 |
throw new UnsupportedOperationException(); |
|
261 |
} |
|
262 |
public boolean addAll(Collection<? extends E> coll) { |
|
263 |
throw new UnsupportedOperationException(); |
|
264 |
} |
|
265 |
||
266 |
}; |
|
267 |
} |
|
268 |
||
269 |
||
270 |
// -- Unsafe access -- |
|
271 |
||
272 |
private static Unsafe unsafe = Unsafe.getUnsafe(); |
|
273 |
||
274 |
private static byte _get(long a) { |
|
275 |
return unsafe.getByte(a); |
|
276 |
} |
|
277 |
||
278 |
private static void _put(long a, byte b) { |
|
279 |
unsafe.putByte(a, b); |
|
280 |
} |
|
281 |
||
282 |
static void erase(ByteBuffer bb) { |
|
283 |
unsafe.setMemory(((DirectBuffer)bb).address(), bb.capacity(), (byte)0); |
|
284 |
} |
|
285 |
||
286 |
static Unsafe unsafe() { |
|
287 |
return unsafe; |
|
288 |
} |
|
289 |
||
290 |
private static int pageSize = -1; |
|
291 |
||
292 |
static int pageSize() { |
|
293 |
if (pageSize == -1) |
|
294 |
pageSize = unsafe().pageSize(); |
|
295 |
return pageSize; |
|
296 |
} |
|
297 |
||
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
298 |
private static volatile Constructor<?> directByteBufferConstructor = null; |
2 | 299 |
|
300 |
private static void initDBBConstructor() { |
|
51 | 301 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
302 |
public Void run() { |
|
2 | 303 |
try { |
51 | 304 |
Class<?> cl = Class.forName("java.nio.DirectByteBuffer"); |
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
305 |
Constructor<?> ctor = cl.getDeclaredConstructor( |
10882
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10419
diff
changeset
|
306 |
new Class<?>[] { int.class, |
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10419
diff
changeset
|
307 |
long.class, |
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10419
diff
changeset
|
308 |
FileDescriptor.class, |
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10419
diff
changeset
|
309 |
Runnable.class }); |
2 | 310 |
ctor.setAccessible(true); |
311 |
directByteBufferConstructor = ctor; |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
312 |
} catch (ClassNotFoundException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
313 |
NoSuchMethodException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
314 |
IllegalArgumentException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
315 |
ClassCastException x) { |
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
10137
diff
changeset
|
316 |
throw new InternalError(x); |
2 | 317 |
} |
318 |
return null; |
|
319 |
}}); |
|
320 |
} |
|
321 |
||
322 |
static MappedByteBuffer newMappedByteBuffer(int size, long addr, |
|
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6301
diff
changeset
|
323 |
FileDescriptor fd, |
2 | 324 |
Runnable unmapper) |
325 |
{ |
|
326 |
MappedByteBuffer dbb; |
|
327 |
if (directByteBufferConstructor == null) |
|
328 |
initDBBConstructor(); |
|
329 |
try { |
|
330 |
dbb = (MappedByteBuffer)directByteBufferConstructor.newInstance( |
|
331 |
new Object[] { new Integer(size), |
|
332 |
new Long(addr), |
|
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6301
diff
changeset
|
333 |
fd, |
2 | 334 |
unmapper }); |
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
335 |
} catch (InstantiationException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
336 |
IllegalAccessException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
337 |
InvocationTargetException e) { |
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
10137
diff
changeset
|
338 |
throw new InternalError(e); |
2 | 339 |
} |
340 |
return dbb; |
|
341 |
} |
|
342 |
||
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
343 |
private static volatile Constructor<?> directByteBufferRConstructor = null; |
2 | 344 |
|
345 |
private static void initDBBRConstructor() { |
|
51 | 346 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
347 |
public Void run() { |
|
2 | 348 |
try { |
51 | 349 |
Class<?> cl = Class.forName("java.nio.DirectByteBufferR"); |
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
350 |
Constructor<?> ctor = cl.getDeclaredConstructor( |
10882
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10419
diff
changeset
|
351 |
new Class<?>[] { int.class, |
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10419
diff
changeset
|
352 |
long.class, |
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10419
diff
changeset
|
353 |
FileDescriptor.class, |
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10419
diff
changeset
|
354 |
Runnable.class }); |
2 | 355 |
ctor.setAccessible(true); |
356 |
directByteBufferRConstructor = ctor; |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
357 |
} catch (ClassNotFoundException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
358 |
NoSuchMethodException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
359 |
IllegalArgumentException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
360 |
ClassCastException x) { |
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
10137
diff
changeset
|
361 |
throw new InternalError(x); |
2 | 362 |
} |
363 |
return null; |
|
364 |
}}); |
|
365 |
} |
|
366 |
||
367 |
static MappedByteBuffer newMappedByteBufferR(int size, long addr, |
|
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6301
diff
changeset
|
368 |
FileDescriptor fd, |
2 | 369 |
Runnable unmapper) |
370 |
{ |
|
371 |
MappedByteBuffer dbb; |
|
372 |
if (directByteBufferRConstructor == null) |
|
373 |
initDBBRConstructor(); |
|
374 |
try { |
|
375 |
dbb = (MappedByteBuffer)directByteBufferRConstructor.newInstance( |
|
376 |
new Object[] { new Integer(size), |
|
377 |
new Long(addr), |
|
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6301
diff
changeset
|
378 |
fd, |
2 | 379 |
unmapper }); |
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
380 |
} catch (InstantiationException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
381 |
IllegalAccessException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
382 |
InvocationTargetException e) { |
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
10137
diff
changeset
|
383 |
throw new InternalError(e); |
2 | 384 |
} |
385 |
return dbb; |
|
386 |
} |
|
387 |
||
388 |
||
389 |
// -- Bug compatibility -- |
|
390 |
||
391 |
private static volatile String bugLevel = null; |
|
392 |
||
393 |
static boolean atBugLevel(String bl) { // package-private |
|
394 |
if (bugLevel == null) { |
|
395 |
if (!sun.misc.VM.isBooted()) |
|
396 |
return false; |
|
397 |
String value = AccessController.doPrivileged( |
|
398 |
new GetPropertyAction("sun.nio.ch.bugLevel")); |
|
399 |
bugLevel = (value != null) ? value : ""; |
|
400 |
} |
|
401 |
return bugLevel.equals(bl); |
|
402 |
} |
|
403 |
||
404 |
} |