Skip to content

Commit

Permalink
Merge pull request #33 from Shohou/master
Browse files Browse the repository at this point in the history
Using Java unsafe implementation crashes JVM on Solaris sparcv9 with 64 bit JVM
  • Loading branch information
jpountz committed Feb 3, 2014
2 parents 81bd286 + aaa2999 commit 10ae801
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/java/net/jpountz/lz4/LZ4Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Arrays;

import net.jpountz.util.Native;
import net.jpountz.util.Utils;

/**
* Entry point for the LZ4 API.
Expand Down Expand Up @@ -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();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/java/net/jpountz/util/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -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: "
Expand Down
11 changes: 11 additions & 0 deletions src/java/net/jpountz/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ public enum Utils {

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);
Expand Down
11 changes: 8 additions & 3 deletions src/java/net/jpountz/xxhash/XXHashFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
}
Expand Down

0 comments on commit 10ae801

Please sign in to comment.