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