diff options
author | jfrijters <jfrijters> | 2011-07-07 11:04:32 +0400 |
---|---|---|
committer | jfrijters <jfrijters> | 2011-07-07 11:04:32 +0400 |
commit | 12a909f21f9f479e97207fce3c6038f874e355f5 (patch) | |
tree | d1a404e59235cd1da3779634bb76b9c4cc0b7b16 | |
parent | 260f7bde7c7216c3aa486986d708464c51a40231 (diff) |
Updated java.nio.Bits to OpenJDK 7.
-rw-r--r-- | openjdk/allsources.lst | 2 | ||||
-rw-r--r-- | openjdk/java/nio/Bits.java | 256 | ||||
-rw-r--r-- | openjdk/map.xml | 10 | ||||
-rw-r--r-- | openjdk/sun/misc/SharedSecrets.java | 15 |
4 files changed, 205 insertions, 78 deletions
diff --git a/openjdk/allsources.lst b/openjdk/allsources.lst index 7ff10380..e104e16c 100644 --- a/openjdk/allsources.lst +++ b/openjdk/allsources.lst @@ -11468,7 +11468,6 @@ sun/security/jgss/wrapper/SunNativeProvider.java @OPENJDK7@/jdk/src/share/classes/sun/management/MemoryUsageCompositeData.java @OPENJDK7@/jdk/src/share/classes/sun/management/MonitorInfoCompositeData.java @OPENJDK7@/jdk/src/share/classes/sun/management/OperatingSystemImpl.java - @OPENJDK7@/jdk/src/share/classes/sun/management/RuntimeImpl.java @OPENJDK7@/jdk/src/share/classes/sun/management/StackTraceElementCompositeData.java @OPENJDK7@/jdk/src/share/classes/sun/management/ThreadInfoCompositeData.java @@ -11509,6 +11508,7 @@ sun/security/jgss/wrapper/SunNativeProvider.java @OPENJDK7@/jdk/src/share/classes/sun/misc/JavaIOFileDescriptorAccess.java @OPENJDK7@/jdk/src/share/classes/sun/misc/JavaLangAccess.java @OPENJDK6@/jdk/src/share/classes/sun/misc/JavaNetAccess.java +@OPENJDK7@/jdk/src/share/classes/sun/misc/JavaNioAccess.java @OPENJDK6@/jdk/src/share/classes/sun/misc/JavaSecurityAccess.java @OPENJDK6@/jdk/src/share/classes/sun/misc/JavaSecurityProtectionDomainAccess.java @OPENJDK6@/jdk/src/share/classes/sun/misc/JavaUtilJarAccess.java diff --git a/openjdk/java/nio/Bits.java b/openjdk/java/nio/Bits.java index d0948804..8dec75b6 100644 --- a/openjdk/java/nio/Bits.java +++ b/openjdk/java/nio/Bits.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2007, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -26,7 +26,6 @@ package java.nio; import java.security.AccessController; -import java.security.PrivilegedAction; import sun.misc.Unsafe; import sun.misc.VM; @@ -42,25 +41,19 @@ class Bits { // package-private // -- Swapping -- static short swap(short x) { - return (short)((x << 8) | - ((char)x >>> 8)); + return Short.reverseBytes(x); } static char swap(char x) { - return (char)((x << 8) | - (x >>> 8)); + return Character.reverseBytes(x); } static int swap(int x) { - return ((x << 24) | - ((x & 0x0000ff00) << 8) | - ((x & 0x00ff0000) >>> 8) | - (x >>> 24)); + return Integer.reverseBytes(x); } static long swap(long x) { - return (((long)swap((int)x) << 32) | - ((long)swap((int)(x >>> 32)) & 0xffffffffL)); + return Long.reverseBytes(x); } @@ -72,57 +65,57 @@ class Bits { // package-private static char getCharL(ByteBuffer bb, int bi) { return makeChar(bb._get(bi + 1), - bb._get(bi + 0)); + bb._get(bi )); } @cli.System.Security.SecurityCriticalAttribute.Annotation static char getCharL(long a) { return makeChar(_get(a + 1), - _get(a + 0)); + _get(a )); } static char getCharB(ByteBuffer bb, int bi) { - return makeChar(bb._get(bi + 0), + return makeChar(bb._get(bi ), bb._get(bi + 1)); } @cli.System.Security.SecurityCriticalAttribute.Annotation static char getCharB(long a) { - return makeChar(_get(a + 0), + return makeChar(_get(a ), _get(a + 1)); } static char getChar(ByteBuffer bb, int bi, boolean bigEndian) { - return (bigEndian ? getCharB(bb, bi) : getCharL(bb, bi)); + return bigEndian ? getCharB(bb, bi) : getCharL(bb, bi); } @cli.System.Security.SecurityCriticalAttribute.Annotation static char getChar(long a, boolean bigEndian) { - return (bigEndian ? getCharB(a) : getCharL(a)); + return bigEndian ? getCharB(a) : getCharL(a); } private static byte char1(char x) { return (byte)(x >> 8); } - private static byte char0(char x) { return (byte)(x >> 0); } + private static byte char0(char x) { return (byte)(x ); } static void putCharL(ByteBuffer bb, int bi, char x) { - bb._put(bi + 0, char0(x)); + bb._put(bi , char0(x)); bb._put(bi + 1, char1(x)); } @cli.System.Security.SecurityCriticalAttribute.Annotation static void putCharL(long a, char x) { - _put(a + 0, char0(x)); + _put(a , char0(x)); _put(a + 1, char1(x)); } static void putCharB(ByteBuffer bb, int bi, char x) { - bb._put(bi + 0, char1(x)); + bb._put(bi , char1(x)); bb._put(bi + 1, char0(x)); } @cli.System.Security.SecurityCriticalAttribute.Annotation static void putCharB(long a, char x) { - _put(a + 0, char1(x)); + _put(a , char1(x)); _put(a + 1, char0(x)); } @@ -150,57 +143,57 @@ class Bits { // package-private static short getShortL(ByteBuffer bb, int bi) { return makeShort(bb._get(bi + 1), - bb._get(bi + 0)); + bb._get(bi )); } @cli.System.Security.SecurityCriticalAttribute.Annotation static short getShortL(long a) { return makeShort(_get(a + 1), - _get(a)); + _get(a )); } static short getShortB(ByteBuffer bb, int bi) { - return makeShort(bb._get(bi + 0), + return makeShort(bb._get(bi ), bb._get(bi + 1)); } @cli.System.Security.SecurityCriticalAttribute.Annotation static short getShortB(long a) { - return makeShort(_get(a), + return makeShort(_get(a ), _get(a + 1)); } static short getShort(ByteBuffer bb, int bi, boolean bigEndian) { - return (bigEndian ? getShortB(bb, bi) : getShortL(bb, bi)); + return bigEndian ? getShortB(bb, bi) : getShortL(bb, bi); } @cli.System.Security.SecurityCriticalAttribute.Annotation static short getShort(long a, boolean bigEndian) { - return (bigEndian ? getShortB(a) : getShortL(a)); + return bigEndian ? getShortB(a) : getShortL(a); } private static byte short1(short x) { return (byte)(x >> 8); } - private static byte short0(short x) { return (byte)(x >> 0); } + private static byte short0(short x) { return (byte)(x ); } static void putShortL(ByteBuffer bb, int bi, short x) { - bb._put(bi + 0, short0(x)); + bb._put(bi , short0(x)); bb._put(bi + 1, short1(x)); } @cli.System.Security.SecurityCriticalAttribute.Annotation static void putShortL(long a, short x) { - _put(a, short0(x)); + _put(a , short0(x)); _put(a + 1, short1(x)); } static void putShortB(ByteBuffer bb, int bi, short x) { - bb._put(bi + 0, short1(x)); + bb._put(bi , short1(x)); bb._put(bi + 1, short0(x)); } @cli.System.Security.SecurityCriticalAttribute.Annotation static void putShortB(long a, short x) { - _put(a, short1(x)); + _put(a , short1(x)); _put(a + 1, short0(x)); } @@ -223,17 +216,17 @@ class Bits { // package-private // -- get/put int -- static private int makeInt(byte b3, byte b2, byte b1, byte b0) { - return (((b3 & 0xff) << 24) | + return (((b3 ) << 24) | ((b2 & 0xff) << 16) | ((b1 & 0xff) << 8) | - ((b0 & 0xff) << 0)); + ((b0 & 0xff) )); } static int getIntL(ByteBuffer bb, int bi) { return makeInt(bb._get(bi + 3), bb._get(bi + 2), bb._get(bi + 1), - bb._get(bi + 0)); + bb._get(bi )); } @cli.System.Security.SecurityCriticalAttribute.Annotation @@ -241,11 +234,11 @@ class Bits { // package-private return makeInt(_get(a + 3), _get(a + 2), _get(a + 1), - _get(a + 0)); + _get(a )); } static int getIntB(ByteBuffer bb, int bi) { - return makeInt(bb._get(bi + 0), + return makeInt(bb._get(bi ), bb._get(bi + 1), bb._get(bi + 2), bb._get(bi + 3)); @@ -253,31 +246,31 @@ class Bits { // package-private @cli.System.Security.SecurityCriticalAttribute.Annotation static int getIntB(long a) { - return makeInt(_get(a + 0), + return makeInt(_get(a ), _get(a + 1), _get(a + 2), _get(a + 3)); } static int getInt(ByteBuffer bb, int bi, boolean bigEndian) { - return (bigEndian ? getIntB(bb, bi) : getIntL(bb, bi)); + return bigEndian ? getIntB(bb, bi) : getIntL(bb, bi) ; } @cli.System.Security.SecurityCriticalAttribute.Annotation static int getInt(long a, boolean bigEndian) { - return (bigEndian ? getIntB(a) : getIntL(a)); + return bigEndian ? getIntB(a) : getIntL(a) ; } private static byte int3(int x) { return (byte)(x >> 24); } private static byte int2(int x) { return (byte)(x >> 16); } private static byte int1(int x) { return (byte)(x >> 8); } - private static byte int0(int x) { return (byte)(x >> 0); } + private static byte int0(int x) { return (byte)(x ); } static void putIntL(ByteBuffer bb, int bi, int x) { bb._put(bi + 3, int3(x)); bb._put(bi + 2, int2(x)); bb._put(bi + 1, int1(x)); - bb._put(bi + 0, int0(x)); + bb._put(bi , int0(x)); } @cli.System.Security.SecurityCriticalAttribute.Annotation @@ -285,11 +278,11 @@ class Bits { // package-private _put(a + 3, int3(x)); _put(a + 2, int2(x)); _put(a + 1, int1(x)); - _put(a + 0, int0(x)); + _put(a , int0(x)); } static void putIntB(ByteBuffer bb, int bi, int x) { - bb._put(bi + 0, int3(x)); + bb._put(bi , int3(x)); bb._put(bi + 1, int2(x)); bb._put(bi + 2, int1(x)); bb._put(bi + 3, int0(x)); @@ -297,7 +290,7 @@ class Bits { // package-private @cli.System.Security.SecurityCriticalAttribute.Annotation static void putIntB(long a, int x) { - _put(a + 0, int3(x)); + _put(a , int3(x)); _put(a + 1, int2(x)); _put(a + 2, int1(x)); _put(a + 3, int0(x)); @@ -324,14 +317,14 @@ class Bits { // package-private static private long makeLong(byte b7, byte b6, byte b5, byte b4, byte b3, byte b2, byte b1, byte b0) { - return ((((long)b7 & 0xff) << 56) | + return ((((long)b7 ) << 56) | (((long)b6 & 0xff) << 48) | (((long)b5 & 0xff) << 40) | (((long)b4 & 0xff) << 32) | (((long)b3 & 0xff) << 24) | (((long)b2 & 0xff) << 16) | (((long)b1 & 0xff) << 8) | - (((long)b0 & 0xff) << 0)); + (((long)b0 & 0xff) )); } static long getLongL(ByteBuffer bb, int bi) { @@ -342,7 +335,7 @@ class Bits { // package-private bb._get(bi + 3), bb._get(bi + 2), bb._get(bi + 1), - bb._get(bi + 0)); + bb._get(bi )); } @cli.System.Security.SecurityCriticalAttribute.Annotation @@ -354,11 +347,11 @@ class Bits { // package-private _get(a + 3), _get(a + 2), _get(a + 1), - _get(a + 0)); + _get(a )); } static long getLongB(ByteBuffer bb, int bi) { - return makeLong(bb._get(bi + 0), + return makeLong(bb._get(bi ), bb._get(bi + 1), bb._get(bi + 2), bb._get(bi + 3), @@ -370,7 +363,7 @@ class Bits { // package-private @cli.System.Security.SecurityCriticalAttribute.Annotation static long getLongB(long a) { - return makeLong(_get(a + 0), + return makeLong(_get(a ), _get(a + 1), _get(a + 2), _get(a + 3), @@ -381,12 +374,12 @@ class Bits { // package-private } static long getLong(ByteBuffer bb, int bi, boolean bigEndian) { - return (bigEndian ? getLongB(bb, bi) : getLongL(bb, bi)); + return bigEndian ? getLongB(bb, bi) : getLongL(bb, bi); } @cli.System.Security.SecurityCriticalAttribute.Annotation static long getLong(long a, boolean bigEndian) { - return (bigEndian ? getLongB(a) : getLongL(a)); + return bigEndian ? getLongB(a) : getLongL(a); } private static byte long7(long x) { return (byte)(x >> 56); } @@ -396,7 +389,7 @@ class Bits { // package-private private static byte long3(long x) { return (byte)(x >> 24); } private static byte long2(long x) { return (byte)(x >> 16); } private static byte long1(long x) { return (byte)(x >> 8); } - private static byte long0(long x) { return (byte)(x >> 0); } + private static byte long0(long x) { return (byte)(x ); } static void putLongL(ByteBuffer bb, int bi, long x) { bb._put(bi + 7, long7(x)); @@ -406,7 +399,7 @@ class Bits { // package-private bb._put(bi + 3, long3(x)); bb._put(bi + 2, long2(x)); bb._put(bi + 1, long1(x)); - bb._put(bi + 0, long0(x)); + bb._put(bi , long0(x)); } @cli.System.Security.SecurityCriticalAttribute.Annotation @@ -418,11 +411,11 @@ class Bits { // package-private _put(a + 3, long3(x)); _put(a + 2, long2(x)); _put(a + 1, long1(x)); - _put(a + 0, long0(x)); + _put(a , long0(x)); } static void putLongB(ByteBuffer bb, int bi, long x) { - bb._put(bi + 0, long7(x)); + bb._put(bi , long7(x)); bb._put(bi + 1, long6(x)); bb._put(bi + 2, long5(x)); bb._put(bi + 3, long4(x)); @@ -434,7 +427,7 @@ class Bits { // package-private @cli.System.Security.SecurityCriticalAttribute.Annotation static void putLongB(long a, long x) { - _put(a + 0, long7(x)); + _put(a , long7(x)); _put(a + 1, long6(x)); _put(a + 2, long5(x)); _put(a + 3, long4(x)); @@ -481,12 +474,12 @@ class Bits { // package-private } static float getFloat(ByteBuffer bb, int bi, boolean bigEndian) { - return (bigEndian ? getFloatB(bb, bi) : getFloatL(bb, bi)); + return bigEndian ? getFloatB(bb, bi) : getFloatL(bb, bi); } @cli.System.Security.SecurityCriticalAttribute.Annotation static float getFloat(long a, boolean bigEndian) { - return (bigEndian ? getFloatB(a) : getFloatL(a)); + return bigEndian ? getFloatB(a) : getFloatL(a); } static void putFloatL(ByteBuffer bb, int bi, float x) { @@ -544,12 +537,12 @@ class Bits { // package-private } static double getDouble(ByteBuffer bb, int bi, boolean bigEndian) { - return (bigEndian ? getDoubleB(bb, bi) : getDoubleL(bb, bi)); + return bigEndian ? getDoubleB(bb, bi) : getDoubleL(bb, bi); } @cli.System.Security.SecurityCriticalAttribute.Annotation static double getDouble(long a, boolean bigEndian) { - return (bigEndian ? getDoubleB(a) : getDoubleL(a)); + return bigEndian ? getDoubleB(a) : getDoubleL(a); } static void putDoubleL(ByteBuffer bb, int bi, double x) { @@ -632,6 +625,9 @@ class Bits { // package-private return pageSize; } + static int pageCount(long size) { + return (int)(size + (long)pageSize() - 1L) / pageSize(); + } private static boolean unaligned; private static boolean unalignedKnown = false; @@ -654,21 +650,27 @@ class Bits { // package-private // direct buffer memory. This value may be changed during VM // initialization if it is launched with "-XX:MaxDirectMemorySize=<size>". private static volatile long maxMemory = VM.maxDirectMemory(); - private static volatile long reservedMemory = 0; + private static volatile long reservedMemory; + private static volatile long totalCapacity; + private static volatile long count; private static boolean memoryLimitSet = false; // These methods should be called whenever direct memory is allocated or // freed. They allow the user to control the amount of direct memory // which a process may access. All sizes are specified in bytes. - static void reserveMemory(long size) { - + static void reserveMemory(long size, int cap) { synchronized (Bits.class) { if (!memoryLimitSet && VM.isBooted()) { maxMemory = VM.maxDirectMemory(); memoryLimitSet = true; } - if (size <= maxMemory - reservedMemory) { + // -XX:MaxDirectMemorySize limits the total capacity rather than the + // actual memory usage, which will differ when buffers are page + // aligned. + if (cap <= maxMemory - totalCapacity) { reservedMemory += size; + totalCapacity += cap; + count++; return; } } @@ -681,20 +683,53 @@ class Bits { // package-private Thread.currentThread().interrupt(); } synchronized (Bits.class) { - if (reservedMemory + size > maxMemory) + if (totalCapacity + cap > maxMemory) throw new OutOfMemoryError("Direct buffer memory"); reservedMemory += size; + totalCapacity += cap; + count++; } } - static synchronized void unreserveMemory(long size) { + static synchronized void unreserveMemory(long size, int cap) { if (reservedMemory > 0) { reservedMemory -= size; + totalCapacity -= cap; + count--; assert (reservedMemory > -1); } } + // -- Monitoring of direct buffer usage -- + + static { + // setup access to this package in SharedSecrets + sun.misc.SharedSecrets.setJavaNioAccess( + new sun.misc.JavaNioAccess() { + @Override + public sun.misc.JavaNioAccess.BufferPool getDirectBufferPool() { + return new sun.misc.JavaNioAccess.BufferPool() { + @Override + public String getName() { + return "direct"; + } + @Override + public long getCount() { + return Bits.count; + } + @Override + public long getTotalCapacity() { + return Bits.totalCapacity; + } + @Override + public long getMemoryUsed() { + return Bits.reservedMemory; + } + }; + } + }); + } // -- Bulk get/put acceleration -- @@ -704,16 +739,68 @@ class Bits { // package-private static final int JNI_COPY_TO_ARRAY_THRESHOLD = 6; static final int JNI_COPY_FROM_ARRAY_THRESHOLD = 6; + // This number limits the number of bytes to copy per call to Unsafe's + // copyMemory method. A limit is imposed to allow for safepoint polling + // during a large copy + static final long UNSAFE_COPY_THRESHOLD = 1024L * 1024L; + // These methods do no bounds checking. Verification that the copy will not // result in memory corruption should be done prior to invocation. // All positions and lengths are specified in bytes. - @cli.System.Security.SecurityCriticalAttribute.Annotation - static native void copyFromByteArray(Object src, long srcPos, long dstAddr, - long length); - @cli.System.Security.SecurityCriticalAttribute.Annotation - static native void copyToByteArray(long srcAddr, Object dst, long dstPos, - long length); + /** + * Copy from given source array to destination address. + * + * @param src + * source array + * @param srcBaseOffset + * offset of first element of storage in source array + * @param srcPos + * offset within source array of the first element to read + * @param dstAddr + * destination address + * @param length + * number of bytes to copy + */ + static void copyFromArray(Object src, long srcBaseOffset, long srcPos, + long dstAddr, long length) + { + long offset = srcBaseOffset + srcPos; + while (length > 0) { + long size = (length > UNSAFE_COPY_THRESHOLD) ? UNSAFE_COPY_THRESHOLD : length; + unsafe.copyMemory(src, offset, null, dstAddr, size); + length -= size; + offset += size; + dstAddr += size; + } + } + + /** + * Copy from source address into given destination array. + * + * @param srcAddr + * source address + * @param dst + * destination array + * @param dstBaseOffset + * offset of first element of storage in destination array + * @param dstPos + * offset within destination array of the first element to write + * @param length + * number of bytes to copy + */ + static void copyToArray(long srcAddr, Object dst, long dstBaseOffset, long dstPos, + long length) + { + long offset = dstBaseOffset + dstPos; + while (length > 0) { + long size = (length > UNSAFE_COPY_THRESHOLD) ? UNSAFE_COPY_THRESHOLD : length; + unsafe.copyMemory(null, srcAddr, dst, offset, size); + length -= size; + srcAddr += size; + offset += size; + } + } @cli.System.Security.SecurityCriticalAttribute.Annotation static void copyFromCharArray(Object src, long srcPos, long dstAddr, @@ -741,7 +828,7 @@ class Bits { // package-private long length); @cli.System.Security.SecurityCriticalAttribute.Annotation static native void copyToIntArray(long srcAddr, Object dst, long dstPos, - long length); + long length); @cli.System.Security.SecurityCriticalAttribute.Annotation static native void copyFromLongArray(Object src, long srcPos, long dstAddr, @@ -750,4 +837,19 @@ class Bits { // package-private static native void copyToLongArray(long srcAddr, Object dst, long dstPos, long length); + + + // [TEMP] while we're updating from OpenJDK 6 to 7 we need these OpenJDK 6 methods + + @cli.System.Security.SecurityCriticalAttribute.Annotation + static native void copyFromByteArray(Object src, long srcPos, long dstAddr, + long length); + @cli.System.Security.SecurityCriticalAttribute.Annotation + static native void copyToByteArray(long srcAddr, Object dst, long dstPos, + long length); + + static void reserveMemory(long size) { + } + static void unreserveMemory(long size) { + } } diff --git a/openjdk/map.xml b/openjdk/map.xml index 86bc2732..53cb87a2 100644 --- a/openjdk/map.xml +++ b/openjdk/map.xml @@ -1892,6 +1892,16 @@ </code> </replace-method-call> </method> + <method name="getJavaNioAccess" sig="()Lsun.misc.JavaNioAccess;"> + <replace-method-call class="sun.misc.Unsafe" name="ensureClassInitialized" sig="(Ljava.lang.Class;)V"> + <code> + <pop /> + <pop /> + <!-- the Java source initializes java.nio.ByteOrder (which causes java.nio.Bits to initialize) --> + <runclassinit class="java.nio.Bits" /> + </code> + </replace-method-call> + </method> <method name="getJavaIOAccess" sig="()Lsun.misc.JavaIOAccess;"> <replace-method-call class="sun.misc.Unsafe" name="ensureClassInitialized" sig="(Ljava.lang.Class;)V"> <code> diff --git a/openjdk/sun/misc/SharedSecrets.java b/openjdk/sun/misc/SharedSecrets.java index f273c6cf..797fa9eb 100644 --- a/openjdk/sun/misc/SharedSecrets.java +++ b/openjdk/sun/misc/SharedSecrets.java @@ -47,6 +47,7 @@ public class SharedSecrets { private static JavaLangAccess javaLangAccess = LangHelper.getJavaLangAccess(); private static JavaIOAccess javaIOAccess; private static JavaNetAccess javaNetAccess; + private static JavaNioAccess javaNioAccess; private static JavaSecurityProtectionDomainAccess javaSecurityProtectionDomainAccess; private static JavaSecurityAccess javaSecurityAccess; private static JavaxSecurityAuthKerberosAccess javaxSecurityAuthKerberosAccess; @@ -76,6 +77,20 @@ public class SharedSecrets { return javaNetAccess; } + public static void setJavaNioAccess(JavaNioAccess jna) { + javaNioAccess = jna; + } + + public static JavaNioAccess getJavaNioAccess() { + if (javaNioAccess == null) { + // Ensure java.nio.ByteOrder is initialized; we know that + // this class initializes java.nio.Bits that provides the + // shared secret. + unsafe.ensureClassInitialized(java.nio.ByteOrder.class); + } + return javaNioAccess; + } + public static void setJavaIOAccess(JavaIOAccess jia) { javaIOAccess = jia; } |