1 /*
   2  * Copyright (c) 2012, 2013, 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_CLASSFILE_CLASSLOADERDATA_HPP
  26 #define SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "memory/memRegion.hpp"
  30 #include "memory/metaspace.hpp"
  31 #include "memory/metaspaceCounters.hpp"
  32 #include "runtime/mutex.hpp"
  33 #include "utilities/growableArray.hpp"
  34 
  35 //
  36 // A class loader represents a linkset. Conceptually, a linkset identifies
  37 // the complete transitive closure of resolved links that a dynamic linker can
  38 // produce.
  39 //
  40 // A ClassLoaderData also encapsulates the allocation space, called a metaspace,
  41 // used by the dynamic linker to allocate the runtime representation of all
  42 // the types it defines.
  43 //
  44 // ClassLoaderData are stored in the runtime representation of classes and the
  45 // system dictionary, are roots of garbage collection, and provides iterators
  46 // for root tracing and other GC operations.
  47 
  48 class ClassLoaderData;
  49 class JNIMethodBlock;
  50 class JNIHandleBlock;
  51 class Metadebug;
  52 // GC root for walking class loader data created
  53 
  54 class ClassLoaderDataGraph : public AllStatic {
  55   friend class ClassLoaderData;
  56   friend class ClassLoaderDataGraphMetaspaceIterator;
  57   friend class VMStructs;
  58  private:
  59   // All CLDs (except the null CLD) can be reached by walking _head->_next->...
  60   static ClassLoaderData* _head;
  61   static ClassLoaderData* _unloading;
  62   // CMS support.
  63   static ClassLoaderData* _saved_head;
  64 
  65   static ClassLoaderData* add(ClassLoaderData** loader_data_addr, Handle class_loader, TRAPS);
  66  public:
  67   static ClassLoaderData* find_or_create(Handle class_loader, TRAPS);
  68   static void purge();
  69   static void clear_claimed_marks();
  70   static void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
  71   static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
  72   static void keep_alive_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
  73   static void classes_do(KlassClosure* klass_closure);
  74   static bool do_unloading(BoolObjectClosure* is_alive);
  75 
  76   // CMS support.
  77   static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
  78   static GrowableArray<ClassLoaderData*>* new_clds();
  79 
  80   static void dump_on(outputStream * const out) PRODUCT_RETURN;
  81   static void dump() { dump_on(tty); }
  82   static void verify();
  83 
  84 #ifndef PRODUCT
  85   // expensive test for pointer in metaspace for debugging
  86   static bool contains(address x);
  87   static bool contains_loader_data(ClassLoaderData* loader_data);
  88 #endif
  89 };
  90 
  91 // ClassLoaderData class
  92 
  93 class ClassLoaderData : public CHeapObj<mtClass> {
  94   friend class VMStructs;
  95  private:
  96   friend class ClassLoaderDataGraph;
  97   friend class ClassLoaderDataGraphMetaspaceIterator;
  98   friend class MetaDataFactory;
  99   friend class Method;
 100 
 101   static ClassLoaderData * _the_null_class_loader_data;
 102 
 103   oop _class_loader;       // oop used to uniquely identify a class loader
 104                            // class loader or a canonical class path
 105   oop _dependencies;       // oop to hold dependencies from this class loader
 106                            // data to others.
 107   Metaspace * _metaspace;  // Meta-space where meta-data defined by the
 108                            // classes in the class loader are allocated.
 109   Mutex* _metaspace_lock;  // Locks the metaspace for allocations and setup.
 110   bool _unloading;         // true if this class loader goes away
 111   bool _keep_alive;        // if this CLD can be unloaded for anonymous loaders
 112   bool _is_anonymous;      // if this CLD is for an anonymous class
 113   volatile int _claimed;   // true if claimed, for example during GC traces.
 114                            // To avoid applying oop closure more than once.
 115                            // Has to be an int because we cas it.
 116   Klass* _klasses;         // The classes defined by the class loader.
 117 
 118   JNIHandleBlock* _handles; // Handles to constant pool arrays
 119 
 120   // These method IDs are created for the class loader and set to NULL when the
 121   // class loader is unloaded.  They are rarely freed, only for redefine classes
 122   // and if they lose a data race in InstanceKlass.
 123   JNIMethodBlock*                  _jmethod_ids;
 124 
 125   // Metadata to be deallocated when it's safe at class unloading, when
 126   // this class loader isn't unloaded itself.
 127   GrowableArray<Metadata*>*      _deallocate_list;
 128 
 129   // Support for walking class loader data objects
 130   ClassLoaderData* _next; /// Next loader_datas created
 131 
 132   // ReadOnly and ReadWrite metaspaces (static because only on the null
 133   // class loader for now).
 134   static Metaspace* _ro_metaspace;
 135   static Metaspace* _rw_metaspace;
 136 
 137   void add_dependency(Handle dependency, TRAPS);
 138   void locked_add_dependency(objArrayHandle last, objArrayHandle new_dependency);
 139 
 140   void set_next(ClassLoaderData* next) { _next = next; }
 141   ClassLoaderData* next() const        { return _next; }
 142 
 143   ClassLoaderData(Handle h_class_loader, bool is_anonymous);
 144   ~ClassLoaderData();
 145 
 146   void set_metaspace(Metaspace* m) { _metaspace = m; }
 147 
 148   JNIHandleBlock* handles() const;
 149   void set_handles(JNIHandleBlock* handles);
 150 
 151   Mutex* metaspace_lock() const { return _metaspace_lock; }
 152 
 153   // GC interface.
 154   void clear_claimed()          { _claimed = 0; }
 155   bool claimed() const          { return _claimed == 1; }
 156   bool claim();
 157 
 158   void unload();
 159   bool keep_alive() const       { return _keep_alive; }
 160   bool is_alive(BoolObjectClosure* is_alive_closure) const;
 161 
 162   void classes_do(void f(InstanceKlass*));
 163 
 164   // Deallocate free list during class unloading.
 165   void free_deallocate_list();
 166 
 167   // Allocate out of this class loader data
 168   MetaWord* allocate(size_t size);
 169 
 170  public:
 171   // Accessors
 172   Metaspace* metaspace_or_null() const     { return _metaspace; }
 173 
 174   static ClassLoaderData* the_null_class_loader_data() {
 175     return _the_null_class_loader_data;
 176   }
 177 
 178   bool is_anonymous() const { return _is_anonymous; }
 179 
 180   static void init_null_class_loader_data() {
 181     assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
 182     assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
 183     _the_null_class_loader_data = new ClassLoaderData((oop)NULL, false);
 184     ClassLoaderDataGraph::_head = _the_null_class_loader_data;
 185     assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
 186     if (DumpSharedSpaces) {
 187       _the_null_class_loader_data->initialize_shared_metaspaces();
 188     }
 189   }
 190 
 191   bool is_the_null_class_loader_data() const {
 192     return this == _the_null_class_loader_data;
 193   }
 194 
 195   // The Metaspace is created lazily so may be NULL.  This
 196   // method will allocate a Metaspace if needed.
 197   Metaspace* metaspace_non_null();
 198 
 199   oop class_loader() const      { return _class_loader; }
 200 
 201   // Returns true if this class loader data is for a loader going away.
 202   bool is_unloading() const     {
 203     assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");
 204     return _unloading;
 205   }
 206   // Anonymous class loader data doesn't have anything to keep them from
 207   // being unloaded during parsing the anonymous class.
 208   void set_keep_alive(bool value) { _keep_alive = value; }
 209 
 210   unsigned int identity_hash() {
 211     return _class_loader == NULL ? 0 : _class_loader->identity_hash();
 212   }
 213 
 214   // Used when tracing from klasses.
 215   void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
 216 
 217   void classes_do(KlassClosure* klass_closure);
 218 
 219   JNIMethodBlock* jmethod_ids() const              { return _jmethod_ids; }
 220   void set_jmethod_ids(JNIMethodBlock* new_block)  { _jmethod_ids = new_block; }
 221 
 222   void print_value() { print_value_on(tty); }
 223   void print_value_on(outputStream* out) const;
 224   void dump(outputStream * const out) PRODUCT_RETURN;
 225   void verify();
 226   const char* loader_name();
 227 
 228   jobject add_handle(Handle h);
 229   void add_class(Klass* k);
 230   void remove_class(Klass* k);
 231   void record_dependency(Klass* to, TRAPS);
 232   void init_dependencies(TRAPS);
 233 
 234   void add_to_deallocate_list(Metadata* m);
 235 
 236   static ClassLoaderData* class_loader_data(oop loader);
 237   static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
 238   static void print_loader(ClassLoaderData *loader_data, outputStream *out);
 239 
 240   // CDS support
 241   Metaspace* ro_metaspace();
 242   Metaspace* rw_metaspace();
 243   void initialize_shared_metaspaces();
 244 };
 245 
 246 class ClassLoaderDataGraphMetaspaceIterator : public StackObj {
 247   ClassLoaderData* _data;
 248  public:
 249   ClassLoaderDataGraphMetaspaceIterator();
 250   ~ClassLoaderDataGraphMetaspaceIterator();
 251   bool repeat() { return _data != NULL; }
 252   Metaspace* get_next() {
 253     assert(_data != NULL, "Should not be NULL in call to the iterator");
 254     Metaspace* result = _data->metaspace_or_null();
 255     _data = _data->next();
 256     // This result might be NULL for class loaders without metaspace
 257     // yet.  It would be nice to return only non-null results but
 258     // there is no guarantee that there will be a non-null result
 259     // down the list so the caller is going to have to check.
 260     return result;
 261   }
 262 };
 263 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP