Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

check for os.arch that known to allow unaligned memory access and ret…

…urn safe instance for other architectures
  • Loading branch information...
commit aaa299972be9767b2a23de910954fac9f7dbf31b 1 parent 81bd286
dmitry.shohov authored
View
11 src/java/net/jpountz/lz4/LZ4Factory.java
@@ -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();
}
}
View
2  src/java/net/jpountz/util/Native.java
@@ -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: "
View
11 src/java/net/jpountz/util/Utils.java
@@ -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);
View
11 src/java/net/jpountz/xxhash/XXHashFactory.java
@@ -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();
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.