summaryrefslogtreecommitdiffstats
path: root/luni/src/main/java
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2011-01-10 17:32:53 -0800
committerAndroid (Google) Code Review <android-gerrit@google.com>2011-01-10 17:32:53 -0800
commitc8dde40c3c62ceb79fc0b62f445d9a539fd470d3 (patch)
tree4d81aac663cfa78793197c0c2eb019a457a52b0f /luni/src/main/java
parent5c0408af32a2b1c78b2d5a93cca60b0fffddd7da (diff)
parent3676bd288bce270a51b1c51f1052b76e83e71412 (diff)
downloadlibcore-c8dde40c3c62ceb79fc0b62f445d9a539fd470d3.zip
libcore-c8dde40c3c62ceb79fc0b62f445d9a539fd470d3.tar.gz
libcore-c8dde40c3c62ceb79fc0b62f445d9a539fd470d3.tar.bz2
Merge "Get memory for direct byte buffers from the Java heap rather than the native heap." into honeycomb
Diffstat (limited to 'luni/src/main/java')
-rw-r--r--luni/src/main/java/java/nio/MemoryBlock.java29
-rw-r--r--luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java3
2 files changed, 17 insertions, 15 deletions
diff --git a/luni/src/main/java/java/nio/MemoryBlock.java b/luni/src/main/java/java/nio/MemoryBlock.java
index 84ff683..76a7b36 100644
--- a/luni/src/main/java/java/nio/MemoryBlock.java
+++ b/luni/src/main/java/java/nio/MemoryBlock.java
@@ -17,6 +17,7 @@
package java.nio;
+import dalvik.system.VMRuntime;
import java.io.IOException;
import java.nio.channels.FileChannel.MapMode;
import org.apache.harmony.luni.platform.OSMemory;
@@ -43,22 +44,23 @@ class MemoryBlock {
}
/**
- * Handles calling free(3) on a native heap block.
+ * Non-movable heap blocks are byte arrays on the Java heap that the GC
+ * guarantees not to move. Used to implement DirectByteBuffer.
+ *
+ * Losing the strong reference to the array is sufficient
+ * to allow the GC to reclaim the storage. No finalizer needed.
*/
- private static class NativeHeapBlock extends MemoryBlock {
- private NativeHeapBlock(int address, long byteCount) {
+ private static class NonMovableHeapBlock extends MemoryBlock {
+ private byte[] array;
+
+ private NonMovableHeapBlock(byte[] array, int address, long byteCount) {
super(address, byteCount);
+ this.array = array;
}
@Override public void free() {
- if (address != 0) {
- OSMemory.free(address);
- address = 0;
- }
- }
-
- @Override protected void finalize() throws Throwable {
- free();
+ array = null;
+ address = 0;
}
}
@@ -86,7 +88,10 @@ class MemoryBlock {
}
public static MemoryBlock allocate(int byteCount) {
- return new NativeHeapBlock(OSMemory.calloc(byteCount), byteCount);
+ VMRuntime runtime = VMRuntime.getRuntime();
+ byte[] array = (byte[]) runtime.newNonMovableArray(byte.class, byteCount);
+ int address = (int) runtime.addressOf(array);
+ return new NonMovableHeapBlock(array, address, byteCount);
}
public static MemoryBlock wrapFromJni(int address, long byteCount) {
diff --git a/luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java b/luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java
index 721f8d6..c567432 100644
--- a/luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java
+++ b/luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java
@@ -139,9 +139,6 @@ public final class OSMemory {
}
}
- public static native int calloc(int byteCount) throws OutOfMemoryError;
- public static native void free(int address);
-
public static native void memmove(int destAddress, int srcAddress, long byteCount);
public static native byte peekByte(int address);