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