author | psandoz |
Wed, 20 Dec 2017 09:14:52 -0800 | |
changeset 48356 | 29e165bdf669 |
parent 47216 | 71c04702a3d5 |
child 53959 | 1542e63eb537 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
40180
e53a90882a23
8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
25859
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 |
||
40180
e53a90882a23
8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
25859
diff
changeset
|
30 |
import jdk.internal.misc.Unsafe; |
e53a90882a23
8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
25859
diff
changeset
|
31 |
|
2 | 32 |
|
33 |
class ByteBufferAs$Type$Buffer$RW$$BO$ // package-private |
|
34 |
extends {#if[ro]?ByteBufferAs}$Type$Buffer{#if[ro]?$BO$} |
|
35 |
{ |
|
36 |
||
37 |
#if[rw] |
|
38 |
||
39 |
protected final ByteBuffer bb; |
|
40 |
||
41 |
#end[rw] |
|
42 |
||
43 |
ByteBufferAs$Type$Buffer$RW$$BO$(ByteBuffer bb) { // package-private |
|
44 |
#if[rw] |
|
45 |
super(-1, 0, |
|
46 |
bb.remaining() >> $LG_BYTES_PER_VALUE$, |
|
47 |
bb.remaining() >> $LG_BYTES_PER_VALUE$); |
|
48 |
this.bb = bb; |
|
49 |
// enforce limit == capacity |
|
50 |
int cap = this.capacity(); |
|
51 |
this.limit(cap); |
|
52 |
int pos = this.position(); |
|
53 |
assert (pos <= cap); |
|
40195
a13e4945de1d
8162458: Buffer view implementations use incorrect offset for Unsafe access
psandoz
parents:
40180
diff
changeset
|
54 |
address = bb.address; |
2 | 55 |
#else[rw] |
56 |
super(bb); |
|
57 |
#end[rw] |
|
58 |
} |
|
59 |
||
60 |
ByteBufferAs$Type$Buffer$RW$$BO$(ByteBuffer bb, |
|
61 |
int mark, int pos, int lim, int cap, |
|
40195
a13e4945de1d
8162458: Buffer view implementations use incorrect offset for Unsafe access
psandoz
parents:
40180
diff
changeset
|
62 |
long addr) |
2 | 63 |
{ |
64 |
#if[rw] |
|
65 |
super(mark, pos, lim, cap); |
|
66 |
this.bb = bb; |
|
40195
a13e4945de1d
8162458: Buffer view implementations use incorrect offset for Unsafe access
psandoz
parents:
40180
diff
changeset
|
67 |
address = addr; |
a13e4945de1d
8162458: Buffer view implementations use incorrect offset for Unsafe access
psandoz
parents:
40180
diff
changeset
|
68 |
assert address >= bb.address; |
2 | 69 |
#else[rw] |
40195
a13e4945de1d
8162458: Buffer view implementations use incorrect offset for Unsafe access
psandoz
parents:
40180
diff
changeset
|
70 |
super(bb, mark, pos, lim, cap, addr); |
2 | 71 |
#end[rw] |
72 |
} |
|
73 |
||
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
74 |
@Override |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
75 |
Object base() { |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
76 |
return bb.hb; |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
77 |
} |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
78 |
|
2 | 79 |
public $Type$Buffer slice() { |
80 |
int pos = this.position(); |
|
81 |
int lim = this.limit(); |
|
82 |
assert (pos <= lim); |
|
83 |
int rem = (pos <= lim ? lim - pos : 0); |
|
40195
a13e4945de1d
8162458: Buffer view implementations use incorrect offset for Unsafe access
psandoz
parents:
40180
diff
changeset
|
84 |
long addr = byteOffset(pos); |
a13e4945de1d
8162458: Buffer view implementations use incorrect offset for Unsafe access
psandoz
parents:
40180
diff
changeset
|
85 |
return new ByteBufferAs$Type$Buffer$RW$$BO$(bb, -1, 0, rem, rem, addr); |
2 | 86 |
} |
87 |
||
88 |
public $Type$Buffer duplicate() { |
|
89 |
return new ByteBufferAs$Type$Buffer$RW$$BO$(bb, |
|
90 |
this.markValue(), |
|
91 |
this.position(), |
|
92 |
this.limit(), |
|
93 |
this.capacity(), |
|
40195
a13e4945de1d
8162458: Buffer view implementations use incorrect offset for Unsafe access
psandoz
parents:
40180
diff
changeset
|
94 |
address); |
2 | 95 |
} |
96 |
||
97 |
public $Type$Buffer asReadOnlyBuffer() { |
|
98 |
#if[rw] |
|
99 |
return new ByteBufferAs$Type$BufferR$BO$(bb, |
|
100 |
this.markValue(), |
|
101 |
this.position(), |
|
102 |
this.limit(), |
|
103 |
this.capacity(), |
|
40195
a13e4945de1d
8162458: Buffer view implementations use incorrect offset for Unsafe access
psandoz
parents:
40180
diff
changeset
|
104 |
address); |
2 | 105 |
#else[rw] |
106 |
return duplicate(); |
|
107 |
#end[rw] |
|
108 |
} |
|
109 |
||
110 |
#if[rw] |
|
111 |
||
40195
a13e4945de1d
8162458: Buffer view implementations use incorrect offset for Unsafe access
psandoz
parents:
40180
diff
changeset
|
112 |
private int ix(int i) { |
a13e4945de1d
8162458: Buffer view implementations use incorrect offset for Unsafe access
psandoz
parents:
40180
diff
changeset
|
113 |
int off = (int) (address - bb.address); |
a13e4945de1d
8162458: Buffer view implementations use incorrect offset for Unsafe access
psandoz
parents:
40180
diff
changeset
|
114 |
return (i << $LG_BYTES_PER_VALUE$) + off; |
2 | 115 |
} |
116 |
||
40195
a13e4945de1d
8162458: Buffer view implementations use incorrect offset for Unsafe access
psandoz
parents:
40180
diff
changeset
|
117 |
protected long byteOffset(long i) { |
a13e4945de1d
8162458: Buffer view implementations use incorrect offset for Unsafe access
psandoz
parents:
40180
diff
changeset
|
118 |
return (i << $LG_BYTES_PER_VALUE$) + address; |
40180
e53a90882a23
8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
25859
diff
changeset
|
119 |
} |
e53a90882a23
8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
25859
diff
changeset
|
120 |
|
2 | 121 |
public $type$ get() { |
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
122 |
$memtype$ x = UNSAFE.get$Memtype$Unaligned(bb.hb, byteOffset(nextGetIndex()), |
40180
e53a90882a23
8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
25859
diff
changeset
|
123 |
{#if[boB]?true:false}); |
e53a90882a23
8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
25859
diff
changeset
|
124 |
return $fromBits$(x); |
2 | 125 |
} |
126 |
||
127 |
public $type$ get(int i) { |
|
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
128 |
$memtype$ x = UNSAFE.get$Memtype$Unaligned(bb.hb, byteOffset(checkIndex(i)), |
40180
e53a90882a23
8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
25859
diff
changeset
|
129 |
{#if[boB]?true:false}); |
e53a90882a23
8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
25859
diff
changeset
|
130 |
return $fromBits$(x); |
2 | 131 |
} |
132 |
||
17922
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
5506
diff
changeset
|
133 |
#if[streamableType] |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
5506
diff
changeset
|
134 |
$type$ getUnchecked(int i) { |
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
135 |
$memtype$ x = UNSAFE.get$Memtype$Unaligned(bb.hb, byteOffset(i), |
40180
e53a90882a23
8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
25859
diff
changeset
|
136 |
{#if[boB]?true:false}); |
e53a90882a23
8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
25859
diff
changeset
|
137 |
return $fromBits$(x); |
17922
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
5506
diff
changeset
|
138 |
} |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
5506
diff
changeset
|
139 |
#end[streamableType] |
d56eec572de5
8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents:
5506
diff
changeset
|
140 |
|
2 | 141 |
#end[rw] |
142 |
||
143 |
public $Type$Buffer put($type$ x) { |
|
144 |
#if[rw] |
|
40180
e53a90882a23
8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
25859
diff
changeset
|
145 |
$memtype$ y = $toBits$(x); |
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
146 |
UNSAFE.put$Memtype$Unaligned(bb.hb, byteOffset(nextPutIndex()), y, |
40180
e53a90882a23
8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
25859
diff
changeset
|
147 |
{#if[boB]?true:false}); |
2 | 148 |
return this; |
149 |
#else[rw] |
|
150 |
throw new ReadOnlyBufferException(); |
|
151 |
#end[rw] |
|
152 |
} |
|
153 |
||
154 |
public $Type$Buffer put(int i, $type$ x) { |
|
155 |
#if[rw] |
|
40180
e53a90882a23
8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
25859
diff
changeset
|
156 |
$memtype$ y = $toBits$(x); |
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
157 |
UNSAFE.put$Memtype$Unaligned(bb.hb, byteOffset(checkIndex(i)), y, |
40180
e53a90882a23
8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
25859
diff
changeset
|
158 |
{#if[boB]?true:false}); |
2 | 159 |
return this; |
160 |
#else[rw] |
|
161 |
throw new ReadOnlyBufferException(); |
|
162 |
#end[rw] |
|
163 |
} |
|
164 |
||
165 |
public $Type$Buffer compact() { |
|
166 |
#if[rw] |
|
167 |
int pos = position(); |
|
168 |
int lim = limit(); |
|
169 |
assert (pos <= lim); |
|
170 |
int rem = (pos <= lim ? lim - pos : 0); |
|
171 |
||
172 |
ByteBuffer db = bb.duplicate(); |
|
173 |
db.limit(ix(lim)); |
|
174 |
db.position(ix(0)); |
|
175 |
ByteBuffer sb = db.slice(); |
|
176 |
sb.position(pos << $LG_BYTES_PER_VALUE$); |
|
177 |
sb.compact(); |
|
178 |
position(rem); |
|
179 |
limit(capacity()); |
|
1634
3871c2046043
6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents:
1247
diff
changeset
|
180 |
discardMark(); |
2 | 181 |
return this; |
182 |
#else[rw] |
|
183 |
throw new ReadOnlyBufferException(); |
|
184 |
#end[rw] |
|
185 |
} |
|
186 |
||
187 |
public boolean isDirect() { |
|
188 |
return bb.isDirect(); |
|
189 |
} |
|
190 |
||
191 |
public boolean isReadOnly() { |
|
192 |
return {#if[rw]?false:true}; |
|
193 |
} |
|
194 |
||
195 |
#if[char] |
|
196 |
||
197 |
public String toString(int start, int end) { |
|
198 |
if ((end > limit()) || (start > end)) |
|
199 |
throw new IndexOutOfBoundsException(); |
|
200 |
try { |
|
201 |
int len = end - start; |
|
202 |
char[] ca = new char[len]; |
|
203 |
CharBuffer cb = CharBuffer.wrap(ca); |
|
204 |
CharBuffer db = this.duplicate(); |
|
205 |
db.position(start); |
|
206 |
db.limit(end); |
|
207 |
cb.put(db); |
|
208 |
return new String(ca); |
|
209 |
} catch (StringIndexOutOfBoundsException x) { |
|
210 |
throw new IndexOutOfBoundsException(); |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
||
215 |
// --- Methods to support CharSequence --- |
|
216 |
||
1224
8c1bc7c5dd00
6733145: (bf) CharBuffer.subSequence can be updated to take advantage of covariant returns
martin
parents:
2
diff
changeset
|
217 |
public CharBuffer subSequence(int start, int end) { |
2 | 218 |
int pos = position(); |
219 |
int lim = limit(); |
|
220 |
assert (pos <= lim); |
|
221 |
pos = (pos <= lim ? pos : lim); |
|
222 |
int len = lim - pos; |
|
223 |
||
224 |
if ((start < 0) || (end > len) || (start > end)) |
|
225 |
throw new IndexOutOfBoundsException(); |
|
2593
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1634
diff
changeset
|
226 |
return new ByteBufferAsCharBuffer$RW$$BO$(bb, |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1634
diff
changeset
|
227 |
-1, |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1634
diff
changeset
|
228 |
pos + start, |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1634
diff
changeset
|
229 |
pos + end, |
76032557be03
6795561: (bf) CharBuffer.subSequence() uses wrong capacity value for new buffer
alanb
parents:
1634
diff
changeset
|
230 |
capacity(), |
40195
a13e4945de1d
8162458: Buffer view implementations use incorrect offset for Unsafe access
psandoz
parents:
40180
diff
changeset
|
231 |
address); |
2 | 232 |
} |
233 |
||
234 |
#end[char] |
|
235 |
||
236 |
||
237 |
public ByteOrder order() { |
|
238 |
#if[boB] |
|
239 |
return ByteOrder.BIG_ENDIAN; |
|
240 |
#end[boB] |
|
241 |
#if[boL] |
|
242 |
return ByteOrder.LITTLE_ENDIAN; |
|
243 |
#end[boL] |
|
244 |
} |
|
245 |
||
48356
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
246 |
#if[char] |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
247 |
ByteOrder charRegionOrder() { |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
248 |
return order(); |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
249 |
} |
29e165bdf669
8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents:
47216
diff
changeset
|
250 |
#end[char] |
2 | 251 |
} |