hotspot/src/share/vm/runtime/handles.hpp
author stefank
Tue, 23 Nov 2010 13:22:55 -0800
changeset 7397 5b173b4ca846
parent 5547 f4b087cbb361
child 8076 96d498ec7ae1
permissions -rw-r--r--
6989984: Use standard include model for Hospot Summary: Replaced MakeDeps and the includeDB files with more standardized solutions. Reviewed-by: coleenp, kvn, kamg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
     2
 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
1
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
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2332
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2332
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2332
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#ifndef SHARE_VM_RUNTIME_HANDLES_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#define SHARE_VM_RUNTIME_HANDLES_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "oops/klass.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "oops/klassOop.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    30
#include "utilities/top.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    31
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
//------------------------------------------------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
// In order to preserve oops during garbage collection, they should be
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// allocated and passed around via Handles within the VM. A handle is
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
// simply an extra indirection allocated in a thread local handle area.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// A handle is a ValueObj, so it can be passed around as a value, can
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
// be used as a parameter w/o using &-passing, and can be returned as a
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
// return value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
// oop parameters and return types should be Handles whenever feasible.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
// Handles are declared in a straight-forward manner, e.g.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
//   oop obj = ...;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
//   Handle h1(obj);              // allocate new handle
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
//   Handle h2(thread, obj);      // faster allocation when current thread is known
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
//   Handle h3;                   // declare handle only, no allocation occurs
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
//   ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
//   h3 = h1;                     // make h3 refer to same indirection as h1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
//   oop obj2 = h2();             // get handle value
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
//   h1->print();                 // invoking operation on oop
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
// Handles are specialized for different oop types to provide extra type
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
// information and avoid unnecessary casting. For each oop type xxxOop
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
// there is a corresponding handle called xxxHandle, e.g.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
//   oop           Handle
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
//   methodOop     methodHandle
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
//   instanceOop   instanceHandle
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
// For klassOops, it is often useful to model the Klass hierarchy in order
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
// to get access to the klass_part without casting. For each xxxKlass there
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
// is a corresponding handle called xxxKlassHandle, e.g.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
//   klassOop      Klass           KlassHandle
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
//   klassOop      methodKlass     methodKlassHandle
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
//   klassOop      instanceKlass   instanceKlassHandle
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
//------------------------------------------------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
// Base class for all handles. Provides overloading of frequently
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
// used operators for ease of use.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
class Handle VALUE_OBJ_CLASS_SPEC {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  oop* _handle;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  oop     obj() const                            { return _handle == NULL ? (oop)NULL : *_handle; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  oop     non_null_obj() const                   { assert(_handle != NULL, "resolving NULL handle"); return *_handle; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  // Constructors
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  Handle()                                       { _handle = NULL; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  Handle(oop obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
#ifndef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  Handle(Thread* thread, oop obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  // Don't inline body with assert for current thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  Handle(Thread* thread, oop obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
#endif // ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  // General access
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  oop     operator () () const                   { return obj(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  oop     operator -> () const                   { return non_null_obj(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  bool    operator == (oop o) const              { return obj() == o; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  bool    operator == (const Handle& h) const          { return obj() == h.obj(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  // Null checks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  bool    is_null() const                        { return _handle == NULL; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  bool    not_null() const                       { return _handle != NULL; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  // Debugging
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  void    print()                                { obj()->print(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  // Direct interface, use very sparingly.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  // Used by JavaCalls to quickly convert handles and to create handles static data structures.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  // Constructor takes a dummy argument to prevent unintentional type conversion in C++.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  Handle(oop *handle, bool dummy)                { _handle = handle; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  // Raw handle access. Allows easy duplication of Handles. This can be very unsafe
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  // since duplicates is only valid as long as original handle is alive.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  oop* raw_value()                               { return _handle; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  static oop raw_resolve(oop *handle)            { return handle == NULL ? (oop)NULL : *handle; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
//------------------------------------------------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
// Base class for Handles containing klassOops. Provides overloading of frequently
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
// used operators for ease of use and typed access to the Klass part.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
class KlassHandle: public Handle {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  klassOop    obj() const                        { return (klassOop)Handle::obj(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  klassOop    non_null_obj() const               { return (klassOop)Handle::non_null_obj(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  Klass*      as_klass() const                   { return non_null_obj()->klass_part(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  // Constructors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  KlassHandle ()                                 : Handle()            {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  KlassHandle (oop obj) : Handle(obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    assert(SharedSkipVerify || is_null() || obj->is_klass(), "not a klassOop");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  KlassHandle (Klass* kl) : Handle(kl ? kl->as_klassOop() : (klassOop)NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
    assert(SharedSkipVerify || is_null() || obj()->is_klass(), "not a klassOop");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  // Faster versions passing Thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  KlassHandle (Thread* thread, oop obj) : Handle(thread, obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
    assert(SharedSkipVerify || is_null() || obj->is_klass(), "not a klassOop");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  KlassHandle (Thread *thread, Klass* kl)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    : Handle(thread, kl ? kl->as_klassOop() : (klassOop)NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
    assert(is_null() || obj()->is_klass(), "not a klassOop");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
2332
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 1
diff changeset
   147
  // Direct interface, use very sparingly.
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 1
diff changeset
   148
  // Used by SystemDictionaryHandles to create handles on existing WKKs.
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 1
diff changeset
   149
  // The obj of such a klass handle may be null, because the handle is formed
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 1
diff changeset
   150
  // during system bootstrapping.
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 1
diff changeset
   151
  KlassHandle(klassOop *handle, bool dummy) : Handle((oop*)handle, dummy) {
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 1
diff changeset
   152
    assert(SharedSkipVerify || is_null() || obj() == NULL || obj()->is_klass(), "not a klassOop");
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 1
diff changeset
   153
  }
5c7b6f4ce0a1 6814659: separable cleanups and subroutines for 6655638
jrose
parents: 1
diff changeset
   154
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  // General access
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  klassOop    operator () () const               { return obj(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  Klass*      operator -> () const               { return as_klass(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
//------------------------------------------------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
// Specific Handles for different oop types
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
#define DEF_HANDLE(type, is_a)                   \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  class type##Handle;                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  class type##Handle: public Handle {            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
   protected:                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    type##Oop    obj() const                     { return (type##Oop)Handle::obj(); } \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
    type##Oop    non_null_obj() const            { return (type##Oop)Handle::non_null_obj(); } \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
                                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
   public:                                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
    /* Constructors */                           \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    type##Handle ()                              : Handle()                 {} \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
    type##Handle (type##Oop obj) : Handle((oop)obj) {                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
      assert(SharedSkipVerify || is_null() || ((oop)obj)->is_a(),             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
             "illegal type");                                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    }                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
    type##Handle (Thread* thread, type##Oop obj) : Handle(thread, (oop)obj) { \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
      assert(SharedSkipVerify || is_null() || ((oop)obj)->is_a(), "illegal type");  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
    }                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
    /* Special constructor, use sparingly */ \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    type##Handle (type##Oop *handle, bool dummy) : Handle((oop*)handle, dummy) {} \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
                                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
    /* Operators for ease of use */              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    type##Oop    operator () () const            { return obj(); } \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
    type##Oop    operator -> () const            { return non_null_obj(); } \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
DEF_HANDLE(instance         , is_instance         )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
DEF_HANDLE(method           , is_method           )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
DEF_HANDLE(constMethod      , is_constMethod      )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
DEF_HANDLE(methodData       , is_methodData       )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
DEF_HANDLE(array            , is_array            )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
DEF_HANDLE(constantPool     , is_constantPool     )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
DEF_HANDLE(constantPoolCache, is_constantPoolCache)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
DEF_HANDLE(objArray         , is_objArray         )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
DEF_HANDLE(typeArray        , is_typeArray        )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
DEF_HANDLE(symbol           , is_symbol           )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
//------------------------------------------------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
// Specific KlassHandles for different Klass types
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
#define DEF_KLASS_HANDLE(type, is_a)             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  class type##Handle : public KlassHandle {      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
   public:                                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    /* Constructors */                           \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
    type##Handle ()                              : KlassHandle()           {} \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
    type##Handle (klassOop obj) : KlassHandle(obj) {                          \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
      assert(SharedSkipVerify || is_null() || obj->klass_part()->is_a(),      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
             "illegal type");                                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
    }                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
    type##Handle (Thread* thread, klassOop obj) : KlassHandle(thread, obj) {  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
      assert(SharedSkipVerify || is_null() || obj->klass_part()->is_a(),      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
             "illegal type");                                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
    }                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
                                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
    /* Access to klass part */                   \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
    type*        operator -> () const            { return (type*)obj()->klass_part(); } \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
                                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
    static type##Handle cast(KlassHandle h)      { return type##Handle(h()); } \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
                                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
DEF_KLASS_HANDLE(instanceKlass         , oop_is_instance_slow )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
DEF_KLASS_HANDLE(methodKlass           , oop_is_method        )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
DEF_KLASS_HANDLE(constMethodKlass      , oop_is_constMethod   )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
DEF_KLASS_HANDLE(klassKlass            , oop_is_klass         )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
DEF_KLASS_HANDLE(arrayKlassKlass       , oop_is_arrayKlass    )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
DEF_KLASS_HANDLE(objArrayKlassKlass    , oop_is_objArrayKlass )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
DEF_KLASS_HANDLE(typeArrayKlassKlass   , oop_is_typeArrayKlass)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
DEF_KLASS_HANDLE(arrayKlass            , oop_is_array         )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
DEF_KLASS_HANDLE(typeArrayKlass        , oop_is_typeArray_slow)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
DEF_KLASS_HANDLE(objArrayKlass         , oop_is_objArray_slow )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
DEF_KLASS_HANDLE(symbolKlass           , oop_is_symbol        )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
DEF_KLASS_HANDLE(constantPoolKlass     , oop_is_constantPool  )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
DEF_KLASS_HANDLE(constantPoolCacheKlass, oop_is_constantPool  )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
//------------------------------------------------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
// Thread local handle area
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
class HandleArea: public Arena {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  friend class HandleMark;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
  friend class NoHandleMark;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  friend class ResetNoHandleMark;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
  int _handle_mark_nesting;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  int _no_handle_mark_nesting;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  HandleArea* _prev;          // link to outer (older) area
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  // Constructor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
  HandleArea(HandleArea* prev) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
    debug_only(_handle_mark_nesting    = 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
    debug_only(_no_handle_mark_nesting = 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
    _prev = prev;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  // Handle allocation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
  oop* real_allocate_handle(oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
    oop* handle = (oop*) (UseMallocOnly ? internal_malloc_4(oopSize) : Amalloc_4(oopSize));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
    oop* handle = (oop*) Amalloc_4(oopSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    *handle = obj;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
    return handle;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  oop* allocate_handle(oop obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  oop* allocate_handle(oop obj) { return real_allocate_handle(obj); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  // Garbage collection support
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  void oops_do(OopClosure* f);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  // Number of handles in use
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  size_t used() const     { return Arena::used() / oopSize; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  debug_only(bool no_handle_mark_active() { return _no_handle_mark_nesting > 0; })
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
//------------------------------------------------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
// Handles are allocated in a (growable) thread local handle area. Deallocation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
// is managed using a HandleMark. It should normally not be necessary to use
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
// HandleMarks manually.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
// A HandleMark constructor will record the current handle area top, and the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
// desctructor will reset the top, destroying all handles allocated in between.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
// The following code will therefore NOT work:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
//   Handle h;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
//   {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
//     HandleMark hm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
//     h = Handle(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
//   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
//   h()->print();       // WRONG, h destroyed by HandleMark destructor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
// If h has to be preserved, it can be converted to an oop or a local JNI handle
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
// across the HandleMark boundary.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
// The base class of HandleMark should have been StackObj but we also heap allocate
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
// a HandleMark when a thread is created.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
class HandleMark {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
  Thread *_thread;              // thread that owns this mark
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  HandleArea *_area;            // saved handle area
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  Chunk *_chunk;                // saved arena chunk
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  char *_hwm, *_max;            // saved arena info
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
  NOT_PRODUCT(size_t _size_in_bytes;) // size of handle area
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
  // Link to previous active HandleMark in thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
  HandleMark* _previous_handle_mark;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  void initialize(Thread* thread);                // common code for constructors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  void set_previous_handle_mark(HandleMark* mark) { _previous_handle_mark = mark; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
  HandleMark* previous_handle_mark() const        { return _previous_handle_mark; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
  HandleMark();                            // see handles_inline.hpp
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
  HandleMark(Thread* thread)                      { initialize(thread); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
  ~HandleMark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
  // Functions used by HandleMarkCleaner
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
  // called in the constructor of HandleMarkCleaner
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
  void push();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
  // called in the destructor of HandleMarkCleaner
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
  void pop_and_restore();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
//------------------------------------------------------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
// A NoHandleMark stack object will verify that no handles are allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
// in its scope. Enabled in debug mode only.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
class NoHandleMark: public StackObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  NoHandleMark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
  ~NoHandleMark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  NoHandleMark()  {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
  ~NoHandleMark() {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
class ResetNoHandleMark: public StackObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
  int _no_handle_mark_nesting;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
  ResetNoHandleMark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
  ~ResetNoHandleMark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
  ResetNoHandleMark()  {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
  ~ResetNoHandleMark() {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
};
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   364
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   365
#endif // SHARE_VM_RUNTIME_HANDLES_HPP