Kaydet (Commit) a8130447 authored tarafından Tomaž Vajngerl's avatar Tomaž Vajngerl

android: VM based implementation of DirectBufferAllocator

DirectBufferAllocator is responsible to allocate buffer. We used
Fennec JNI based allocation (and freeing) because of overallocation
bug in some Android versions. With this the VM based allocator
implementation is added and used by default because of bugs that
happen in newer Android versions (specifically when ART is used
as the Android VM).

Change-Id: I07eb364fd1647b3a09d1568d4fef82398a02dfeb
üst 46ccb683
...@@ -11,10 +11,14 @@ package org.libreoffice.kit; ...@@ -11,10 +11,14 @@ package org.libreoffice.kit;
// https://code.google.com/p/android/issues/detail?id=16941 // https://code.google.com/p/android/issues/detail?id=16941
// //
import android.util.Log;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
public class DirectBufferAllocator { public class DirectBufferAllocator {
private static final String LOGTAG = DirectBufferAllocator.class.getSimpleName();
private DirectBufferAllocator() { private DirectBufferAllocator() {
} }
...@@ -23,13 +27,23 @@ public class DirectBufferAllocator { ...@@ -23,13 +27,23 @@ public class DirectBufferAllocator {
private static native void freeDirectBufferNative(ByteBuffer aBuffer); private static native void freeDirectBufferNative(ByteBuffer aBuffer);
public static ByteBuffer allocate(int size) { public static ByteBuffer allocate(int size) {
if (size <= 0) { Log.i(LOGTAG, "Buffer size: " + size);
throw new IllegalArgumentException("Invalid size " + size); return allocateVM(size);
} }
public static ByteBuffer free(ByteBuffer buffer) {
return freeVM(buffer);
}
private static ByteBuffer allocateJNI(int size) {
ByteBuffer directBuffer = allocateDirectBufferNative(size); ByteBuffer directBuffer = allocateDirectBufferNative(size);
if (directBuffer == null) { if (directBuffer == null) {
throw new OutOfMemoryError("allocateDirectBuffer() returned null"); if (size <= 0) {
throw new IllegalArgumentException("Invalid allocation size: " + size);
} else {
throw new OutOfMemoryError("allocateDirectBuffer() returned null");
}
} else if (!directBuffer.isDirect()) { } else if (!directBuffer.isDirect()) {
throw new AssertionError("allocateDirectBuffer() did not return a direct buffer"); throw new AssertionError("allocateDirectBuffer() did not return a direct buffer");
} }
...@@ -37,7 +51,7 @@ public class DirectBufferAllocator { ...@@ -37,7 +51,7 @@ public class DirectBufferAllocator {
return directBuffer; return directBuffer;
} }
public static ByteBuffer free(ByteBuffer buffer) { private static ByteBuffer freeJNI(ByteBuffer buffer) {
if (buffer == null) { if (buffer == null) {
return null; return null;
} }
...@@ -49,4 +63,31 @@ public class DirectBufferAllocator { ...@@ -49,4 +63,31 @@ public class DirectBufferAllocator {
freeDirectBufferNative(buffer); freeDirectBufferNative(buffer);
return null; return null;
} }
}
\ No newline at end of file private static ByteBuffer allocateVM(int size) {
ByteBuffer directBuffer = ByteBuffer.allocateDirect(size);
if (directBuffer == null) {
if (size <= 0) {
throw new IllegalArgumentException("Invalid allocation size: " + size);
} else {
throw new OutOfMemoryError("allocateDirectBuffer() returned null");
}
} else if (!directBuffer.isDirect()) {
throw new AssertionError("allocateDirectBuffer() did not return a direct buffer");
}
return directBuffer;
}
private static ByteBuffer freeVM(ByteBuffer buffer) {
if (buffer == null) {
return null;
}
if (!buffer.isDirect()) {
throw new IllegalArgumentException("buffer must be direct");
}
return null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment