author | bpb |
Tue, 17 Oct 2017 16:51:11 -0700 | |
changeset 47428 | d72d7d55c765 |
parent 47216 | 71c04702a3d5 |
child 50719 | 106dc156ce6b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
46094
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
37781
diff
changeset
|
2 |
* Copyright (c) 2000, 2017, 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 |
||
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6301
diff
changeset
|
28 |
import java.io.FileDescriptor; |
46094
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
37781
diff
changeset
|
29 |
import java.lang.reflect.Constructor; |
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
37781
diff
changeset
|
30 |
import java.lang.reflect.InvocationTargetException; |
2 | 31 |
import java.nio.ByteBuffer; |
32 |
import java.nio.MappedByteBuffer; |
|
33 |
import java.security.AccessController; |
|
34 |
import java.security.PrivilegedAction; |
|
46094
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
37781
diff
changeset
|
35 |
import java.util.Collection; |
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
37781
diff
changeset
|
36 |
import java.util.Iterator; |
0c23b05caf7d
8184330: Remove sun.nio.ch.Util.atBugLevel() either completely or at least get rid of volatile field bugLevel
clanger
parents:
37781
diff
changeset
|
37 |
import java.util.Set; |
33674
566777f73c32
8140606: Update library code to use internal Unsafe
chegar
parents:
25859
diff
changeset
|
38 |
import jdk.internal.misc.Unsafe; |
2 | 39 |
import sun.security.action.GetPropertyAction; |
47428 | 40 |
import java.io.IOException; |
2 | 41 |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10882
diff
changeset
|
42 |
public class Util { |
2 | 43 |
|
44 |
// -- Caches -- |
|
45 |
||
46 |
// 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
|
47 |
private static final int TEMP_BUF_POOL_SIZE = IOUtil.IOV_MAX; |
2 | 48 |
|
35386
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
49 |
// The max size allowed for a cached temp buffer, in bytes |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
50 |
private static final long MAX_CACHED_BUFFER_SIZE = getMaxCachedBufferSize(); |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
51 |
|
6301 | 52 |
// Per-thread cache of temporary direct buffers |
53 |
private static ThreadLocal<BufferCache> bufferCache = |
|
54 |
new ThreadLocal<BufferCache>() |
|
55 |
{ |
|
56 |
@Override |
|
57 |
protected BufferCache initialValue() { |
|
58 |
return new BufferCache(); |
|
59 |
} |
|
60 |
}; |
|
61 |
||
62 |
/** |
|
35386
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
63 |
* Returns the max size allowed for a cached temp buffers, in |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
64 |
* bytes. It defaults to Long.MAX_VALUE. It can be set with the |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
65 |
* jdk.nio.maxCachedBufferSize property. Even though |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
66 |
* ByteBuffer.capacity() returns an int, we're using a long here |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
67 |
* for potential future-proofing. |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
68 |
*/ |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
69 |
private static long getMaxCachedBufferSize() { |
37781
71ed5645f17c
8155775: Re-examine naming of privileged methods to access System properties
redestad
parents:
37593
diff
changeset
|
70 |
String s = GetPropertyAction |
71ed5645f17c
8155775: Re-examine naming of privileged methods to access System properties
redestad
parents:
37593
diff
changeset
|
71 |
.privilegedGetProperty("jdk.nio.maxCachedBufferSize"); |
35386
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
72 |
if (s != null) { |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
73 |
try { |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
74 |
long m = Long.parseLong(s); |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
75 |
if (m >= 0) { |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
76 |
return m; |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
77 |
} else { |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
78 |
// if it's negative, ignore the system property |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
79 |
} |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
80 |
} catch (NumberFormatException e) { |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
81 |
// if the string is not well formed, ignore the system property |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
82 |
} |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
83 |
} |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
84 |
return Long.MAX_VALUE; |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
85 |
} |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
86 |
|
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
87 |
/** |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
88 |
* Returns true if a buffer of this size is too large to be |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
89 |
* added to the buffer cache, false otherwise. |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
90 |
*/ |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
91 |
private static boolean isBufferTooLarge(int size) { |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
92 |
return size > MAX_CACHED_BUFFER_SIZE; |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
93 |
} |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
94 |
|
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
95 |
/** |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
96 |
* Returns true if the buffer is too large to be added to the |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
97 |
* buffer cache, false otherwise. |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
98 |
*/ |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
99 |
private static boolean isBufferTooLarge(ByteBuffer buf) { |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
100 |
return isBufferTooLarge(buf.capacity()); |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
101 |
} |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
102 |
|
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
103 |
/** |
6301 | 104 |
* A simple cache of direct buffers. |
105 |
*/ |
|
106 |
private static class BufferCache { |
|
107 |
// the array of buffers |
|
108 |
private ByteBuffer[] buffers; |
|
2 | 109 |
|
6301 | 110 |
// the number of buffers in the cache |
111 |
private int count; |
|
112 |
||
113 |
// the index of the first valid buffer (undefined if count == 0) |
|
114 |
private int start; |
|
115 |
||
116 |
private int next(int i) { |
|
117 |
return (i + 1) % TEMP_BUF_POOL_SIZE; |
|
118 |
} |
|
119 |
||
120 |
BufferCache() { |
|
121 |
buffers = new ByteBuffer[TEMP_BUF_POOL_SIZE]; |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Removes and returns a buffer from the cache of at least the given |
|
126 |
* size (or null if no suitable buffer is found). |
|
127 |
*/ |
|
128 |
ByteBuffer get(int size) { |
|
35386
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
129 |
// Don't call this if the buffer would be too large. |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
130 |
assert !isBufferTooLarge(size); |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
131 |
|
6301 | 132 |
if (count == 0) |
133 |
return null; // cache is empty |
|
895 | 134 |
|
6301 | 135 |
ByteBuffer[] buffers = this.buffers; |
2 | 136 |
|
6301 | 137 |
// search for suitable buffer (often the first buffer will do) |
138 |
ByteBuffer buf = buffers[start]; |
|
139 |
if (buf.capacity() < size) { |
|
140 |
buf = null; |
|
141 |
int i = start; |
|
142 |
while ((i = next(i)) != start) { |
|
143 |
ByteBuffer bb = buffers[i]; |
|
144 |
if (bb == null) |
|
145 |
break; |
|
146 |
if (bb.capacity() >= size) { |
|
147 |
buf = bb; |
|
148 |
break; |
|
149 |
} |
|
150 |
} |
|
151 |
if (buf == null) |
|
152 |
return null; |
|
153 |
// move first element to here to avoid re-packing |
|
154 |
buffers[i] = buffers[start]; |
|
155 |
} |
|
156 |
||
157 |
// remove first element |
|
158 |
buffers[start] = null; |
|
159 |
start = next(start); |
|
160 |
count--; |
|
161 |
||
162 |
// prepare the buffer and return it |
|
163 |
buf.rewind(); |
|
164 |
buf.limit(size); |
|
165 |
return buf; |
|
166 |
} |
|
167 |
||
168 |
boolean offerFirst(ByteBuffer buf) { |
|
35386
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
169 |
// Don't call this if the buffer is too large. |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
170 |
assert !isBufferTooLarge(buf); |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
171 |
|
6301 | 172 |
if (count >= TEMP_BUF_POOL_SIZE) { |
173 |
return false; |
|
174 |
} else { |
|
175 |
start = (start + TEMP_BUF_POOL_SIZE - 1) % TEMP_BUF_POOL_SIZE; |
|
176 |
buffers[start] = buf; |
|
177 |
count++; |
|
178 |
return true; |
|
2 | 179 |
} |
180 |
} |
|
181 |
||
6301 | 182 |
boolean offerLast(ByteBuffer buf) { |
35386
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
183 |
// Don't call this if the buffer is too large. |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
184 |
assert !isBufferTooLarge(buf); |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
185 |
|
6301 | 186 |
if (count >= TEMP_BUF_POOL_SIZE) { |
187 |
return false; |
|
188 |
} else { |
|
189 |
int next = (start + count) % TEMP_BUF_POOL_SIZE; |
|
190 |
buffers[next] = buf; |
|
191 |
count++; |
|
192 |
return true; |
|
2 | 193 |
} |
194 |
} |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
895
diff
changeset
|
195 |
|
6301 | 196 |
boolean isEmpty() { |
197 |
return count == 0; |
|
198 |
} |
|
199 |
||
200 |
ByteBuffer removeFirst() { |
|
201 |
assert count > 0; |
|
202 |
ByteBuffer buf = buffers[start]; |
|
203 |
buffers[start] = null; |
|
204 |
start = next(start); |
|
205 |
count--; |
|
206 |
return buf; |
|
207 |
} |
|
208 |
} |
|
209 |
||
210 |
/** |
|
211 |
* Returns a temporary buffer of at least the given size |
|
212 |
*/ |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10882
diff
changeset
|
213 |
public static ByteBuffer getTemporaryDirectBuffer(int size) { |
35386
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
214 |
// If a buffer of this size is too large for the cache, there |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
215 |
// should not be a buffer in the cache that is at least as |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
216 |
// large. So we'll just create a new one. Also, we don't have |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
217 |
// to remove the buffer from the cache (as this method does |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
218 |
// below) given that we won't put the new buffer in the cache. |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
219 |
if (isBufferTooLarge(size)) { |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
220 |
return ByteBuffer.allocateDirect(size); |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
221 |
} |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
222 |
|
6301 | 223 |
BufferCache cache = bufferCache.get(); |
224 |
ByteBuffer buf = cache.get(size); |
|
225 |
if (buf != null) { |
|
226 |
return buf; |
|
227 |
} else { |
|
228 |
// No suitable buffer in the cache so we need to allocate a new |
|
229 |
// one. To avoid the cache growing then we remove the first |
|
230 |
// buffer from the cache and free it. |
|
231 |
if (!cache.isEmpty()) { |
|
232 |
buf = cache.removeFirst(); |
|
233 |
free(buf); |
|
234 |
} |
|
235 |
return ByteBuffer.allocateDirect(size); |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
/** |
|
47428 | 240 |
* Returns a temporary buffer of at least the given size and |
241 |
* aligned to the alignment |
|
242 |
*/ |
|
243 |
public static ByteBuffer getTemporaryAlignedDirectBuffer(int size, |
|
244 |
int alignment) { |
|
245 |
if (isBufferTooLarge(size)) { |
|
246 |
return ByteBuffer.allocateDirect(size + alignment - 1) |
|
247 |
.alignedSlice(alignment); |
|
248 |
} |
|
249 |
||
250 |
BufferCache cache = bufferCache.get(); |
|
251 |
ByteBuffer buf = cache.get(size); |
|
252 |
if (buf != null) { |
|
253 |
if (buf.alignmentOffset(0, alignment) == 0) { |
|
254 |
return buf; |
|
255 |
} |
|
256 |
} else { |
|
257 |
if (!cache.isEmpty()) { |
|
258 |
buf = cache.removeFirst(); |
|
259 |
free(buf); |
|
260 |
} |
|
261 |
} |
|
262 |
return ByteBuffer.allocateDirect(size + alignment - 1) |
|
263 |
.alignedSlice(alignment); |
|
264 |
} |
|
265 |
||
266 |
/** |
|
6301 | 267 |
* Releases a temporary buffer by returning to the cache or freeing it. |
268 |
*/ |
|
11823
ee83ae88512d
7041778: Move SCTP implementation out of sun.nio.ch and into its own package
chegar
parents:
10882
diff
changeset
|
269 |
public static void releaseTemporaryDirectBuffer(ByteBuffer buf) { |
6301 | 270 |
offerFirstTemporaryDirectBuffer(buf); |
271 |
} |
|
272 |
||
273 |
/** |
|
274 |
* Releases a temporary buffer by returning to the cache or freeing it. If |
|
275 |
* returning to the cache then insert it at the start so that it is |
|
276 |
* likely to be returned by a subsequent call to getTemporaryDirectBuffer. |
|
277 |
*/ |
|
278 |
static void offerFirstTemporaryDirectBuffer(ByteBuffer buf) { |
|
35386
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
279 |
// If the buffer is too large for the cache we don't have to |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
280 |
// check the cache. We'll just free it. |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
281 |
if (isBufferTooLarge(buf)) { |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
282 |
free(buf); |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
283 |
return; |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
284 |
} |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
285 |
|
6301 | 286 |
assert buf != null; |
287 |
BufferCache cache = bufferCache.get(); |
|
288 |
if (!cache.offerFirst(buf)) { |
|
289 |
// cache is full |
|
290 |
free(buf); |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
/** |
|
295 |
* Releases a temporary buffer by returning to the cache or freeing it. If |
|
296 |
* returning to the cache then insert it at the end. This makes it |
|
297 |
* suitable for scatter/gather operations where the buffers are returned to |
|
298 |
* cache in same order that they were obtained. |
|
299 |
*/ |
|
300 |
static void offerLastTemporaryDirectBuffer(ByteBuffer buf) { |
|
35386
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
301 |
// If the buffer is too large for the cache we don't have to |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
302 |
// check the cache. We'll just free it. |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
303 |
if (isBufferTooLarge(buf)) { |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
304 |
free(buf); |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
305 |
return; |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
306 |
} |
a54f3c20e83d
8147468: Allow users to bound the size of buffers cached in the per-thread buffer caches
tonyp
parents:
34882
diff
changeset
|
307 |
|
6301 | 308 |
assert buf != null; |
309 |
BufferCache cache = bufferCache.get(); |
|
310 |
if (!cache.offerLast(buf)) { |
|
311 |
// cache is full |
|
312 |
free(buf); |
|
313 |
} |
|
314 |
} |
|
315 |
||
316 |
/** |
|
317 |
* Frees the memory for the given direct buffer |
|
318 |
*/ |
|
319 |
private static void free(ByteBuffer buf) { |
|
320 |
((DirectBuffer)buf).cleaner().clean(); |
|
2 | 321 |
} |
322 |
||
323 |
||
324 |
// -- Random stuff -- |
|
325 |
||
326 |
static ByteBuffer[] subsequence(ByteBuffer[] bs, int offset, int length) { |
|
327 |
if ((offset == 0) && (length == bs.length)) |
|
328 |
return bs; |
|
329 |
int n = length; |
|
330 |
ByteBuffer[] bs2 = new ByteBuffer[n]; |
|
331 |
for (int i = 0; i < n; i++) |
|
332 |
bs2[i] = bs[offset + i]; |
|
333 |
return bs2; |
|
334 |
} |
|
335 |
||
336 |
static <E> Set<E> ungrowableSet(final Set<E> s) { |
|
337 |
return new Set<E>() { |
|
338 |
||
339 |
public int size() { return s.size(); } |
|
340 |
public boolean isEmpty() { return s.isEmpty(); } |
|
341 |
public boolean contains(Object o) { return s.contains(o); } |
|
342 |
public Object[] toArray() { return s.toArray(); } |
|
343 |
public <T> T[] toArray(T[] a) { return s.toArray(a); } |
|
344 |
public String toString() { return s.toString(); } |
|
345 |
public Iterator<E> iterator() { return s.iterator(); } |
|
346 |
public boolean equals(Object o) { return s.equals(o); } |
|
347 |
public int hashCode() { return s.hashCode(); } |
|
348 |
public void clear() { s.clear(); } |
|
349 |
public boolean remove(Object o) { return s.remove(o); } |
|
350 |
||
351 |
public boolean containsAll(Collection<?> coll) { |
|
352 |
return s.containsAll(coll); |
|
353 |
} |
|
354 |
public boolean removeAll(Collection<?> coll) { |
|
355 |
return s.removeAll(coll); |
|
356 |
} |
|
357 |
public boolean retainAll(Collection<?> coll) { |
|
358 |
return s.retainAll(coll); |
|
359 |
} |
|
360 |
||
361 |
public boolean add(E o){ |
|
362 |
throw new UnsupportedOperationException(); |
|
363 |
} |
|
364 |
public boolean addAll(Collection<? extends E> coll) { |
|
365 |
throw new UnsupportedOperationException(); |
|
366 |
} |
|
367 |
||
368 |
}; |
|
369 |
} |
|
370 |
||
371 |
||
372 |
// -- Unsafe access -- |
|
373 |
||
374 |
private static Unsafe unsafe = Unsafe.getUnsafe(); |
|
375 |
||
376 |
private static byte _get(long a) { |
|
377 |
return unsafe.getByte(a); |
|
378 |
} |
|
379 |
||
380 |
private static void _put(long a, byte b) { |
|
381 |
unsafe.putByte(a, b); |
|
382 |
} |
|
383 |
||
384 |
static void erase(ByteBuffer bb) { |
|
385 |
unsafe.setMemory(((DirectBuffer)bb).address(), bb.capacity(), (byte)0); |
|
386 |
} |
|
387 |
||
388 |
static Unsafe unsafe() { |
|
389 |
return unsafe; |
|
390 |
} |
|
391 |
||
392 |
private static int pageSize = -1; |
|
393 |
||
394 |
static int pageSize() { |
|
395 |
if (pageSize == -1) |
|
396 |
pageSize = unsafe().pageSize(); |
|
397 |
return pageSize; |
|
398 |
} |
|
399 |
||
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
33674
diff
changeset
|
400 |
private static volatile Constructor<?> directByteBufferConstructor; |
2 | 401 |
|
402 |
private static void initDBBConstructor() { |
|
51 | 403 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
404 |
public Void run() { |
|
2 | 405 |
try { |
51 | 406 |
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
|
407 |
Constructor<?> ctor = cl.getDeclaredConstructor( |
10882
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10419
diff
changeset
|
408 |
new Class<?>[] { int.class, |
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10419
diff
changeset
|
409 |
long.class, |
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10419
diff
changeset
|
410 |
FileDescriptor.class, |
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10419
diff
changeset
|
411 |
Runnable.class }); |
2 | 412 |
ctor.setAccessible(true); |
413 |
directByteBufferConstructor = ctor; |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
414 |
} catch (ClassNotFoundException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
415 |
NoSuchMethodException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
416 |
IllegalArgumentException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
417 |
ClassCastException x) { |
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
10137
diff
changeset
|
418 |
throw new InternalError(x); |
2 | 419 |
} |
420 |
return null; |
|
421 |
}}); |
|
422 |
} |
|
423 |
||
424 |
static MappedByteBuffer newMappedByteBuffer(int size, long addr, |
|
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6301
diff
changeset
|
425 |
FileDescriptor fd, |
2 | 426 |
Runnable unmapper) |
427 |
{ |
|
428 |
MappedByteBuffer dbb; |
|
429 |
if (directByteBufferConstructor == null) |
|
430 |
initDBBConstructor(); |
|
431 |
try { |
|
432 |
dbb = (MappedByteBuffer)directByteBufferConstructor.newInstance( |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
25186
diff
changeset
|
433 |
new Object[] { size, |
25186
63e1a2ec30f5
8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents:
23010
diff
changeset
|
434 |
addr, |
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6301
diff
changeset
|
435 |
fd, |
2 | 436 |
unmapper }); |
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
437 |
} catch (InstantiationException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
438 |
IllegalAccessException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
439 |
InvocationTargetException e) { |
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
10137
diff
changeset
|
440 |
throw new InternalError(e); |
2 | 441 |
} |
442 |
return dbb; |
|
443 |
} |
|
444 |
||
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
33674
diff
changeset
|
445 |
private static volatile Constructor<?> directByteBufferRConstructor; |
2 | 446 |
|
447 |
private static void initDBBRConstructor() { |
|
51 | 448 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
449 |
public Void run() { |
|
2 | 450 |
try { |
51 | 451 |
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
|
452 |
Constructor<?> ctor = cl.getDeclaredConstructor( |
10882
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10419
diff
changeset
|
453 |
new Class<?>[] { int.class, |
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10419
diff
changeset
|
454 |
long.class, |
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10419
diff
changeset
|
455 |
FileDescriptor.class, |
7ddadf2b8b4c
7104650: rawtype warnings in several net, nio and security source files
chegar
parents:
10419
diff
changeset
|
456 |
Runnable.class }); |
2 | 457 |
ctor.setAccessible(true); |
458 |
directByteBufferRConstructor = ctor; |
|
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
459 |
} catch (ClassNotFoundException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
460 |
NoSuchMethodException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
461 |
IllegalArgumentException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
462 |
ClassCastException x) { |
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
10137
diff
changeset
|
463 |
throw new InternalError(x); |
2 | 464 |
} |
465 |
return null; |
|
466 |
}}); |
|
467 |
} |
|
468 |
||
469 |
static MappedByteBuffer newMappedByteBufferR(int size, long addr, |
|
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6301
diff
changeset
|
470 |
FileDescriptor fd, |
2 | 471 |
Runnable unmapper) |
472 |
{ |
|
473 |
MappedByteBuffer dbb; |
|
474 |
if (directByteBufferRConstructor == null) |
|
475 |
initDBBRConstructor(); |
|
476 |
try { |
|
477 |
dbb = (MappedByteBuffer)directByteBufferRConstructor.newInstance( |
|
25522
10d789df41bb
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents:
25186
diff
changeset
|
478 |
new Object[] { size, |
25186
63e1a2ec30f5
8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents:
23010
diff
changeset
|
479 |
addr, |
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6301
diff
changeset
|
480 |
fd, |
2 | 481 |
unmapper }); |
10137
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
482 |
} catch (InstantiationException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
483 |
IllegalAccessException | |
d92637d3d673
7068616: NIO libraries do not build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
7668
diff
changeset
|
484 |
InvocationTargetException e) { |
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
10137
diff
changeset
|
485 |
throw new InternalError(e); |
2 | 486 |
} |
487 |
return dbb; |
|
488 |
} |
|
47428 | 489 |
|
490 |
static void checkBufferPositionAligned(ByteBuffer bb, |
|
491 |
int pos, int alignment) |
|
492 |
throws IOException |
|
493 |
{ |
|
494 |
if (bb.alignmentOffset(pos, alignment) != 0) { |
|
495 |
throw new IOException("Current location of the bytebuffer (" |
|
496 |
+ pos + ") is not a multiple of the block size (" |
|
497 |
+ alignment + ")"); |
|
498 |
} |
|
499 |
} |
|
500 |
||
501 |
static void checkRemainingBufferSizeAligned(int rem, |
|
502 |
int alignment) |
|
503 |
throws IOException |
|
504 |
{ |
|
505 |
if (rem % alignment != 0) { |
|
506 |
throw new IOException("Number of remaining bytes (" |
|
507 |
+ rem + ") is not a multiple of the block size (" |
|
508 |
+ alignment + ")"); |
|
509 |
} |
|
510 |
} |
|
511 |
||
512 |
static void checkChannelPositionAligned(long position, |
|
513 |
int alignment) |
|
514 |
throws IOException |
|
515 |
{ |
|
516 |
if (position % alignment != 0) { |
|
517 |
throw new IOException("Channel position (" + position |
|
518 |
+ ") is not a multiple of the block size (" |
|
519 |
+ alignment + ")"); |
|
520 |
} |
|
521 |
} |
|
2 | 522 |
} |