how to replace Unsafe.objectFieldOffset in jdk 11

229 views
Skip to first unread message

xiaobai andrew

unread,
Jun 18, 2019, 8:48:14 AM6/18/19
to mechanical-sympathy
i usr Unsafe.objectFieldOffset in jdk 8 ,but when i want to update jdk to 11, the idea compile error , it cannot  found the symbol Unsafe.
i have try to find VarHandle api,but it did not support Unsafe.objectFieldOffset

Gil Tene

unread,
Jun 18, 2019, 11:42:59 AM6/18/19
to mechanical-sympathy
Out of curiosity: What do you use Unsafe.objectFieldOffset for that VarHandle is lacking functionality for?

It is useful to gather use cases not covered by VarHandle so that we can propose additional capabilities there (preparing for a world with no Unsafe in it). Can you share an example of code?

As to your question: I assume it is Unsafe as a whole you are having issues with, and not just Unsafe.objectFieldOffset, is that correct? You can find examples of how to use Unsafe in a post-java-8 world e.g. here: http://gregluck.com/blog/archives/2017/03/using-sun-misc-unsafe-in-java-9/

xiaobai andrew

unread,
Jun 19, 2019, 10:04:26 AM6/19/19
to mechanica...@googlegroups.com
thank for you reply,i look for some api just like  VarHandle, which i can replace Unsafe.objectFildOffset without requires jdk.unsupported .
any good idea?


Gil Tene <g...@azul.com> 于2019年6月18日周二 下午11:43写道:
--
You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/mechanical-sympathy/fd3d12a2-5f1d-4795-9e58-28723b8f02d7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gil Tene

unread,
Jun 19, 2019, 10:59:17 AM6/19/19
to mechanical-sympathy
Use VarHandle. It is there and it works. It allows you to replace Unsafe.objectFildOffset with a better, safer way to do the same thing you would be doing with the information that Unsafe.objectFildOffset provides, but without ever giving you the (potentially incorrect and unsafe to use) information about the "offset" of a field within an object. It allows the manipulation field contents in pretty much any useful way that having the offset would allow you to do with Unsafe.getX(object, offset) or Unsafe.putX(object, offset), but without providing with unsafe means that will break things (like e.g. writing a long to a reference field, or overflowing an int with a long write).


On Wednesday, June 19, 2019 at 7:04:26 AM UTC-7, xiaobai andrew wrote:
thank for you reply,i look for some api just like  VarHandle, which i can replace Unsafe.objectFildOffset without requires jdk.unsupported .
any good idea?


Gil Tene <g...@azul.com> 于2019年6月18日周二 下午11:43写道:
Out of curiosity: What do you use Unsafe.objectFieldOffset for that VarHandle is lacking functionality for?

It is useful to gather use cases not covered by VarHandle so that we can propose additional capabilities there (preparing for a world with no Unsafe in it). Can you share an example of code?

As to your question: I assume it is Unsafe as a whole you are having issues with, and not just Unsafe.objectFieldOffset, is that correct? You can find examples of how to use Unsafe in a post-java-8 world e.g. here: http://gregluck.com/blog/archives/2017/03/using-sun-misc-unsafe-in-java-9/

On Tuesday, June 18, 2019 at 5:48:14 AM UTC-7, xiaobai andrew wrote:
i usr Unsafe.objectFieldOffset in jdk 8 ,but when i want to update jdk to 11, the idea compile error , it cannot  found the symbol Unsafe.
i have try to find VarHandle api,but it did not support Unsafe.objectFieldOffset

--
You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-sympathy+unsub...@googlegroups.com.

Vitaly Davidovich

unread,
Jul 17, 2019, 8:35:54 AM7/17/19
to mechanical-sympathy
VarHandle does provide suitable (and better) replacements for the various Unsafe.get/putXXX methods.  But, U.objectFieldOffset() is the only (easy?) Java way to inspect the layout of a class; e.g. say you apply a hacky class-hierarchy based cacheline padding to a field, and then want to assert that it got placed at the expected offset.  I suppose Panama may yield a better solution here, but I'm not aware of any VarHandle APIs to replicate this usecase.

On Wed, Jun 19, 2019 at 10:59 AM Gil Tene <g...@azul.com> wrote:
Use VarHandle. It is there and it works. It allows you to replace Unsafe.objectFildOffset with a better, safer way to do the same thing you would be doing with the information that Unsafe.objectFildOffset provides, but without ever giving you the (potentially incorrect and unsafe to use) information about the "offset" of a field within an object. It allows the manipulation field contents in pretty much any useful way that having the offset would allow you to do with Unsafe.getX(object, offset) or Unsafe.putX(object, offset), but without providing with unsafe means that will break things (like e.g. writing a long to a reference field, or overflowing an int with a long write).

On Wednesday, June 19, 2019 at 7:04:26 AM UTC-7, xiaobai andrew wrote:
thank for you reply,i look for some api just like  VarHandle, which i can replace Unsafe.objectFildOffset without requires jdk.unsupported .
any good idea?


Gil Tene <g...@azul.com> 于2019年6月18日周二 下午11:43写道:
Out of curiosity: What do you use Unsafe.objectFieldOffset for that VarHandle is lacking functionality for?

It is useful to gather use cases not covered by VarHandle so that we can propose additional capabilities there (preparing for a world with no Unsafe in it). Can you share an example of code?

As to your question: I assume it is Unsafe as a whole you are having issues with, and not just Unsafe.objectFieldOffset, is that correct? You can find examples of how to use Unsafe in a post-java-8 world e.g. here: http://gregluck.com/blog/archives/2017/03/using-sun-misc-unsafe-in-java-9/

On Tuesday, June 18, 2019 at 5:48:14 AM UTC-7, xiaobai andrew wrote:
i usr Unsafe.objectFieldOffset in jdk 8 ,but when i want to update jdk to 11, the idea compile error , it cannot  found the symbol Unsafe.
i have try to find VarHandle api,but it did not support Unsafe.objectFieldOffset

--
You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/mechanical-sympathy/d628940f-7e88-4456-abbc-3377633f4e74%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages