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