jdk/src/java.base/share/classes/java/nio/Bits.java
author chegar
Mon, 28 Sep 2015 13:39:27 +0100
changeset 32834 e1dca5fe4de3
parent 32649 2ee9017c7597
child 33656 ef901bc43f7a
permissions -rw-r--r--
8137056: Move SharedSecrets and interface friends out of sun.misc Reviewed-by: alanb, mchung, psandoz, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30338
957ee8b5a33a 8026049: (bf) Intrinsify ByteBuffer.put{Int, Double, Float, ...} methods
aph
parents: 25859
diff changeset
     2
 * Copyright (c) 2000, 2015, 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: 3066
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: 3066
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: 3066
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3066
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3066
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
package java.nio;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.AccessController;
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
    29
import java.util.concurrent.atomic.AtomicLong;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
    30
import java.util.concurrent.atomic.LongAdder;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
    31
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32649
diff changeset
    32
import jdk.internal.misc.JavaNioAccess;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32649
diff changeset
    33
import jdk.internal.misc.JavaLangRefAccess;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32649
diff changeset
    34
import jdk.internal.misc.SharedSecrets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.misc.Unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.misc.VM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Access to bits, native and otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
class Bits {                            // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private Bits() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // -- Swapping --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static short swap(short x) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
    50
        return Short.reverseBytes(x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static char swap(char x) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
    54
        return Character.reverseBytes(x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static int swap(int x) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
    58
        return Integer.reverseBytes(x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static long swap(long x) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
    62
        return Long.reverseBytes(x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // -- get/put char --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30338
diff changeset
    68
    private static char makeChar(byte b1, byte b0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return (char)((b1 << 8) | (b0 & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static char getCharL(ByteBuffer bb, int bi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return makeChar(bb._get(bi + 1),
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
    74
                        bb._get(bi    ));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static char getCharL(long a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return makeChar(_get(a + 1),
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
    79
                        _get(a    ));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static char getCharB(ByteBuffer bb, int bi) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
    83
        return makeChar(bb._get(bi    ),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        bb._get(bi + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static char getCharB(long a) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
    88
        return makeChar(_get(a    ),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        _get(a + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    static char getChar(ByteBuffer bb, int bi, boolean bigEndian) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
    93
        return bigEndian ? getCharB(bb, bi) : getCharL(bb, bi);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static char getChar(long a, boolean bigEndian) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
    97
        return bigEndian ? getCharB(a) : getCharL(a);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static byte char1(char x) { return (byte)(x >> 8); }
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   101
    private static byte char0(char x) { return (byte)(x     ); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    static void putCharL(ByteBuffer bb, int bi, char x) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   104
        bb._put(bi    , char0(x));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        bb._put(bi + 1, char1(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    static void putCharL(long a, char x) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   109
        _put(a    , char0(x));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        _put(a + 1, char1(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static void putCharB(ByteBuffer bb, int bi, char x) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   114
        bb._put(bi    , char1(x));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        bb._put(bi + 1, char0(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static void putCharB(long a, char x) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   119
        _put(a    , char1(x));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        _put(a + 1, char0(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    static void putChar(ByteBuffer bb, int bi, char x, boolean bigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (bigEndian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            putCharB(bb, bi, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            putCharL(bb, bi, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    static void putChar(long a, char x, boolean bigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (bigEndian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            putCharB(a, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            putCharL(a, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    // -- get/put short --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30338
diff changeset
   140
    private static short makeShort(byte b1, byte b0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return (short)((b1 << 8) | (b0 & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    static short getShortL(ByteBuffer bb, int bi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return makeShort(bb._get(bi + 1),
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   146
                         bb._get(bi    ));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    static short getShortL(long a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return makeShort(_get(a + 1),
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   151
                         _get(a    ));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    static short getShortB(ByteBuffer bb, int bi) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   155
        return makeShort(bb._get(bi    ),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                         bb._get(bi + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    static short getShortB(long a) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   160
        return makeShort(_get(a    ),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                         _get(a + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    static short getShort(ByteBuffer bb, int bi, boolean bigEndian) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   165
        return bigEndian ? getShortB(bb, bi) : getShortL(bb, bi);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    static short getShort(long a, boolean bigEndian) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   169
        return bigEndian ? getShortB(a) : getShortL(a);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private static byte short1(short x) { return (byte)(x >> 8); }
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   173
    private static byte short0(short x) { return (byte)(x     ); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    static void putShortL(ByteBuffer bb, int bi, short x) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   176
        bb._put(bi    , short0(x));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        bb._put(bi + 1, short1(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    static void putShortL(long a, short x) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   181
        _put(a    , short0(x));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        _put(a + 1, short1(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    static void putShortB(ByteBuffer bb, int bi, short x) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   186
        bb._put(bi    , short1(x));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        bb._put(bi + 1, short0(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    static void putShortB(long a, short x) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   191
        _put(a    , short1(x));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        _put(a + 1, short0(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    static void putShort(ByteBuffer bb, int bi, short x, boolean bigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (bigEndian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            putShortB(bb, bi, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            putShortL(bb, bi, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    static void putShort(long a, short x, boolean bigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (bigEndian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            putShortB(a, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            putShortL(a, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    // -- get/put int --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30338
diff changeset
   212
    private static int makeInt(byte b3, byte b2, byte b1, byte b0) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   213
        return (((b3       ) << 24) |
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                ((b2 & 0xff) << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                ((b1 & 0xff) <<  8) |
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   216
                ((b0 & 0xff)      ));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    static int getIntL(ByteBuffer bb, int bi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return makeInt(bb._get(bi + 3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                       bb._get(bi + 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                       bb._get(bi + 1),
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   223
                       bb._get(bi    ));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    static int getIntL(long a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return makeInt(_get(a + 3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                       _get(a + 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                       _get(a + 1),
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   230
                       _get(a    ));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    static int getIntB(ByteBuffer bb, int bi) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   234
        return makeInt(bb._get(bi    ),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                       bb._get(bi + 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                       bb._get(bi + 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                       bb._get(bi + 3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    static int getIntB(long a) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   241
        return makeInt(_get(a    ),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                       _get(a + 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                       _get(a + 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                       _get(a + 3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    static int getInt(ByteBuffer bb, int bi, boolean bigEndian) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   248
        return bigEndian ? getIntB(bb, bi) : getIntL(bb, bi) ;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    static int getInt(long a, boolean bigEndian) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   252
        return bigEndian ? getIntB(a) : getIntL(a) ;
2
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 static byte int3(int x) { return (byte)(x >> 24); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private static byte int2(int x) { return (byte)(x >> 16); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private static byte int1(int x) { return (byte)(x >>  8); }
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   258
    private static byte int0(int x) { return (byte)(x      ); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    static void putIntL(ByteBuffer bb, int bi, int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        bb._put(bi + 3, int3(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        bb._put(bi + 2, int2(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        bb._put(bi + 1, int1(x));
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   264
        bb._put(bi    , int0(x));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    static void putIntL(long a, int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        _put(a + 3, int3(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        _put(a + 2, int2(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        _put(a + 1, int1(x));
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   271
        _put(a    , int0(x));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    static void putIntB(ByteBuffer bb, int bi, int x) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   275
        bb._put(bi    , int3(x));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        bb._put(bi + 1, int2(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        bb._put(bi + 2, int1(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        bb._put(bi + 3, int0(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    static void putIntB(long a, int x) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   282
        _put(a    , int3(x));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        _put(a + 1, int2(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        _put(a + 2, int1(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        _put(a + 3, int0(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    static void putInt(ByteBuffer bb, int bi, int x, boolean bigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (bigEndian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            putIntB(bb, bi, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            putIntL(bb, bi, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    static void putInt(long a, int x, boolean bigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (bigEndian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            putIntB(a, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            putIntL(a, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    // -- get/put long --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30338
diff changeset
   305
    private static long makeLong(byte b7, byte b6, byte b5, byte b4,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                 byte b3, byte b2, byte b1, byte b0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   308
        return ((((long)b7       ) << 56) |
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                (((long)b6 & 0xff) << 48) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                (((long)b5 & 0xff) << 40) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                (((long)b4 & 0xff) << 32) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                (((long)b3 & 0xff) << 24) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                (((long)b2 & 0xff) << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                (((long)b1 & 0xff) <<  8) |
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   315
                (((long)b0 & 0xff)      ));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    static long getLongL(ByteBuffer bb, int bi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return makeLong(bb._get(bi + 7),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        bb._get(bi + 6),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        bb._get(bi + 5),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        bb._get(bi + 4),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                        bb._get(bi + 3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        bb._get(bi + 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        bb._get(bi + 1),
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   326
                        bb._get(bi    ));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    static long getLongL(long a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return makeLong(_get(a + 7),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        _get(a + 6),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        _get(a + 5),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        _get(a + 4),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                        _get(a + 3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        _get(a + 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        _get(a + 1),
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   337
                        _get(a    ));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    static long getLongB(ByteBuffer bb, int bi) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   341
        return makeLong(bb._get(bi    ),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        bb._get(bi + 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        bb._get(bi + 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        bb._get(bi + 3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        bb._get(bi + 4),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        bb._get(bi + 5),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        bb._get(bi + 6),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                        bb._get(bi + 7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    static long getLongB(long a) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   352
        return makeLong(_get(a    ),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        _get(a + 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                        _get(a + 2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                        _get(a + 3),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        _get(a + 4),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                        _get(a + 5),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                        _get(a + 6),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        _get(a + 7));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    static long getLong(ByteBuffer bb, int bi, boolean bigEndian) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   363
        return bigEndian ? getLongB(bb, bi) : getLongL(bb, bi);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    static long getLong(long a, boolean bigEndian) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   367
        return bigEndian ? getLongB(a) : getLongL(a);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    private static byte long7(long x) { return (byte)(x >> 56); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    private static byte long6(long x) { return (byte)(x >> 48); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    private static byte long5(long x) { return (byte)(x >> 40); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    private static byte long4(long x) { return (byte)(x >> 32); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    private static byte long3(long x) { return (byte)(x >> 24); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    private static byte long2(long x) { return (byte)(x >> 16); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    private static byte long1(long x) { return (byte)(x >>  8); }
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   377
    private static byte long0(long x) { return (byte)(x      ); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    static void putLongL(ByteBuffer bb, int bi, long x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        bb._put(bi + 7, long7(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        bb._put(bi + 6, long6(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        bb._put(bi + 5, long5(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        bb._put(bi + 4, long4(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        bb._put(bi + 3, long3(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        bb._put(bi + 2, long2(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        bb._put(bi + 1, long1(x));
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   387
        bb._put(bi    , long0(x));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    static void putLongL(long a, long x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        _put(a + 7, long7(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        _put(a + 6, long6(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        _put(a + 5, long5(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        _put(a + 4, long4(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        _put(a + 3, long3(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        _put(a + 2, long2(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        _put(a + 1, long1(x));
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   398
        _put(a    , long0(x));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    static void putLongB(ByteBuffer bb, int bi, long x) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   402
        bb._put(bi    , long7(x));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        bb._put(bi + 1, long6(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        bb._put(bi + 2, long5(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        bb._put(bi + 3, long4(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        bb._put(bi + 4, long3(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        bb._put(bi + 5, long2(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        bb._put(bi + 6, long1(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        bb._put(bi + 7, long0(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    static void putLongB(long a, long x) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   413
        _put(a    , long7(x));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        _put(a + 1, long6(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        _put(a + 2, long5(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        _put(a + 3, long4(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        _put(a + 4, long3(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        _put(a + 5, long2(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        _put(a + 6, long1(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        _put(a + 7, long0(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    static void putLong(ByteBuffer bb, int bi, long x, boolean bigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (bigEndian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            putLongB(bb, bi, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            putLongL(bb, bi, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    static void putLong(long a, long x, boolean bigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        if (bigEndian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            putLongB(a, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            putLongL(a, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    // -- get/put float --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    static float getFloatL(ByteBuffer bb, int bi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        return Float.intBitsToFloat(getIntL(bb, bi));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    static float getFloatL(long a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        return Float.intBitsToFloat(getIntL(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    static float getFloatB(ByteBuffer bb, int bi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        return Float.intBitsToFloat(getIntB(bb, bi));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    static float getFloatB(long a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        return Float.intBitsToFloat(getIntB(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    static float getFloat(ByteBuffer bb, int bi, boolean bigEndian) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   457
        return bigEndian ? getFloatB(bb, bi) : getFloatL(bb, bi);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    static float getFloat(long a, boolean bigEndian) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   461
        return bigEndian ? getFloatB(a) : getFloatL(a);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    static void putFloatL(ByteBuffer bb, int bi, float x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        putIntL(bb, bi, Float.floatToRawIntBits(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    static void putFloatL(long a, float x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        putIntL(a, Float.floatToRawIntBits(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    static void putFloatB(ByteBuffer bb, int bi, float x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        putIntB(bb, bi, Float.floatToRawIntBits(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    static void putFloatB(long a, float x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        putIntB(a, Float.floatToRawIntBits(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    static void putFloat(ByteBuffer bb, int bi, float x, boolean bigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (bigEndian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            putFloatB(bb, bi, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            putFloatL(bb, bi, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    static void putFloat(long a, float x, boolean bigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if (bigEndian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            putFloatB(a, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            putFloatL(a, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    // -- get/put double --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    static double getDoubleL(ByteBuffer bb, int bi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        return Double.longBitsToDouble(getLongL(bb, bi));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    static double getDoubleL(long a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        return Double.longBitsToDouble(getLongL(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    static double getDoubleB(ByteBuffer bb, int bi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        return Double.longBitsToDouble(getLongB(bb, bi));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    static double getDoubleB(long a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        return Double.longBitsToDouble(getLongB(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    static double getDouble(ByteBuffer bb, int bi, boolean bigEndian) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   514
        return bigEndian ? getDoubleB(bb, bi) : getDoubleL(bb, bi);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    static double getDouble(long a, boolean bigEndian) {
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   518
        return bigEndian ? getDoubleB(a) : getDoubleL(a);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    static void putDoubleL(ByteBuffer bb, int bi, double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        putLongL(bb, bi, Double.doubleToRawLongBits(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    static void putDoubleL(long a, double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        putLongL(a, Double.doubleToRawLongBits(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    static void putDoubleB(ByteBuffer bb, int bi, double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        putLongB(bb, bi, Double.doubleToRawLongBits(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    static void putDoubleB(long a, double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        putLongB(a, Double.doubleToRawLongBits(x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    static void putDouble(ByteBuffer bb, int bi, double x, boolean bigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        if (bigEndian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            putDoubleB(bb, bi, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            putDoubleL(bb, bi, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    static void putDouble(long a, double x, boolean bigEndian) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (bigEndian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            putDoubleB(a, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            putDoubleL(a, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    // -- Unsafe access --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    private static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    private static byte _get(long a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        return unsafe.getByte(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    private static void _put(long a, byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        unsafe.putByte(a, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    static Unsafe unsafe() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        return unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    // -- Processor and memory-system properties --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
30338
957ee8b5a33a 8026049: (bf) Intrinsify ByteBuffer.put{Int, Double, Float, ...} methods
aph
parents: 25859
diff changeset
   571
    private static final ByteOrder byteOrder
957ee8b5a33a 8026049: (bf) Intrinsify ByteBuffer.put{Int, Double, Float, ...} methods
aph
parents: 25859
diff changeset
   572
        = unsafe.isBigEndian() ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    static ByteOrder byteOrder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        return byteOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    private static int pageSize = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    static int pageSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        if (pageSize == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            pageSize = unsafe().pageSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        return pageSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5994
diff changeset
   586
    static int pageCount(long size) {
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5994
diff changeset
   587
        return (int)(size + (long)pageSize() - 1L) / pageSize();
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5994
diff changeset
   588
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
30338
957ee8b5a33a 8026049: (bf) Intrinsify ByteBuffer.put{Int, Double, Float, ...} methods
aph
parents: 25859
diff changeset
   590
    private static boolean unaligned = unsafe.unalignedAccess();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    static boolean unaligned() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        return unaligned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    // -- Direct memory management --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    // A user-settable upper limit on the maximum amount of allocatable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    // direct buffer memory.  This value may be changed during VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    // initialization if it is launched with "-XX:MaxDirectMemorySize=<size>".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    private static volatile long maxMemory = VM.maxDirectMemory();
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   603
    private static final AtomicLong reservedMemory = new AtomicLong();
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   604
    private static final AtomicLong totalCapacity = new AtomicLong();
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   605
    private static final AtomicLong count = new AtomicLong();
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   606
    private static volatile boolean memoryLimitSet = false;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   607
    // max. number of sleeps during try-reserving with exponentially
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   608
    // increasing delay before throwing OutOfMemoryError:
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   609
    // 1, 2, 4, 8, 16, 32, 64, 128, 256 (total 511 ms ~ 0.5 s)
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   610
    // which means that OOME will be thrown after 0.5 s of trying
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   611
    private static final int MAX_SLEEPS = 9;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    // These methods should be called whenever direct memory is allocated or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    // freed.  They allow the user to control the amount of direct memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    // which a process may access.  All sizes are specified in bytes.
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 2
diff changeset
   616
    static void reserveMemory(long size, int cap) {
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   617
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   618
        if (!memoryLimitSet && VM.isBooted()) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   619
            maxMemory = VM.maxDirectMemory();
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   620
            memoryLimitSet = true;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   621
        }
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   622
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   623
        // optimist!
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   624
        if (tryReserveMemory(size, cap)) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   625
            return;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   626
        }
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   627
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   628
        final JavaLangRefAccess jlra = SharedSecrets.getJavaLangRefAccess();
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   629
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   630
        // retry while helping enqueue pending Reference objects
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   631
        // which includes executing pending Cleaner(s) which includes
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   632
        // Cleaner(s) that free direct buffer memory
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   633
        while (jlra.tryHandlePendingReference()) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   634
            if (tryReserveMemory(size, cap)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   639
        // trigger VM's Reference processing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        System.gc();
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   641
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   642
        // a retry loop with exponential back-off delays
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   643
        // (this gives VM some time to do it's job)
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   644
        boolean interrupted = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        try {
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   646
            long sleepTime = 1;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   647
            int sleeps = 0;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   648
            while (true) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   649
                if (tryReserveMemory(size, cap)) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   650
                    return;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   651
                }
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   652
                if (sleeps >= MAX_SLEEPS) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   653
                    break;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   654
                }
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   655
                if (!jlra.tryHandlePendingReference()) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   656
                    try {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   657
                        Thread.sleep(sleepTime);
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   658
                        sleepTime <<= 1;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   659
                        sleeps++;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   660
                    } catch (InterruptedException e) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   661
                        interrupted = true;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   662
                    }
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   663
                }
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   664
            }
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   665
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   666
            // no luck
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   667
            throw new OutOfMemoryError("Direct buffer memory");
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   668
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   669
        } finally {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   670
            if (interrupted) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   671
                // don't swallow interrupts
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   672
                Thread.currentThread().interrupt();
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   673
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   675
    }
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   676
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   677
    private static boolean tryReserveMemory(long size, int cap) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   678
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   679
        // -XX:MaxDirectMemorySize limits the total capacity rather than the
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   680
        // actual memory usage, which will differ when buffers are page
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   681
        // aligned.
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   682
        long totalCap;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   683
        while (cap <= maxMemory - (totalCap = totalCapacity.get())) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   684
            if (totalCapacity.compareAndSet(totalCap, totalCap + cap)) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   685
                reservedMemory.addAndGet(size);
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   686
                count.incrementAndGet();
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   687
                return true;
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   688
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   691
        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   694
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   695
    static void unreserveMemory(long size, int cap) {
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   696
        long cnt = count.decrementAndGet();
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   697
        long reservedMem = reservedMemory.addAndGet(-size);
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   698
        long totalCap = totalCapacity.addAndGet(-cap);
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   699
        assert cnt >= 0 && reservedMem >= 0 && totalCap >= 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
3066
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 1247
diff changeset
   702
    // -- Monitoring of direct buffer usage --
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 2
diff changeset
   703
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 2
diff changeset
   704
    static {
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 2
diff changeset
   705
        // setup access to this package in SharedSecrets
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32649
diff changeset
   706
        SharedSecrets.setJavaNioAccess(
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32649
diff changeset
   707
            new JavaNioAccess() {
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 2
diff changeset
   708
                @Override
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32649
diff changeset
   709
                public JavaNioAccess.BufferPool getDirectBufferPool() {
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32649
diff changeset
   710
                    return new JavaNioAccess.BufferPool() {
3066
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 1247
diff changeset
   711
                        @Override
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 1247
diff changeset
   712
                        public String getName() {
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 1247
diff changeset
   713
                            return "direct";
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 1247
diff changeset
   714
                        }
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 1247
diff changeset
   715
                        @Override
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 1247
diff changeset
   716
                        public long getCount() {
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   717
                            return Bits.count.get();
3066
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 1247
diff changeset
   718
                        }
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 1247
diff changeset
   719
                        @Override
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 1247
diff changeset
   720
                        public long getTotalCapacity() {
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   721
                            return Bits.totalCapacity.get();
3066
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 1247
diff changeset
   722
                        }
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 1247
diff changeset
   723
                        @Override
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 1247
diff changeset
   724
                        public long getMemoryUsed() {
23009
e2c92ddeb57f 6857566: (bf) DirectByteBuffer garbage creation can outpace reclamation
plevart
parents: 12538
diff changeset
   725
                            return Bits.reservedMemory.get();
3066
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 1247
diff changeset
   726
                        }
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 1247
diff changeset
   727
                    };
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 2
diff changeset
   728
                }
10325
b72c20cd583a 7047325: Internal API to improve management of direct buffers
coffeys
parents: 7668
diff changeset
   729
                @Override
b72c20cd583a 7047325: Internal API to improve management of direct buffers
coffeys
parents: 7668
diff changeset
   730
                public ByteBuffer newDirectByteBuffer(long addr, int cap, Object ob) {
b72c20cd583a 7047325: Internal API to improve management of direct buffers
coffeys
parents: 7668
diff changeset
   731
                    return new DirectByteBuffer(addr, cap, ob);
b72c20cd583a 7047325: Internal API to improve management of direct buffers
coffeys
parents: 7668
diff changeset
   732
                }
b72c20cd583a 7047325: Internal API to improve management of direct buffers
coffeys
parents: 7668
diff changeset
   733
                @Override
b72c20cd583a 7047325: Internal API to improve management of direct buffers
coffeys
parents: 7668
diff changeset
   734
                public void truncate(Buffer buf) {
b72c20cd583a 7047325: Internal API to improve management of direct buffers
coffeys
parents: 7668
diff changeset
   735
                    buf.truncate();
b72c20cd583a 7047325: Internal API to improve management of direct buffers
coffeys
parents: 7668
diff changeset
   736
                }
3066
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 1247
diff changeset
   737
        });
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 2
diff changeset
   738
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    // -- Bulk get/put acceleration --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    // These numbers represent the point at which we have empirically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    // determined that the average cost of a JNI call exceeds the expense
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    // of an element by element copy.  These numbers may change over time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    static final int JNI_COPY_TO_ARRAY_THRESHOLD   = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    static final int JNI_COPY_FROM_ARRAY_THRESHOLD = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
1151
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   748
    // This number limits the number of bytes to copy per call to Unsafe's
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   749
    // copyMemory method. A limit is imposed to allow for safepoint polling
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   750
    // during a large copy
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   751
    static final long UNSAFE_COPY_THRESHOLD = 1024L * 1024L;
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   752
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    // These methods do no bounds checking.  Verification that the copy will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    // result in memory corruption should be done prior to invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    // All positions and lengths are specified in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
1151
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   757
    /**
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   758
     * Copy from given source array to destination address.
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   759
     *
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   760
     * @param   src
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   761
     *          source array
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   762
     * @param   srcBaseOffset
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   763
     *          offset of first element of storage in source array
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   764
     * @param   srcPos
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   765
     *          offset within source array of the first element to read
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   766
     * @param   dstAddr
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   767
     *          destination address
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   768
     * @param   length
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   769
     *          number of bytes to copy
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   770
     */
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   771
    static void copyFromArray(Object src, long srcBaseOffset, long srcPos,
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   772
                              long dstAddr, long length)
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   773
    {
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   774
        long offset = srcBaseOffset + srcPos;
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   775
        while (length > 0) {
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   776
            long size = (length > UNSAFE_COPY_THRESHOLD) ? UNSAFE_COPY_THRESHOLD : length;
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   777
            unsafe.copyMemory(src, offset, null, dstAddr, size);
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   778
            length -= size;
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   779
            offset += size;
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   780
            dstAddr += size;
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   781
        }
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   782
    }
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   783
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   784
    /**
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   785
     * Copy from source address into given destination array.
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   786
     *
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   787
     * @param   srcAddr
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   788
     *          source address
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   789
     * @param   dst
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   790
     *          destination array
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   791
     * @param   dstBaseOffset
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   792
     *          offset of first element of storage in destination array
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   793
     * @param   dstPos
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   794
     *          offset within destination array of the first element to write
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   795
     * @param   length
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   796
     *          number of bytes to copy
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   797
     */
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   798
    static void copyToArray(long srcAddr, Object dst, long dstBaseOffset, long dstPos,
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   799
                            long length)
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   800
    {
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   801
        long offset = dstBaseOffset + dstPos;
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   802
        while (length > 0) {
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   803
            long size = (length > UNSAFE_COPY_THRESHOLD) ? UNSAFE_COPY_THRESHOLD : length;
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   804
            unsafe.copyMemory(null, srcAddr, dst, offset, size);
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   805
            length -= size;
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   806
            srcAddr += size;
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   807
            offset += size;
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   808
        }
4070cecdb99d 6570619: (bf) DirectByteBuffer.get/put(byte[]) does not scale well
alanb
parents: 1143
diff changeset
   809
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    static void copyFromCharArray(Object src, long srcPos, long dstAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                                  long length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        copyFromShortArray(src, srcPos, dstAddr, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    static void copyToCharArray(long srcAddr, Object dst, long dstPos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                                long length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        copyToShortArray(srcAddr, dst, dstPos, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    static native void copyFromShortArray(Object src, long srcPos, long dstAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                                          long length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    static native void copyToShortArray(long srcAddr, Object dst, long dstPos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                                        long length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    static native void copyFromIntArray(Object src, long srcPos, long dstAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                                        long length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    static native void copyToIntArray(long srcAddr, Object dst, long dstPos,
5994
267f4ce6a585 6940258: (bf) Use intrinsified reverseBytes operation; elide no-op constructs
martin
parents: 5506
diff changeset
   831
                                      long length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    static native void copyFromLongArray(Object src, long srcPos, long dstAddr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                                         long length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    static native void copyToLongArray(long srcAddr, Object dst, long dstPos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                                       long length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
}