< prev index next >

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

Print this page

  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 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 jdk.internal.misc.Unsafe;
 29 
 30 import java.io.FileDescriptor;
 31 import java.io.IOException;
 32 import java.io.UncheckedIOException;


 33 
 34 /* package */ class MappedMemoryUtils {
 35 
 36     static boolean isLoaded(long address, boolean isSync, long size) {
 37         // a sync mapped buffer is always loaded
 38         if (isSync) {
 39             return true;
 40         }
 41         if ((address == 0) || (size == 0))
 42             return true;
 43         long offset = mappingOffset(address);
 44         long length = mappingLength(offset, size);
 45         return isLoaded0(mappingAddress(address, offset), length, Bits.pageCount(length));
 46     }
 47 
 48     static void load(long address, boolean isSync, long size) {
 49         // no need to load a sync mapped buffer
 50         if (isSync) {
 51             return;
 52         }

 79 
 80     static void unload(long address, boolean isSync, long size) {
 81         // no need to load a sync mapped buffer
 82         if (isSync) {
 83             return;
 84         }
 85         if ((address == 0) || (size == 0))
 86             return;
 87         long offset = mappingOffset(address);
 88         long length = mappingLength(offset, size);
 89         unload0(mappingAddress(address, offset), length);
 90     }
 91 
 92     static void force(FileDescriptor fd, long address, boolean isSync, long index, long length) {
 93         if (isSync) {
 94             // simply force writeback of associated cache lines
 95             Unsafe.getUnsafe().writebackMemory(address + index, length);
 96         } else {
 97             // force writeback via file descriptor
 98             long offset = mappingOffset(address, index);


 99             try {
100                 force0(fd, mappingAddress(address, offset, index), mappingLength(offset, length));




101             } catch (IOException cause) {
102                 throw new UncheckedIOException(cause);
103             }
104         }
105     }
106 
107     // native methods
108 
109     private static native boolean isLoaded0(long address, long length, long pageCount);
110     private static native void load0(long address, long length);
111     private static native void unload0(long address, long length);
112     private static native void force0(FileDescriptor fd, long address, long length) throws IOException;
113 
114     // utility methods
115 
116     // Returns the distance (in bytes) of the buffer start from the
117     // largest page aligned address of the mapping less than or equal
118     // to the start address.
119     private static long mappingOffset(long address) {
120         return mappingOffset(address, 0);

  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 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.io.IOException;
 30 import java.io.UncheckedIOException;
 31 import jdk.internal.misc.Blocker;
 32 import jdk.internal.misc.Unsafe;
 33 
 34 /* package */ class MappedMemoryUtils {
 35 
 36     static boolean isLoaded(long address, boolean isSync, long size) {
 37         // a sync mapped buffer is always loaded
 38         if (isSync) {
 39             return true;
 40         }
 41         if ((address == 0) || (size == 0))
 42             return true;
 43         long offset = mappingOffset(address);
 44         long length = mappingLength(offset, size);
 45         return isLoaded0(mappingAddress(address, offset), length, Bits.pageCount(length));
 46     }
 47 
 48     static void load(long address, boolean isSync, long size) {
 49         // no need to load a sync mapped buffer
 50         if (isSync) {
 51             return;
 52         }

 79 
 80     static void unload(long address, boolean isSync, long size) {
 81         // no need to load a sync mapped buffer
 82         if (isSync) {
 83             return;
 84         }
 85         if ((address == 0) || (size == 0))
 86             return;
 87         long offset = mappingOffset(address);
 88         long length = mappingLength(offset, size);
 89         unload0(mappingAddress(address, offset), length);
 90     }
 91 
 92     static void force(FileDescriptor fd, long address, boolean isSync, long index, long length) {
 93         if (isSync) {
 94             // simply force writeback of associated cache lines
 95             Unsafe.getUnsafe().writebackMemory(address + index, length);
 96         } else {
 97             // force writeback via file descriptor
 98             long offset = mappingOffset(address, index);
 99             long mappingAddress = mappingAddress(address, offset, index);
100             long mappingLength = mappingLength(offset, length);
101             try {
102                 if (Thread.currentThread().isVirtual()) {
103                     Blocker.managedBlock(() -> force0(fd, mappingAddress, mappingLength));
104                 } else {
105                     force0(fd, mappingAddress, mappingLength);
106                 }
107             } catch (IOException cause) {
108                 throw new UncheckedIOException(cause);
109             }
110         }
111     }
112 
113     // native methods
114 
115     private static native boolean isLoaded0(long address, long length, long pageCount);
116     private static native void load0(long address, long length);
117     private static native void unload0(long address, long length);
118     private static native void force0(FileDescriptor fd, long address, long length) throws IOException;
119 
120     // utility methods
121 
122     // Returns the distance (in bytes) of the buffer start from the
123     // largest page aligned address of the mapping less than or equal
124     // to the start address.
125     private static long mappingOffset(long address) {
126         return mappingOffset(address, 0);
< prev index next >