From 756f58210aa35591a6140a545cf23eeae8d94978 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Tue, 13 Mar 2012 08:28:33 -0600 Subject: [PATCH] add test for sun.misc.Unsafe functionality --- classpath/avian/Machine.java | 8 +++++ classpath/sun/misc/Unsafe.java | 49 +++++++++++++++++++++++++ test/UnsafeTest.java | 65 ++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 classpath/sun/misc/Unsafe.java create mode 100644 test/UnsafeTest.java diff --git a/classpath/avian/Machine.java b/classpath/avian/Machine.java index cbc62efae6..a17cbff9d7 100644 --- a/classpath/avian/Machine.java +++ b/classpath/avian/Machine.java @@ -10,8 +10,16 @@ package avian; +import sun.misc.Unsafe; + public abstract class Machine { + private static final Unsafe unsafe = Unsafe.getUnsafe(); + public static native void dumpHeap(String outputFile); + public static Unsafe getUnsafe() { + return unsafe; + } + } diff --git a/classpath/sun/misc/Unsafe.java b/classpath/sun/misc/Unsafe.java new file mode 100644 index 0000000000..84f22b88db --- /dev/null +++ b/classpath/sun/misc/Unsafe.java @@ -0,0 +1,49 @@ +package sun.misc; + +public final class Unsafe { + private void Unsafe() { } + + private static final Unsafe Instance = new Unsafe(); + + public static Unsafe getUnsafe() { + return Instance; + } + + public native long allocateMemory(long bytes); + + public native void setMemory(long address, long count, byte value); + + public native void freeMemory(long address); + + public native byte getByte(long address); + + public native void putByte(long address, byte x); + + public native short getShort(long address); + + public native void putShort(long address, short x); + + public native char getChar(long address); + + public native void putChar(long address, char x); + + public native int getInt(long address); + + public native void putInt(long address, int x); + + public native long getLong(long address); + + public native void putLong(long address, long x); + + public native float getFloat(long address); + + public native void putFloat(long address, float x); + + public native double getDouble(long address); + + public native void putDouble(long address, double x); + + public native long getAddress(long address); + + public native void putAddress(long address, long x); +} diff --git a/test/UnsafeTest.java b/test/UnsafeTest.java new file mode 100644 index 0000000000..5167526c3b --- /dev/null +++ b/test/UnsafeTest.java @@ -0,0 +1,65 @@ +import sun.misc.Unsafe; + +public class UnsafeTest { + private static void expect(boolean v) { + if (! v) throw new RuntimeException(); + } + + public static void main(String[] args) { + Unsafe u = avian.Machine.getUnsafe(); + + final long size = 64; + long memory = u.allocateMemory(size); + try { + for (int i = 0; i < size; ++i) + u.putByte(memory + i, (byte) 42); + + for (int i = 0; i < size; ++i) + expect(u.getByte(memory + i) == 42); + + for (int i = 0; i < size / 2; ++i) + u.putShort(memory + (i * 2), (short) -12345); + + for (int i = 0; i < size / 2; ++i) + expect(u.getShort(memory + (i * 2)) == -12345); + + for (int i = 0; i < size / 2; ++i) + u.putChar(memory + (i * 2), (char) 23456); + + for (int i = 0; i < size / 2; ++i) + expect(u.getChar(memory + (i * 2)) == 23456); + + for (int i = 0; i < size / 4; ++i) + u.putInt(memory + (i * 4), 0x12345678); + + for (int i = 0; i < size / 4; ++i) + expect(u.getInt(memory + (i * 4)) == 0x12345678); + + for (int i = 0; i < size / 4; ++i) + u.putFloat(memory + (i * 4), 1.2345678F); + + for (int i = 0; i < size / 4; ++i) + expect(u.getFloat(memory + (i * 4)) == 1.2345678F); + + for (int i = 0; i < size / 8; ++i) + u.putLong(memory + (i * 8), 0x1234567890ABCDEFL); + + for (int i = 0; i < size / 8; ++i) + expect(u.getLong(memory + (i * 8)) == 0x1234567890ABCDEFL); + + for (int i = 0; i < size / 8; ++i) + u.putDouble(memory + (i * 8), 1.23456789012345D); + + for (int i = 0; i < size / 8; ++i) + expect(u.getDouble(memory + (i * 8)) == 1.23456789012345D); + + for (int i = 0; i < size / 8; ++i) + u.putAddress(memory + (i * 8), 0x12345678); + + for (int i = 0; i < size / 8; ++i) + expect(u.getAddress(memory + (i * 8)) == 0x12345678); + } finally { + u.freeMemory(memory); + } + } +}