changeset 5052:7aa3e08ba7c1

7168505: (bf) MappedByteBuffer.load does not load buffer's content into memory Reviewed-by: mduigou, forax
author alanb
date Wed, 16 May 2012 12:43:27 +0100
parents 70bf12e0b82c
children 3fc0b57dcdf3
files src/share/classes/java/nio/MappedByteBuffer.java
diffstat 1 files changed, 10 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/nio/MappedByteBuffer.java	Mon Jun 04 15:25:36 2012 +0400
+++ b/src/share/classes/java/nio/MappedByteBuffer.java	Wed May 16 12:43:27 2012 +0100
@@ -139,6 +139,9 @@
         return isLoaded0(mappingAddress(offset), length, Bits.pageCount(length));
     }
 
+    // not used, but a potential target for a store, see load() for details.
+    private static byte unused;
+
     /**
      * Loads this buffer's content into physical memory.
      *
@@ -157,15 +160,20 @@
         long length = mappingLength(offset);
         load0(mappingAddress(offset), length);
 
-        // touch each page
+        // Read a byte from each page to bring it into memory. A checksum
+        // is computed as we go along to prevent the compiler from otherwise
+        // considering the loop as dead code.
         Unsafe unsafe = Unsafe.getUnsafe();
         int ps = Bits.pageSize();
         int count = Bits.pageCount(length);
         long a = mappingAddress(offset);
+        byte x = 0;
         for (int i=0; i<count; i++) {
-            unsafe.getByte(a);
+            x ^= unsafe.getByte(a);
             a += ps;
         }
+        if (unused != 0)
+            unused = x;
 
         return this;
     }