Path: blob/master/src/hotspot/share/runtime/handles.hpp
40951 views
/*1* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_RUNTIME_HANDLES_HPP25#define SHARE_RUNTIME_HANDLES_HPP2627#include "memory/arena.hpp"28#include "oops/oop.hpp"29#include "oops/oopsHierarchy.hpp"3031class InstanceKlass;32class Klass;33class Thread;3435//------------------------------------------------------------------------------------------------------------------------36// In order to preserve oops during garbage collection, they should be37// allocated and passed around via Handles within the VM. A handle is38// simply an extra indirection allocated in a thread local handle area.39//40// A handle is a value object, so it can be passed around as a value, can41// be used as a parameter w/o using &-passing, and can be returned as a42// 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 h2(thread, obj); // allocate a new handle in thread50// Handle h3; // declare handle only, no allocation occurs51// ...52// h3 = h1; // make h3 refer to same indirection as h153// oop obj2 = h2(); // get handle value54// h1->print(); // invoking operation on oop55//56// Handles are specialized for different oop types to provide extra type57// information and avoid unnecessary casting. For each oop type xxxOop58// there is a corresponding handle called xxxHandle.5960//------------------------------------------------------------------------------------------------------------------------61// Base class for all handles. Provides overloading of frequently62// used operators for ease of use.6364class Handle {65private:66oop* _handle;6768protected:69oop obj() const { return _handle == NULL ? (oop)NULL : *_handle; }70oop non_null_obj() const { assert(_handle != NULL, "resolving NULL handle"); return *_handle; }7172public:73// Constructors74Handle() { _handle = NULL; }75inline Handle(Thread* thread, oop obj);7677// General access78oop operator () () const { return obj(); }79oop operator -> () const { return non_null_obj(); }8081bool operator == (oop o) const { return obj() == o; }82bool operator != (oop o) const { return obj() != o; }83bool operator == (const Handle& h) const { return obj() == h.obj(); }84bool operator != (const Handle& h) const { return obj() != h.obj(); }8586// Null checks87bool is_null() const { return _handle == NULL; }88bool not_null() const { return _handle != NULL; }8990// Debugging91void print() { obj()->print(); }9293// Direct interface, use very sparingly.94// Used by JavaCalls to quickly convert handles and to create handles static data structures.95// Constructor takes a dummy argument to prevent unintentional type conversion in C++.96Handle(oop *handle, bool dummy) { _handle = handle; }9798// Raw handle access. Allows easy duplication of Handles. This can be very unsafe99// since duplicates is only valid as long as original handle is alive.100oop* raw_value() const { return _handle; }101static oop raw_resolve(oop *handle) { return handle == NULL ? (oop)NULL : *handle; }102};103104// Specific Handles for different oop types105#define DEF_HANDLE(type, is_a) \106class type##Handle: public Handle { \107protected: \108type##Oop obj() const { return (type##Oop)Handle::obj(); } \109type##Oop non_null_obj() const { return (type##Oop)Handle::non_null_obj(); } \110\111public: \112/* Constructors */ \113type##Handle () : Handle() {} \114inline type##Handle (Thread* thread, type##Oop obj); \115\116/* Operators for ease of use */ \117type##Oop operator () () const { return obj(); } \118type##Oop operator -> () const { return non_null_obj(); } \119};120121122DEF_HANDLE(instance , is_instance_noinline )123DEF_HANDLE(array , is_array_noinline )124DEF_HANDLE(objArray , is_objArray_noinline )125DEF_HANDLE(typeArray , is_typeArray_noinline )126127//------------------------------------------------------------------------------------------------------------------------128129// Metadata Handles. Unlike oop Handles these are needed to prevent metadata130// from being reclaimed by RedefineClasses.131// Metadata Handles should be passed around as const references to avoid copy construction132// and destruction for parameters.133134// Specific Handles for different oop types135#define DEF_METADATA_HANDLE(name, type) \136class name##Handle; \137class name##Handle : public StackObj { \138type* _value; \139Thread* _thread; \140protected: \141type* obj() const { return _value; } \142type* non_null_obj() const { assert(_value != NULL, "resolving NULL _value"); return _value; } \143\144public: \145/* Constructors */ \146name##Handle () : _value(NULL), _thread(NULL) {} \147name##Handle (Thread* thread, type* obj); \148\149name##Handle (const name##Handle &h); \150name##Handle& operator=(const name##Handle &s); \151\152/* Destructor */ \153~name##Handle (); \154void remove(); \155\156/* Operators for ease of use */ \157type* operator () () const { return obj(); } \158type* operator -> () const { return non_null_obj(); } \159\160bool operator == (type* o) const { return obj() == o; } \161bool operator == (const name##Handle& h) const { return obj() == h.obj(); } \162\163/* Null checks */ \164bool is_null() const { return _value == NULL; } \165bool not_null() const { return _value != NULL; } \166};167168169DEF_METADATA_HANDLE(method, Method)170DEF_METADATA_HANDLE(constantPool, ConstantPool)171172//------------------------------------------------------------------------------------------------------------------------173// Thread local handle area174class HandleArea: public Arena {175friend class HandleMark;176friend class NoHandleMark;177friend class ResetNoHandleMark;178#ifdef ASSERT179int _handle_mark_nesting;180int _no_handle_mark_nesting;181#endif182HandleArea* _prev; // link to outer (older) area183public:184// Constructor185HandleArea(HandleArea* prev) : Arena(mtThread, Chunk::tiny_size) {186debug_only(_handle_mark_nesting = 0);187debug_only(_no_handle_mark_nesting = 0);188_prev = prev;189}190191// Handle allocation192private:193oop* real_allocate_handle(oop obj) {194#ifdef ASSERT195oop* handle = (oop*) (UseMallocOnly ? internal_malloc_4(oopSize) : Amalloc_4(oopSize));196#else197oop* handle = (oop*) Amalloc_4(oopSize);198#endif199*handle = obj;200return handle;201}202public:203#ifdef ASSERT204oop* allocate_handle(oop obj);205#else206oop* allocate_handle(oop obj) { return real_allocate_handle(obj); }207#endif208209// Garbage collection support210void oops_do(OopClosure* f);211212// Number of handles in use213size_t used() const { return Arena::used() / oopSize; }214215debug_only(bool no_handle_mark_active() { return _no_handle_mark_nesting > 0; })216};217218219//------------------------------------------------------------------------------------------------------------------------220// Handles are allocated in a (growable) thread local handle area. Deallocation221// is managed using a HandleMark. It should normally not be necessary to use222// HandleMarks manually.223//224// A HandleMark constructor will record the current handle area top, and the225// destructor will reset the top, destroying all handles allocated in between.226// The following code will therefore NOT work:227//228// Handle h;229// {230// HandleMark hm(THREAD);231// h = Handle(THREAD, obj);232// }233// h()->print(); // WRONG, h destroyed by HandleMark destructor.234//235// If h has to be preserved, it can be converted to an oop or a local JNI handle236// across the HandleMark boundary.237238// The base class of HandleMark should have been StackObj but we also heap allocate239// a HandleMark when a thread is created. The operator new is for this special case.240241class HandleMark {242private:243Thread *_thread; // thread that owns this mark244HandleArea *_area; // saved handle area245Chunk *_chunk; // saved arena chunk246char *_hwm, *_max; // saved arena info247size_t _size_in_bytes; // size of handle area248// Link to previous active HandleMark in thread249HandleMark* _previous_handle_mark;250251void initialize(Thread* thread); // common code for constructors252void set_previous_handle_mark(HandleMark* mark) { _previous_handle_mark = mark; }253HandleMark* previous_handle_mark() const { return _previous_handle_mark; }254255size_t size_in_bytes() const { return _size_in_bytes; }256// remove all chunks beginning with the next257void chop_later_chunks();258public:259HandleMark(Thread* thread) { initialize(thread); }260~HandleMark();261262// Functions used by HandleMarkCleaner263// called in the constructor of HandleMarkCleaner264void push();265// called in the destructor of HandleMarkCleaner266void pop_and_restore();267// overloaded operators268void* operator new(size_t size) throw();269void* operator new [](size_t size) throw();270void operator delete(void* p);271void operator delete[](void* p);272};273274//------------------------------------------------------------------------------------------------------------------------275// A NoHandleMark stack object will verify that no handles are allocated276// in its scope. Enabled in debug mode only.277278class NoHandleMark: public StackObj {279public:280#ifdef ASSERT281NoHandleMark();282~NoHandleMark();283#else284NoHandleMark() {}285~NoHandleMark() {}286#endif287};288289290// ResetNoHandleMark is called in a context where there is an enclosing291// NoHandleMark. A thread in _thread_in_native must not create handles so292// this is used when transitioning via ThreadInVMfromNative.293class ResetNoHandleMark: public StackObj {294int _no_handle_mark_nesting;295public:296#ifdef ASSERT297ResetNoHandleMark();298~ResetNoHandleMark();299#else300ResetNoHandleMark() {}301~ResetNoHandleMark() {}302#endif303};304305// The HandleMarkCleaner is a faster version of HandleMark.306// It relies on the fact that there is a HandleMark further307// down the stack (in JavaCalls::call_helper), and just resets308// to the saved values in that HandleMark.309310class HandleMarkCleaner: public StackObj {311private:312Thread* _thread;313public:314inline HandleMarkCleaner(Thread* thread);315inline ~HandleMarkCleaner();316};317318#endif // SHARE_RUNTIME_HANDLES_HPP319320321