< prev index next >

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

Print this page




  60  *
  61  *
  62  * @author Mark Reinhold
  63  * @author JSR-51 Expert Group
  64  * @since 1.4
  65  */
  66 
  67 public abstract class MappedByteBuffer
  68     extends ByteBuffer
  69 {
  70 
  71     // This is a little bit backwards: By rights MappedByteBuffer should be a
  72     // subclass of DirectByteBuffer, but to keep the spec clear and simple, and
  73     // for optimization purposes, it's easier to do it the other way around.
  74     // This works because DirectByteBuffer is a package-private class.
  75 
  76     // For mapped buffers, a FileDescriptor that may be used for mapping
  77     // operations if valid; null if the buffer is not mapped.
  78     private final FileDescriptor fd;
  79 







  80     // This should only be invoked by the DirectByteBuffer constructors
  81     //
  82     MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private
  83                      FileDescriptor fd)
  84     {
  85         super(mark, pos, lim, cap);
  86         this.fd = fd;








  87     }
  88 
  89     MappedByteBuffer(int mark, int pos, int lim, int cap) { // package-private
  90         super(mark, pos, lim, cap);
  91         this.fd = null;

  92     }
  93 
  94     // Returns the distance (in bytes) of the buffer from the page aligned address
  95     // of the mapping. Computed each time to avoid storing in every direct buffer.
  96     private long mappingOffset() {
  97         int ps = Bits.pageSize();
  98         long offset = address % ps;
  99         return (offset >= 0) ? offset : (ps + offset);
 100     }
 101 
 102     private long mappingAddress(long mappingOffset) {
 103         return address - mappingOffset;
 104     }
 105 
 106     private long mappingLength(long mappingOffset) {
 107         return (long)capacity() + mappingOffset;
 108     }
 109 
 110     /**

























 111      * Tells whether or not this buffer's content is resident in physical
 112      * memory.
 113      *
 114      * <p> A return value of {@code true} implies that it is highly likely
 115      * that all of the data in this buffer is resident in physical memory and
 116      * may therefore be accessed without incurring any virtual-memory page
 117      * faults or I/O operations.  A return value of {@code false} does not
 118      * necessarily imply that the buffer's content is not resident in physical
 119      * memory.
 120      *
 121      * <p> The returned value is a hint, rather than a guarantee, because the
 122      * underlying operating system may have paged out some of the buffer's data
 123      * by the time that an invocation of this method returns.  </p>
 124      *
 125      * @return  {@code true} if it is likely that this buffer's content
 126      *          is resident in physical memory
 127      */
 128     public final boolean isLoaded() {
 129         if (fd == null) {
 130             return true;
 131         }




 132         if ((address == 0) || (capacity() == 0))
 133             return true;
 134         long offset = mappingOffset();
 135         long length = mappingLength(offset);
 136         return isLoaded0(mappingAddress(offset), length, Bits.pageCount(length));
 137     }
 138 
 139     // not used, but a potential target for a store, see load() for details.
 140     private static byte unused;
 141 
 142     /**
 143      * Loads this buffer's content into physical memory.
 144      *
 145      * <p> This method makes a best effort to ensure that, when it returns,
 146      * this buffer's content is resident in physical memory.  Invoking this
 147      * method may cause some number of page faults and I/O operations to
 148      * occur. </p>
 149      *
 150      * @return  This buffer
 151      */
 152     public final MappedByteBuffer load() {
 153         if (fd == null) {
 154             return this;
 155         }




 156         if ((address == 0) || (capacity() == 0))
 157             return this;
 158         long offset = mappingOffset();
 159         long length = mappingLength(offset);
 160         load0(mappingAddress(offset), length);
 161 
 162         // Read a byte from each page to bring it into memory. A checksum
 163         // is computed as we go along to prevent the compiler from otherwise
 164         // considering the loop as dead code.
 165         Unsafe unsafe = Unsafe.getUnsafe();
 166         int ps = Bits.pageSize();
 167         int count = Bits.pageCount(length);
 168         long a = mappingAddress(offset);
 169         byte x = 0;
 170         try {
 171             for (int i=0; i<count; i++) {
 172                 // TODO consider changing to getByteOpaque thus avoiding
 173                 // dead code elimination and the need to calculate a checksum
 174                 x ^= unsafe.getByte(a);
 175                 a += ps;


 185 
 186     /**
 187      * Forces any changes made to this buffer's content to be written to the
 188      * storage device containing the mapped file.
 189      *
 190      * <p> If the file mapped into this buffer resides on a local storage
 191      * device then when this method returns it is guaranteed that all changes
 192      * made to the buffer since it was created, or since this method was last
 193      * invoked, will have been written to that device.
 194      *
 195      * <p> If the file does not reside on a local device then no such guarantee
 196      * is made.
 197      *
 198      * <p> If this buffer was not mapped in read/write mode ({@link
 199      * java.nio.channels.FileChannel.MapMode#READ_WRITE}) then invoking this
 200      * method has no effect. </p>
 201      *
 202      * @return  This buffer
 203      */
 204     public final MappedByteBuffer force() {
































 205         if (fd == null) {
 206             return this;
 207         }
 208         if ((address != 0) && (capacity() != 0)) {








 209             long offset = mappingOffset();
 210             force0(fd, mappingAddress(offset), mappingLength(offset));









 211         }
 212         return this;
 213     }
 214 
 215     private native boolean isLoaded0(long address, long length, int pageCount);
 216     private native void load0(long address, long length);
 217     private native void force0(FileDescriptor fd, long address, long length);
 218 
 219     // -- Covariant return type overrides
 220 
 221     /**
 222      * {@inheritDoc}
 223      */
 224     @Override
 225     public final MappedByteBuffer position(int newPosition) {
 226         super.position(newPosition);
 227         return this;
 228     }
 229 
 230     /**




  60  *
  61  *
  62  * @author Mark Reinhold
  63  * @author JSR-51 Expert Group
  64  * @since 1.4
  65  */
  66 
  67 public abstract class MappedByteBuffer
  68     extends ByteBuffer
  69 {
  70 
  71     // This is a little bit backwards: By rights MappedByteBuffer should be a
  72     // subclass of DirectByteBuffer, but to keep the spec clear and simple, and
  73     // for optimization purposes, it's easier to do it the other way around.
  74     // This works because DirectByteBuffer is a package-private class.
  75 
  76     // For mapped buffers, a FileDescriptor that may be used for mapping
  77     // operations if valid; null if the buffer is not mapped.
  78     private final FileDescriptor fd;
  79 
  80     // A flag true if this buffer is mapped against persistent memory
  81     // using one of the persistent FileChannel.MapMode modes,
  82     // MapMode.READ_ONLY_PERSISTENT or MapMode.READ_WRITE_PERSISTENT
  83     // and false if it is mapped using any of other modes. this flag
  84     // only determines the behaviour of force operations.
  85     private final boolean isPersistent;
  86     
  87     // This should only be invoked by the DirectByteBuffer constructors
  88     //
  89     MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private
  90                      FileDescriptor fd, boolean isPersistent) {

  91         super(mark, pos, lim, cap);
  92         this.fd = fd;
  93         this.isPersistent = isPersistent;
  94     }
  95 
  96     MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private
  97                      boolean isPersistent) {
  98         super(mark, pos, lim, cap);
  99         this.fd = null;
 100         this.isPersistent = isPersistent;
 101     }
 102 
 103     MappedByteBuffer(int mark, int pos, int lim, int cap) { // package-private
 104         super(mark, pos, lim, cap);
 105         this.fd = null;
 106         this.isPersistent = false;
 107     }
 108 
 109     // Returns the distance (in bytes) of the buffer from the page aligned address
 110     // of the mapping. Computed each time to avoid storing in every direct buffer.
 111     private long mappingOffset() {
 112         int ps = Bits.pageSize();
 113         long offset = address % ps;
 114         return (offset >= 0) ? offset : (ps + offset);
 115     }
 116 
 117     private long mappingAddress(long mappingOffset) {
 118         return address - mappingOffset;
 119     }
 120 
 121     private long mappingLength(long mappingOffset) {
 122         return (long)capacity() + mappingOffset;
 123     }
 124 
 125     /**
 126      * Tells whether this buffer was mapped against a non-volatile
 127      * memory device by passing one of the persistent map modes {@link
 128      * java.nio.channels.FileChannel.MapMode#READ_ONLY_PERSISTENT
 129      * MapMode#READ_ONLY_PERSISTENT} or {@link
 130      * java.nio.channels.FileChannel.MapMode#READ_ONLY_PERSISTENT
 131      * MapMode#READ_WRITE_PERSISTENT} in the call to {@link
 132      * java.nio.channels.FileChannel#map FileChannel.map} or mapped
 133      * against some other form of device file by pasing one of the
 134      * other map modes.
 135      *
 136      * @return true if the file was mapped using against a
 137      * non-volatile memory device by passing one of the persistent map
 138      * modes {@link
 139      * java.nio.channels.FileChannel.MapMode#READ_ONLY_PERSISTENT
 140      * MapMode#READ_ONLY_PERSISTENT} or {@link
 141      * java.nio.channels.FileChannel.MapMode#READ_ONLY_PERSISTENT
 142      * MapMode#READ_WRITE_PERSISTENT} in the call to {@link
 143      * java.nio.channels.FileChannel#map FileChannel.map} otherwise
 144      * false.
 145      */
 146     public boolean isPersistent() {
 147         return isPersistent;
 148     }
 149     
 150     /**
 151      * Tells whether or not this buffer's content is resident in physical
 152      * memory.
 153      *
 154      * <p> A return value of {@code true} implies that it is highly likely
 155      * that all of the data in this buffer is resident in physical memory and
 156      * may therefore be accessed without incurring any virtual-memory page
 157      * faults or I/O operations.  A return value of {@code false} does not
 158      * necessarily imply that the buffer's content is not resident in physical
 159      * memory.
 160      *
 161      * <p> The returned value is a hint, rather than a guarantee, because the
 162      * underlying operating system may have paged out some of the buffer's data
 163      * by the time that an invocation of this method returns.  </p>
 164      *
 165      * @return  {@code true} if it is likely that this buffer's content
 166      *          is resident in physical memory
 167      */
 168     public final boolean isLoaded() {
 169         if (fd == null) {
 170             return true;
 171         }
 172         // a persistent mapped buffer is always loaded
 173         if (isPersistent()) {
 174             return true;
 175         }
 176         if ((address == 0) || (capacity() == 0))
 177             return true;
 178         long offset = mappingOffset();
 179         long length = mappingLength(offset);
 180         return isLoaded0(mappingAddress(offset), length, Bits.pageCount(length));
 181     }
 182 
 183     // not used, but a potential target for a store, see load() for details.
 184     private static byte unused;
 185 
 186     /**
 187      * Loads this buffer's content into physical memory.
 188      *
 189      * <p> This method makes a best effort to ensure that, when it returns,
 190      * this buffer's content is resident in physical memory.  Invoking this
 191      * method may cause some number of page faults and I/O operations to
 192      * occur. </p>
 193      *
 194      * @return  This buffer
 195      */
 196     public final MappedByteBuffer load() {
 197         if (fd == null) {
 198             return this;
 199         }
 200         // no need to load a persistent mapped buffer
 201         if (isPersistent()) {
 202             return this;
 203         }
 204         if ((address == 0) || (capacity() == 0))
 205             return this;
 206         long offset = mappingOffset();
 207         long length = mappingLength(offset);
 208         load0(mappingAddress(offset), length);
 209 
 210         // Read a byte from each page to bring it into memory. A checksum
 211         // is computed as we go along to prevent the compiler from otherwise
 212         // considering the loop as dead code.
 213         Unsafe unsafe = Unsafe.getUnsafe();
 214         int ps = Bits.pageSize();
 215         int count = Bits.pageCount(length);
 216         long a = mappingAddress(offset);
 217         byte x = 0;
 218         try {
 219             for (int i=0; i<count; i++) {
 220                 // TODO consider changing to getByteOpaque thus avoiding
 221                 // dead code elimination and the need to calculate a checksum
 222                 x ^= unsafe.getByte(a);
 223                 a += ps;


 233 
 234     /**
 235      * Forces any changes made to this buffer's content to be written to the
 236      * storage device containing the mapped file.
 237      *
 238      * <p> If the file mapped into this buffer resides on a local storage
 239      * device then when this method returns it is guaranteed that all changes
 240      * made to the buffer since it was created, or since this method was last
 241      * invoked, will have been written to that device.
 242      *
 243      * <p> If the file does not reside on a local device then no such guarantee
 244      * is made.
 245      *
 246      * <p> If this buffer was not mapped in read/write mode ({@link
 247      * java.nio.channels.FileChannel.MapMode#READ_WRITE}) then invoking this
 248      * method has no effect. </p>
 249      *
 250      * @return  This buffer
 251      */
 252     public final MappedByteBuffer force() {
 253         return force(0, capacity());
 254     }
 255 
 256     /**
 257      * Forces any changes made to some region of this buffer's content
 258      * to be written to the storage device containing the mapped file.
 259      *
 260      * <p> If the file mapped into this buffer resides on a local storage
 261      * device then when this method returns it is guaranteed that all changes
 262      * made to the buffer since it was created, or since this method was last
 263      * invoked, will have been written to that device.
 264      *
 265      * <p> If the file does not reside on a local device then no such guarantee
 266      * is made.
 267      *
 268      * <p> If this buffer was not mapped in read/write mode ({@link
 269      * java.nio.channels.FileChannel.MapMode#READ_WRITE}) then invoking this
 270      * method has no effect. </p>
 271      *
 272      * @param from
 273      *        The offset to the first byte in the buffer region that
 274      *        is to be written back to storage
 275      *
 276      * @param to
 277      *        The offset to the first byte beyond the buffer region
 278      *        that is to be written back to storage
 279      *
 280      * @return  This buffer
 281      *
 282      * @since 12
 283      */
 284     public final MappedByteBuffer force(long from, long to) {
 285         if (fd == null) {
 286             return this;
 287         }
 288         if ((address != 0) && (capacity() != 0)) {
 289             // check inputs
 290             if (from < 0 || from >= capacity()) {
 291                 throw new IllegalArgumentException();
 292             }
 293             if (to < from || to > capacity()) {
 294                 throw new IllegalArgumentException();
 295             }
 296             
 297             long offset = mappingOffset();
 298             long a = mappingAddress(offset) + from;
 299             long length = to - from;
 300             if (isPersistent) {
 301                 // simply force writeback of associated cache lines
 302                 Unsafe unsafe = Unsafe.getUnsafe();
 303                 unsafe.writebackMemory(a, length);
 304             } else {
 305                 // writeback using device associated with fd
 306                 force0(fd, a, length);
 307             }
 308         }
 309         return this;
 310     }
 311 
 312     private native boolean isLoaded0(long address, long length, int pageCount);
 313     private native void load0(long address, long length);
 314     private native void force0(FileDescriptor fd, long address, long length);
 315 
 316     // -- Covariant return type overrides
 317 
 318     /**
 319      * {@inheritDoc}
 320      */
 321     @Override
 322     public final MappedByteBuffer position(int newPosition) {
 323         super.position(newPosition);
 324         return this;
 325     }
 326 
 327     /**


< prev index next >