src/share/vm/runtime/jniHandles.hpp

Mon, 01 Feb 2010 17:35:05 -0700

author
dcubed
date
Mon, 01 Feb 2010 17:35:05 -0700
changeset 1648
6deeaebad47a
parent 1445
354d3184f6b2
child 1891
892898e961c5
permissions
-rw-r--r--

6902182: 4/4 Starting with jdwp agent should not incur performance penalty
Summary: Rename can_post_exceptions support to can_post_on_exceptions. Add support for should_post_on_exceptions flag to permit per JavaThread optimizations.
Reviewed-by: never, kvn, dcubed
Contributed-by: tom.deneau@amd.com

     1 /*
     2  * Copyright 1998-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 class JNIHandleBlock;
    28 // Interface for creating and resolving local/global JNI handles
    30 class JNIHandles : AllStatic {
    31   friend class VMStructs;
    32  private:
    33   static JNIHandleBlock* _global_handles;             // First global handle block
    34   static JNIHandleBlock* _weak_global_handles;        // First weak global handle block
    35   static oop _deleted_handle;                         // Sentinel marking deleted handles
    37  public:
    38   // Resolve handle into oop
    39   inline static oop resolve(jobject handle);
    40   // Resolve externally provided handle into oop with some guards
    41   inline static oop resolve_external_guard(jobject handle);
    42   // Resolve handle into oop, result guaranteed not to be null
    43   inline static oop resolve_non_null(jobject handle);
    45   // Local handles
    46   static jobject make_local(oop obj);
    47   static jobject make_local(JNIEnv* env, oop obj);    // Fast version when env is known
    48   static jobject make_local(Thread* thread, oop obj); // Even faster version when current thread is known
    49   inline static void destroy_local(jobject handle);
    51   // Global handles
    52   static jobject make_global(Handle  obj);
    53   static void destroy_global(jobject handle);
    55   // Weak global handles
    56   static jobject make_weak_global(Handle obj);
    57   static void destroy_weak_global(jobject handle);
    59   // jmethodID handling (as Weak global handles).
    60   // Because the useful life-span of a jmethodID cannot be determined, once created they are
    61   // never reclaimed.  The methods to which they refer, however, can be GC'ed away if the class
    62   // is unloaded or if the method is made obsolete or deleted -- in these cases, the jmethodID
    63   // refers to NULL (as is the case for any weak reference).
    64   static jmethodID make_jmethod_id(methodHandle mh);
    65   static void destroy_jmethod_id(jmethodID mid);
    66   inline static methodOop resolve_jmethod_id(jmethodID mid);
    67   inline static methodOop checked_resolve_jmethod_id(jmethodID mid); // NULL on invalid jmethodID
    68   static void change_method_associated_with_jmethod_id(jmethodID jmid, methodHandle mh);
    70   // Sentinel marking deleted handles in block. Note that we cannot store NULL as
    71   // the sentinel, since clearing weak global JNI refs are done by storing NULL in
    72   // the handle. The handle may not be reused before destroy_weak_global is called.
    73   static oop deleted_handle()   { return _deleted_handle; }
    75   // Initialization
    76   static void initialize();
    78   // Debugging
    79   static void print_on(outputStream* st);
    80   static void print()           { print_on(tty); }
    81   static void verify();
    82   static bool is_local_handle(Thread* thread, jobject handle);
    83   static bool is_frame_handle(JavaThread* thr, jobject obj);
    84   static bool is_global_handle(jobject handle);
    85   static bool is_weak_global_handle(jobject handle);
    86   static long global_handle_memory_usage();
    87   static long weak_global_handle_memory_usage();
    89   // Garbage collection support(global handles only, local handles are traversed from thread)
    90   // Traversal of regular global handles
    91   static void oops_do(OopClosure* f);
    92   // Traversal of weak global handles. Unreachable oops are cleared.
    93   static void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f);
    94 };
    98 // JNI handle blocks holding local/global JNI handles
   100 class JNIHandleBlock : public CHeapObj {
   101   friend class VMStructs;
   102   friend class CppInterpreter;
   104  private:
   105   enum SomeConstants {
   106     block_size_in_oops  = 32                    // Number of handles per handle block
   107   };
   109   oop             _handles[block_size_in_oops]; // The handles
   110   int             _top;                         // Index of next unused handle
   111   JNIHandleBlock* _next;                        // Link to next block
   113   // The following instance variables are only used by the first block in a chain.
   114   // Having two types of blocks complicates the code and the space overhead in negligble.
   115   JNIHandleBlock* _last;                        // Last block in use
   116   JNIHandleBlock* _pop_frame_link;              // Block to restore on PopLocalFrame call
   117   oop*            _free_list;                   // Handle free list
   118   int             _allocate_before_rebuild;     // Number of blocks to allocate before rebuilding free list
   120   #ifndef PRODUCT
   121   JNIHandleBlock* _block_list_link;             // Link for list below
   122   static JNIHandleBlock* _block_list;           // List of all allocated blocks (for debugging only)
   123   #endif
   125   static JNIHandleBlock* _block_free_list;      // Free list of currently unused blocks
   126   static int      _blocks_allocated;            // For debugging/printing
   128   // Fill block with bad_handle values
   129   void zap();
   131  protected:
   132   // No more handles in the both the current and following blocks
   133   void clear() { _top = 0; }
   135  private:
   136   // Free list computation
   137   void rebuild_free_list();
   139  public:
   140   // Handle allocation
   141   jobject allocate_handle(oop obj);
   143   // Block allocation and block free list management
   144   static JNIHandleBlock* allocate_block(Thread* thread = NULL);
   145   static void release_block(JNIHandleBlock* block, Thread* thread = NULL);
   147   // JNI PushLocalFrame/PopLocalFrame support
   148   JNIHandleBlock* pop_frame_link() const          { return _pop_frame_link; }
   149   void set_pop_frame_link(JNIHandleBlock* block)  { _pop_frame_link = block; }
   151   // Stub generator support
   152   static int top_offset_in_bytes()                { return offset_of(JNIHandleBlock, _top); }
   154   // Garbage collection support
   155   // Traversal of regular handles
   156   void oops_do(OopClosure* f);
   157   // Traversal of weak handles. Unreachable oops are cleared.
   158   void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f);
   160   // Debugging
   161   bool chain_contains(jobject handle) const;    // Does this block or following blocks contain handle
   162   bool contains(jobject handle) const;          // Does this block contain handle
   163   int length() const;                           // Length of chain starting with this block
   164   long memory_usage() const;
   165   #ifndef PRODUCT
   166   static bool any_contains(jobject handle);     // Does any block currently in use contain handle
   167   static void print_statistics();
   168   #endif
   169 };
   172 inline oop JNIHandles::resolve(jobject handle) {
   173   oop result = (handle == NULL ? (oop)NULL : *(oop*)handle);
   174   assert(result != NULL || (handle == NULL || !CheckJNICalls || is_weak_global_handle(handle)), "Invalid value read from jni handle");
   175   assert(result != badJNIHandle, "Pointing to zapped jni handle area");
   176   return result;
   177 };
   180 inline oop JNIHandles::resolve_external_guard(jobject handle) {
   181   if (handle == NULL) return NULL;
   182   oop result = *(oop*)handle;
   183   if (result == NULL || result == badJNIHandle) return NULL;
   184   return result;
   185 };
   188 inline oop JNIHandles::resolve_non_null(jobject handle) {
   189   assert(handle != NULL, "JNI handle should not be null");
   190   oop result = *(oop*)handle;
   191   assert(result != NULL, "Invalid value read from jni handle");
   192   assert(result != badJNIHandle, "Pointing to zapped jni handle area");
   193   // Don't let that private _deleted_handle object escape into the wild.
   194   assert(result != deleted_handle(), "Used a deleted global handle.");
   195   return result;
   196 };
   198 inline methodOop JNIHandles::resolve_jmethod_id(jmethodID mid) {
   199   return (methodOop) resolve_non_null((jobject)mid);
   200 };
   202 inline methodOop JNIHandles::checked_resolve_jmethod_id(jmethodID mid) {
   203   if (mid == NULL) {
   204     return (methodOop) NULL;
   205   }
   207   oop o = resolve_non_null((jobject) mid);
   208   if (!o->is_method()) {
   209     return (methodOop) NULL;
   210   }
   212   return (methodOop) o;
   213 };
   216 inline void JNIHandles::destroy_local(jobject handle) {
   217   if (handle != NULL) {
   218     *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it
   219   }
   220 }

mercurial