src/share/vm/runtime/handles.hpp

Wed, 27 Aug 2014 08:19:12 -0400

author
zgu
date
Wed, 27 Aug 2014 08:19:12 -0400
changeset 7074
833b0f92429a
parent 5749
4f9a42c33738
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

8046598: Scalable Native memory tracking development
Summary: Enhance scalability of native memory tracking
Reviewed-by: coleenp, ctornqvi, gtriantafill

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

mercurial