src/share/vm/runtime/handles.hpp

Thu, 24 May 2018 20:03:11 +0800

author
aoqi
date
Thu, 24 May 2018 20:03:11 +0800
changeset 8868
91ddc23482a4
parent 7535
7ae4e26cb1e0
permissions
-rw-r--r--

Increase MaxHeapSize for better performance on MIPS

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

mercurial