0N/A/*
553N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A *
0N/A */
0N/A
0N/A#ifndef SHARE_VM_RUNTIME_JFIELDIDWORKAROUND_HPP
0N/A#define SHARE_VM_RUNTIME_JFIELDIDWORKAROUND_HPP
0N/A
0N/Aclass jfieldIDWorkaround: AllStatic {
0N/A // This workaround is because JVMTI doesn't have distinct entry points
131N/A // for methods that use static jfieldIDs and instance jfieldIDs.
0N/A // The workaround is to steal a low-order bit:
0N/A // a 1 means the jfieldID is an instance jfieldID,
0N/A // and the rest of the word is the offset of the field.
0N/A // a 0 means the jfieldID is a static jfieldID,
0N/A // and the rest of the word is the JNIid*.
0N/A //
0N/A // Another low-order bit is used to mark if an instance field
0N/A // is accompanied by an indication of which class it applies to.
0N/A //
0N/A // Bit-format of a jfieldID (most significant first):
0N/A // address:30 instance=0:1 checked=0:1
0N/A // offset:30 instance=1:1 checked=0:1
0N/A // klass:23 offset:7 instance=1:1 checked=1:1
0N/A //
0N/A // If the offset does not fit in 7 bits, or if the fieldID is
0N/A // not checked, then the checked bit is zero and the rest of
0N/A // the word (30 bits) contains only the offset.
0N/A //
0N/A private:
0N/A enum {
0N/A checked_bits = 1,
0N/A instance_bits = 1,
0N/A address_bits = BitsPerWord - checked_bits - instance_bits,
0N/A
0N/A large_offset_bits = address_bits, // unioned with address
0N/A small_offset_bits = 7,
0N/A klass_bits = address_bits - small_offset_bits,
0N/A
0N/A checked_shift = 0,
0N/A instance_shift = checked_shift + checked_bits,
0N/A address_shift = instance_shift + instance_bits,
0N/A
0N/A offset_shift = address_shift, // unioned with address
0N/A klass_shift = offset_shift + small_offset_bits,
0N/A
0N/A checked_mask_in_place = right_n_bits(checked_bits) << checked_shift,
0N/A instance_mask_in_place = right_n_bits(instance_bits) << instance_shift,
0N/A#ifndef _WIN64
0N/A large_offset_mask = right_n_bits(large_offset_bits),
0N/A small_offset_mask = right_n_bits(small_offset_bits),
0N/A klass_mask = right_n_bits(klass_bits)
0N/A#endif
0N/A };
0N/A
0N/A#ifdef _WIN64
0N/A // These values are too big for Win64
0N/A const static uintptr_t large_offset_mask = right_n_bits(large_offset_bits);
0N/A const static uintptr_t small_offset_mask = right_n_bits(small_offset_bits);
0N/A const static uintptr_t klass_mask = right_n_bits(klass_bits);
0N/A#endif
0N/A
0N/A // helper routines:
0N/A static bool is_checked_jfieldID(jfieldID id) {
0N/A uintptr_t as_uint = (uintptr_t) id;
0N/A return ((as_uint & checked_mask_in_place) != 0);
0N/A }
0N/A static intptr_t raw_instance_offset(jfieldID id) {
0N/A uintptr_t result = (uintptr_t) id >> address_shift;
0N/A if (VerifyJNIFields && is_checked_jfieldID(id)) {
0N/A result &= small_offset_mask; // cut off the hash bits
0N/A }
0N/A return (intptr_t)result;
0N/A }
0N/A static intptr_t encode_klass_hash(klassOop k, intptr_t offset);
0N/A static bool klass_hash_ok(klassOop k, jfieldID id);
0N/A static void verify_instance_jfieldID(klassOop k, jfieldID id);
0N/A
0N/A public:
0N/A static bool is_valid_jfieldID(klassOop k, jfieldID id);
0N/A
0N/A static bool is_instance_jfieldID(klassOop k, jfieldID id) {
0N/A uintptr_t as_uint = (uintptr_t) id;
0N/A return ((as_uint & instance_mask_in_place) != 0);
0N/A }
0N/A static bool is_static_jfieldID(jfieldID id) {
0N/A uintptr_t as_uint = (uintptr_t) id;
0N/A return ((as_uint & instance_mask_in_place) == 0);
0N/A }
0N/A
0N/A static jfieldID to_instance_jfieldID(klassOop k, int offset) {
0N/A intptr_t as_uint = ((offset & large_offset_mask) << offset_shift) | instance_mask_in_place;
0N/A if (VerifyJNIFields) {
0N/A as_uint |= encode_klass_hash(k, offset);
0N/A }
0N/A jfieldID result = (jfieldID) as_uint;
0N/A#ifndef ASSERT
0N/A // always verify in debug mode; switchable in anything else
0N/A if (VerifyJNIFields)
0N/A#endif // ASSERT
0N/A {
0N/A verify_instance_jfieldID(k, result);
0N/A }
0N/A assert(raw_instance_offset(result) == (offset & large_offset_mask), "extract right offset");
0N/A return result;
0N/A }
0N/A
0N/A static intptr_t from_instance_jfieldID(klassOop k, jfieldID id) {
0N/A#ifndef ASSERT
0N/A // always verify in debug mode; switchable in anything else
0N/A if (VerifyJNIFields)
0N/A#endif // ASSERT
0N/A {
0N/A verify_instance_jfieldID(k, id);
0N/A }
0N/A return raw_instance_offset(id);
0N/A }
0N/A
0N/A static jfieldID to_static_jfieldID(JNIid* id) {
0N/A assert(id->is_static_field_id(), "from_JNIid, but not static field id");
0N/A jfieldID result = (jfieldID) id;
0N/A assert(from_static_jfieldID(result) == id, "must produce the same static id");
0N/A return result;
0N/A }
0N/A
0N/A static JNIid* from_static_jfieldID(jfieldID id) {
0N/A assert(jfieldIDWorkaround::is_static_jfieldID(id),
0N/A "to_JNIid, but not static jfieldID");
0N/A JNIid* result = (JNIid*) id;
0N/A assert(result->is_static_field_id(), "to_JNIid, but not static field id");
0N/A return result;
0N/A }
0N/A
0N/A static jfieldID to_jfieldID(instanceKlassHandle k, int offset, bool is_static) {
0N/A if (is_static) {
0N/A JNIid *id = k->jni_id_for(offset);
0N/A debug_only(id->set_is_static_field_id());
0N/A return jfieldIDWorkaround::to_static_jfieldID(id);
0N/A } else {
0N/A return jfieldIDWorkaround::to_instance_jfieldID(k(), offset);
0N/A }
0N/A }
0N/A};
0N/A
0N/A#endif // SHARE_VM_RUNTIME_JFIELDIDWORKAROUND_HPP
0N/A