diff options
author | Elliott Hughes <enh@google.com> | 2011-01-10 18:06:10 -0800 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2011-01-10 18:06:10 -0800 |
commit | 66d9e787af999d3c6b26cfd46e10e0750a5f3e6e (patch) | |
tree | a7441678420f85a1d1f0d3fda71abf5abbf22549 /luni/src/main/java | |
parent | 75582a192a436f30af115bddf709fa41c544d2e3 (diff) | |
parent | cbe26f42f5b57db71402fbb1955898d3ab79e350 (diff) | |
download | libcore-66d9e787af999d3c6b26cfd46e10e0750a5f3e6e.zip libcore-66d9e787af999d3c6b26cfd46e10e0750a5f3e6e.tar.gz libcore-66d9e787af999d3c6b26cfd46e10e0750a5f3e6e.tar.bz2 |
am cbe26f42: am c8dde40c: Merge "Get memory for direct byte buffers from the Java heap rather than the native heap." into honeycomb
* commit 'cbe26f42f5b57db71402fbb1955898d3ab79e350':
Get memory for direct byte buffers from the Java heap rather than the native heap.
Diffstat (limited to 'luni/src/main/java')
-rw-r--r-- | luni/src/main/java/java/nio/MemoryBlock.java | 29 | ||||
-rw-r--r-- | luni/src/main/java/org/apache/harmony/luni/platform/OSMemory.java | 3 |
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); |