< prev index next >

src/java.base/share/classes/java/nio/MappedByteBuffer.java

Print this page
rev 57242 : imported patch 8234049.patch


  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.nio;
  27 
  28 import java.io.FileDescriptor;
  29 import java.lang.ref.Reference;
  30 import java.util.Objects;


  31 import jdk.internal.misc.Unsafe;
  32 
  33 
  34 /**
  35  * A direct byte buffer whose content is a memory-mapped region of a file.
  36  *
  37  * <p> Mapped byte buffers are created via the {@link
  38  * java.nio.channels.FileChannel#map FileChannel.map} method.  This class
  39  * extends the {@link ByteBuffer} class with operations that are specific to
  40  * memory-mapped file regions.
  41  *
  42  * <p> A mapped byte buffer and the file mapping that it represents remain
  43  * valid until the buffer itself is garbage-collected.
  44  *
  45  * <p> The content of a mapped byte buffer can change at any time, for example
  46  * if the content of the corresponding region of the mapped file is changed by
  47  * this program or another.  Whether or not such changes occur, and when they
  48  * occur, is operating-system dependent and therefore unspecified.
  49  *
  50  * <a id="inaccess"></a><p> All or part of a mapped byte buffer may become


  71 
  72     // This is a little bit backwards: By rights MappedByteBuffer should be a
  73     // subclass of DirectByteBuffer, but to keep the spec clear and simple, and
  74     // for optimization purposes, it's easier to do it the other way around.
  75     // This works because DirectByteBuffer is a package-private class.
  76 
  77     // For mapped buffers, a FileDescriptor that may be used for mapping
  78     // operations if valid; null if the buffer is not mapped.
  79     private final FileDescriptor fd;
  80 
  81     // A flag true if this buffer is mapped against non-volatile
  82     // memory using one of the extended FileChannel.MapMode modes,
  83     // MapMode.READ_ONLY_SYNC or MapMode.READ_WRITE_SYNC and false if
  84     // it is mapped using any of the other modes. This flag only
  85     // determines the behavior of force operations.
  86     private final boolean isSync;
  87 
  88     // This should only be invoked by the DirectByteBuffer constructors
  89     //
  90     MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private
  91                      FileDescriptor fd, boolean isSync) {
  92         super(mark, pos, lim, cap);
  93         this.fd = fd;
  94         this.isSync = isSync;
  95     }
  96 
  97     MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private
  98                      boolean isSync) {
  99         super(mark, pos, lim, cap);
 100         this.fd = null;
 101         this.isSync = isSync;
 102     }
 103 
 104     MappedByteBuffer(int mark, int pos, int lim, int cap) { // package-private
 105         super(mark, pos, lim, cap);
 106         this.fd = null;
 107         this.isSync = false;
 108     }
 109 
 110     // Returns the distance (in bytes) of the buffer start from the
 111     // largest page aligned address of the mapping less than or equal
 112     // to the start address.
 113     private long mappingOffset() {
 114         return mappingOffset(0);
 115     }
 116 
 117     // Returns the distance (in bytes) of the buffer element
 118     // identified by index from the largest page aligned address of
 119     // the mapping less than or equal to the element address. Computed
 120     // each time to avoid storing in every direct buffer.
 121     private long mappingOffset(int index) {
 122         int ps = Bits.pageSize();
 123         long indexAddress = address + index;
 124         long baseAddress = alignDown(indexAddress, ps);
 125         return indexAddress - baseAddress;




  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.nio;
  27 
  28 import java.io.FileDescriptor;
  29 import java.lang.ref.Reference;
  30 import java.util.Objects;
  31 
  32 import jdk.internal.access.foreign.MemorySegmentProxy;
  33 import jdk.internal.misc.Unsafe;
  34 
  35 
  36 /**
  37  * A direct byte buffer whose content is a memory-mapped region of a file.
  38  *
  39  * <p> Mapped byte buffers are created via the {@link
  40  * java.nio.channels.FileChannel#map FileChannel.map} method.  This class
  41  * extends the {@link ByteBuffer} class with operations that are specific to
  42  * memory-mapped file regions.
  43  *
  44  * <p> A mapped byte buffer and the file mapping that it represents remain
  45  * valid until the buffer itself is garbage-collected.
  46  *
  47  * <p> The content of a mapped byte buffer can change at any time, for example
  48  * if the content of the corresponding region of the mapped file is changed by
  49  * this program or another.  Whether or not such changes occur, and when they
  50  * occur, is operating-system dependent and therefore unspecified.
  51  *
  52  * <a id="inaccess"></a><p> All or part of a mapped byte buffer may become


  73 
  74     // This is a little bit backwards: By rights MappedByteBuffer should be a
  75     // subclass of DirectByteBuffer, but to keep the spec clear and simple, and
  76     // for optimization purposes, it's easier to do it the other way around.
  77     // This works because DirectByteBuffer is a package-private class.
  78 
  79     // For mapped buffers, a FileDescriptor that may be used for mapping
  80     // operations if valid; null if the buffer is not mapped.
  81     private final FileDescriptor fd;
  82 
  83     // A flag true if this buffer is mapped against non-volatile
  84     // memory using one of the extended FileChannel.MapMode modes,
  85     // MapMode.READ_ONLY_SYNC or MapMode.READ_WRITE_SYNC and false if
  86     // it is mapped using any of the other modes. This flag only
  87     // determines the behavior of force operations.
  88     private final boolean isSync;
  89 
  90     // This should only be invoked by the DirectByteBuffer constructors
  91     //
  92     MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private
  93                      FileDescriptor fd, boolean isSync, MemorySegmentProxy segment) {
  94         super(mark, pos, lim, cap, segment);
  95         this.fd = fd;
  96         this.isSync = isSync;
  97     }
  98 
  99     MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private
 100                      boolean isSync, MemorySegmentProxy segment) {
 101         super(mark, pos, lim, cap, segment);
 102         this.fd = null;
 103         this.isSync = isSync;
 104     }
 105 
 106     MappedByteBuffer(int mark, int pos, int lim, int cap, MemorySegmentProxy segment) { // package-private
 107         super(mark, pos, lim, cap, segment);
 108         this.fd = null;
 109         this.isSync = false;
 110     }
 111 
 112     // Returns the distance (in bytes) of the buffer start from the
 113     // largest page aligned address of the mapping less than or equal
 114     // to the start address.
 115     private long mappingOffset() {
 116         return mappingOffset(0);
 117     }
 118 
 119     // Returns the distance (in bytes) of the buffer element
 120     // identified by index from the largest page aligned address of
 121     // the mapping less than or equal to the element address. Computed
 122     // each time to avoid storing in every direct buffer.
 123     private long mappingOffset(int index) {
 124         int ps = Bits.pageSize();
 125         long indexAddress = address + index;
 126         long baseAddress = alignDown(indexAddress, ps);
 127         return indexAddress - baseAddress;


< prev index next >