0N/A/*
2273N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#ifndef SHARE_VM_RUNTIME_HANDLES_HPP
1879N/A#define SHARE_VM_RUNTIME_HANDLES_HPP
1879N/A
1879N/A#include "oops/klass.hpp"
1879N/A#include "oops/klassOop.hpp"
1879N/A#include "utilities/top.hpp"
1879N/A
0N/A//------------------------------------------------------------------------------------------------------------------------
0N/A// In order to preserve oops during garbage collection, they should be
0N/A// allocated and passed around via Handles within the VM. A handle is
0N/A// simply an extra indirection allocated in a thread local handle area.
0N/A//
0N/A// A handle is a ValueObj, so it can be passed around as a value, can
0N/A// be used as a parameter w/o using &-passing, and can be returned as a
0N/A// return value.
0N/A//
0N/A// oop parameters and return types should be Handles whenever feasible.
0N/A//
0N/A// Handles are declared in a straight-forward manner, e.g.
0N/A//
0N/A// oop obj = ...;
0N/A// Handle h1(obj); // allocate new handle
0N/A// Handle h2(thread, obj); // faster allocation when current thread is known
0N/A// Handle h3; // declare handle only, no allocation occurs
0N/A// ...
0N/A// h3 = h1; // make h3 refer to same indirection as h1
0N/A// oop obj2 = h2(); // get handle value
0N/A// h1->print(); // invoking operation on oop
0N/A//
0N/A// Handles are specialized for different oop types to provide extra type
0N/A// information and avoid unnecessary casting. For each oop type xxxOop
0N/A// there is a corresponding handle called xxxHandle, e.g.
0N/A//
0N/A// oop Handle
0N/A// methodOop methodHandle
0N/A// instanceOop instanceHandle
0N/A//
0N/A// For klassOops, it is often useful to model the Klass hierarchy in order
0N/A// to get access to the klass_part without casting. For each xxxKlass there
0N/A// is a corresponding handle called xxxKlassHandle, e.g.
0N/A//
0N/A// klassOop Klass KlassHandle
0N/A// klassOop methodKlass methodKlassHandle
0N/A// klassOop instanceKlass instanceKlassHandle
0N/A//
0N/A
0N/A//------------------------------------------------------------------------------------------------------------------------
0N/A// Base class for all handles. Provides overloading of frequently
0N/A// used operators for ease of use.
0N/A
0N/Aclass Handle VALUE_OBJ_CLASS_SPEC {
0N/A private:
0N/A oop* _handle;
0N/A
0N/A protected:
0N/A oop obj() const { return _handle == NULL ? (oop)NULL : *_handle; }
0N/A oop non_null_obj() const { assert(_handle != NULL, "resolving NULL handle"); return *_handle; }
0N/A
0N/A public:
0N/A // Constructors
0N/A Handle() { _handle = NULL; }
0N/A Handle(oop obj);
0N/A#ifndef ASSERT
0N/A Handle(Thread* thread, oop obj);
0N/A#else
0N/A // Don't inline body with assert for current thread
0N/A Handle(Thread* thread, oop obj);
0N/A#endif // ASSERT
0N/A
0N/A // General access
0N/A oop operator () () const { return obj(); }
0N/A oop operator -> () const { return non_null_obj(); }
0N/A bool operator == (oop o) const { return obj() == o; }
0N/A bool operator == (const Handle& h) const { return obj() == h.obj(); }
0N/A
0N/A // Null checks
0N/A bool is_null() const { return _handle == NULL; }
0N/A bool not_null() const { return _handle != NULL; }
0N/A
0N/A // Debugging
0N/A void print() { obj()->print(); }
0N/A
0N/A // Direct interface, use very sparingly.
0N/A // Used by JavaCalls to quickly convert handles and to create handles static data structures.
0N/A // Constructor takes a dummy argument to prevent unintentional type conversion in C++.
0N/A Handle(oop *handle, bool dummy) { _handle = handle; }
0N/A
0N/A // Raw handle access. Allows easy duplication of Handles. This can be very unsafe
0N/A // since duplicates is only valid as long as original handle is alive.
0N/A oop* raw_value() { return _handle; }
0N/A static oop raw_resolve(oop *handle) { return handle == NULL ? (oop)NULL : *handle; }
0N/A};
0N/A
0N/A
0N/A//------------------------------------------------------------------------------------------------------------------------
0N/A// Base class for Handles containing klassOops. Provides overloading of frequently
0N/A// used operators for ease of use and typed access to the Klass part.
0N/Aclass KlassHandle: public Handle {
0N/A protected:
0N/A klassOop obj() const { return (klassOop)Handle::obj(); }
0N/A klassOop non_null_obj() const { return (klassOop)Handle::non_null_obj(); }
0N/A Klass* as_klass() const { return non_null_obj()->klass_part(); }
0N/A
0N/A public:
0N/A // Constructors
0N/A KlassHandle () : Handle() {}
0N/A KlassHandle (oop obj) : Handle(obj) {
0N/A assert(SharedSkipVerify || is_null() || obj->is_klass(), "not a klassOop");
0N/A }
0N/A KlassHandle (Klass* kl) : Handle(kl ? kl->as_klassOop() : (klassOop)NULL) {
0N/A assert(SharedSkipVerify || is_null() || obj()->is_klass(), "not a klassOop");
0N/A }
0N/A
0N/A // Faster versions passing Thread
0N/A KlassHandle (Thread* thread, oop obj) : Handle(thread, obj) {
0N/A assert(SharedSkipVerify || is_null() || obj->is_klass(), "not a klassOop");
0N/A }
0N/A KlassHandle (Thread *thread, Klass* kl)
0N/A : Handle(thread, kl ? kl->as_klassOop() : (klassOop)NULL) {
0N/A assert(is_null() || obj()->is_klass(), "not a klassOop");
0N/A }
0N/A
665N/A // Direct interface, use very sparingly.
665N/A // Used by SystemDictionaryHandles to create handles on existing WKKs.
665N/A // The obj of such a klass handle may be null, because the handle is formed
665N/A // during system bootstrapping.
665N/A KlassHandle(klassOop *handle, bool dummy) : Handle((oop*)handle, dummy) {
665N/A assert(SharedSkipVerify || is_null() || obj() == NULL || obj()->is_klass(), "not a klassOop");
665N/A }
665N/A
0N/A // General access
0N/A klassOop operator () () const { return obj(); }
0N/A Klass* operator -> () const { return as_klass(); }
0N/A};
0N/A
0N/A
0N/A//------------------------------------------------------------------------------------------------------------------------
0N/A// Specific Handles for different oop types
0N/A#define DEF_HANDLE(type, is_a) \
0N/A class type##Handle; \
0N/A class type##Handle: public Handle { \
0N/A protected: \
0N/A type##Oop obj() const { return (type##Oop)Handle::obj(); } \
0N/A type##Oop non_null_obj() const { return (type##Oop)Handle::non_null_obj(); } \
0N/A \
0N/A public: \
0N/A /* Constructors */ \
0N/A type##Handle () : Handle() {} \
0N/A type##Handle (type##Oop obj) : Handle((oop)obj) { \
0N/A assert(SharedSkipVerify || is_null() || ((oop)obj)->is_a(), \
0N/A "illegal type"); \
0N/A } \
0N/A type##Handle (Thread* thread, type##Oop obj) : Handle(thread, (oop)obj) { \
0N/A assert(SharedSkipVerify || is_null() || ((oop)obj)->is_a(), "illegal type"); \
0N/A } \
0N/A \
0N/A /* Special constructor, use sparingly */ \
0N/A type##Handle (type##Oop *handle, bool dummy) : Handle((oop*)handle, dummy) {} \
0N/A \
0N/A /* Operators for ease of use */ \
0N/A type##Oop operator () () const { return obj(); } \
0N/A type##Oop operator -> () const { return non_null_obj(); } \
0N/A };
0N/A
0N/A
0N/ADEF_HANDLE(instance , is_instance )
0N/ADEF_HANDLE(method , is_method )
0N/ADEF_HANDLE(constMethod , is_constMethod )
0N/ADEF_HANDLE(methodData , is_methodData )
0N/ADEF_HANDLE(array , is_array )
0N/ADEF_HANDLE(constantPool , is_constantPool )
0N/ADEF_HANDLE(constantPoolCache, is_constantPoolCache)
0N/ADEF_HANDLE(objArray , is_objArray )
0N/ADEF_HANDLE(typeArray , is_typeArray )
0N/A
0N/A//------------------------------------------------------------------------------------------------------------------------
0N/A// Specific KlassHandles for different Klass types
0N/A
0N/A#define DEF_KLASS_HANDLE(type, is_a) \
0N/A class type##Handle : public KlassHandle { \
0N/A public: \
0N/A /* Constructors */ \
0N/A type##Handle () : KlassHandle() {} \
0N/A type##Handle (klassOop obj) : KlassHandle(obj) { \
0N/A assert(SharedSkipVerify || is_null() || obj->klass_part()->is_a(), \
0N/A "illegal type"); \
0N/A } \
0N/A type##Handle (Thread* thread, klassOop obj) : KlassHandle(thread, obj) { \
0N/A assert(SharedSkipVerify || is_null() || obj->klass_part()->is_a(), \
0N/A "illegal type"); \
0N/A } \
0N/A \
0N/A /* Access to klass part */ \
0N/A type* operator -> () const { return (type*)obj()->klass_part(); } \
0N/A \
0N/A static type##Handle cast(KlassHandle h) { return type##Handle(h()); } \
0N/A \
0N/A };
0N/A
0N/A
0N/ADEF_KLASS_HANDLE(instanceKlass , oop_is_instance_slow )
0N/ADEF_KLASS_HANDLE(methodKlass , oop_is_method )
0N/ADEF_KLASS_HANDLE(constMethodKlass , oop_is_constMethod )
0N/ADEF_KLASS_HANDLE(klassKlass , oop_is_klass )
0N/ADEF_KLASS_HANDLE(arrayKlassKlass , oop_is_arrayKlass )
0N/ADEF_KLASS_HANDLE(objArrayKlassKlass , oop_is_objArrayKlass )
0N/ADEF_KLASS_HANDLE(typeArrayKlassKlass , oop_is_typeArrayKlass)
0N/ADEF_KLASS_HANDLE(arrayKlass , oop_is_array )
0N/ADEF_KLASS_HANDLE(typeArrayKlass , oop_is_typeArray_slow)
0N/ADEF_KLASS_HANDLE(objArrayKlass , oop_is_objArray_slow )
0N/ADEF_KLASS_HANDLE(constantPoolKlass , oop_is_constantPool )
0N/ADEF_KLASS_HANDLE(constantPoolCacheKlass, oop_is_constantPool )
0N/A
0N/A
0N/A//------------------------------------------------------------------------------------------------------------------------
0N/A// Thread local handle area
0N/Aclass HandleArea: public Arena {
0N/A friend class HandleMark;
0N/A friend class NoHandleMark;
0N/A friend class ResetNoHandleMark;
0N/A#ifdef ASSERT
0N/A int _handle_mark_nesting;
0N/A int _no_handle_mark_nesting;
0N/A#endif
0N/A HandleArea* _prev; // link to outer (older) area
0N/A public:
0N/A // Constructor
0N/A HandleArea(HandleArea* prev) {
0N/A debug_only(_handle_mark_nesting = 0);
0N/A debug_only(_no_handle_mark_nesting = 0);
0N/A _prev = prev;
0N/A }
0N/A
0N/A // Handle allocation
0N/A private:
0N/A oop* real_allocate_handle(oop obj) {
0N/A#ifdef ASSERT
0N/A oop* handle = (oop*) (UseMallocOnly ? internal_malloc_4(oopSize) : Amalloc_4(oopSize));
0N/A#else
0N/A oop* handle = (oop*) Amalloc_4(oopSize);
0N/A#endif
0N/A *handle = obj;
0N/A return handle;
0N/A }
0N/A public:
0N/A#ifdef ASSERT
0N/A oop* allocate_handle(oop obj);
0N/A#else
0N/A oop* allocate_handle(oop obj) { return real_allocate_handle(obj); }
0N/A#endif
0N/A
0N/A // Garbage collection support
0N/A void oops_do(OopClosure* f);
0N/A
0N/A // Number of handles in use
0N/A size_t used() const { return Arena::used() / oopSize; }
0N/A
0N/A debug_only(bool no_handle_mark_active() { return _no_handle_mark_nesting > 0; })
0N/A};
0N/A
0N/A
0N/A//------------------------------------------------------------------------------------------------------------------------
0N/A// Handles are allocated in a (growable) thread local handle area. Deallocation
0N/A// is managed using a HandleMark. It should normally not be necessary to use
0N/A// HandleMarks manually.
0N/A//
0N/A// A HandleMark constructor will record the current handle area top, and the
0N/A// desctructor will reset the top, destroying all handles allocated in between.
0N/A// The following code will therefore NOT work:
0N/A//
0N/A// Handle h;
0N/A// {
0N/A// HandleMark hm;
0N/A// h = Handle(obj);
0N/A// }
0N/A// h()->print(); // WRONG, h destroyed by HandleMark destructor.
0N/A//
0N/A// If h has to be preserved, it can be converted to an oop or a local JNI handle
0N/A// across the HandleMark boundary.
0N/A
0N/A// The base class of HandleMark should have been StackObj but we also heap allocate
0N/A// a HandleMark when a thread is created.
0N/A
0N/Aclass HandleMark {
0N/A private:
0N/A Thread *_thread; // thread that owns this mark
0N/A HandleArea *_area; // saved handle area
0N/A Chunk *_chunk; // saved arena chunk
0N/A char *_hwm, *_max; // saved arena info
3863N/A size_t _size_in_bytes; // size of handle area
0N/A // Link to previous active HandleMark in thread
0N/A HandleMark* _previous_handle_mark;
0N/A
0N/A void initialize(Thread* thread); // common code for constructors
0N/A void set_previous_handle_mark(HandleMark* mark) { _previous_handle_mark = mark; }
0N/A HandleMark* previous_handle_mark() const { return _previous_handle_mark; }
0N/A
4064N/A size_t size_in_bytes() const { return _size_in_bytes; }
0N/A public:
0N/A HandleMark(); // see handles_inline.hpp
0N/A HandleMark(Thread* thread) { initialize(thread); }
0N/A ~HandleMark();
0N/A
0N/A // Functions used by HandleMarkCleaner
0N/A // called in the constructor of HandleMarkCleaner
0N/A void push();
0N/A // called in the destructor of HandleMarkCleaner
0N/A void pop_and_restore();
0N/A};
0N/A
0N/A//------------------------------------------------------------------------------------------------------------------------
0N/A// A NoHandleMark stack object will verify that no handles are allocated
0N/A// in its scope. Enabled in debug mode only.
0N/A
0N/Aclass NoHandleMark: public StackObj {
0N/A public:
0N/A#ifdef ASSERT
0N/A NoHandleMark();
0N/A ~NoHandleMark();
0N/A#else
0N/A NoHandleMark() {}
0N/A ~NoHandleMark() {}
0N/A#endif
0N/A};
0N/A
0N/A
0N/Aclass ResetNoHandleMark: public StackObj {
0N/A int _no_handle_mark_nesting;
0N/A public:
0N/A#ifdef ASSERT
0N/A ResetNoHandleMark();
0N/A ~ResetNoHandleMark();
0N/A#else
0N/A ResetNoHandleMark() {}
0N/A ~ResetNoHandleMark() {}
0N/A#endif
0N/A};
1879N/A
1879N/A#endif // SHARE_VM_RUNTIME_HANDLES_HPP