From a538f5403319187654916e493927066e3deb6308 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Tue, 1 Mar 2016 18:28:04 -0700 Subject: [PATCH] add sun.misc.Unsafe.putDouble(Object,long,double) --- classpath/sun/misc/Unsafe.java | 8 ++++++++ src/classpath-openjdk.cpp | 14 ++++++++++++++ test/UnsafeTest.java | 26 +++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/classpath/sun/misc/Unsafe.java b/classpath/sun/misc/Unsafe.java index 3d2e9bd787..a8fdedc227 100644 --- a/classpath/sun/misc/Unsafe.java +++ b/classpath/sun/misc/Unsafe.java @@ -96,6 +96,14 @@ public final class Unsafe { putLongVolatile(o, offset, x); } + public double getDouble(Object o, long offset) { + return getDoubleVolatile(o, offset); + } + + public void putDouble(Object o, long offset, double x) { + putDoubleVolatile(o, offset, x); + } + public native void putOrderedLong(Object o, long offset, long x); public native void putOrderedInt(Object o, long offset, int x); diff --git a/src/classpath-openjdk.cpp b/src/classpath-openjdk.cpp index 0da02d0eef..bbfc94a6e8 100644 --- a/src/classpath-openjdk.cpp +++ b/src/classpath-openjdk.cpp @@ -3093,6 +3093,20 @@ extern "C" AVIAN_EXPORT void JNICALL fieldAtOffset(o, offset) = value; } +extern "C" AVIAN_EXPORT void JNICALL +Avian_sun_misc_Unsafe_putDouble__Ljava_lang_Object_2JD(Thread*, + object, + uintptr_t* arguments) +{ + object o = reinterpret_cast(arguments[1]); + int64_t offset; + memcpy(&offset, arguments + 2, 8); + jdouble value; + memcpy(&value, arguments + 4, 8); + + fieldAtOffset(o, offset) = value; +} + extern "C" AVIAN_EXPORT int64_t JNICALL Avian_sun_misc_Unsafe_pageSize(Thread*, object, uintptr_t*) { diff --git a/test/UnsafeTest.java b/test/UnsafeTest.java index c7f4555773..732c281a4a 100644 --- a/test/UnsafeTest.java +++ b/test/UnsafeTest.java @@ -134,7 +134,30 @@ public class UnsafeTest { == 1.23456789012345D); } - public static void main(String[] args) { + private static class Data { + public long longField; + public double doubleField; + } + + private static void unsafeObject(Unsafe u) throws Exception { + final long longOffset = u.objectFieldOffset + (Data.class.getField("longField")); + + final long doubleOffset = u.objectFieldOffset + (Data.class.getField("doubleField")); + + Data data = new Data(); + + u.putLong(data, longOffset, 0x1234567890ABCDEFL); + + u.putDouble(data, doubleOffset, 1.23456789012345D); + + expect(u.getLong(data, longOffset) == 0x1234567890ABCDEFL); + + expect(u.getDouble(data, doubleOffset) == 1.23456789012345D); + } + + public static void main(String[] args) throws Exception { System.out.println("method count is " + Unsafe.class.getDeclaredMethods().length); @@ -143,5 +166,6 @@ public class UnsafeTest { unsafeCatch(u); unsafeMemory(u); unsafeArray(u); + unsafeObject(u); } }