Skip to content

Commit

Permalink
do not use unsafe on android
Browse files Browse the repository at this point in the history
  • Loading branch information
krisskross committed Jan 20, 2015
1 parent 7334751 commit cff4a17
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
12 changes: 10 additions & 2 deletions lmdbjni/src/main/java/org/fusesource/lmdbjni/NativeBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ public void write(long at, byte []source, int offset, int length) {
if( at < 0 ) throw new IllegalArgumentException("at cannot be negative");
if( at+length > capacity ) throw new ArrayIndexOutOfBoundsException("at + length exceeds the capacity of this object");
if( offset+length > source.length) throw new ArrayIndexOutOfBoundsException("offset + length exceed the length of the source buffer");
Unsafe.UNSAFE.copyMemory(source, Unsafe.ARRAY_BASE_OFFSET, null, self + at, length);
if (Unsafe.UNSAFE != null) {
Unsafe.UNSAFE.copyMemory(source, Unsafe.ARRAY_BASE_OFFSET, null, self + at, length);
} else {
JNI.buffer_copy(source, offset, self, at, length);
}
}

public void read(long at, byte []target, int offset, int length) {
Expand All @@ -187,7 +191,11 @@ public void read(long at, byte []target, int offset, int length) {
if( at < 0 ) throw new IllegalArgumentException("at cannot be negative");
if( at+length > capacity ) throw new ArrayIndexOutOfBoundsException("at + length exceeds the capacity of this object");
if( offset+length > target.length) throw new ArrayIndexOutOfBoundsException("offset + length exceed the length of the target buffer");
Unsafe.UNSAFE.copyMemory(null, self + at, target, Unsafe.ARRAY_BASE_OFFSET, length);
if (Unsafe.UNSAFE != null) {
Unsafe.UNSAFE.copyMemory(null, self + at, target, Unsafe.ARRAY_BASE_OFFSET, length);
} else {
JNI.buffer_copy(self, at, target, offset, length);
}
}

public byte[] toByteArray() {
Expand Down
38 changes: 21 additions & 17 deletions lmdbjni/src/main/java/org/fusesource/lmdbjni/Unsafe.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,32 @@
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.util.Optional;

class Unsafe {
public static final sun.misc.Unsafe UNSAFE;
public static final int ADDRESS_SIZE;
public static final long ARRAY_BASE_OFFSET;
/** unsafe may be null on android devices */
public static sun.misc.Unsafe UNSAFE;
public static int ADDRESS_SIZE;
public static long ARRAY_BASE_OFFSET;

static {
try {
final PrivilegedExceptionAction<sun.misc.Unsafe> action = new PrivilegedExceptionAction<sun.misc.Unsafe>() {
public sun.misc.Unsafe run() throws Exception {
final Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
return (sun.misc.Unsafe) field.get(null);
}
};

UNSAFE = AccessController.doPrivileged(action);
} catch (final Exception ex) {
throw new RuntimeException(ex);
if (!Util.isAndroid) {
try {
final PrivilegedExceptionAction<sun.misc.Unsafe> action = new PrivilegedExceptionAction<sun.misc.Unsafe>() {
public sun.misc.Unsafe run() throws Exception {
final Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
return (sun.misc.Unsafe) field.get(null);
}
};

UNSAFE = AccessController.doPrivileged(action);
ADDRESS_SIZE = UNSAFE.addressSize();
ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(byte[].class);
} catch (final Exception ex) {
throw new RuntimeException(ex);
}
}
ADDRESS_SIZE = UNSAFE.addressSize();
ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(byte[].class);
}

public static long getAddress(long address, int offset) {
Expand Down
9 changes: 9 additions & 0 deletions lmdbjni/src/main/java/org/fusesource/lmdbjni/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @author <a href="http://hiramchirino.com">Hiram Chirino</a>
*/
class Util {
public static final boolean isAndroid = isAndroid();

public static String string(long ptr) {
if( ptr == 0 )
Expand All @@ -47,4 +48,12 @@ public static void checkArgNotNull(Object value, String name) {
}
}

private static boolean isAndroid() {
try {
Class.forName("android.os.Process");
return true;
} catch (Throwable ignored) {
return false;
}
}
}
6 changes: 5 additions & 1 deletion lmdbjni/src/main/java/org/fusesource/lmdbjni/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ public byte[] toByteArray() {
throw new ArrayIndexOutOfBoundsException("Native slice is larger than the maximum Java array");
}
byte[] rc = new byte[(int) mv_size];
Unsafe.getBytes(mv_data, 0, rc);
if (Unsafe.UNSAFE != null) {
Unsafe.getBytes(mv_data, 0, rc);
} else {
JNI.buffer_copy(mv_data, 0, rc, 0, rc.length);
}
return rc;
}

Expand Down

0 comments on commit cff4a17

Please sign in to comment.