src/share/vm/runtime/handles.hpp

Mon, 24 Oct 2011 07:53:17 -0700

author
twisti
date
Mon, 24 Oct 2011 07:53:17 -0700
changeset 3238
b20d64f83668
parent 2708
1d1603768966
child 3900
d2a62e0f25eb
permissions
-rw-r--r--

7090904: JSR 292: JRuby junit test crashes in PSScavengeRootsClosure::do_oop
Reviewed-by: kvn, never, jrose

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

mercurial