src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template
author psandoz
Wed, 20 Dec 2017 09:14:52 -0800
changeset 48356 29e165bdf669
parent 47216 71c04702a3d5
child 49210 7c795d301dbf
permissions -rw-r--r--
8193085: Vectorize the nio Buffer equals and compareTo implementations Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4115
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4115
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4115
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4115
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4115
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#warn This file is preprocessed before being compiled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
package java.nio;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6902
diff changeset
    30
import java.io.FileDescriptor;
34882
ce2a8ec851c1 8145544: Move sun.misc.VM to jdk.internal.misc
chegar
parents: 33660
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.nio.ch.DirectBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
class Direct$Type$Buffer$RW$$BO$
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#if[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    extends {#if[byte]?Mapped$Type$Buffer:$Type$Buffer}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#else[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    extends Direct$Type$Buffer$BO$
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    implements DirectBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#if[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // Base address, used in all indexing calculations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // NOTE: moved up to Buffer.java for speed in JNI GetDirectBufferAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    //    protected long address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static class Deallocator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        implements Runnable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        private int capacity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            assert (address != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            this.capacity = capacity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (address == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                // Paranoia
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
48356
29e165bdf669 8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents: 47216
diff changeset
    88
            UNSAFE.freeMemory(address);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private final Cleaner cleaner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public Cleaner cleaner() { return cleaner; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
#else[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public Cleaner cleaner() { return null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // Primary constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    Direct$Type$Buffer$RW$(int cap) {                   // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        long base = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        try {
48356
29e165bdf669 8193085: Vectorize the nio Buffer equals and compareTo implementations
psandoz
parents: 47216
diff changeset
   121
            base = UNSAFE.allocateMemory(size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            // Round up to page boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            address = base + ps - (base & (ps - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            address = base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
#else[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        super(cap);
36933
3e6453e2d833 8149469: ByteBuffer API and implementation enhancements for VarHandles
psandoz
parents: 36222
diff changeset
   137
        this.isReadOnly = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
#if[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    // Invoked only by JNI: NewDirectByteBuffer(void*, long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        address = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        cleaner = null;
10325
b72c20cd583a 7047325: Internal API to improve management of direct buffers
coffeys
parents: 7668
diff changeset
   160
        att = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // For memory-mapped buffers -- invoked by FileChannelImpl via reflection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        address = addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    // For duplicates and slices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    Direct$Type$Buffer$RW$$BO$(DirectBuffer db,         // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                               int mark, int pos, int lim, int cap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                               int off)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
#if[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        super(mark, pos, lim, cap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        address = db.address() + off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        cleaner = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
#end[byte]
10325
b72c20cd583a 7047325: Internal API to improve management of direct buffers
coffeys
parents: 7668
diff changeset
   196
        att = db;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
#else[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public $Type$Buffer slice() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        int pos = this.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        int lim = this.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        int off = (pos << $LG_BYTES_PER_VALUE$);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        assert (off >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return new Direct$Type$Buffer$RW$$BO$(this, -1, 0, rem, rem, off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public $Type$Buffer duplicate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return new Direct$Type$Buffer$RW$$BO$(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                              this.markValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                              this.position(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                              this.limit(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                              this.capacity(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                              0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public $Type$Buffer asReadOnlyBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
#if[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return new Direct$Type$BufferR$BO$(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                           this.markValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                           this.position(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                                           this.limit(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                                           this.capacity(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                           0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
#else[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
#if[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public long address() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private long ix(int i) {
33300
bb014fdf246b 8130891: (bf) More direct buffering
bpb
parents: 31873
diff changeset
   256
        return address + ((long)i << $LG_BYTES_PER_VALUE$);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public $Type$Buffer get($type$[] dst, int offset, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
#if[rw]
33300
bb014fdf246b 8130891: (bf) More direct buffering
bpb
parents: 31873
diff changeset
   275
        if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            checkBounds(offset, length, dst.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            int pos = position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            int lim = limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            if (length > rem)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                throw new BufferUnderflowException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            position(pos + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            super.get(dst, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
#else[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        throw new ReadOnlyBufferException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public $Type$Buffer put($type$ x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
#else[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        throw new ReadOnlyBufferException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public $Type$Buffer put(int i, $type$ x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
#else[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        throw new ReadOnlyBufferException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public $Type$Buffer put($Type$Buffer src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
#if[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (src instanceof Direct$Type$Buffer$BO$) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            Direct$Type$Buffer$RW$$BO$ sb = (Direct$Type$Buffer$RW$$BO$)src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            int spos = sb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            int slim = sb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            assert (spos <= slim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            int srem = (spos <= slim ? slim - spos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            int pos = position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            int lim = limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (srem > rem)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            sb.position(spos + srem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            position(pos + srem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        } else if (src.hb != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            int spos = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            int slim = src.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            assert (spos <= slim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            int srem = (spos <= slim ? slim - spos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            put(src.hb, src.offset + spos, srem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            src.position(spos + srem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            super.put(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
#else[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        throw new ReadOnlyBufferException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public $Type$Buffer put($type$[] src, int offset, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
#if[rw]
33300
bb014fdf246b 8130891: (bf) More direct buffering
bpb
parents: 31873
diff changeset
   373
        if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            checkBounds(offset, length, src.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            int pos = position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            int lim = limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            if (length > rem)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                throw new BufferOverflowException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            position(pos + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            super.put(src, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
#else[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        throw new ReadOnlyBufferException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public $Type$Buffer compact() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
#if[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        int pos = position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        int lim = limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        position(rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        limit(capacity());
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 1247
diff changeset
   418
        discardMark();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
#else[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        throw new ReadOnlyBufferException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public boolean isDirect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    public boolean isReadOnly() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        return {#if[rw]?false:true};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
#if[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    public String toString(int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if ((end > limit()) || (start > end))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            int len = end - start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            char[] ca = new char[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            CharBuffer cb = CharBuffer.wrap(ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            CharBuffer db = this.duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            db.position(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            db.limit(end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            cb.put(db);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            return new String(ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        } catch (StringIndexOutOfBoundsException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    // --- Methods to support CharSequence ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        int pos = position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        int lim = limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        pos = (pos <= lim ? pos : lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        int len = lim - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if ((start < 0) || (end > len) || (start > end))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
#end[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
#if[!byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    public ByteOrder order() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
#if[boS]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        return ((ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
#end[boS]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
#if[boU]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        return ((ByteOrder.nativeOrder() != ByteOrder.BIG_ENDIAN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
#end[boU]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
#end[!byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    void _put(int i, byte b) {                  // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
#else[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        throw new ReadOnlyBufferException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
#end[rw]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    // #BIN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    // Binary-data access methods  for short, char, int, long, float,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    // and double will be inserted here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
}