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