src/java.base/share/classes/java/nio/MappedByteBuffer.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58242 94bb65cb37d3
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54823
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
     2
 * Copyright (c) 2000, 2019, 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
package java.nio;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
    28
import java.io.FileDescriptor;
49210
7c795d301dbf 8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents: 48847
diff changeset
    29
import java.lang.ref.Reference;
54823
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
    30
import java.util.Objects;
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 32143
diff changeset
    31
import jdk.internal.misc.Unsafe;
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
    32
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
 * A direct byte buffer whose content is a memory-mapped region of a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p> Mapped byte buffers are created via the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * java.nio.channels.FileChannel#map FileChannel.map} method.  This class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * extends the {@link ByteBuffer} class with operations that are specific to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * memory-mapped file regions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p> A mapped byte buffer and the file mapping that it represents remain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * valid until the buffer itself is garbage-collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p> The content of a mapped byte buffer can change at any time, for example
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * if the content of the corresponding region of the mapped file is changed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * this program or another.  Whether or not such changes occur, and when they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * occur, is operating-system dependent and therefore unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 44369
diff changeset
    50
 * <a id="inaccess"></a><p> All or part of a mapped byte buffer may become
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * inaccessible at any time, for example if the mapped file is truncated.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * attempt to access an inaccessible region of a mapped byte buffer will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * change the buffer's content and will cause an unspecified exception to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * thrown either at the time of the access or at some later time.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * therefore strongly recommended that appropriate precautions be taken to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * avoid the manipulation of a mapped file by this program, or by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * concurrently running program, except to read or write the file's content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p> Mapped byte buffers otherwise behave no differently than ordinary direct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * byte buffers. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
public abstract class MappedByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    extends ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // This is a little bit backwards: By rights MappedByteBuffer should be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // subclass of DirectByteBuffer, but to keep the spec clear and simple, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // for optimization purposes, it's easier to do it the other way around.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // This works because DirectByteBuffer is a package-private class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
    77
    // For mapped buffers, a FileDescriptor that may be used for mapping
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
    78
    // operations if valid; null if the buffer is not mapped.
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
    79
    private final FileDescriptor fd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
57804
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
    81
    // A flag true if this buffer is mapped against non-volatile
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
    82
    // memory using one of the extended FileChannel.MapMode modes,
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
    83
    // MapMode.READ_ONLY_SYNC or MapMode.READ_WRITE_SYNC and false if
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
    84
    // it is mapped using any of the other modes. This flag only
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
    85
    // determines the behavior of force operations.
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
    86
    private final boolean isSync;
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
    87
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // This should only be invoked by the DirectByteBuffer constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private
57804
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
    91
                     FileDescriptor fd, boolean isSync) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        super(mark, pos, lim, cap);
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
    93
        this.fd = fd;
57804
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
    94
        this.isSync = isSync;
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
    95
    }
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
    96
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
    97
    MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
    98
                     boolean isSync) {
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
    99
        super(mark, pos, lim, cap);
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   100
        this.fd = null;
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   101
        this.isSync = isSync;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    MappedByteBuffer(int mark, int pos, int lim, int cap) { // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        super(mark, pos, lim, cap);
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
   106
        this.fd = null;
57804
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   107
        this.isSync = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
54823
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   110
    // Returns the distance (in bytes) of the buffer start from the
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   111
    // largest page aligned address of the mapping less than or equal
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   112
    // to the start address.
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   113
    private long mappingOffset() {
54823
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   114
        return mappingOffset(0);
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   115
    }
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   116
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   117
    // Returns the distance (in bytes) of the buffer element
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   118
    // identified by index from the largest page aligned address of
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   119
    // the mapping less than or equal to the element address. Computed
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   120
    // each time to avoid storing in every direct buffer.
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   121
    private long mappingOffset(int index) {
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   122
        int ps = Bits.pageSize();
54823
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   123
        long indexAddress = address + index;
54904
c1143a0d4e22 8224042: Add private alignDown method to MappedByteBuffer
adinn
parents: 54823
diff changeset
   124
        long baseAddress = alignDown(indexAddress, ps);
54823
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   125
        return indexAddress - baseAddress;
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   126
    }
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   127
54823
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   128
    // Given an offset previously obtained from calling
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   129
    // mappingOffset() returns the largest page aligned address of the
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   130
    // mapping less than or equal to the buffer start address.
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   131
    private long mappingAddress(long mappingOffset) {
54823
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   132
        return mappingAddress(mappingOffset, 0);
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   133
    }
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   134
54823
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   135
    // Given an offset previously otained from calling
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   136
    // mappingOffset(index) returns the largest page aligned address
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   137
    // of the mapping less than or equal to the address of the buffer
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   138
    // element identified by index.
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   139
    private long mappingAddress(long mappingOffset, long index) {
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   140
        long indexAddress = address + index;
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   141
        return indexAddress - mappingOffset;
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   142
    }
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   143
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   144
    // given a mappingOffset previously otained from calling
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   145
    // mappingOffset() return that offset added to the buffer
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   146
    // capacity.
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   147
    private long mappingLength(long mappingOffset) {
54823
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   148
        return mappingLength(mappingOffset, (long)capacity());
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   149
    }
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   150
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   151
    // given a mappingOffset previously otained from calling
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   152
    // mappingOffset(index) return that offset added to the supplied
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   153
    // length.
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   154
    private long mappingLength(long mappingOffset, long length) {
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   155
        return length + mappingOffset;
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   156
    }
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   157
54904
c1143a0d4e22 8224042: Add private alignDown method to MappedByteBuffer
adinn
parents: 54823
diff changeset
   158
    // align address down to page size
c1143a0d4e22 8224042: Add private alignDown method to MappedByteBuffer
adinn
parents: 54823
diff changeset
   159
    private static long alignDown(long address, int pageSize) {
c1143a0d4e22 8224042: Add private alignDown method to MappedByteBuffer
adinn
parents: 54823
diff changeset
   160
        // pageSize must be a power of 2
c1143a0d4e22 8224042: Add private alignDown method to MappedByteBuffer
adinn
parents: 54823
diff changeset
   161
        return address & ~(pageSize - 1);
c1143a0d4e22 8224042: Add private alignDown method to MappedByteBuffer
adinn
parents: 54823
diff changeset
   162
    }
c1143a0d4e22 8224042: Add private alignDown method to MappedByteBuffer
adinn
parents: 54823
diff changeset
   163
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
57804
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   165
     * Tells whether this buffer was mapped against a non-volatile
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   166
     * memory device by passing one of the sync map modes {@link
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   167
     * jdk.nio.mapmode.ExtendedMapMode#READ_ONLY_SYNC
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   168
     * ExtendedMapModeMapMode#READ_ONLY_SYNC} or {@link
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   169
     * jdk.nio.mapmode.ExtendedMapMode#READ_ONLY_SYNC
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   170
     * ExtendedMapMode#READ_WRITE_SYNC} in the call to {@link
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   171
     * java.nio.channels.FileChannel#map FileChannel.map} or was
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   172
     * mapped by passing one of the other map modes.
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   173
     *
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   174
     * @return true if the file was mapped using one of the sync map
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   175
     * modes, otherwise false.
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   176
     */
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   177
    private boolean isSync() {
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   178
        return isSync;
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   179
    }
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   180
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   181
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Tells whether or not this buffer's content is resident in physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 27292
diff changeset
   185
     * <p> A return value of {@code true} implies that it is highly likely
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * that all of the data in this buffer is resident in physical memory and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * may therefore be accessed without incurring any virtual-memory page
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 27292
diff changeset
   188
     * faults or I/O operations.  A return value of {@code false} does not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * necessarily imply that the buffer's content is not resident in physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * <p> The returned value is a hint, rather than a guarantee, because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * underlying operating system may have paged out some of the buffer's data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * by the time that an invocation of this method returns.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 27292
diff changeset
   196
     * @return  {@code true} if it is likely that this buffer's content
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *          is resident in physical memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public final boolean isLoaded() {
48847
da4b1106787e 8191416: (bf) DirectByteBuffer extends MappedByteBuffer, confuses instanceof tests
bpb
parents: 47216
diff changeset
   200
        if (fd == null) {
da4b1106787e 8191416: (bf) DirectByteBuffer extends MappedByteBuffer, confuses instanceof tests
bpb
parents: 47216
diff changeset
   201
            return true;
da4b1106787e 8191416: (bf) DirectByteBuffer extends MappedByteBuffer, confuses instanceof tests
bpb
parents: 47216
diff changeset
   202
        }
57804
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   203
        // a sync mapped buffer is always loaded
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   204
        if (isSync()) {
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   205
            return true;
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   206
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if ((address == 0) || (capacity() == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return true;
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   209
        long offset = mappingOffset();
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   210
        long length = mappingLength(offset);
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   211
        return isLoaded0(mappingAddress(offset), length, Bits.pageCount(length));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
12696
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   214
    // not used, but a potential target for a store, see load() for details.
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   215
    private static byte unused;
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   216
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Loads this buffer's content into physical memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * <p> This method makes a best effort to ensure that, when it returns,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * this buffer's content is resident in physical memory.  Invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * method may cause some number of page faults and I/O operations to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * occur. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public final MappedByteBuffer load() {
48847
da4b1106787e 8191416: (bf) DirectByteBuffer extends MappedByteBuffer, confuses instanceof tests
bpb
parents: 47216
diff changeset
   228
        if (fd == null) {
da4b1106787e 8191416: (bf) DirectByteBuffer extends MappedByteBuffer, confuses instanceof tests
bpb
parents: 47216
diff changeset
   229
            return this;
da4b1106787e 8191416: (bf) DirectByteBuffer extends MappedByteBuffer, confuses instanceof tests
bpb
parents: 47216
diff changeset
   230
        }
57804
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   231
        // no need to load a sync mapped buffer
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   232
        if (isSync()) {
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   233
            return this;
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   234
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if ((address == 0) || (capacity() == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return this;
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   237
        long offset = mappingOffset();
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   238
        long length = mappingLength(offset);
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   239
        load0(mappingAddress(offset), length);
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   240
12696
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   241
        // Read a byte from each page to bring it into memory. A checksum
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   242
        // is computed as we go along to prevent the compiler from otherwise
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   243
        // considering the loop as dead code.
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   244
        Unsafe unsafe = Unsafe.getUnsafe();
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   245
        int ps = Bits.pageSize();
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   246
        int count = Bits.pageCount(length);
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   247
        long a = mappingAddress(offset);
12696
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   248
        byte x = 0;
49210
7c795d301dbf 8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents: 48847
diff changeset
   249
        try {
7c795d301dbf 8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents: 48847
diff changeset
   250
            for (int i=0; i<count; i++) {
7c795d301dbf 8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents: 48847
diff changeset
   251
                // TODO consider changing to getByteOpaque thus avoiding
7c795d301dbf 8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents: 48847
diff changeset
   252
                // dead code elimination and the need to calculate a checksum
7c795d301dbf 8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents: 48847
diff changeset
   253
                x ^= unsafe.getByte(a);
7c795d301dbf 8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents: 48847
diff changeset
   254
                a += ps;
7c795d301dbf 8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents: 48847
diff changeset
   255
            }
7c795d301dbf 8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents: 48847
diff changeset
   256
        } finally {
7c795d301dbf 8199462: Use Reference.reachabilityFence in direct ByteBuffer methods
psandoz
parents: 48847
diff changeset
   257
            Reference.reachabilityFence(this);
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   258
        }
12696
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   259
        if (unused != 0)
74ad88cf81e2 7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory
alanb
parents: 7668
diff changeset
   260
            unused = x;
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   261
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Forces any changes made to this buffer's content to be written to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * storage device containing the mapped file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <p> If the file mapped into this buffer resides on a local storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * device then when this method returns it is guaranteed that all changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * made to the buffer since it was created, or since this method was last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * invoked, will have been written to that device.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * <p> If the file does not reside on a local device then no such guarantee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * is made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * <p> If this buffer was not mapped in read/write mode ({@link
55466
a6411f1e63f3 8226203: MappedByteBuffer.force method may have no effect on implementation specific map modes
adinn
parents: 54904
diff changeset
   278
     * java.nio.channels.FileChannel.MapMode#READ_WRITE}) then
a6411f1e63f3 8226203: MappedByteBuffer.force method may have no effect on implementation specific map modes
adinn
parents: 54904
diff changeset
   279
     * invoking this method may have no effect. In particular, the
a6411f1e63f3 8226203: MappedByteBuffer.force method may have no effect on implementation specific map modes
adinn
parents: 54904
diff changeset
   280
     * method has no effect for buffers mapped in read-only or private
a6411f1e63f3 8226203: MappedByteBuffer.force method may have no effect on implementation specific map modes
adinn
parents: 54904
diff changeset
   281
     * mapping modes. This method may or may not have an effect for
a6411f1e63f3 8226203: MappedByteBuffer.force method may have no effect on implementation specific map modes
adinn
parents: 54904
diff changeset
   282
     * implementation-specific mapping modes. </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public final MappedByteBuffer force() {
48847
da4b1106787e 8191416: (bf) DirectByteBuffer extends MappedByteBuffer, confuses instanceof tests
bpb
parents: 47216
diff changeset
   287
        if (fd == null) {
da4b1106787e 8191416: (bf) DirectByteBuffer extends MappedByteBuffer, confuses instanceof tests
bpb
parents: 47216
diff changeset
   288
            return this;
da4b1106787e 8191416: (bf) DirectByteBuffer extends MappedByteBuffer, confuses instanceof tests
bpb
parents: 47216
diff changeset
   289
        }
57804
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   290
        if (isSync) {
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   291
            return force(0, limit());
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   292
        }
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   293
        if ((address != 0) && (capacity() != 0)) {
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   294
            long offset = mappingOffset();
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
   295
            force0(fd, mappingAddress(offset), mappingLength(offset));
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   296
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
54823
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   300
    /**
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   301
     * Forces any changes made to a region of this buffer's content to
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   302
     * be written to the storage device containing the mapped
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   303
     * file. The region starts at the given {@code index} in this
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   304
     * buffer and is {@code length} bytes.
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   305
     *
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   306
     * <p> If the file mapped into this buffer resides on a local
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   307
     * storage device then when this method returns it is guaranteed
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   308
     * that all changes made to the selected region buffer since it
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   309
     * was created, or since this method was last invoked, will have
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   310
     * been written to that device. The force operation is free to
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   311
     * write bytes that lie outside the specified region, for example
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   312
     * to ensure that data blocks of some device-specific granularity
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   313
     * are transferred in their entirety.
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   314
     *
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   315
     * <p> If the file does not reside on a local device then no such
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   316
     * guarantee is made.
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   317
     *
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   318
     * <p> If this buffer was not mapped in read/write mode ({@link
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   319
     * java.nio.channels.FileChannel.MapMode#READ_WRITE}) then
55466
a6411f1e63f3 8226203: MappedByteBuffer.force method may have no effect on implementation specific map modes
adinn
parents: 54904
diff changeset
   320
     * invoking this method may have no effect. In particular, the
a6411f1e63f3 8226203: MappedByteBuffer.force method may have no effect on implementation specific map modes
adinn
parents: 54904
diff changeset
   321
     * method has no effect for buffers mapped in read-only or private
a6411f1e63f3 8226203: MappedByteBuffer.force method may have no effect on implementation specific map modes
adinn
parents: 54904
diff changeset
   322
     * mapping modes. This method may or may not have an effect for
a6411f1e63f3 8226203: MappedByteBuffer.force method may have no effect on implementation specific map modes
adinn
parents: 54904
diff changeset
   323
     * implementation-specific mapping modes. </p>
54823
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   324
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57804
diff changeset
   325
     * @param  index
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57804
diff changeset
   326
     *         The index of the first byte in the buffer region that is
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57804
diff changeset
   327
     *         to be written back to storage; must be non-negative
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57804
diff changeset
   328
     *         and less than limit()
54823
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   329
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57804
diff changeset
   330
     * @param  length
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57804
diff changeset
   331
     *         The length of the region in bytes; must be non-negative
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57804
diff changeset
   332
     *         and no larger than limit() - index
54823
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   333
     *
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   334
     * @throws IndexOutOfBoundsException
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   335
     *         if the preconditions on the index and length do not
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   336
     *         hold.
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   337
     *
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   338
     * @return  This buffer
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   339
     *
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   340
     * @since 13
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   341
     */
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   342
    public final MappedByteBuffer force(int index, int length) {
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   343
        if (fd == null) {
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   344
            return this;
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   345
        }
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   346
        if ((address != 0) && (limit() != 0)) {
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   347
            // check inputs
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   348
            Objects.checkFromIndexSize(index, length, limit());
57804
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   349
            if (isSync) {
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   350
                // simply force writeback of associated cache lines
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   351
                Unsafe.getUnsafe().writebackMemory(address + index, length);
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   352
            } else {
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   353
                // force writeback via file descriptor
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   354
                long offset = mappingOffset(index);
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   355
                force0(fd, mappingAddress(offset, index), mappingLength(offset, length));
9b7b9f16dfd9 8224974: Implement JEP 352
adinn
parents: 55466
diff changeset
   356
            }
54823
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   357
        }
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   358
        return this;
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   359
    }
1b940da275d2 8221696: MappedByteBuffer.force method to specify range
adinn
parents: 49210
diff changeset
   360
6128
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   361
    private native boolean isLoaded0(long address, long length, int pageCount);
f95e5fdc1a65 6934977: (bf) MappedByteBuffer.load can SIGBUS if file is truncated
alanb
parents: 5506
diff changeset
   362
    private native void load0(long address, long length);
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6128
diff changeset
   363
    private native void force0(FileDescriptor fd, long address, long length);
27292
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   364
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   365
    // -- Covariant return type overrides
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   366
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   367
    /**
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   368
     * {@inheritDoc}
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   369
     */
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   370
    @Override
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   371
    public final MappedByteBuffer position(int newPosition) {
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   372
        super.position(newPosition);
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   373
        return this;
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   374
    }
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   375
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   376
    /**
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   377
     * {@inheritDoc}
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   378
     */
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   379
    @Override
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   380
    public final MappedByteBuffer limit(int newLimit) {
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   381
        super.limit(newLimit);
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   382
        return this;
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   383
    }
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   384
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   385
    /**
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   386
     * {@inheritDoc}
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   387
     */
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   388
    @Override
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   389
    public final MappedByteBuffer mark() {
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   390
        super.mark();
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   391
        return this;
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   392
    }
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   393
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   394
    /**
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   395
     * {@inheritDoc}
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   396
     */
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   397
    @Override
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   398
    public final MappedByteBuffer reset() {
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   399
        super.reset();
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   400
        return this;
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   401
    }
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   402
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   403
    /**
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   404
     * {@inheritDoc}
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   405
     */
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   406
    @Override
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   407
    public final MappedByteBuffer clear() {
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   408
        super.clear();
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   409
        return this;
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   410
    }
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   411
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   412
    /**
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   413
     * {@inheritDoc}
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   414
     */
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   415
    @Override
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   416
    public final MappedByteBuffer flip() {
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   417
        super.flip();
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   418
        return this;
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   419
    }
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   420
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   421
    /**
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   422
     * {@inheritDoc}
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   423
     */
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   424
    @Override
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   425
    public final MappedByteBuffer rewind() {
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   426
        super.rewind();
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   427
        return this;
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   428
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
}