OpenJDK / jdk-updates / jdk12u
changeset 41109:a2d5b18586fa
8161720: Better byte behavior for off-heap data
Summary: Normalize boolean values read with Unsafe.
Reviewed-by: aph, simonis, jrose, psandoz
author | zmajo |
---|---|
date | Tue, 30 Aug 2016 09:30:07 +0200 |
parents | 90ec9ea5c9d0 |
children | 7344bf3b3a6f e09e871860a7 |
files | jdk/src/java.base/share/classes/jdk/internal/misc/Unsafe.java |
diffstat | 1 files changed, 40 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/jdk/src/java.base/share/classes/jdk/internal/misc/Unsafe.java Fri Aug 26 10:14:15 2016 -0700 +++ b/jdk/src/java.base/share/classes/jdk/internal/misc/Unsafe.java Tue Aug 30 09:30:07 2016 +0200 @@ -1600,11 +1600,50 @@ return weakCompareAndSwapShort(o, offset, c2s(expected), c2s(x)); } + /** + * The JVM converts integral values to boolean values using two + * different conventions, byte testing against zero and truncation + * to least-significant bit. + * + * <p>The JNI documents specify that, at least for returning + * values from native methods, a Java boolean value is converted + * to the value-set 0..1 by first truncating to a byte (0..255 or + * maybe -128..127) and then testing against zero. Thus, Java + * booleans in non-Java data structures are by convention + * represented as 8-bit containers containing either zero (for + * false) or any non-zero value (for true). + * + * <p>Java booleans in the heap are also stored in bytes, but are + * strongly normalized to the value-set 0..1 (i.e., they are + * truncated to the least-significant bit). + * + * <p>The main reason for having different conventions for + * conversion is performance: Truncation to the least-significant + * bit can be usually implemented with fewer (machine) + * instructions than byte testing against zero. + * + * <p>A number of Unsafe methods load boolean values from the heap + * as bytes. Unsafe converts those values according to the JNI + * rules (i.e, using the "testing against zero" convention). The + * method {@code byte2bool} implements that conversion. + * + * @param b the byte to be converted to boolean + * @return the result of the conversion + */ @ForceInline private boolean byte2bool(byte b) { - return b > 0; + return b != 0; } + /** + * Convert a boolean value to a byte. The return value is strongly + * normalized to the value-set 0..1 (i.e., the value is truncated + * to the least-significant bit). See {@link #byte2bool(byte)} for + * more details on conversion conventions. + * + * @param b the boolean to be converted to byte (and then normalized) + * @return the result of the conversion + */ @ForceInline private byte bool2byte(boolean b) { return b ? (byte)1 : (byte)0;