duke@435: /* minqi@5103: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_RUNTIME_HANDLES_HPP stefank@2314: #define SHARE_VM_RUNTIME_HANDLES_HPP stefank@2314: stefank@2314: #include "oops/klass.hpp" stefank@2314: duke@435: //------------------------------------------------------------------------------------------------------------------------ duke@435: // In order to preserve oops during garbage collection, they should be duke@435: // allocated and passed around via Handles within the VM. A handle is duke@435: // simply an extra indirection allocated in a thread local handle area. duke@435: // duke@435: // A handle is a ValueObj, so it can be passed around as a value, can duke@435: // be used as a parameter w/o using &-passing, and can be returned as a duke@435: // return value. duke@435: // duke@435: // oop parameters and return types should be Handles whenever feasible. duke@435: // duke@435: // Handles are declared in a straight-forward manner, e.g. duke@435: // duke@435: // oop obj = ...; duke@435: // Handle h1(obj); // allocate new handle duke@435: // Handle h2(thread, obj); // faster allocation when current thread is known duke@435: // Handle h3; // declare handle only, no allocation occurs duke@435: // ... duke@435: // h3 = h1; // make h3 refer to same indirection as h1 duke@435: // oop obj2 = h2(); // get handle value duke@435: // h1->print(); // invoking operation on oop duke@435: // duke@435: // Handles are specialized for different oop types to provide extra type duke@435: // information and avoid unnecessary casting. For each oop type xxxOop duke@435: // there is a corresponding handle called xxxHandle, e.g. duke@435: // duke@435: // oop Handle coleenp@4037: // Method* methodHandle duke@435: // instanceOop instanceHandle duke@435: duke@435: //------------------------------------------------------------------------------------------------------------------------ duke@435: // Base class for all handles. Provides overloading of frequently duke@435: // used operators for ease of use. duke@435: duke@435: class Handle VALUE_OBJ_CLASS_SPEC { duke@435: private: duke@435: oop* _handle; duke@435: duke@435: protected: duke@435: oop obj() const { return _handle == NULL ? (oop)NULL : *_handle; } duke@435: oop non_null_obj() const { assert(_handle != NULL, "resolving NULL handle"); return *_handle; } duke@435: duke@435: public: duke@435: // Constructors duke@435: Handle() { _handle = NULL; } duke@435: Handle(oop obj); duke@435: Handle(Thread* thread, oop obj); duke@435: duke@435: // General access duke@435: oop operator () () const { return obj(); } duke@435: oop operator -> () const { return non_null_obj(); } duke@435: bool operator == (oop o) const { return obj() == o; } duke@435: bool operator == (const Handle& h) const { return obj() == h.obj(); } duke@435: duke@435: // Null checks duke@435: bool is_null() const { return _handle == NULL; } duke@435: bool not_null() const { return _handle != NULL; } duke@435: duke@435: // Debugging duke@435: void print() { obj()->print(); } duke@435: duke@435: // Direct interface, use very sparingly. duke@435: // Used by JavaCalls to quickly convert handles and to create handles static data structures. duke@435: // Constructor takes a dummy argument to prevent unintentional type conversion in C++. duke@435: Handle(oop *handle, bool dummy) { _handle = handle; } duke@435: duke@435: // Raw handle access. Allows easy duplication of Handles. This can be very unsafe duke@435: // since duplicates is only valid as long as original handle is alive. duke@435: oop* raw_value() { return _handle; } duke@435: static oop raw_resolve(oop *handle) { return handle == NULL ? (oop)NULL : *handle; } duke@435: }; duke@435: duke@435: // Specific Handles for different oop types duke@435: #define DEF_HANDLE(type, is_a) \ duke@435: class type##Handle: public Handle { \ duke@435: protected: \ duke@435: type##Oop obj() const { return (type##Oop)Handle::obj(); } \ duke@435: type##Oop non_null_obj() const { return (type##Oop)Handle::non_null_obj(); } \ duke@435: \ duke@435: public: \ duke@435: /* Constructors */ \ duke@435: type##Handle () : Handle() {} \ duke@435: type##Handle (type##Oop obj) : Handle((oop)obj) { \ coleenp@4178: assert(is_null() || ((oop)obj)->is_a(), \ duke@435: "illegal type"); \ duke@435: } \ duke@435: type##Handle (Thread* thread, type##Oop obj) : Handle(thread, (oop)obj) { \ coleenp@4178: assert(is_null() || ((oop)obj)->is_a(), "illegal type"); \ duke@435: } \ duke@435: \ duke@435: /* Operators for ease of use */ \ duke@435: type##Oop operator () () const { return obj(); } \ duke@435: type##Oop operator -> () const { return non_null_obj(); } \ duke@435: }; duke@435: duke@435: duke@435: DEF_HANDLE(instance , is_instance ) duke@435: DEF_HANDLE(array , is_array ) duke@435: DEF_HANDLE(objArray , is_objArray ) duke@435: DEF_HANDLE(typeArray , is_typeArray ) duke@435: duke@435: //------------------------------------------------------------------------------------------------------------------------ duke@435: coleenp@4037: // Metadata Handles. Unlike oop Handles these are needed to prevent metadata coleenp@4037: // from being reclaimed by RedefineClasses. coleenp@4037: coleenp@4037: // Specific Handles for different oop types coleenp@4037: #define DEF_METADATA_HANDLE(name, type) \ coleenp@4037: class name##Handle; \ coleenp@4037: class name##Handle { \ coleenp@4037: type* _value; \ coleenp@4037: Thread* _thread; \ coleenp@4037: protected: \ coleenp@4037: type* obj() const { return _value; } \ coleenp@4037: type* non_null_obj() const { assert(_value != NULL, "resolving NULL _value"); return _value; } \ coleenp@4037: \ duke@435: public: \ duke@435: /* Constructors */ \ coleenp@4037: name##Handle () : _value(NULL), _thread(NULL) {} \ coleenp@4037: name##Handle (type* obj); \ coleenp@4037: name##Handle (Thread* thread, type* obj); \ duke@435: \ coleenp@4037: name##Handle (const name##Handle &h); \ coleenp@4037: name##Handle& operator=(const name##Handle &s); \ duke@435: \ coleenp@4037: /* Destructor */ \ coleenp@4037: ~name##Handle (); \ coleenp@4037: void remove(); \ duke@435: \ coleenp@4037: /* Operators for ease of use */ \ coleenp@4037: type* operator () () const { return obj(); } \ coleenp@4037: type* operator -> () const { return non_null_obj(); } \ coleenp@4037: \ coleenp@4037: bool operator == (type* o) const { return obj() == o; } \ coleenp@4037: bool operator == (const name##Handle& h) const { return obj() == h.obj(); } \ coleenp@4037: \ coleenp@4037: /* Null checks */ \ coleenp@4037: bool is_null() const { return _value == NULL; } \ coleenp@4037: bool not_null() const { return _value != NULL; } \ duke@435: }; duke@435: duke@435: coleenp@4037: DEF_METADATA_HANDLE(method, Method) coleenp@4037: DEF_METADATA_HANDLE(constantPool, ConstantPool) coleenp@4037: coleenp@4037: // Writing this class explicitly, since DEF_METADATA_HANDLE(klass) doesn't coleenp@4037: // provide the necessary Klass* <-> Klass* conversions. This Klass coleenp@4037: // could be removed when we don't have the Klass* typedef anymore. coleenp@4037: class KlassHandle { coleenp@4037: Klass* _value; coleenp@4037: protected: coleenp@4037: Klass* obj() const { return _value; } coleenp@4037: Klass* non_null_obj() const { assert(_value != NULL, "resolving NULL _value"); return _value; } coleenp@4037: coleenp@4037: public: coleenp@4037: KlassHandle() : _value(NULL) {} coleenp@4037: KlassHandle(const Klass* obj) : _value(const_cast(obj)) {}; coleenp@4037: KlassHandle(Thread* thread, const Klass* obj) : _value(const_cast(obj)) {}; coleenp@4037: coleenp@4037: Klass* operator () () const { return obj(); } coleenp@4037: Klass* operator -> () const { return non_null_obj(); } coleenp@4037: coleenp@4037: bool operator == (Klass* o) const { return obj() == o; } coleenp@4037: bool operator == (const KlassHandle& h) const { return obj() == h.obj(); } coleenp@4037: coleenp@4037: bool is_null() const { return _value == NULL; } coleenp@4037: bool not_null() const { return _value != NULL; } coleenp@4037: }; coleenp@4037: coleenp@4037: class instanceKlassHandle : public KlassHandle { coleenp@4037: public: coleenp@4037: /* Constructors */ coleenp@4037: instanceKlassHandle () : KlassHandle() {} coleenp@4037: instanceKlassHandle (const Klass* k) : KlassHandle(k) { coleenp@4178: assert(k == NULL || k->oop_is_instance(), coleenp@4037: "illegal type"); coleenp@4037: } coleenp@4037: instanceKlassHandle (Thread* thread, const Klass* k) : KlassHandle(thread, k) { coleenp@4178: assert(k == NULL || k->oop_is_instance(), coleenp@4037: "illegal type"); coleenp@4037: } coleenp@4037: /* Access to klass part */ coleenp@4037: InstanceKlass* operator () () const { return (InstanceKlass*)obj(); } coleenp@4037: InstanceKlass* operator -> () const { return (InstanceKlass*)obj(); } coleenp@4037: }; duke@435: duke@435: duke@435: //------------------------------------------------------------------------------------------------------------------------ duke@435: // Thread local handle area duke@435: class HandleArea: public Arena { duke@435: friend class HandleMark; duke@435: friend class NoHandleMark; duke@435: friend class ResetNoHandleMark; duke@435: #ifdef ASSERT duke@435: int _handle_mark_nesting; duke@435: int _no_handle_mark_nesting; duke@435: #endif duke@435: HandleArea* _prev; // link to outer (older) area duke@435: public: duke@435: // Constructor iklam@5368: HandleArea(HandleArea* prev) : Arena(Chunk::tiny_size) { duke@435: debug_only(_handle_mark_nesting = 0); duke@435: debug_only(_no_handle_mark_nesting = 0); duke@435: _prev = prev; duke@435: } duke@435: duke@435: // Handle allocation duke@435: private: duke@435: oop* real_allocate_handle(oop obj) { duke@435: #ifdef ASSERT duke@435: oop* handle = (oop*) (UseMallocOnly ? internal_malloc_4(oopSize) : Amalloc_4(oopSize)); duke@435: #else duke@435: oop* handle = (oop*) Amalloc_4(oopSize); duke@435: #endif duke@435: *handle = obj; duke@435: return handle; duke@435: } duke@435: public: duke@435: #ifdef ASSERT duke@435: oop* allocate_handle(oop obj); duke@435: #else duke@435: oop* allocate_handle(oop obj) { return real_allocate_handle(obj); } duke@435: #endif duke@435: duke@435: // Garbage collection support duke@435: void oops_do(OopClosure* f); duke@435: duke@435: // Number of handles in use duke@435: size_t used() const { return Arena::used() / oopSize; } duke@435: duke@435: debug_only(bool no_handle_mark_active() { return _no_handle_mark_nesting > 0; }) duke@435: }; duke@435: duke@435: duke@435: //------------------------------------------------------------------------------------------------------------------------ duke@435: // Handles are allocated in a (growable) thread local handle area. Deallocation duke@435: // is managed using a HandleMark. It should normally not be necessary to use duke@435: // HandleMarks manually. duke@435: // duke@435: // A HandleMark constructor will record the current handle area top, and the duke@435: // desctructor will reset the top, destroying all handles allocated in between. duke@435: // The following code will therefore NOT work: duke@435: // duke@435: // Handle h; duke@435: // { duke@435: // HandleMark hm; duke@435: // h = Handle(obj); duke@435: // } duke@435: // h()->print(); // WRONG, h destroyed by HandleMark destructor. duke@435: // duke@435: // If h has to be preserved, it can be converted to an oop or a local JNI handle duke@435: // across the HandleMark boundary. duke@435: dcubed@4967: // The base class of HandleMark should have been StackObj but we also heap allocate minqi@5103: // a HandleMark when a thread is created. The operator new is for this special case. duke@435: dcubed@4967: class HandleMark { duke@435: private: duke@435: Thread *_thread; // thread that owns this mark duke@435: HandleArea *_area; // saved handle area duke@435: Chunk *_chunk; // saved arena chunk duke@435: char *_hwm, *_max; // saved arena info zgu@3900: size_t _size_in_bytes; // size of handle area duke@435: // Link to previous active HandleMark in thread duke@435: HandleMark* _previous_handle_mark; duke@435: dcubed@4967: void initialize(Thread* thread); // common code for constructors duke@435: void set_previous_handle_mark(HandleMark* mark) { _previous_handle_mark = mark; } duke@435: HandleMark* previous_handle_mark() const { return _previous_handle_mark; } duke@435: zgu@4193: size_t size_in_bytes() const { return _size_in_bytes; } duke@435: public: duke@435: HandleMark(); // see handles_inline.hpp duke@435: HandleMark(Thread* thread) { initialize(thread); } duke@435: ~HandleMark(); duke@435: duke@435: // Functions used by HandleMarkCleaner duke@435: // called in the constructor of HandleMarkCleaner duke@435: void push(); duke@435: // called in the destructor of HandleMarkCleaner duke@435: void pop_and_restore(); minqi@5103: // overloaded operators coleenp@5614: void* operator new(size_t size) throw(); coleenp@5614: void* operator new [](size_t size) throw(); minqi@5103: void operator delete(void* p); minqi@5103: void operator delete[](void* p); duke@435: }; duke@435: duke@435: //------------------------------------------------------------------------------------------------------------------------ duke@435: // A NoHandleMark stack object will verify that no handles are allocated duke@435: // in its scope. Enabled in debug mode only. duke@435: duke@435: class NoHandleMark: public StackObj { duke@435: public: duke@435: #ifdef ASSERT duke@435: NoHandleMark(); duke@435: ~NoHandleMark(); duke@435: #else duke@435: NoHandleMark() {} duke@435: ~NoHandleMark() {} duke@435: #endif duke@435: }; duke@435: duke@435: duke@435: class ResetNoHandleMark: public StackObj { duke@435: int _no_handle_mark_nesting; duke@435: public: duke@435: #ifdef ASSERT duke@435: ResetNoHandleMark(); duke@435: ~ResetNoHandleMark(); duke@435: #else duke@435: ResetNoHandleMark() {} duke@435: ~ResetNoHandleMark() {} duke@435: #endif duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_RUNTIME_HANDLES_HPP