8224042: Add private alignDown method to MappedByteBuffer
Summary: Use alignDown method to round addresses down to page boundary
Reviewed-by: stuefe, mikael, alanb
--- 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.