hotspot/src/share/vm/runtime/jfieldIDWorkaround.hpp
author apetrusenko
Wed, 25 Mar 2009 13:10:54 -0700
changeset 2344 f2e09ba7ceab
parent 1 489c9b5090e2
child 5547 f4b087cbb361
permissions -rw-r--r--
6543938: G1: remove the concept of popularity Reviewed-by: iveresov, tonyp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
class jfieldIDWorkaround: AllStatic {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
  // This workaround is because JVMTI doesn't have distinct entry points
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
  // for methods that use static jfieldIDs and instance jfieldIDs.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
  // The workaround is to steal a low-order bit:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
  //   a 1 means the jfieldID is an instance jfieldID,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
  //             and the rest of the word is the offset of the field.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
  //   a 0 means the jfieldID is a static jfieldID,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  //             and the rest of the word is the JNIid*.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  // Another low-order bit is used to mark if an instance field
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  // is accompanied by an indication of which class it applies to.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  // Bit-format of a jfieldID (most significant first):
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  //  address:30        instance=0:1 checked=0:1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  //  offset:30         instance=1:1 checked=0:1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  //  klass:23 offset:7 instance=1:1 checked=1:1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  // If the offset does not fit in 7 bits, or if the fieldID is
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  // not checked, then the checked bit is zero and the rest of
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  // the word (30 bits) contains only the offset.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    checked_bits           = 1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
    instance_bits          = 1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
    address_bits           = BitsPerWord - checked_bits - instance_bits,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
    large_offset_bits      = address_bits,  // unioned with address
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    small_offset_bits      = 7,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    klass_bits             = address_bits - small_offset_bits,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    checked_shift          = 0,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    instance_shift         = checked_shift  + checked_bits,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    address_shift          = instance_shift + instance_bits,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
    offset_shift           = address_shift,  // unioned with address
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
    klass_shift            = offset_shift + small_offset_bits,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
    checked_mask_in_place  = right_n_bits(checked_bits)  << checked_shift,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
    instance_mask_in_place = right_n_bits(instance_bits) << instance_shift,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
#ifndef _WIN64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
    large_offset_mask      = right_n_bits(large_offset_bits),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    small_offset_mask      = right_n_bits(small_offset_bits),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
    klass_mask             = right_n_bits(klass_bits)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
    };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
#ifdef _WIN64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
    // These values are too big for Win64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    const static uintptr_t large_offset_mask = right_n_bits(large_offset_bits);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
    const static uintptr_t small_offset_mask = right_n_bits(small_offset_bits);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
    const static uintptr_t klass_mask        = right_n_bits(klass_bits);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  // helper routines:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  static bool is_checked_jfieldID(jfieldID id) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
    uintptr_t as_uint = (uintptr_t) id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
    return ((as_uint & checked_mask_in_place) != 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  static intptr_t raw_instance_offset(jfieldID id) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    uintptr_t result = (uintptr_t) id >> address_shift;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    if (VerifyJNIFields && is_checked_jfieldID(id)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
      result &= small_offset_mask;  // cut off the hash bits
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    return (intptr_t)result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  static intptr_t encode_klass_hash(klassOop k, intptr_t offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  static bool             klass_hash_ok(klassOop k, jfieldID id);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  static void  verify_instance_jfieldID(klassOop k, jfieldID id);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  static bool is_valid_jfieldID(klassOop k, jfieldID id);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  static bool is_instance_jfieldID(klassOop k, jfieldID id) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    uintptr_t as_uint = (uintptr_t) id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    return ((as_uint & instance_mask_in_place) != 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  static bool is_static_jfieldID(jfieldID id) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    uintptr_t as_uint = (uintptr_t) id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    return ((as_uint & instance_mask_in_place) == 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  static jfieldID to_instance_jfieldID(klassOop k, int offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
    intptr_t as_uint = ((offset & large_offset_mask) << offset_shift) | instance_mask_in_place;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    if (VerifyJNIFields) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
      as_uint |= encode_klass_hash(k, offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    jfieldID result = (jfieldID) as_uint;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
#ifndef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    // always verify in debug mode; switchable in anything else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    if (VerifyJNIFields)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
#endif // ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
      verify_instance_jfieldID(k, result);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
    assert(raw_instance_offset(result) == (offset & large_offset_mask), "extract right offset");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  static intptr_t from_instance_jfieldID(klassOop k, jfieldID id) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
#ifndef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    // always verify in debug mode; switchable in anything else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    if (VerifyJNIFields)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
#endif // ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
      verify_instance_jfieldID(k, id);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    return raw_instance_offset(id);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  static jfieldID to_static_jfieldID(JNIid* id) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    assert(id->is_static_field_id(), "from_JNIid, but not static field id");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    jfieldID result = (jfieldID) id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
    assert(from_static_jfieldID(result) == id, "must produce the same static id");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
    return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  static JNIid* from_static_jfieldID(jfieldID id) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    assert(jfieldIDWorkaround::is_static_jfieldID(id),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
           "to_JNIid, but not static jfieldID");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    JNIid* result = (JNIid*) id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    assert(result->is_static_field_id(), "to_JNIid, but not static field id");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
    return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  static jfieldID to_jfieldID(instanceKlassHandle k, int offset, bool is_static) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    if (is_static) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
      JNIid *id = k->jni_id_for(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
      debug_only(id->set_is_static_field_id());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
      return jfieldIDWorkaround::to_static_jfieldID(id);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
      return jfieldIDWorkaround::to_instance_jfieldID(k(), offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
};