Permalink
Browse files

Merge pull request #33 from Shohou/master

Using Java unsafe implementation crashes JVM on Solaris sparcv9 with 64 bit JVM
  • Loading branch information...
2 parents 81bd286 + aaa2999 commit 10ae801af9b0cc7d9081dce991549ba2e3ce20dd @jpountz jpountz committed Feb 3, 2014
@@ -18,6 +18,7 @@
import java.util.Arrays;
import net.jpountz.util.Native;
+import net.jpountz.util.Utils;
/**
* Entry point for the LZ4 API.
@@ -105,9 +106,13 @@ public static synchronized LZ4Factory unsafeInstance() {
* working {@link sun.misc.Unsafe}.
*/
public static LZ4Factory fastestJavaInstance() {
- try {
- return unsafeInstance();
- } catch (Throwable t) {
+ if (Utils.isUnalignedAccessAllowed()) {
+ try {
+ return unsafeInstance();
+ } catch (Throwable t) {
+ return safeInstance();
+ }
+ } else {
return safeInstance();
}
}
@@ -46,7 +46,7 @@ private static OS os() {
return OS.MAC;
} else if (osName.contains("Windows")) {
return OS.WINDOWS;
- } else if (osName.contains("Solaris")) {
+ } else if (osName.contains("Solaris") || osName.contains("SunOS")) {
return OS.SOLARIS;
} else {
throw new UnsupportedOperationException("Unsupported operating system: "
@@ -21,6 +21,17 @@
public static final ByteOrder NATIVE_BYTE_ORDER = ByteOrder.nativeOrder();
+ private static final boolean unalignedAccessAllowed;
+ static {
+ String arch = System.getProperty("os.arch");
+ unalignedAccessAllowed = arch.equals("i386") || arch.equals("x86")
+ || arch.equals("amd64") || arch.equals("x86_64");
+ }
+
+ public static boolean isUnalignedAccessAllowed() {
+ return unalignedAccessAllowed;
+ }
+
public static void checkRange(byte[] buf, int off) {
if (off < 0 || off >= buf.length) {
throw new ArrayIndexOutOfBoundsException(off);
@@ -18,6 +18,7 @@
import java.util.Random;
import net.jpountz.util.Native;
+import net.jpountz.util.Utils;
/**
* Entry point to get {@link XXHash32} and {@link StreamingXXHash32} instances.
@@ -103,9 +104,13 @@ public static synchronized XXHashFactory unsafeInstance() {
* working {@link sun.misc.Unsafe}.
*/
public static XXHashFactory fastestJavaInstance() {
- try {
- return unsafeInstance();
- } catch (Throwable t) {
+ if (Utils.isUnalignedAccessAllowed()) {
+ try {
+ return unsafeInstance();
+ } catch (Throwable t) {
+ return safeInstance();
+ }
+ } else {
return safeInstance();
}
}

0 comments on commit 10ae801

Please sign in to comment.