src/java.base/share/classes/sun/nio/ch/NativeObject.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 49531 e8ada9b2dd89
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2002, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package sun.nio.ch;                                     // Formerly in sun.misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.ByteOrder;
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 25859
diff changeset
    32
import jdk.internal.misc.Unsafe;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
// ## In the fullness of time, this class will be eliminated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Proxies for objects that reside in native memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class NativeObject {                                    // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    protected static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // Native allocation address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // may be smaller than the base address due to page-size rounding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    protected long allocationAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // Native base address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private final long address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Creates a new native object that is based at the given native address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    NativeObject(long address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.allocationAddress = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.address = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Creates a new native object allocated at the given native address but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * whose base is at the additional offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    NativeObject(long address, long offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.allocationAddress = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.address = address + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // Invoked only by AllocatedNativeObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    protected NativeObject(int size, boolean pageAligned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (!pageAligned) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            this.allocationAddress = unsafe.allocateMemory(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            this.address = this.allocationAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            int ps = pageSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            long a = unsafe.allocateMemory(size + ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            this.allocationAddress = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            this.address = a + ps - (a & (ps - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Returns the native base address of this native object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @return The native base address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    long address() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    long allocationAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return allocationAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Creates a new native object starting at the given offset from the base
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * of this native object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *         The offset from the base of this native object that is to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *         the base of the new native object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @return The newly created native object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    NativeObject subObject(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return new NativeObject(offset + address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Reads an address from this native object at the given offset and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * constructs a native object using that address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *         The offset of the address to be read.  Note that the size of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *         address is implementation-dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @return The native object created using the address read from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *         given offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    NativeObject getObject(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        long newAddress = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        switch (addressSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            case 8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                newAddress = unsafe.getLong(offset + address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                newAddress = unsafe.getInt(offset + address) & 0x00000000FFFFFFFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                throw new InternalError("Address size not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return new NativeObject(newAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Writes the base address of the given native object at the given offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * of this native object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *         The offset at which the address is to be written.  Note that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *         size of an address is implementation-dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param  ob
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *         The native object whose address is to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    void putObject(int offset, NativeObject ob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        switch (addressSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            case 8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                putLong(offset, ob.address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                putInt(offset, (int)(ob.address & 0x00000000FFFFFFFF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                throw new InternalError("Address size not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /* -- Value accessors: No range checking! -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Reads a byte starting at the given offset from base of this native
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *         The offset at which to read the byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @return The byte value read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    final byte getByte(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return unsafe.getByte(offset + address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Writes a byte at the specified offset from this native object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * base address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *         The offset at which to write the byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param  value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *         The byte value to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    final void putByte(int offset, byte value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        unsafe.putByte(offset + address,  value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Reads a short starting at the given offset from base of this native
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *         The offset at which to read the short
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return The short value read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    final short getShort(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return unsafe.getShort(offset + address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Writes a short at the specified offset from this native object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * base address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *         The offset at which to write the short
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param  value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *         The short value to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    final void putShort(int offset, short value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        unsafe.putShort(offset + address,  value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Reads a char starting at the given offset from base of this native
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *         The offset at which to read the char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @return The char value read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    final char getChar(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return unsafe.getChar(offset + address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Writes a char at the specified offset from this native object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * base address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *         The offset at which to write the char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param  value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *         The char value to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    final void putChar(int offset, char value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        unsafe.putChar(offset + address,  value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Reads an int starting at the given offset from base of this native
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *         The offset at which to read the int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @return The int value read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    final int getInt(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return unsafe.getInt(offset + address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Writes an int at the specified offset from this native object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * base address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *         The offset at which to write the int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param  value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *         The int value to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    final void putInt(int offset, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        unsafe.putInt(offset + address, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Reads a long starting at the given offset from base of this native
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *         The offset at which to read the long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @return The long value read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    final long getLong(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return unsafe.getLong(offset + address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Writes a long at the specified offset from this native object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * base address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *         The offset at which to write the long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param  value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *         The long value to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    final void putLong(int offset, long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        unsafe.putLong(offset + address, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Reads a float starting at the given offset from base of this native
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *         The offset at which to read the float
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @return The float value read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    final float getFloat(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        return unsafe.getFloat(offset + address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Writes a float at the specified offset from this native object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * base address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *         The offset at which to write the float
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param  value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *         The float value to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    final void putFloat(int offset, float value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        unsafe.putFloat(offset + address, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Reads a double starting at the given offset from base of this native
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *         The offset at which to read the double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @return The double value read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    final double getDouble(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        return unsafe.getDouble(offset + address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Writes a double at the specified offset from this native object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * base address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *         The offset at which to write the double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @param  value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *         The double value to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    final void putDouble(int offset, double value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        unsafe.putDouble(offset + address, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Returns the native architecture's address size in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @return The address size of the native architecture
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    static int addressSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return unsafe.addressSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    // Cache for byte order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    private static ByteOrder byteOrder = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Returns the byte order of the underlying hardware.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @return  An instance of {@link java.nio.ByteOrder}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    static ByteOrder byteOrder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (byteOrder != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return byteOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        long a = unsafe.allocateMemory(8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            unsafe.putLong(a, 0x0102030405060708L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            byte b = unsafe.getByte(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            switch (b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            case 0x01: byteOrder = ByteOrder.BIG_ENDIAN;     break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            case 0x08: byteOrder = ByteOrder.LITTLE_ENDIAN;  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            unsafe.freeMemory(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return byteOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
49531
e8ada9b2dd89 8200131: Improve lazy init of InetAddress.canonicalHostName and NativeObject.pageSize
martin
parents: 47216
diff changeset
   391
    /**
e8ada9b2dd89 8200131: Improve lazy init of InetAddress.canonicalHostName and NativeObject.pageSize
martin
parents: 47216
diff changeset
   392
     * Cache for page size.
e8ada9b2dd89 8200131: Improve lazy init of InetAddress.canonicalHostName and NativeObject.pageSize
martin
parents: 47216
diff changeset
   393
     * Lazily initialized via a data race; safe because ints are atomic.
e8ada9b2dd89 8200131: Improve lazy init of InetAddress.canonicalHostName and NativeObject.pageSize
martin
parents: 47216
diff changeset
   394
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    private static int pageSize = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Returns the page size of the underlying hardware.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @return  The page size, in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    static int pageSize() {
49531
e8ada9b2dd89 8200131: Improve lazy init of InetAddress.canonicalHostName and NativeObject.pageSize
martin
parents: 47216
diff changeset
   403
        int value = pageSize;
e8ada9b2dd89 8200131: Improve lazy init of InetAddress.canonicalHostName and NativeObject.pageSize
martin
parents: 47216
diff changeset
   404
        if (value == -1)
e8ada9b2dd89 8200131: Improve lazy init of InetAddress.canonicalHostName and NativeObject.pageSize
martin
parents: 47216
diff changeset
   405
            pageSize = value = unsafe.pageSize();
e8ada9b2dd89 8200131: Improve lazy init of InetAddress.canonicalHostName and NativeObject.pageSize
martin
parents: 47216
diff changeset
   406
        return value;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
}