8224042: Add private alignDown method to MappedByteBuffer
authoradinn
Thu, 16 May 2019 15:45:46 +0100
changeset 54904 c1143a0d4e22
parent 54903 5a211ee83bf1
child 54905 d43cc2039ccf
8224042: Add private alignDown method to MappedByteBuffer Summary: Use alignDown method to round addresses down to page boundary Reviewed-by: stuefe, mikael, alanb
src/java.base/share/classes/java/nio/MappedByteBuffer.java
--- a/src/java.base/share/classes/java/nio/MappedByteBuffer.java	Wed May 15 17:13:57 2019 -0700
+++ b/src/java.base/share/classes/java/nio/MappedByteBuffer.java	Thu May 16 15:45:46 2019 +0100
@@ -106,7 +106,7 @@
     private long mappingOffset(int index) {
         int ps = Bits.pageSize();
         long indexAddress = address + index;
-        long baseAddress = (indexAddress & ~(ps-1));
+        long baseAddress = alignDown(indexAddress, ps);
         return indexAddress - baseAddress;
     }
 
@@ -140,6 +140,12 @@
         return length + mappingOffset;
     }
 
+    // align address down to page size
+    private static long alignDown(long address, int pageSize) {
+        // pageSize must be a power of 2
+        return address & ~(pageSize - 1);
+    }
+
     /**
      * Tells whether or not this buffer's content is resident in physical
      * memory.