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