1 /* 2 * Copyright (c) 1997, 2015, 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 "gc/shared/barrierSet.hpp" 29 #include "oops/oop.hpp" 30 #include "oops/oopsHierarchy.hpp" 31 32 class InstanceKlass; 33 class Klass; 34 35 //------------------------------------------------------------------------------------------------------------------------ 36 // In order to preserve oops during garbage collection, they should be 37 // allocated and passed around via Handles within the VM. A handle is 38 // simply an extra indirection allocated in a thread local handle area. 39 // 40 // A handle is a ValueObj, so it can be passed around as a value, can 41 // be used as a parameter w/o using &-passing, and can be returned as a 42 // return value. 43 // 44 // oop parameters and return types should be Handles whenever feasible. 45 // 46 // Handles are declared in a straight-forward manner, e.g. 47 // 48 // oop obj = ...; 49 // Handle h1(obj); // allocate new handle 50 // Handle h2(thread, obj); // faster allocation when current thread is known 51 // Handle h3; // declare handle only, no allocation occurs 52 // ... 53 // h3 = h1; // make h3 refer to same indirection as h1 54 // oop obj2 = h2(); // get handle value 55 // h1->print(); // invoking operation on oop 56 // 57 // Handles are specialized for different oop types to provide extra type 58 // information and avoid unnecessary casting. For each oop type xxxOop 59 // there is a corresponding handle called xxxHandle, e.g. 60 // 61 // oop Handle 62 // Method* methodHandle 63 // instanceOop instanceHandle 64 65 //------------------------------------------------------------------------------------------------------------------------ 66 // Base class for all handles. Provides overloading of frequently 67 // used operators for ease of use. 68 69 class Handle VALUE_OBJ_CLASS_SPEC { 70 private: 71 oop* _handle; 72 73 protected: 74 oop obj() const { return _handle == NULL ? (oop)NULL : *_handle; } 75 oop non_null_obj() const { assert(_handle != NULL, "resolving NULL handle"); return *_handle; } 76 77 public: 78 // Constructors 79 Handle() { _handle = NULL; } 80 Handle(oop obj); 81 Handle(Thread* thread, oop obj); 82 83 // General access 84 oop operator () () const { return obj(); } 85 oop operator -> () const { return non_null_obj(); } 86 bool operator == (oop o) const { return oopDesc::bs()->write_barrier(obj()) == oopDesc::bs()->write_barrier(o); } 87 bool operator == (const Handle& h) const { return oopDesc::bs()->write_barrier(obj()) == oopDesc::bs()->write_barrier(h.obj()); } 88 89 // Null checks 90 bool is_null() const { return _handle == NULL; } 91 bool not_null() const { return _handle != NULL; } 92 93 // Debugging 94 void print() { obj()->print(); } 95 96 // Direct interface, use very sparingly. 97 // Used by JavaCalls to quickly convert handles and to create handles static data structures. 98 // Constructor takes a dummy argument to prevent unintentional type conversion in C++. 99 Handle(oop *handle, bool dummy) { _handle = handle; } 100 101 // Raw handle access. Allows easy duplication of Handles. This can be very unsafe 102 // since duplicates is only valid as long as original handle is alive. 103 oop* raw_value() { return _handle; } 104 static oop raw_resolve(oop *handle) { return handle == NULL ? (oop)NULL : *handle; } 105 }; 106 107 // Specific Handles for different oop types 108 #define DEF_HANDLE(type, is_a) \ 109 class type##Handle: public Handle { \ 110 protected: \ 111 type##Oop obj() const { return (type##Oop)Handle::obj(); } \ 112 type##Oop non_null_obj() const { return (type##Oop)Handle::non_null_obj(); } \ 113 \ 114 public: \ 115 /* Constructors */ \ 116 type##Handle () : Handle() {} \ 117 type##Handle (type##Oop obj) : Handle((oop)obj) { \ 118 assert(is_null() || ((oop)obj)->is_a(), \ 119 "illegal type"); \ 120 } \ 121 type##Handle (Thread* thread, type##Oop obj) : Handle(thread, (oop)obj) { \ 122 assert(is_null() || ((oop)obj)->is_a(), "illegal type"); \ 123 } \ 124 \ 125 /* Operators for ease of use */ \ 126 type##Oop operator () () const { return obj(); } \ 127 type##Oop operator -> () const { return non_null_obj(); } \ 128 }; 129 130 131 DEF_HANDLE(instance , is_instance_noinline ) 132 DEF_HANDLE(array , is_array_noinline ) 133 DEF_HANDLE(objArray , is_objArray_noinline ) 134 DEF_HANDLE(typeArray , is_typeArray_noinline ) 135 136 //------------------------------------------------------------------------------------------------------------------------ 137 138 // Metadata Handles. Unlike oop Handles these are needed to prevent metadata 139 // from being reclaimed by RedefineClasses. 140 141 // Specific Handles for different oop types 142 #define DEF_METADATA_HANDLE(name, type) \ 143 class name##Handle; \ 144 class name##Handle : public StackObj { \ 145 type* _value; \ 146 Thread* _thread; \ 147 protected: \ 148 type* obj() const { return _value; } \ 149 type* non_null_obj() const { assert(_value != NULL, "resolving NULL _value"); return _value; } \ 150 \ 151 public: \ 152 /* Constructors */ \ 153 name##Handle () : _value(NULL), _thread(NULL) {} \ 154 name##Handle (type* obj); \ 155 name##Handle (Thread* thread, type* obj); \ 156 \ 157 name##Handle (const name##Handle &h); \ 158 name##Handle& operator=(const name##Handle &s); \ 159 \ 160 /* Destructor */ \ 161 ~name##Handle (); \ 162 void remove(); \ 163 \ 164 /* Operators for ease of use */ \ 165 type* operator () () const { return obj(); } \ 166 type* operator -> () const { return non_null_obj(); } \ 167 \ 168 bool operator == (type* o) const { return obj() == o; } \ 169 bool operator == (const name##Handle& h) const { return obj() == h.obj(); } \ 170 \ 171 /* Null checks */ \ 172 bool is_null() const { return _value == NULL; } \ 173 bool not_null() const { return _value != NULL; } \ 174 }; 175 176 177 DEF_METADATA_HANDLE(method, Method) 178 DEF_METADATA_HANDLE(constantPool, ConstantPool) 179 180 // Writing this class explicitly, since DEF_METADATA_HANDLE(klass) doesn't 181 // provide the necessary Klass* <-> Klass* conversions. This Klass 182 // could be removed when we don't have the Klass* typedef anymore. 183 class KlassHandle : public StackObj { 184 Klass* _value; 185 protected: 186 Klass* obj() const { return _value; } 187 Klass* non_null_obj() const { assert(_value != NULL, "resolving NULL _value"); return _value; } 188 189 public: 190 KlassHandle() : _value(NULL) {} 191 KlassHandle(const Klass* obj) : _value(const_cast<Klass *>(obj)) {}; 192 KlassHandle(Thread* thread, const Klass* obj) : _value(const_cast<Klass *>(obj)) {}; 193 194 Klass* operator () () const { return obj(); } 195 Klass* operator -> () const { return non_null_obj(); } 196 197 bool operator == (Klass* o) const { return obj() == o; } 198 bool operator == (const KlassHandle& h) const { return obj() == h.obj(); } 199 200 bool is_null() const { return _value == NULL; } 201 bool not_null() const { return _value != NULL; } 202 }; 203 204 class instanceKlassHandle : public KlassHandle { 205 public: 206 /* Constructors */ 207 instanceKlassHandle () : KlassHandle() {} 208 instanceKlassHandle (const Klass* k) : KlassHandle(k) { 209 assert(k == NULL || is_instanceKlass(k), "illegal type"); 210 } 211 instanceKlassHandle (Thread* thread, const Klass* k) : KlassHandle(thread, k) { 212 assert(k == NULL || is_instanceKlass(k), "illegal type"); 213 } 214 /* Access to klass part */ 215 InstanceKlass* operator () () const { return (InstanceKlass*)obj(); } 216 InstanceKlass* operator -> () const { return (InstanceKlass*)obj(); } 217 218 debug_only(bool is_instanceKlass(const Klass* k)); 219 }; 220 221 222 //------------------------------------------------------------------------------------------------------------------------ 223 // Thread local handle area 224 class HandleArea: public Arena { 225 friend class HandleMark; 226 friend class NoHandleMark; 227 friend class ResetNoHandleMark; 228 #ifdef ASSERT 229 int _handle_mark_nesting; 230 int _no_handle_mark_nesting; 231 #endif 232 HandleArea* _prev; // link to outer (older) area 233 public: 234 // Constructor 235 HandleArea(HandleArea* prev) : Arena(mtThread, Chunk::tiny_size) { 236 debug_only(_handle_mark_nesting = 0); 237 debug_only(_no_handle_mark_nesting = 0); 238 _prev = prev; 239 } 240 241 // Handle allocation 242 private: 243 oop* real_allocate_handle(oop obj) { 244 #ifdef ASSERT 245 oop* handle = (oop*) (UseMallocOnly ? internal_malloc_4(oopSize) : Amalloc_4(oopSize)); 246 #else 247 oop* handle = (oop*) Amalloc_4(oopSize); 248 #endif 249 *handle = obj; 250 return handle; 251 } 252 public: 253 #ifdef ASSERT 254 oop* allocate_handle(oop obj); 255 #else 256 oop* allocate_handle(oop obj) { return real_allocate_handle(obj); } 257 #endif 258 259 // Garbage collection support 260 void oops_do(OopClosure* f); 261 262 // Number of handles in use 263 size_t used() const { return Arena::used() / oopSize; } 264 265 debug_only(bool no_handle_mark_active() { return _no_handle_mark_nesting > 0; }) 266 }; 267 268 269 //------------------------------------------------------------------------------------------------------------------------ 270 // Handles are allocated in a (growable) thread local handle area. Deallocation 271 // is managed using a HandleMark. It should normally not be necessary to use 272 // HandleMarks manually. 273 // 274 // A HandleMark constructor will record the current handle area top, and the 275 // destructor will reset the top, destroying all handles allocated in between. 276 // The following code will therefore NOT work: 277 // 278 // Handle h; 279 // { 280 // HandleMark hm; 281 // h = Handle(obj); 282 // } 283 // h()->print(); // WRONG, h destroyed by HandleMark destructor. 284 // 285 // If h has to be preserved, it can be converted to an oop or a local JNI handle 286 // across the HandleMark boundary. 287 288 // The base class of HandleMark should have been StackObj but we also heap allocate 289 // a HandleMark when a thread is created. The operator new is for this special case. 290 291 class HandleMark { 292 private: 293 Thread *_thread; // thread that owns this mark 294 HandleArea *_area; // saved handle area 295 Chunk *_chunk; // saved arena chunk 296 char *_hwm, *_max; // saved arena info 297 size_t _size_in_bytes; // size of handle area 298 // Link to previous active HandleMark in thread 299 HandleMark* _previous_handle_mark; 300 301 void initialize(Thread* thread); // common code for constructors 302 void set_previous_handle_mark(HandleMark* mark) { _previous_handle_mark = mark; } 303 HandleMark* previous_handle_mark() const { return _previous_handle_mark; } 304 305 size_t size_in_bytes() const { return _size_in_bytes; } 306 public: 307 HandleMark(); // see handles_inline.hpp 308 HandleMark(Thread* thread) { initialize(thread); } 309 ~HandleMark(); 310 311 // Functions used by HandleMarkCleaner 312 // called in the constructor of HandleMarkCleaner 313 void push(); 314 // called in the destructor of HandleMarkCleaner 315 void pop_and_restore(); 316 // overloaded operators 317 void* operator new(size_t size) throw(); 318 void* operator new [](size_t size) throw(); 319 void operator delete(void* p); 320 void operator delete[](void* p); 321 }; 322 323 //------------------------------------------------------------------------------------------------------------------------ 324 // A NoHandleMark stack object will verify that no handles are allocated 325 // in its scope. Enabled in debug mode only. 326 327 class NoHandleMark: public StackObj { 328 public: 329 #ifdef ASSERT 330 NoHandleMark(); 331 ~NoHandleMark(); 332 #else 333 NoHandleMark() {} 334 ~NoHandleMark() {} 335 #endif 336 }; 337 338 339 class ResetNoHandleMark: public StackObj { 340 int _no_handle_mark_nesting; 341 public: 342 #ifdef ASSERT 343 ResetNoHandleMark(); 344 ~ResetNoHandleMark(); 345 #else 346 ResetNoHandleMark() {} 347 ~ResetNoHandleMark() {} 348 #endif 349 }; 350 351 #endif // SHARE_VM_RUNTIME_HANDLES_HPP