author | fweimer |
Mon, 06 Aug 2018 14:55:04 -0700 | |
changeset 51318 | 60f9b6b5ad86 |
parent 49210 | 7c795d301dbf |
child 53855 | 7c362992527a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
2 |
* Copyright (c) 2000, 2018, 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 |
#warn This file is preprocessed before being compiled |
|
27 |
||
28 |
package java.nio; |
|
29 |
||
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
30 |
import java.io.FileDescriptor; |
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
31 |
import java.lang.ref.Reference; |
34882 | 32 |
import jdk.internal.misc.VM; |
35641
da165fd9c886
8148117: Move sun.misc.Cleaner to jdk.internal.ref
chegar
parents:
34882
diff
changeset
|
33 |
import jdk.internal.ref.Cleaner; |
2 | 34 |
import sun.nio.ch.DirectBuffer; |
35 |
||
36 |
||
37 |
class Direct$Type$Buffer$RW$$BO$ |
|
38 |
#if[rw] |
|
39 |
extends {#if[byte]?Mapped$Type$Buffer:$Type$Buffer} |
|
40 |
#else[rw] |
|
41 |
extends Direct$Type$Buffer$BO$ |
|
42 |
#end[rw] |
|
43 |
implements DirectBuffer |
|
44 |
{ |
|
45 |
||
46 |
#if[rw] |
|
47 |
||
1151
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
48 |
// Cached array base offset |
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
49 |
private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset($type$[].class); |
1151
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
50 |
|
2 | 51 |
// Cached unaligned-access capability |
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
52 |
protected static final boolean UNALIGNED = Bits.unaligned(); |
2 | 53 |
|
54 |
// Base address, used in all indexing calculations |
|
55 |
// NOTE: moved up to Buffer.java for speed in JNI GetDirectBufferAddress |
|
56 |
// protected long address; |
|
57 |
||
10325
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
58 |
// An object attached to this buffer. If this buffer is a view of another |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
59 |
// buffer then we use this field to keep a reference to that buffer to |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
60 |
// ensure that its memory isn't freed before we are done with it. |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
61 |
private final Object att; |
2 | 62 |
|
10325
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
63 |
public Object attachment() { |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
64 |
return att; |
2 | 65 |
} |
66 |
||
67 |
#if[byte] |
|
68 |
||
69 |
private static class Deallocator |
|
70 |
implements Runnable |
|
71 |
{ |
|
72 |
||
73 |
private long address; |
|
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
74 |
private long size; |
2 | 75 |
private int capacity; |
76 |
||
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
77 |
private Deallocator(long address, long size, int capacity) { |
2 | 78 |
assert (address != 0); |
79 |
this.address = address; |
|
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
80 |
this.size = size; |
2 | 81 |
this.capacity = capacity; |
82 |
} |
|
83 |
||
84 |
public void run() { |
|
85 |
if (address == 0) { |
|
86 |
// Paranoia |
|
87 |
return; |
|
88 |
} |
|
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
89 |
UNSAFE.freeMemory(address); |
2 | 90 |
address = 0; |
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
91 |
Bits.unreserveMemory(size, capacity); |
2 | 92 |
} |
93 |
||
94 |
} |
|
95 |
||
96 |
private final Cleaner cleaner; |
|
97 |
||
98 |
public Cleaner cleaner() { return cleaner; } |
|
99 |
||
100 |
#else[byte] |
|
101 |
||
102 |
public Cleaner cleaner() { return null; } |
|
103 |
||
104 |
#end[byte] |
|
105 |
||
106 |
#end[rw] |
|
107 |
||
108 |
#if[byte] |
|
109 |
||
110 |
// Primary constructor |
|
111 |
// |
|
112 |
Direct$Type$Buffer$RW$(int cap) { // package-private |
|
113 |
#if[rw] |
|
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
114 |
super(-1, 0, cap, cap); |
6902
3352f8839320
4837564: (bf) Please make DirectByteBuffer performance enhancements
alanb
parents:
5506
diff
changeset
|
115 |
boolean pa = VM.isDirectMemoryPageAligned(); |
2 | 116 |
int ps = Bits.pageSize(); |
6902
3352f8839320
4837564: (bf) Please make DirectByteBuffer performance enhancements
alanb
parents:
5506
diff
changeset
|
117 |
long size = Math.max(1L, (long)cap + (pa ? ps : 0)); |
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
118 |
Bits.reserveMemory(size, cap); |
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
119 |
|
2 | 120 |
long base = 0; |
121 |
try { |
|
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
122 |
base = UNSAFE.allocateMemory(size); |
2 | 123 |
} catch (OutOfMemoryError x) { |
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
124 |
Bits.unreserveMemory(size, cap); |
2 | 125 |
throw x; |
126 |
} |
|
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
127 |
UNSAFE.setMemory(base, size, (byte) 0); |
6902
3352f8839320
4837564: (bf) Please make DirectByteBuffer performance enhancements
alanb
parents:
5506
diff
changeset
|
128 |
if (pa && (base % ps != 0)) { |
2 | 129 |
// Round up to page boundary |
130 |
address = base + ps - (base & (ps - 1)); |
|
131 |
} else { |
|
132 |
address = base; |
|
133 |
} |
|
1143
645d4b930f93
6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents:
2
diff
changeset
|
134 |
cleaner = Cleaner.create(this, new Deallocator(base, size, cap)); |
10325
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
135 |
att = null; |
2 | 136 |
#else[rw] |
137 |
super(cap); |
|
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
36222
diff
changeset
|
138 |
this.isReadOnly = true; |
2 | 139 |
#end[rw] |
140 |
} |
|
141 |
||
142 |
#if[rw] |
|
143 |
||
10325
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
144 |
// Invoked to construct a direct ByteBuffer referring to the block of |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
145 |
// memory. A given arbitrary object may also be attached to the buffer. |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
146 |
// |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
147 |
Direct$Type$Buffer(long addr, int cap, Object ob) { |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
148 |
super(-1, 0, cap, cap); |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
149 |
address = addr; |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
150 |
cleaner = null; |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
151 |
att = ob; |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
152 |
} |
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
153 |
|
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
154 |
|
2 | 155 |
// Invoked only by JNI: NewDirectByteBuffer(void*, long) |
156 |
// |
|
157 |
private Direct$Type$Buffer(long addr, int cap) { |
|
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
158 |
super(-1, 0, cap, cap); |
2 | 159 |
address = addr; |
160 |
cleaner = null; |
|
10325
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
161 |
att = null; |
2 | 162 |
} |
163 |
||
164 |
#end[rw] |
|
165 |
||
166 |
// For memory-mapped buffers -- invoked by FileChannelImpl via reflection |
|
167 |
// |
|
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
168 |
protected Direct$Type$Buffer$RW$(int cap, long addr, |
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
169 |
FileDescriptor fd, |
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
170 |
Runnable unmapper) |
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
171 |
{ |
2 | 172 |
#if[rw] |
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
173 |
super(-1, 0, cap, cap, fd); |
2 | 174 |
address = addr; |
175 |
cleaner = Cleaner.create(this, unmapper); |
|
10325
b72c20cd583a
7047325: Internal API to improve management of direct buffers
coffeys
parents:
7668
diff
changeset
|
176 |
att = null; |
2 | 177 |
#else[rw] |
7025
6e002f9a2899
6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents:
6902
diff
changeset
|
178 |
super(cap, addr, fd, unmapper); |
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
36222
diff
changeset
|
179 |
this.isReadOnly = true; |
2 | 180 |
#end[rw] |
181 |
} |
|
182 |
||
183 |
#end[byte] |
|
184 |
||
185 |
// For duplicates and slices |
|
186 |
// |
|
187 |
Direct$Type$Buffer$RW$$BO$(DirectBuffer db, // package-private |
|
188 |
int mark, int pos, int lim, int cap, |
|
189 |
int off) |
|
190 |
{ |
|
191 |
#if[rw] |
|
192 |
super(mark, pos, lim, cap); |
|
193 |
address = db.address() + off; |
|
194 |
#if[byte] |
|
195 |
cleaner = null; |
|
196 |
#end[byte] |
|
51318
60f9b6b5ad86
8208362: (bf) Long chains created by direct Buffer::slice
fweimer
parents:
49210
diff
changeset
|
197 |
Object attachment = db.attachment(); |
60f9b6b5ad86
8208362: (bf) Long chains created by direct Buffer::slice
fweimer
parents:
49210
diff
changeset
|
198 |
att = (attachment == null ? db : attachment); |
2 | 199 |
#else[rw] |
200 |
super(db, mark, pos, lim, cap, off); |
|
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
36222
diff
changeset
|
201 |
this.isReadOnly = true; |
2 | 202 |
#end[rw] |
203 |
} |
|
204 |
||
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
205 |
@Override |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
206 |
Object base() { |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
207 |
return null; |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
208 |
} |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
209 |
|
2 | 210 |
public $Type$Buffer slice() { |
211 |
int pos = this.position(); |
|
212 |
int lim = this.limit(); |
|
213 |
assert (pos <= lim); |
|
214 |
int rem = (pos <= lim ? lim - pos : 0); |
|
215 |
int off = (pos << $LG_BYTES_PER_VALUE$); |
|
216 |
assert (off >= 0); |
|
217 |
return new Direct$Type$Buffer$RW$$BO$(this, -1, 0, rem, rem, off); |
|
218 |
} |
|
219 |
||
36933
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
36222
diff
changeset
|
220 |
#if[byte] |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
36222
diff
changeset
|
221 |
public $Type$Buffer slice(int pos, int lim) { |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
36222
diff
changeset
|
222 |
assert (pos >= 0); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
36222
diff
changeset
|
223 |
assert (pos <= lim); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
36222
diff
changeset
|
224 |
int rem = lim - pos; |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
36222
diff
changeset
|
225 |
return new Direct$Type$Buffer$RW$$BO$(this, -1, 0, rem, rem, pos); |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
36222
diff
changeset
|
226 |
} |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
36222
diff
changeset
|
227 |
#end[byte] |
3e6453e2d833
8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents:
36222
diff
changeset
|
228 |
|
2 | 229 |
public $Type$Buffer duplicate() { |
230 |
return new Direct$Type$Buffer$RW$$BO$(this, |
|
231 |
this.markValue(), |
|
232 |
this.position(), |
|
233 |
this.limit(), |
|
234 |
this.capacity(), |
|
235 |
0); |
|
236 |
} |
|
237 |
||
238 |
public $Type$Buffer asReadOnlyBuffer() { |
|
239 |
#if[rw] |
|
240 |
return new Direct$Type$BufferR$BO$(this, |
|
241 |
this.markValue(), |
|
242 |
this.position(), |
|
243 |
this.limit(), |
|
244 |
this.capacity(), |
|
245 |
0); |
|
246 |
#else[rw] |
|
247 |
return duplicate(); |
|
248 |
#end[rw] |
|
249 |
} |
|
250 |
||
251 |
#if[rw] |
|
252 |
||
253 |
public long address() { |
|
254 |
return address; |
|
255 |
} |
|
256 |
||
257 |
private long ix(int i) { |
|
33300 | 258 |
return address + ((long)i << $LG_BYTES_PER_VALUE$); |
2 | 259 |
} |
260 |
||
261 |
public $type$ get() { |
|
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
262 |
try { |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
263 |
return $fromBits$($swap$(UNSAFE.get$Swaptype$(ix(nextGetIndex())))); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
264 |
} finally { |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
265 |
Reference.reachabilityFence(this); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
266 |
} |
2 | 267 |
} |
268 |
||
269 |
public $type$ get(int i) { |
|
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
270 |
try { |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
271 |
return $fromBits$($swap$(UNSAFE.get$Swaptype$(ix(checkIndex(i))))); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
272 |
} finally { |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
273 |
Reference.reachabilityFence(this); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
274 |
} |
2 | 275 |
} |
276 |
||
17922
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
10325
diff
changeset
|
277 |
#if[streamableType] |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
10325
diff
changeset
|
278 |
$type$ getUnchecked(int i) { |
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
279 |
try { |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
280 |
return $fromBits$($swap$(UNSAFE.get$Swaptype$(ix(i)))); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
281 |
} finally { |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
282 |
Reference.reachabilityFence(this); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
283 |
} |
17922
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
10325
diff
changeset
|
284 |
} |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
10325
diff
changeset
|
285 |
#end[streamableType] |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
10325
diff
changeset
|
286 |
|
2 | 287 |
public $Type$Buffer get($type$[] dst, int offset, int length) { |
288 |
#if[rw] |
|
33300 | 289 |
if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) { |
2 | 290 |
checkBounds(offset, length, dst.length); |
291 |
int pos = position(); |
|
292 |
int lim = limit(); |
|
293 |
assert (pos <= lim); |
|
294 |
int rem = (pos <= lim ? lim - pos : 0); |
|
295 |
if (length > rem) |
|
296 |
throw new BufferUnderflowException(); |
|
297 |
||
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
298 |
long dstOffset = ARRAY_BASE_OFFSET + ((long)offset << $LG_BYTES_PER_VALUE$); |
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
299 |
try { |
1151
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
300 |
#if[!byte] |
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
301 |
if (order() != ByteOrder.nativeOrder()) |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
302 |
UNSAFE.copySwapMemory(null, |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
303 |
ix(pos), |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
304 |
dst, |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
305 |
dstOffset, |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
306 |
(long)length << $LG_BYTES_PER_VALUE$, |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
307 |
(long)1 << $LG_BYTES_PER_VALUE$); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
308 |
else |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
309 |
#end[!byte] |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
310 |
UNSAFE.copyMemory(null, |
36222
dfed693e648b
8149596: Remove java.nio.Bits copy wrapper methods
mikael
parents:
35641
diff
changeset
|
311 |
ix(pos), |
dfed693e648b
8149596: Remove java.nio.Bits copy wrapper methods
mikael
parents:
35641
diff
changeset
|
312 |
dst, |
dfed693e648b
8149596: Remove java.nio.Bits copy wrapper methods
mikael
parents:
35641
diff
changeset
|
313 |
dstOffset, |
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
314 |
(long)length << $LG_BYTES_PER_VALUE$); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
315 |
} finally { |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
316 |
Reference.reachabilityFence(this); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
317 |
} |
2 | 318 |
position(pos + length); |
319 |
} else { |
|
320 |
super.get(dst, offset, length); |
|
321 |
} |
|
322 |
return this; |
|
323 |
#else[rw] |
|
324 |
throw new ReadOnlyBufferException(); |
|
325 |
#end[rw] |
|
326 |
} |
|
327 |
||
328 |
#end[rw] |
|
329 |
||
330 |
public $Type$Buffer put($type$ x) { |
|
331 |
#if[rw] |
|
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
332 |
try { |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
333 |
UNSAFE.put$Swaptype$(ix(nextPutIndex()), $swap$($toBits$(x))); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
334 |
} finally { |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
335 |
Reference.reachabilityFence(this); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
336 |
} |
2 | 337 |
return this; |
338 |
#else[rw] |
|
339 |
throw new ReadOnlyBufferException(); |
|
340 |
#end[rw] |
|
341 |
} |
|
342 |
||
343 |
public $Type$Buffer put(int i, $type$ x) { |
|
344 |
#if[rw] |
|
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
345 |
try { |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
346 |
UNSAFE.put$Swaptype$(ix(checkIndex(i)), $swap$($toBits$(x))); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
347 |
} finally { |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
348 |
Reference.reachabilityFence(this); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
349 |
} |
2 | 350 |
return this; |
351 |
#else[rw] |
|
352 |
throw new ReadOnlyBufferException(); |
|
353 |
#end[rw] |
|
354 |
} |
|
355 |
||
356 |
public $Type$Buffer put($Type$Buffer src) { |
|
357 |
#if[rw] |
|
358 |
if (src instanceof Direct$Type$Buffer$BO$) { |
|
359 |
if (src == this) |
|
31873
87b015c2cd36
8065556: (bf) Buffer.position and other methods should include detail in IAE
bpb
parents:
25859
diff
changeset
|
360 |
throw createSameBufferException(); |
2 | 361 |
Direct$Type$Buffer$RW$$BO$ sb = (Direct$Type$Buffer$RW$$BO$)src; |
362 |
||
363 |
int spos = sb.position(); |
|
364 |
int slim = sb.limit(); |
|
365 |
assert (spos <= slim); |
|
366 |
int srem = (spos <= slim ? slim - spos : 0); |
|
367 |
||
368 |
int pos = position(); |
|
369 |
int lim = limit(); |
|
370 |
assert (pos <= lim); |
|
371 |
int rem = (pos <= lim ? lim - pos : 0); |
|
372 |
||
373 |
if (srem > rem) |
|
374 |
throw new BufferOverflowException(); |
|
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
375 |
try { |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
376 |
UNSAFE.copyMemory(sb.ix(spos), ix(pos), (long)srem << $LG_BYTES_PER_VALUE$); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
377 |
} finally { |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
378 |
Reference.reachabilityFence(sb); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
379 |
Reference.reachabilityFence(this); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
380 |
} |
2 | 381 |
sb.position(spos + srem); |
382 |
position(pos + srem); |
|
383 |
} else if (src.hb != null) { |
|
384 |
||
385 |
int spos = src.position(); |
|
386 |
int slim = src.limit(); |
|
387 |
assert (spos <= slim); |
|
388 |
int srem = (spos <= slim ? slim - spos : 0); |
|
389 |
||
390 |
put(src.hb, src.offset + spos, srem); |
|
391 |
src.position(spos + srem); |
|
392 |
||
393 |
} else { |
|
394 |
super.put(src); |
|
395 |
} |
|
396 |
return this; |
|
397 |
#else[rw] |
|
398 |
throw new ReadOnlyBufferException(); |
|
399 |
#end[rw] |
|
400 |
} |
|
401 |
||
402 |
public $Type$Buffer put($type$[] src, int offset, int length) { |
|
403 |
#if[rw] |
|
33300 | 404 |
if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) { |
2 | 405 |
checkBounds(offset, length, src.length); |
406 |
int pos = position(); |
|
407 |
int lim = limit(); |
|
408 |
assert (pos <= lim); |
|
409 |
int rem = (pos <= lim ? lim - pos : 0); |
|
410 |
if (length > rem) |
|
411 |
throw new BufferOverflowException(); |
|
412 |
||
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
413 |
long srcOffset = ARRAY_BASE_OFFSET + ((long)offset << $LG_BYTES_PER_VALUE$); |
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
414 |
try { |
1151
4070cecdb99d
6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents:
1143
diff
changeset
|
415 |
#if[!byte] |
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
416 |
if (order() != ByteOrder.nativeOrder()) |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
417 |
UNSAFE.copySwapMemory(src, |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
418 |
srcOffset, |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
419 |
null, |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
420 |
ix(pos), |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
421 |
(long)length << $LG_BYTES_PER_VALUE$, |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
422 |
(long)1 << $LG_BYTES_PER_VALUE$); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
423 |
else |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
424 |
#end[!byte] |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
425 |
UNSAFE.copyMemory(src, |
36222
dfed693e648b
8149596: Remove java.nio.Bits copy wrapper methods
mikael
parents:
35641
diff
changeset
|
426 |
srcOffset, |
dfed693e648b
8149596: Remove java.nio.Bits copy wrapper methods
mikael
parents:
35641
diff
changeset
|
427 |
null, |
dfed693e648b
8149596: Remove java.nio.Bits copy wrapper methods
mikael
parents:
35641
diff
changeset
|
428 |
ix(pos), |
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
429 |
(long)length << $LG_BYTES_PER_VALUE$); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
430 |
} finally { |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
431 |
Reference.reachabilityFence(this); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
432 |
} |
2 | 433 |
position(pos + length); |
434 |
} else { |
|
435 |
super.put(src, offset, length); |
|
436 |
} |
|
437 |
return this; |
|
438 |
#else[rw] |
|
439 |
throw new ReadOnlyBufferException(); |
|
440 |
#end[rw] |
|
441 |
} |
|
442 |
||
443 |
public $Type$Buffer compact() { |
|
444 |
#if[rw] |
|
445 |
int pos = position(); |
|
446 |
int lim = limit(); |
|
447 |
assert (pos <= lim); |
|
448 |
int rem = (pos <= lim ? lim - pos : 0); |
|
49210
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
449 |
try { |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
450 |
UNSAFE.copyMemory(ix(pos), ix(0), (long)rem << $LG_BYTES_PER_VALUE$); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
451 |
} finally { |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
452 |
Reference.reachabilityFence(this); |
7c795d301dbf
8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents:
48356
diff
changeset
|
453 |
} |
2 | 454 |
position(rem); |
455 |
limit(capacity()); |
|
1634
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
1247
diff
changeset
|
456 |
discardMark(); |
2 | 457 |
return this; |
458 |
#else[rw] |
|
459 |
throw new ReadOnlyBufferException(); |
|
460 |
#end[rw] |
|
461 |
} |
|
462 |
||
463 |
public boolean isDirect() { |
|
464 |
return true; |
|
465 |
} |
|
466 |
||
467 |
public boolean isReadOnly() { |
|
468 |
return {#if[rw]?false:true}; |
|
469 |
} |
|
470 |
||
471 |
||
472 |
#if[char] |
|
473 |
||
474 |
public String toString(int start, int end) { |
|
475 |
if ((end > limit()) || (start > end)) |
|
476 |
throw new IndexOutOfBoundsException(); |
|
477 |
try { |
|
478 |
int len = end - start; |
|
479 |
char[] ca = new char[len]; |
|
480 |
CharBuffer cb = CharBuffer.wrap(ca); |
|
481 |
CharBuffer db = this.duplicate(); |
|
482 |
db.position(start); |
|
483 |
db.limit(end); |
|
484 |
cb.put(db); |
|
485 |
return new String(ca); |
|
486 |
} catch (StringIndexOutOfBoundsException x) { |
|
487 |
throw new IndexOutOfBoundsException(); |
|
488 |
} |
|
489 |
} |
|
490 |
||
491 |
||
492 |
// --- Methods to support CharSequence --- |
|
493 |
||
1224
8c1bc7c5dd00
6733145: (bf) CharBuffer.subSequence can be updated to take advantage of covariant returns
martin
parents:
1151
diff
changeset
|
494 |
public CharBuffer subSequence(int start, int end) { |
2 | 495 |
int pos = position(); |
496 |
int lim = limit(); |
|
497 |
assert (pos <= lim); |
|
498 |
pos = (pos <= lim ? pos : lim); |
|
499 |
int len = lim - pos; |
|
500 |
||
501 |
if ((start < 0) || (end > len) || (start > end)) |
|
502 |
throw new IndexOutOfBoundsException(); |
|
2593
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1634
diff
changeset
|
503 |
return new DirectCharBuffer$RW$$BO$(this, |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1634
diff
changeset
|
504 |
-1, |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1634
diff
changeset
|
505 |
pos + start, |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1634
diff
changeset
|
506 |
pos + end, |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1634
diff
changeset
|
507 |
capacity(), |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1634
diff
changeset
|
508 |
offset); |
2 | 509 |
} |
510 |
||
511 |
#end[char] |
|
512 |
||
513 |
||
514 |
||
515 |
#if[!byte] |
|
516 |
||
517 |
public ByteOrder order() { |
|
518 |
#if[boS] |
|
519 |
return ((ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) |
|
520 |
? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN); |
|
521 |
#end[boS] |
|
522 |
#if[boU] |
|
523 |
return ((ByteOrder.nativeOrder() != ByteOrder.BIG_ENDIAN) |
|
524 |
? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN); |
|
525 |
#end[boU] |
|
526 |
} |
|
527 |
||
528 |
#end[!byte] |
|
529 |
||
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
530 |
#if[char] |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
531 |
ByteOrder charRegionOrder() { |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
532 |
return order(); |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
533 |
} |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
534 |
#end[char] |
2 | 535 |
|
536 |
||
537 |
#if[byte] |
|
538 |
// #BIN |
|
539 |
// |
|
540 |
// Binary-data access methods for short, char, int, long, float, |
|
541 |
// and double will be inserted here |
|
542 |
||
543 |
#end[byte] |
|
544 |
||
545 |
} |