author | martin |
Tue, 15 Sep 2015 21:56:04 -0700 | |
changeset 32649 | 2ee9017c7597 |
parent 30338 | 957ee8b5a33a |
child 32834 | e1dca5fe4de3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
30338
957ee8b5a33a
8026049: (bf) Intrinsify ByteBuffer.put{Int, Double, Float, ...} methods
aph
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 2000, 2015, 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 java.nio; |
|
27 |
||
28 |
import java.security.AccessController; |
|
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
29 |
import java.util.concurrent.atomic.AtomicLong; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
30 |
import java.util.concurrent.atomic.LongAdder; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
31 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
32 |
import sun.misc.JavaLangRefAccess; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
33 |
import sun.misc.SharedSecrets; |
2 | 34 |
import sun.misc.Unsafe; |
35 |
import sun.misc.VM; |
|
36 |
||
37 |
/** |
|
38 |
* Access to bits, native and otherwise. |
|
39 |
*/ |
|
40 |
||
41 |
class Bits { // package-private |
|
42 |
||
43 |
private Bits() { } |
|
44 |
||
45 |
||
46 |
// -- Swapping -- |
|
47 |
||
48 |
static short swap(short x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
49 |
return Short.reverseBytes(x); |
2 | 50 |
} |
51 |
||
52 |
static char swap(char x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
53 |
return Character.reverseBytes(x); |
2 | 54 |
} |
55 |
||
56 |
static int swap(int x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
57 |
return Integer.reverseBytes(x); |
2 | 58 |
} |
59 |
||
60 |
static long swap(long x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
61 |
return Long.reverseBytes(x); |
2 | 62 |
} |
63 |
||
64 |
||
65 |
// -- get/put char -- |
|
66 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30338
diff
changeset
|
67 |
private static char makeChar(byte b1, byte b0) { |
2 | 68 |
return (char)((b1 << 8) | (b0 & 0xff)); |
69 |
} |
|
70 |
||
71 |
static char getCharL(ByteBuffer bb, int bi) { |
|
72 |
return makeChar(bb._get(bi + 1), |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
73 |
bb._get(bi )); |
2 | 74 |
} |
75 |
||
76 |
static char getCharL(long a) { |
|
77 |
return makeChar(_get(a + 1), |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
78 |
_get(a )); |
2 | 79 |
} |
80 |
||
81 |
static char getCharB(ByteBuffer bb, int bi) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
82 |
return makeChar(bb._get(bi ), |
2 | 83 |
bb._get(bi + 1)); |
84 |
} |
|
85 |
||
86 |
static char getCharB(long a) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
87 |
return makeChar(_get(a ), |
2 | 88 |
_get(a + 1)); |
89 |
} |
|
90 |
||
91 |
static char getChar(ByteBuffer bb, int bi, boolean bigEndian) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
92 |
return bigEndian ? getCharB(bb, bi) : getCharL(bb, bi); |
2 | 93 |
} |
94 |
||
95 |
static char getChar(long a, boolean bigEndian) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
96 |
return bigEndian ? getCharB(a) : getCharL(a); |
2 | 97 |
} |
98 |
||
99 |
private static byte char1(char x) { return (byte)(x >> 8); } |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
100 |
private static byte char0(char x) { return (byte)(x ); } |
2 | 101 |
|
102 |
static void putCharL(ByteBuffer bb, int bi, char x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
103 |
bb._put(bi , char0(x)); |
2 | 104 |
bb._put(bi + 1, char1(x)); |
105 |
} |
|
106 |
||
107 |
static void putCharL(long a, char x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
108 |
_put(a , char0(x)); |
2 | 109 |
_put(a + 1, char1(x)); |
110 |
} |
|
111 |
||
112 |
static void putCharB(ByteBuffer bb, int bi, char x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
113 |
bb._put(bi , char1(x)); |
2 | 114 |
bb._put(bi + 1, char0(x)); |
115 |
} |
|
116 |
||
117 |
static void putCharB(long a, char x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
118 |
_put(a , char1(x)); |
2 | 119 |
_put(a + 1, char0(x)); |
120 |
} |
|
121 |
||
122 |
static void putChar(ByteBuffer bb, int bi, char x, boolean bigEndian) { |
|
123 |
if (bigEndian) |
|
124 |
putCharB(bb, bi, x); |
|
125 |
else |
|
126 |
putCharL(bb, bi, x); |
|
127 |
} |
|
128 |
||
129 |
static void putChar(long a, char x, boolean bigEndian) { |
|
130 |
if (bigEndian) |
|
131 |
putCharB(a, x); |
|
132 |
else |
|
133 |
putCharL(a, x); |
|
134 |
} |
|
135 |
||
136 |
||
137 |
// -- get/put short -- |
|
138 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30338
diff
changeset
|
139 |
private static short makeShort(byte b1, byte b0) { |
2 | 140 |
return (short)((b1 << 8) | (b0 & 0xff)); |
141 |
} |
|
142 |
||
143 |
static short getShortL(ByteBuffer bb, int bi) { |
|
144 |
return makeShort(bb._get(bi + 1), |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
145 |
bb._get(bi )); |
2 | 146 |
} |
147 |
||
148 |
static short getShortL(long a) { |
|
149 |
return makeShort(_get(a + 1), |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
150 |
_get(a )); |
2 | 151 |
} |
152 |
||
153 |
static short getShortB(ByteBuffer bb, int bi) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
154 |
return makeShort(bb._get(bi ), |
2 | 155 |
bb._get(bi + 1)); |
156 |
} |
|
157 |
||
158 |
static short getShortB(long a) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
159 |
return makeShort(_get(a ), |
2 | 160 |
_get(a + 1)); |
161 |
} |
|
162 |
||
163 |
static short getShort(ByteBuffer bb, int bi, boolean bigEndian) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
164 |
return bigEndian ? getShortB(bb, bi) : getShortL(bb, bi); |
2 | 165 |
} |
166 |
||
167 |
static short getShort(long a, boolean bigEndian) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
168 |
return bigEndian ? getShortB(a) : getShortL(a); |
2 | 169 |
} |
170 |
||
171 |
private static byte short1(short x) { return (byte)(x >> 8); } |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
172 |
private static byte short0(short x) { return (byte)(x ); } |
2 | 173 |
|
174 |
static void putShortL(ByteBuffer bb, int bi, short x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
175 |
bb._put(bi , short0(x)); |
2 | 176 |
bb._put(bi + 1, short1(x)); |
177 |
} |
|
178 |
||
179 |
static void putShortL(long a, short x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
180 |
_put(a , short0(x)); |
2 | 181 |
_put(a + 1, short1(x)); |
182 |
} |
|
183 |
||
184 |
static void putShortB(ByteBuffer bb, int bi, short x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
185 |
bb._put(bi , short1(x)); |
2 | 186 |
bb._put(bi + 1, short0(x)); |
187 |
} |
|
188 |
||
189 |
static void putShortB(long a, short x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
190 |
_put(a , short1(x)); |
2 | 191 |
_put(a + 1, short0(x)); |
192 |
} |
|
193 |
||
194 |
static void putShort(ByteBuffer bb, int bi, short x, boolean bigEndian) { |
|
195 |
if (bigEndian) |
|
196 |
putShortB(bb, bi, x); |
|
197 |
else |
|
198 |
putShortL(bb, bi, x); |
|
199 |
} |
|
200 |
||
201 |
static void putShort(long a, short x, boolean bigEndian) { |
|
202 |
if (bigEndian) |
|
203 |
putShortB(a, x); |
|
204 |
else |
|
205 |
putShortL(a, x); |
|
206 |
} |
|
207 |
||
208 |
||
209 |
// -- get/put int -- |
|
210 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30338
diff
changeset
|
211 |
private static int makeInt(byte b3, byte b2, byte b1, byte b0) { |
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
212 |
return (((b3 ) << 24) | |
2 | 213 |
((b2 & 0xff) << 16) | |
214 |
((b1 & 0xff) << 8) | |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
215 |
((b0 & 0xff) )); |
2 | 216 |
} |
217 |
||
218 |
static int getIntL(ByteBuffer bb, int bi) { |
|
219 |
return makeInt(bb._get(bi + 3), |
|
220 |
bb._get(bi + 2), |
|
221 |
bb._get(bi + 1), |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
222 |
bb._get(bi )); |
2 | 223 |
} |
224 |
||
225 |
static int getIntL(long a) { |
|
226 |
return makeInt(_get(a + 3), |
|
227 |
_get(a + 2), |
|
228 |
_get(a + 1), |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
229 |
_get(a )); |
2 | 230 |
} |
231 |
||
232 |
static int getIntB(ByteBuffer bb, int bi) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
233 |
return makeInt(bb._get(bi ), |
2 | 234 |
bb._get(bi + 1), |
235 |
bb._get(bi + 2), |
|
236 |
bb._get(bi + 3)); |
|
237 |
} |
|
238 |
||
239 |
static int getIntB(long a) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
240 |
return makeInt(_get(a ), |
2 | 241 |
_get(a + 1), |
242 |
_get(a + 2), |
|
243 |
_get(a + 3)); |
|
244 |
} |
|
245 |
||
246 |
static int getInt(ByteBuffer bb, int bi, boolean bigEndian) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
247 |
return bigEndian ? getIntB(bb, bi) : getIntL(bb, bi) ; |
2 | 248 |
} |
249 |
||
250 |
static int getInt(long a, boolean bigEndian) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
251 |
return bigEndian ? getIntB(a) : getIntL(a) ; |
2 | 252 |
} |
253 |
||
254 |
private static byte int3(int x) { return (byte)(x >> 24); } |
|
255 |
private static byte int2(int x) { return (byte)(x >> 16); } |
|
256 |
private static byte int1(int x) { return (byte)(x >> 8); } |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
257 |
private static byte int0(int x) { return (byte)(x ); } |
2 | 258 |
|
259 |
static void putIntL(ByteBuffer bb, int bi, int x) { |
|
260 |
bb._put(bi + 3, int3(x)); |
|
261 |
bb._put(bi + 2, int2(x)); |
|
262 |
bb._put(bi + 1, int1(x)); |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
263 |
bb._put(bi , int0(x)); |
2 | 264 |
} |
265 |
||
266 |
static void putIntL(long a, int x) { |
|
267 |
_put(a + 3, int3(x)); |
|
268 |
_put(a + 2, int2(x)); |
|
269 |
_put(a + 1, int1(x)); |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
270 |
_put(a , int0(x)); |
2 | 271 |
} |
272 |
||
273 |
static void putIntB(ByteBuffer bb, int bi, int x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
274 |
bb._put(bi , int3(x)); |
2 | 275 |
bb._put(bi + 1, int2(x)); |
276 |
bb._put(bi + 2, int1(x)); |
|
277 |
bb._put(bi + 3, int0(x)); |
|
278 |
} |
|
279 |
||
280 |
static void putIntB(long a, int x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
281 |
_put(a , int3(x)); |
2 | 282 |
_put(a + 1, int2(x)); |
283 |
_put(a + 2, int1(x)); |
|
284 |
_put(a + 3, int0(x)); |
|
285 |
} |
|
286 |
||
287 |
static void putInt(ByteBuffer bb, int bi, int x, boolean bigEndian) { |
|
288 |
if (bigEndian) |
|
289 |
putIntB(bb, bi, x); |
|
290 |
else |
|
291 |
putIntL(bb, bi, x); |
|
292 |
} |
|
293 |
||
294 |
static void putInt(long a, int x, boolean bigEndian) { |
|
295 |
if (bigEndian) |
|
296 |
putIntB(a, x); |
|
297 |
else |
|
298 |
putIntL(a, x); |
|
299 |
} |
|
300 |
||
301 |
||
302 |
// -- get/put long -- |
|
303 |
||
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
30338
diff
changeset
|
304 |
private static long makeLong(byte b7, byte b6, byte b5, byte b4, |
2 | 305 |
byte b3, byte b2, byte b1, byte b0) |
306 |
{ |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
307 |
return ((((long)b7 ) << 56) | |
2 | 308 |
(((long)b6 & 0xff) << 48) | |
309 |
(((long)b5 & 0xff) << 40) | |
|
310 |
(((long)b4 & 0xff) << 32) | |
|
311 |
(((long)b3 & 0xff) << 24) | |
|
312 |
(((long)b2 & 0xff) << 16) | |
|
313 |
(((long)b1 & 0xff) << 8) | |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
314 |
(((long)b0 & 0xff) )); |
2 | 315 |
} |
316 |
||
317 |
static long getLongL(ByteBuffer bb, int bi) { |
|
318 |
return makeLong(bb._get(bi + 7), |
|
319 |
bb._get(bi + 6), |
|
320 |
bb._get(bi + 5), |
|
321 |
bb._get(bi + 4), |
|
322 |
bb._get(bi + 3), |
|
323 |
bb._get(bi + 2), |
|
324 |
bb._get(bi + 1), |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
325 |
bb._get(bi )); |
2 | 326 |
} |
327 |
||
328 |
static long getLongL(long a) { |
|
329 |
return makeLong(_get(a + 7), |
|
330 |
_get(a + 6), |
|
331 |
_get(a + 5), |
|
332 |
_get(a + 4), |
|
333 |
_get(a + 3), |
|
334 |
_get(a + 2), |
|
335 |
_get(a + 1), |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
336 |
_get(a )); |
2 | 337 |
} |
338 |
||
339 |
static long getLongB(ByteBuffer bb, int bi) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
340 |
return makeLong(bb._get(bi ), |
2 | 341 |
bb._get(bi + 1), |
342 |
bb._get(bi + 2), |
|
343 |
bb._get(bi + 3), |
|
344 |
bb._get(bi + 4), |
|
345 |
bb._get(bi + 5), |
|
346 |
bb._get(bi + 6), |
|
347 |
bb._get(bi + 7)); |
|
348 |
} |
|
349 |
||
350 |
static long getLongB(long a) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
351 |
return makeLong(_get(a ), |
2 | 352 |
_get(a + 1), |
353 |
_get(a + 2), |
|
354 |
_get(a + 3), |
|
355 |
_get(a + 4), |
|
356 |
_get(a + 5), |
|
357 |
_get(a + 6), |
|
358 |
_get(a + 7)); |
|
359 |
} |
|
360 |
||
361 |
static long getLong(ByteBuffer bb, int bi, boolean bigEndian) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
362 |
return bigEndian ? getLongB(bb, bi) : getLongL(bb, bi); |
2 | 363 |
} |
364 |
||
365 |
static long getLong(long a, boolean bigEndian) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
366 |
return bigEndian ? getLongB(a) : getLongL(a); |
2 | 367 |
} |
368 |
||
369 |
private static byte long7(long x) { return (byte)(x >> 56); } |
|
370 |
private static byte long6(long x) { return (byte)(x >> 48); } |
|
371 |
private static byte long5(long x) { return (byte)(x >> 40); } |
|
372 |
private static byte long4(long x) { return (byte)(x >> 32); } |
|
373 |
private static byte long3(long x) { return (byte)(x >> 24); } |
|
374 |
private static byte long2(long x) { return (byte)(x >> 16); } |
|
375 |
private static byte long1(long x) { return (byte)(x >> 8); } |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
376 |
private static byte long0(long x) { return (byte)(x ); } |
2 | 377 |
|
378 |
static void putLongL(ByteBuffer bb, int bi, long x) { |
|
379 |
bb._put(bi + 7, long7(x)); |
|
380 |
bb._put(bi + 6, long6(x)); |
|
381 |
bb._put(bi + 5, long5(x)); |
|
382 |
bb._put(bi + 4, long4(x)); |
|
383 |
bb._put(bi + 3, long3(x)); |
|
384 |
bb._put(bi + 2, long2(x)); |
|
385 |
bb._put(bi + 1, long1(x)); |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
386 |
bb._put(bi , long0(x)); |
2 | 387 |
} |
388 |
||
389 |
static void putLongL(long a, long x) { |
|
390 |
_put(a + 7, long7(x)); |
|
391 |
_put(a + 6, long6(x)); |
|
392 |
_put(a + 5, long5(x)); |
|
393 |
_put(a + 4, long4(x)); |
|
394 |
_put(a + 3, long3(x)); |
|
395 |
_put(a + 2, long2(x)); |
|
396 |
_put(a + 1, long1(x)); |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
397 |
_put(a , long0(x)); |
2 | 398 |
} |
399 |
||
400 |
static void putLongB(ByteBuffer bb, int bi, long x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
401 |
bb._put(bi , long7(x)); |
2 | 402 |
bb._put(bi + 1, long6(x)); |
403 |
bb._put(bi + 2, long5(x)); |
|
404 |
bb._put(bi + 3, long4(x)); |
|
405 |
bb._put(bi + 4, long3(x)); |
|
406 |
bb._put(bi + 5, long2(x)); |
|
407 |
bb._put(bi + 6, long1(x)); |
|
408 |
bb._put(bi + 7, long0(x)); |
|
409 |
} |
|
410 |
||
411 |
static void putLongB(long a, long x) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
412 |
_put(a , long7(x)); |
2 | 413 |
_put(a + 1, long6(x)); |
414 |
_put(a + 2, long5(x)); |
|
415 |
_put(a + 3, long4(x)); |
|
416 |
_put(a + 4, long3(x)); |
|
417 |
_put(a + 5, long2(x)); |
|
418 |
_put(a + 6, long1(x)); |
|
419 |
_put(a + 7, long0(x)); |
|
420 |
} |
|
421 |
||
422 |
static void putLong(ByteBuffer bb, int bi, long x, boolean bigEndian) { |
|
423 |
if (bigEndian) |
|
424 |
putLongB(bb, bi, x); |
|
425 |
else |
|
426 |
putLongL(bb, bi, x); |
|
427 |
} |
|
428 |
||
429 |
static void putLong(long a, long x, boolean bigEndian) { |
|
430 |
if (bigEndian) |
|
431 |
putLongB(a, x); |
|
432 |
else |
|
433 |
putLongL(a, x); |
|
434 |
} |
|
435 |
||
436 |
||
437 |
// -- get/put float -- |
|
438 |
||
439 |
static float getFloatL(ByteBuffer bb, int bi) { |
|
440 |
return Float.intBitsToFloat(getIntL(bb, bi)); |
|
441 |
} |
|
442 |
||
443 |
static float getFloatL(long a) { |
|
444 |
return Float.intBitsToFloat(getIntL(a)); |
|
445 |
} |
|
446 |
||
447 |
static float getFloatB(ByteBuffer bb, int bi) { |
|
448 |
return Float.intBitsToFloat(getIntB(bb, bi)); |
|
449 |
} |
|
450 |
||
451 |
static float getFloatB(long a) { |
|
452 |
return Float.intBitsToFloat(getIntB(a)); |
|
453 |
} |
|
454 |
||
455 |
static float getFloat(ByteBuffer bb, int bi, boolean bigEndian) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
456 |
return bigEndian ? getFloatB(bb, bi) : getFloatL(bb, bi); |
2 | 457 |
} |
458 |
||
459 |
static float getFloat(long a, boolean bigEndian) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
460 |
return bigEndian ? getFloatB(a) : getFloatL(a); |
2 | 461 |
} |
462 |
||
463 |
static void putFloatL(ByteBuffer bb, int bi, float x) { |
|
464 |
putIntL(bb, bi, Float.floatToRawIntBits(x)); |
|
465 |
} |
|
466 |
||
467 |
static void putFloatL(long a, float x) { |
|
468 |
putIntL(a, Float.floatToRawIntBits(x)); |
|
469 |
} |
|
470 |
||
471 |
static void putFloatB(ByteBuffer bb, int bi, float x) { |
|
472 |
putIntB(bb, bi, Float.floatToRawIntBits(x)); |
|
473 |
} |
|
474 |
||
475 |
static void putFloatB(long a, float x) { |
|
476 |
putIntB(a, Float.floatToRawIntBits(x)); |
|
477 |
} |
|
478 |
||
479 |
static void putFloat(ByteBuffer bb, int bi, float x, boolean bigEndian) { |
|
480 |
if (bigEndian) |
|
481 |
putFloatB(bb, bi, x); |
|
482 |
else |
|
483 |
putFloatL(bb, bi, x); |
|
484 |
} |
|
485 |
||
486 |
static void putFloat(long a, float x, boolean bigEndian) { |
|
487 |
if (bigEndian) |
|
488 |
putFloatB(a, x); |
|
489 |
else |
|
490 |
putFloatL(a, x); |
|
491 |
} |
|
492 |
||
493 |
||
494 |
// -- get/put double -- |
|
495 |
||
496 |
static double getDoubleL(ByteBuffer bb, int bi) { |
|
497 |
return Double.longBitsToDouble(getLongL(bb, bi)); |
|
498 |
} |
|
499 |
||
500 |
static double getDoubleL(long a) { |
|
501 |
return Double.longBitsToDouble(getLongL(a)); |
|
502 |
} |
|
503 |
||
504 |
static double getDoubleB(ByteBuffer bb, int bi) { |
|
505 |
return Double.longBitsToDouble(getLongB(bb, bi)); |
|
506 |
} |
|
507 |
||
508 |
static double getDoubleB(long a) { |
|
509 |
return Double.longBitsToDouble(getLongB(a)); |
|
510 |
} |
|
511 |
||
512 |
static double getDouble(ByteBuffer bb, int bi, boolean bigEndian) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
513 |
return bigEndian ? getDoubleB(bb, bi) : getDoubleL(bb, bi); |
2 | 514 |
} |
515 |
||
516 |
static double getDouble(long a, boolean bigEndian) { |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
517 |
return bigEndian ? getDoubleB(a) : getDoubleL(a); |
2 | 518 |
} |
519 |
||
520 |
static void putDoubleL(ByteBuffer bb, int bi, double x) { |
|
521 |
putLongL(bb, bi, Double.doubleToRawLongBits(x)); |
|
522 |
} |
|
523 |
||
524 |
static void putDoubleL(long a, double x) { |
|
525 |
putLongL(a, Double.doubleToRawLongBits(x)); |
|
526 |
} |
|
527 |
||
528 |
static void putDoubleB(ByteBuffer bb, int bi, double x) { |
|
529 |
putLongB(bb, bi, Double.doubleToRawLongBits(x)); |
|
530 |
} |
|
531 |
||
532 |
static void putDoubleB(long a, double x) { |
|
533 |
putLongB(a, Double.doubleToRawLongBits(x)); |
|
534 |
} |
|
535 |
||
536 |
static void putDouble(ByteBuffer bb, int bi, double x, boolean bigEndian) { |
|
537 |
if (bigEndian) |
|
538 |
putDoubleB(bb, bi, x); |
|
539 |
else |
|
540 |
putDoubleL(bb, bi, x); |
|
541 |
} |
|
542 |
||
543 |
static void putDouble(long a, double x, boolean bigEndian) { |
|
544 |
if (bigEndian) |
|
545 |
putDoubleB(a, x); |
|
546 |
else |
|
547 |
putDoubleL(a, x); |
|
548 |
} |
|
549 |
||
550 |
||
551 |
// -- Unsafe access -- |
|
552 |
||
553 |
private static final Unsafe unsafe = Unsafe.getUnsafe(); |
|
554 |
||
555 |
private static byte _get(long a) { |
|
556 |
return unsafe.getByte(a); |
|
557 |
} |
|
558 |
||
559 |
private static void _put(long a, byte b) { |
|
560 |
unsafe.putByte(a, b); |
|
561 |
} |
|
562 |
||
563 |
static Unsafe unsafe() { |
|
564 |
return unsafe; |
|
565 |
} |
|
566 |
||
567 |
||
568 |
// -- Processor and memory-system properties -- |
|
569 |
||
30338
957ee8b5a33a
8026049: (bf) Intrinsify ByteBuffer.put{Int, Double, Float, ...} methods
aph
parents:
25859
diff
changeset
|
570 |
private static final ByteOrder byteOrder |
957ee8b5a33a
8026049: (bf) Intrinsify ByteBuffer.put{Int, Double, Float, ...} methods
aph
parents:
25859
diff
changeset
|
571 |
= unsafe.isBigEndian() ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN; |
2 | 572 |
|
573 |
static ByteOrder byteOrder() { |
|
574 |
return byteOrder; |
|
575 |
} |
|
576 |
||
577 |
private static int pageSize = -1; |
|
578 |
||
579 |
static int pageSize() { |
|
580 |
if (pageSize == -1) |
|
581 |
pageSize = unsafe().pageSize(); |
|
582 |
return pageSize; |
|
583 |
} |
|
584 |
||
6128
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5994
diff
changeset
|
585 |
static int pageCount(long size) { |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5994
diff
changeset
|
586 |
return (int)(size + (long)pageSize() - 1L) / pageSize(); |
f95e5fdc1a65
6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents:
5994
diff
changeset
|
587 |
} |
2 | 588 |
|
30338
957ee8b5a33a
8026049: (bf) Intrinsify ByteBuffer.put{Int, Double, Float, ...} methods
aph
parents:
25859
diff
changeset
|
589 |
private static boolean unaligned = unsafe.unalignedAccess(); |
2 | 590 |
|
591 |
static boolean unaligned() { |
|
592 |
return unaligned; |
|
593 |
} |
|
594 |
||
595 |
||
596 |
// -- Direct memory management -- |
|
597 |
||
598 |
// A user-settable upper limit on the maximum amount of allocatable |
|
599 |
// direct buffer memory. This value may be changed during VM |
|
600 |
// initialization if it is launched with "-XX:MaxDirectMemorySize=<size>". |
|
601 |
private static volatile long maxMemory = VM.maxDirectMemory(); |
|
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
602 |
private static final AtomicLong reservedMemory = new AtomicLong(); |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
603 |
private static final AtomicLong totalCapacity = new AtomicLong(); |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
604 |
private static final AtomicLong count = new AtomicLong(); |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
605 |
private static volatile boolean memoryLimitSet = false; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
606 |
// max. number of sleeps during try-reserving with exponentially |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
607 |
// increasing delay before throwing OutOfMemoryError: |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
608 |
// 1, 2, 4, 8, 16, 32, 64, 128, 256 (total 511 ms ~ 0.5 s) |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
609 |
// which means that OOME will be thrown after 0.5 s of trying |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
610 |
private static final int MAX_SLEEPS = 9; |
2 | 611 |
|
612 |
// These methods should be called whenever direct memory is allocated or |
|
613 |
// freed. They allow the user to control the amount of direct memory |
|
614 |
// which a process may access. All sizes are specified in bytes. |
|
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
615 |
static void reserveMemory(long size, int cap) { |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
616 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
617 |
if (!memoryLimitSet && VM.isBooted()) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
618 |
maxMemory = VM.maxDirectMemory(); |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
619 |
memoryLimitSet = true; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
620 |
} |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
621 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
622 |
// optimist! |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
623 |
if (tryReserveMemory(size, cap)) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
624 |
return; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
625 |
} |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
626 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
627 |
final JavaLangRefAccess jlra = SharedSecrets.getJavaLangRefAccess(); |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
628 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
629 |
// retry while helping enqueue pending Reference objects |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
630 |
// which includes executing pending Cleaner(s) which includes |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
631 |
// Cleaner(s) that free direct buffer memory |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
632 |
while (jlra.tryHandlePendingReference()) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
633 |
if (tryReserveMemory(size, cap)) { |
2 | 634 |
return; |
635 |
} |
|
636 |
} |
|
637 |
||
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
638 |
// trigger VM's Reference processing |
2 | 639 |
System.gc(); |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
640 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
641 |
// a retry loop with exponential back-off delays |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
642 |
// (this gives VM some time to do it's job) |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
643 |
boolean interrupted = false; |
2 | 644 |
try { |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
645 |
long sleepTime = 1; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
646 |
int sleeps = 0; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
647 |
while (true) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
648 |
if (tryReserveMemory(size, cap)) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
649 |
return; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
650 |
} |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
651 |
if (sleeps >= MAX_SLEEPS) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
652 |
break; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
653 |
} |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
654 |
if (!jlra.tryHandlePendingReference()) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
655 |
try { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
656 |
Thread.sleep(sleepTime); |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
657 |
sleepTime <<= 1; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
658 |
sleeps++; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
659 |
} catch (InterruptedException e) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
660 |
interrupted = true; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
661 |
} |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
662 |
} |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
663 |
} |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
664 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
665 |
// no luck |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
666 |
throw new OutOfMemoryError("Direct buffer memory"); |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
667 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
668 |
} finally { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
669 |
if (interrupted) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
670 |
// don't swallow interrupts |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
671 |
Thread.currentThread().interrupt(); |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
672 |
} |
2 | 673 |
} |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
674 |
} |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
675 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
676 |
private static boolean tryReserveMemory(long size, int cap) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
677 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
678 |
// -XX:MaxDirectMemorySize limits the total capacity rather than the |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
679 |
// actual memory usage, which will differ when buffers are page |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
680 |
// aligned. |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
681 |
long totalCap; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
682 |
while (cap <= maxMemory - (totalCap = totalCapacity.get())) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
683 |
if (totalCapacity.compareAndSet(totalCap, totalCap + cap)) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
684 |
reservedMemory.addAndGet(size); |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
685 |
count.incrementAndGet(); |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
686 |
return true; |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
687 |
} |
2 | 688 |
} |
689 |
||
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
690 |
return false; |
2 | 691 |
} |
692 |
||
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
693 |
|
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
694 |
static void unreserveMemory(long size, int cap) { |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
695 |
long cnt = count.decrementAndGet(); |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
696 |
long reservedMem = reservedMemory.addAndGet(-size); |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
697 |
long totalCap = totalCapacity.addAndGet(-cap); |
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
698 |
assert cnt >= 0 && reservedMem >= 0 && totalCap >= 0; |
2 | 699 |
} |
700 |
||
3066
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
701 |
// -- Monitoring of direct buffer usage -- |
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
702 |
|
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
703 |
static { |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
704 |
// setup access to this package in SharedSecrets |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
705 |
sun.misc.SharedSecrets.setJavaNioAccess( |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
706 |
new sun.misc.JavaNioAccess() { |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
707 |
@Override |
3066
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
708 |
public sun.misc.JavaNioAccess.BufferPool getDirectBufferPool() { |
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
709 |
return new sun.misc.JavaNioAccess.BufferPool() { |
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
710 |
@Override |
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
711 |
public String getName() { |
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
712 |
return "direct"; |
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
713 |
} |
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
714 |
@Override |
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
715 |
public long getCount() { |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
716 |
return Bits.count.get(); |
3066
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
717 |
} |
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
718 |
@Override |
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
719 |
public long getTotalCapacity() { |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
720 |
return Bits.totalCapacity.get(); |
3066
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
721 |
} |
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
722 |
@Override |
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
723 |
public long getMemoryUsed() { |
23009
e2c92ddeb57f
6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents:
12538
diff
changeset
|
724 |
return Bits.reservedMemory.get(); |
3066
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
725 |
} |
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
726 |
}; |
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
727 |
} |
10325
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
728 |
@Override |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
729 |
public ByteBuffer newDirectByteBuffer(long addr, int cap, Object ob) { |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
730 |
return new DirectByteBuffer(addr, cap, ob); |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
731 |
} |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
732 |
@Override |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
733 |
public void truncate(Buffer buf) { |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
734 |
buf.truncate(); |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
735 |
} |
3066
cd3861104f4d
6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents:
1247
diff
changeset
|
736 |
}); |
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
737 |
} |
2 | 738 |
|
739 |
// -- Bulk get/put acceleration -- |
|
740 |
||
741 |
// These numbers represent the point at which we have empirically |
|
742 |
// determined that the average cost of a JNI call exceeds the expense |
|
743 |
// of an element by element copy. These numbers may change over time. |
|
744 |
static final int JNI_COPY_TO_ARRAY_THRESHOLD = 6; |
|
745 |
static final int JNI_COPY_FROM_ARRAY_THRESHOLD = 6; |
|
746 |
||
1151
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
747 |
// This number limits the number of bytes to copy per call to Unsafe's |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
748 |
// copyMemory method. A limit is imposed to allow for safepoint polling |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
749 |
// during a large copy |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
750 |
static final long UNSAFE_COPY_THRESHOLD = 1024L * 1024L; |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
751 |
|
2 | 752 |
// These methods do no bounds checking. Verification that the copy will not |
753 |
// result in memory corruption should be done prior to invocation. |
|
754 |
// All positions and lengths are specified in bytes. |
|
755 |
||
1151
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
756 |
/** |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
757 |
* Copy from given source array to destination address. |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
758 |
* |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
759 |
* @param src |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
760 |
* source array |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
761 |
* @param srcBaseOffset |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
762 |
* offset of first element of storage in source array |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
763 |
* @param srcPos |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
764 |
* offset within source array of the first element to read |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
765 |
* @param dstAddr |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
766 |
* destination address |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
767 |
* @param length |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
768 |
* number of bytes to copy |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
769 |
*/ |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
770 |
static void copyFromArray(Object src, long srcBaseOffset, long srcPos, |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
771 |
long dstAddr, long length) |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
772 |
{ |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
773 |
long offset = srcBaseOffset + srcPos; |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
774 |
while (length > 0) { |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
775 |
long size = (length > UNSAFE_COPY_THRESHOLD) ? UNSAFE_COPY_THRESHOLD : length; |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
776 |
unsafe.copyMemory(src, offset, null, dstAddr, size); |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
777 |
length -= size; |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
778 |
offset += size; |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
779 |
dstAddr += size; |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
780 |
} |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
781 |
} |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
782 |
|
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
783 |
/** |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
784 |
* Copy from source address into given destination array. |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
785 |
* |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
786 |
* @param srcAddr |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
787 |
* source address |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
788 |
* @param dst |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
789 |
* destination array |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
790 |
* @param dstBaseOffset |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
791 |
* offset of first element of storage in destination array |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
792 |
* @param dstPos |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
793 |
* offset within destination array of the first element to write |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
794 |
* @param length |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
795 |
* number of bytes to copy |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
796 |
*/ |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
797 |
static void copyToArray(long srcAddr, Object dst, long dstBaseOffset, long dstPos, |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
798 |
long length) |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
799 |
{ |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
800 |
long offset = dstBaseOffset + dstPos; |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
801 |
while (length > 0) { |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
802 |
long size = (length > UNSAFE_COPY_THRESHOLD) ? UNSAFE_COPY_THRESHOLD : length; |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
803 |
unsafe.copyMemory(null, srcAddr, dst, offset, size); |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
804 |
length -= size; |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
805 |
srcAddr += size; |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
806 |
offset += size; |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
807 |
} |
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
808 |
} |
2 | 809 |
|
810 |
static void copyFromCharArray(Object src, long srcPos, long dstAddr, |
|
811 |
long length) |
|
812 |
{ |
|
813 |
copyFromShortArray(src, srcPos, dstAddr, length); |
|
814 |
} |
|
815 |
||
816 |
static void copyToCharArray(long srcAddr, Object dst, long dstPos, |
|
817 |
long length) |
|
818 |
{ |
|
819 |
copyToShortArray(srcAddr, dst, dstPos, length); |
|
820 |
} |
|
821 |
||
822 |
static native void copyFromShortArray(Object src, long srcPos, long dstAddr, |
|
823 |
long length); |
|
824 |
static native void copyToShortArray(long srcAddr, Object dst, long dstPos, |
|
825 |
long length); |
|
826 |
||
827 |
static native void copyFromIntArray(Object src, long srcPos, long dstAddr, |
|
828 |
long length); |
|
829 |
static native void copyToIntArray(long srcAddr, Object dst, long dstPos, |
|
5994
267f4ce6a585
6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents:
5506
diff
changeset
|
830 |
long length); |
2 | 831 |
|
832 |
static native void copyFromLongArray(Object src, long srcPos, long dstAddr, |
|
833 |
long length); |
|
834 |
static native void copyToLongArray(long srcAddr, Object dst, long dstPos, |
|
835 |
long length); |
|
836 |
||
837 |
} |