/
Direct-X-Buffer.java.template
608 lines (529 loc) · 18.2 KB
/
Direct-X-Buffer.java.template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#warn This file is preprocessed before being compiled
package java.nio;
import java.io.FileDescriptor;
import java.lang.ref.Reference;
import java.util.Objects;
import jdk.internal.access.foreign.MemorySegmentProxy;
import jdk.internal.misc.VM;
import jdk.internal.ref.Cleaner;
import sun.nio.ch.DirectBuffer;
class Direct$Type$Buffer$RW$$BO$
#if[rw]
extends {#if[byte]?Mapped$Type$Buffer:$Type$Buffer}
#else[rw]
extends Direct$Type$Buffer$BO$
#end[rw]
implements DirectBuffer
{
#if[rw]
// Cached array base offset
private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset($type$[].class);
// Cached unaligned-access capability
protected static final boolean UNALIGNED = Bits.unaligned();
// Base address, used in all indexing calculations
// NOTE: moved up to Buffer.java for speed in JNI GetDirectBufferAddress
// protected long address;
// An object attached to this buffer. If this buffer is a view of another
// buffer then we use this field to keep a reference to that buffer to
// ensure that its memory isn't freed before we are done with it.
private final Object att;
public Object attachment() {
return att;
}
#if[byte]
private static class Deallocator
implements Runnable
{
private long address;
private long size;
private int capacity;
private Deallocator(long address, long size, int capacity) {
assert (address != 0);
this.address = address;
this.size = size;
this.capacity = capacity;
}
public void run() {
if (address == 0) {
// Paranoia
return;
}
UNSAFE.freeMemory(address);
address = 0;
Bits.unreserveMemory(size, capacity);
}
}
private final Cleaner cleaner;
public Cleaner cleaner() { return cleaner; }
#else[byte]
public Cleaner cleaner() { return null; }
#end[byte]
#end[rw]
#if[byte]
// Primary constructor
//
Direct$Type$Buffer$RW$(int cap) { // package-private
#if[rw]
super(-1, 0, cap, cap, null);
boolean pa = VM.isDirectMemoryPageAligned();
int ps = Bits.pageSize();
long size = Math.max(1L, (long)cap + (pa ? ps : 0));
Bits.reserveMemory(size, cap);
long base = 0;
try {
base = UNSAFE.allocateMemory(size);
} catch (OutOfMemoryError x) {
Bits.unreserveMemory(size, cap);
throw x;
}
UNSAFE.setMemory(base, size, (byte) 0);
if (pa && (base % ps != 0)) {
// Round up to page boundary
address = base + ps - (base & (ps - 1));
} else {
address = base;
}
cleaner = Cleaner.create(this, new Deallocator(base, size, cap));
att = null;
#else[rw]
super(cap);
this.isReadOnly = true;
#end[rw]
}
#if[rw]
// Invoked to construct a direct ByteBuffer referring to the block of
// memory. A given arbitrary object may also be attached to the buffer.
//
Direct$Type$Buffer(long addr, int cap, Object ob, MemorySegmentProxy segment) {
super(-1, 0, cap, cap, segment);
address = addr;
cleaner = null;
att = ob;
}
// Invoked to construct a direct ByteBuffer referring to the block of
// memory. A given arbitrary object may also be attached to the buffer.
//
Direct$Type$Buffer(long addr, int cap, Object ob, FileDescriptor fd, boolean isSync, MemorySegmentProxy segment) {
super(-1, 0, cap, cap, fd, isSync, segment);
address = addr;
cleaner = null;
att = ob;
}
// Invoked only by JNI: NewDirectByteBuffer(void*, long)
//
private Direct$Type$Buffer(long addr, int cap) {
super(-1, 0, cap, cap, null);
address = addr;
cleaner = null;
att = null;
}
#end[rw]
// For memory-mapped buffers -- invoked by FileChannelImpl via reflection
//
protected Direct$Type$Buffer$RW$(int cap, long addr,
FileDescriptor fd,
Runnable unmapper,
boolean isSync, MemorySegmentProxy segment)
{
#if[rw]
super(-1, 0, cap, cap, fd, isSync, segment);
address = addr;
cleaner = Cleaner.create(this, unmapper);
att = null;
#else[rw]
super(cap, addr, fd, unmapper, isSync, segment);
this.isReadOnly = true;
#end[rw]
}
#end[byte]
// For duplicates and slices
//
Direct$Type$Buffer$RW$$BO$(DirectBuffer db, // package-private
int mark, int pos, int lim, int cap,
int off, MemorySegmentProxy segment)
{
#if[rw]
super(mark, pos, lim, cap, segment);
address = db.address() + off;
#if[byte]
cleaner = null;
#end[byte]
Object attachment = db.attachment();
att = (attachment == null ? db : attachment);
#else[rw]
super(db, mark, pos, lim, cap, off, segment);
this.isReadOnly = true;
#end[rw]
}
@Override
Object base() {
return null;
}
public $Type$Buffer slice() {
int pos = this.position();
int lim = this.limit();
int rem = (pos <= lim ? lim - pos : 0);
int off = (pos << $LG_BYTES_PER_VALUE$);
assert (off >= 0);
return new Direct$Type$Buffer$RW$$BO$(this, -1, 0, rem, rem, off, segment);
}
@Override
public $Type$Buffer slice(int index, int length) {
Objects.checkFromIndexSize(index, length, limit());
return new Direct$Type$Buffer$RW$$BO$(this,
-1,
0,
length,
length,
index, segment);
}
public $Type$Buffer duplicate() {
return new Direct$Type$Buffer$RW$$BO$(this,
this.markValue(),
this.position(),
this.limit(),
this.capacity(),
0, segment);
}
public $Type$Buffer asReadOnlyBuffer() {
#if[rw]
return new Direct$Type$BufferR$BO$(this,
this.markValue(),
this.position(),
this.limit(),
this.capacity(),
0, segment);
#else[rw]
return duplicate();
#end[rw]
}
#if[rw]
public long address() {
return address;
}
private long ix(int i) {
return address + ((long)i << $LG_BYTES_PER_VALUE$);
}
public $type$ get() {
try {
checkSegment();
return $fromBits$($swap$(UNSAFE.get$Swaptype$(ix(nextGetIndex()))));
} finally {
Reference.reachabilityFence(this);
}
}
public $type$ get(int i) {
try {
checkSegment();
return $fromBits$($swap$(UNSAFE.get$Swaptype$(ix(checkIndex(i)))));
} finally {
Reference.reachabilityFence(this);
}
}
#if[streamableType]
$type$ getUnchecked(int i) {
try {
return $fromBits$($swap$(UNSAFE.get$Swaptype$(ix(i))));
} finally {
Reference.reachabilityFence(this);
}
}
#end[streamableType]
public $Type$Buffer get($type$[] dst, int offset, int length) {
#if[rw]
checkSegment();
if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
Objects.checkFromIndexSize(offset, length, dst.length);
int pos = position();
int lim = limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
if (length > rem)
throw new BufferUnderflowException();
long dstOffset = ARRAY_BASE_OFFSET + ((long)offset << $LG_BYTES_PER_VALUE$);
try {
#if[!byte]
if (order() != ByteOrder.nativeOrder())
UNSAFE.copySwapMemory(null,
ix(pos),
dst,
dstOffset,
(long)length << $LG_BYTES_PER_VALUE$,
(long)1 << $LG_BYTES_PER_VALUE$);
else
#end[!byte]
UNSAFE.copyMemory(null,
ix(pos),
dst,
dstOffset,
(long)length << $LG_BYTES_PER_VALUE$);
} finally {
Reference.reachabilityFence(this);
}
position(pos + length);
} else {
super.get(dst, offset, length);
}
return this;
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
public $Type$Buffer get(int index, $type$[] dst, int offset, int length) {
#if[rw]
checkSegment();
if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
Objects.checkFromIndexSize(index, length, limit());
Objects.checkFromIndexSize(offset, length, dst.length);
long dstOffset = ARRAY_BASE_OFFSET + ((long)offset << $LG_BYTES_PER_VALUE$);
try {
#if[!byte]
if (order() != ByteOrder.nativeOrder())
UNSAFE.copySwapMemory(null,
ix(index),
dst,
dstOffset,
(long)length << $LG_BYTES_PER_VALUE$,
(long)1 << $LG_BYTES_PER_VALUE$);
else
#end[!byte]
UNSAFE.copyMemory(null,
ix(index),
dst,
dstOffset,
(long)length << $LG_BYTES_PER_VALUE$);
} finally {
Reference.reachabilityFence(this);
}
} else {
super.get(index, dst, offset, length);
}
return this;
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
#end[rw]
public $Type$Buffer put($type$ x) {
#if[rw]
try {
checkSegment();
UNSAFE.put$Swaptype$(ix(nextPutIndex()), $swap$($toBits$(x)));
} finally {
Reference.reachabilityFence(this);
}
return this;
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
public $Type$Buffer put(int i, $type$ x) {
#if[rw]
try {
checkSegment();
UNSAFE.put$Swaptype$(ix(checkIndex(i)), $swap$($toBits$(x)));
} finally {
Reference.reachabilityFence(this);
}
return this;
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
public $Type$Buffer put($Type$Buffer src) {
#if[rw]
checkSegment();
super.put(src);
return this;
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
public $Type$Buffer put(int index, $Type$Buffer src, int offset, int length) {
#if[rw]
checkSegment();
super.put(index, src, offset, length);
return this;
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
public $Type$Buffer put($type$[] src, int offset, int length) {
#if[rw]
checkSegment();
if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
Objects.checkFromIndexSize(offset, length, src.length);
int pos = position();
int lim = limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
if (length > rem)
throw new BufferOverflowException();
long srcOffset = ARRAY_BASE_OFFSET + ((long)offset << $LG_BYTES_PER_VALUE$);
try {
#if[!byte]
if (order() != ByteOrder.nativeOrder())
UNSAFE.copySwapMemory(src,
srcOffset,
null,
ix(pos),
(long)length << $LG_BYTES_PER_VALUE$,
(long)1 << $LG_BYTES_PER_VALUE$);
else
#end[!byte]
UNSAFE.copyMemory(src,
srcOffset,
null,
ix(pos),
(long)length << $LG_BYTES_PER_VALUE$);
} finally {
Reference.reachabilityFence(this);
}
position(pos + length);
} else {
super.put(src, offset, length);
}
return this;
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
public $Type$Buffer put(int index, $type$[] src, int offset, int length) {
#if[rw]
checkSegment();
if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
Objects.checkFromIndexSize(index, length, limit());
Objects.checkFromIndexSize(offset, length, src.length);
long srcOffset = ARRAY_BASE_OFFSET + ((long)offset << $LG_BYTES_PER_VALUE$);
try {
#if[!byte]
if (order() != ByteOrder.nativeOrder())
UNSAFE.copySwapMemory(src,
srcOffset,
null,
ix(index),
(long)length << $LG_BYTES_PER_VALUE$,
(long)1 << $LG_BYTES_PER_VALUE$);
else
#end[!byte]
UNSAFE.copyMemory(src,
srcOffset,
null,
ix(index),
(long)length << $LG_BYTES_PER_VALUE$);
} finally {
Reference.reachabilityFence(this);
}
} else {
super.put(index, src, offset, length);
}
return this;
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
public $Type$Buffer compact() {
#if[rw]
int pos = position();
int lim = limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
try {
UNSAFE.copyMemory(ix(pos), ix(0), (long)rem << $LG_BYTES_PER_VALUE$);
} finally {
Reference.reachabilityFence(this);
}
position(rem);
limit(capacity());
discardMark();
return this;
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
public boolean isDirect() {
return true;
}
public boolean isReadOnly() {
return {#if[rw]?false:true};
}
#if[char]
public String toString(int start, int end) {
Objects.checkFromToIndex(start, end, limit());
try {
int len = end - start;
char[] ca = new char[len];
CharBuffer cb = CharBuffer.wrap(ca);
CharBuffer db = this.duplicate();
db.position(start);
db.limit(end);
cb.put(db);
return new String(ca);
} catch (StringIndexOutOfBoundsException x) {
throw new IndexOutOfBoundsException();
}
}
// --- Methods to support CharSequence ---
public CharBuffer subSequence(int start, int end) {
int pos = position();
int lim = limit();
assert (pos <= lim);
pos = (pos <= lim ? pos : lim);
int len = lim - pos;
Objects.checkFromToIndex(start, end, len);
return new DirectCharBuffer$RW$$BO$(this,
-1,
pos + start,
pos + end,
capacity(),
offset, segment);
}
#end[char]
#if[!byte]
public ByteOrder order() {
#if[boS]
return ((ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN)
? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
#end[boS]
#if[boU]
return ((ByteOrder.nativeOrder() != ByteOrder.BIG_ENDIAN)
? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
#end[boU]
}
#end[!byte]
#if[char]
ByteOrder charRegionOrder() {
return order();
}
#end[char]
#if[byte]
// #BIN
//
// Binary-data access methods for short, char, int, long, float,
// and double will be inserted here
#end[byte]
}