hotspot/src/share/vm/classfile/classLoaderData.cpp
changeset 46338 e84b501fa52e
parent 46329 53ccc37bda19
parent 44084 501b6d07e6dd
child 46380 4a51438196cf
equal deleted inserted replaced
46337:307e52ec20cd 46338:e84b501fa52e
    91   // it from being unloaded during parsing of the anonymous class.
    91   // it from being unloaded during parsing of the anonymous class.
    92   // The null-class-loader should always be kept alive.
    92   // The null-class-loader should always be kept alive.
    93   _keep_alive((is_anonymous || h_class_loader.is_null()) ? 1 : 0),
    93   _keep_alive((is_anonymous || h_class_loader.is_null()) ? 1 : 0),
    94   _metaspace(NULL), _unloading(false), _klasses(NULL),
    94   _metaspace(NULL), _unloading(false), _klasses(NULL),
    95   _modules(NULL), _packages(NULL),
    95   _modules(NULL), _packages(NULL),
    96   _claimed(0), _jmethod_ids(NULL), _handles(NULL), _deallocate_list(NULL),
    96   _claimed(0), _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
    97   _next(NULL), _dependencies(dependencies),
    97   _next(NULL), _dependencies(dependencies),
    98   _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
    98   _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
    99                             Monitor::_safepoint_check_never)) {
    99                             Monitor::_safepoint_check_never)) {
   100   TRACE_INIT_ID(this);
   100   TRACE_INIT_ID(this);
   101 }
   101 }
   109 void ClassLoaderData::Dependencies::init(TRAPS) {
   109 void ClassLoaderData::Dependencies::init(TRAPS) {
   110   // Create empty dependencies array to add to. CMS requires this to be
   110   // Create empty dependencies array to add to. CMS requires this to be
   111   // an oop so that it can track additions via card marks.  We think.
   111   // an oop so that it can track additions via card marks.  We think.
   112   _list_head = oopFactory::new_objectArray(2, CHECK);
   112   _list_head = oopFactory::new_objectArray(2, CHECK);
   113 }
   113 }
       
   114 
       
   115 ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() {
       
   116   Chunk* c = _head;
       
   117   while (c != NULL) {
       
   118     Chunk* next = c->_next;
       
   119     delete c;
       
   120     c = next;
       
   121   }
       
   122 }
       
   123 
       
   124 oop* ClassLoaderData::ChunkedHandleList::add(oop o) {
       
   125   if (_head == NULL || _head->_size == Chunk::CAPACITY) {
       
   126     Chunk* next = new Chunk(_head);
       
   127     OrderAccess::release_store_ptr(&_head, next);
       
   128   }
       
   129   oop* handle = &_head->_data[_head->_size];
       
   130   *handle = o;
       
   131   OrderAccess::release_store(&_head->_size, _head->_size + 1);
       
   132   return handle;
       
   133 }
       
   134 
       
   135 inline void ClassLoaderData::ChunkedHandleList::oops_do_chunk(OopClosure* f, Chunk* c, const juint size) {
       
   136   for (juint i = 0; i < size; i++) {
       
   137     if (c->_data[i] != NULL) {
       
   138       f->do_oop(&c->_data[i]);
       
   139     }
       
   140   }
       
   141 }
       
   142 
       
   143 void ClassLoaderData::ChunkedHandleList::oops_do(OopClosure* f) {
       
   144   Chunk* head = (Chunk*) OrderAccess::load_ptr_acquire(&_head);
       
   145   if (head != NULL) {
       
   146     // Must be careful when reading size of head
       
   147     oops_do_chunk(f, head, OrderAccess::load_acquire(&head->_size));
       
   148     for (Chunk* c = head->_next; c != NULL; c = c->_next) {
       
   149       oops_do_chunk(f, c, c->_size);
       
   150     }
       
   151   }
       
   152 }
       
   153 
       
   154 #ifdef ASSERT
       
   155 class VerifyContainsOopClosure : public OopClosure {
       
   156   oop* _target;
       
   157   bool _found;
       
   158 
       
   159  public:
       
   160   VerifyContainsOopClosure(oop* target) : _target(target), _found(false) {}
       
   161 
       
   162   void do_oop(oop* p) {
       
   163     if (p == _target) {
       
   164       _found = true;
       
   165     }
       
   166   }
       
   167 
       
   168   void do_oop(narrowOop* p) {
       
   169     // The ChunkedHandleList should not contain any narrowOop
       
   170     ShouldNotReachHere();
       
   171   }
       
   172 
       
   173   bool found() const {
       
   174     return _found;
       
   175   }
       
   176 };
       
   177 
       
   178 bool ClassLoaderData::ChunkedHandleList::contains(oop* p) {
       
   179   VerifyContainsOopClosure cl(p);
       
   180   oops_do(&cl);
       
   181   return cl.found();
       
   182 }
       
   183 #endif
   114 
   184 
   115 bool ClassLoaderData::claim() {
   185 bool ClassLoaderData::claim() {
   116   if (_claimed == 1) {
   186   if (_claimed == 1) {
   117     return false;
   187     return false;
   118   }
   188   }
   144     return;
   214     return;
   145   }
   215   }
   146 
   216 
   147   f->do_oop(&_class_loader);
   217   f->do_oop(&_class_loader);
   148   _dependencies.oops_do(f);
   218   _dependencies.oops_do(f);
   149   if (_handles != NULL) {
   219 
   150     _handles->oops_do(f);
   220   _handles.oops_do(f);
   151   }
   221 
   152   if (klass_closure != NULL) {
   222   if (klass_closure != NULL) {
   153     classes_do(klass_closure);
   223     classes_do(klass_closure);
   154   }
   224   }
   155 }
   225 }
   156 
   226 
   482   Metaspace *m = _metaspace;
   552   Metaspace *m = _metaspace;
   483   if (m != NULL) {
   553   if (m != NULL) {
   484     _metaspace = NULL;
   554     _metaspace = NULL;
   485     delete m;
   555     delete m;
   486   }
   556   }
   487   // release the handles
       
   488   if (_handles != NULL) {
       
   489     JNIHandleBlock::release_block(_handles);
       
   490     _handles = NULL;
       
   491   }
       
   492 
       
   493   // Clear all the JNI handles for methods
   557   // Clear all the JNI handles for methods
   494   // These aren't deallocated and are going to look like a leak, but that's
   558   // These aren't deallocated and are going to look like a leak, but that's
   495   // needed because we can't really get rid of jmethodIDs because we don't
   559   // needed because we can't really get rid of jmethodIDs because we don't
   496   // know when native code is going to stop using them.  The spec says that
   560   // know when native code is going to stop using them.  The spec says that
   497   // they're "invalid" but existing programs likely rely on their being
   561   // they're "invalid" but existing programs likely rely on their being
   560     }
   624     }
   561   }
   625   }
   562   return metaspace;
   626   return metaspace;
   563 }
   627 }
   564 
   628 
   565 JNIHandleBlock* ClassLoaderData::handles() const           { return _handles; }
       
   566 void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; }
       
   567 
       
   568 jobject ClassLoaderData::add_handle(Handle h) {
   629 jobject ClassLoaderData::add_handle(Handle h) {
   569   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   630   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   570   if (handles() == NULL) {
   631   return (jobject) _handles.add(h());
   571     set_handles(JNIHandleBlock::allocate_block());
   632 }
   572   }
   633 
   573   return handles()->allocate_handle(h());
   634 void ClassLoaderData::remove_handle_unsafe(jobject h) {
   574 }
   635   assert(_handles.contains((oop*) h), "Got unexpected handle " PTR_FORMAT, p2i((oop*) h));
   575 
   636   *((oop*) h) = NULL;
   576 void ClassLoaderData::remove_handle(jobject h) {
       
   577   _handles->release_handle(h);
       
   578 }
   637 }
   579 
   638 
   580 // Add this metadata pointer to be freed when it's safe.  This is only during
   639 // Add this metadata pointer to be freed when it's safe.  This is only during
   581 // class unloading because Handles might point to this metadata field.
   640 // class unloading because Handles might point to this metadata field.
   582 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
   641 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
   643   out->print("ClassLoaderData CLD: " PTR_FORMAT ", loader: " PTR_FORMAT ", loader_klass: " PTR_FORMAT " %s {",
   702   out->print("ClassLoaderData CLD: " PTR_FORMAT ", loader: " PTR_FORMAT ", loader_klass: " PTR_FORMAT " %s {",
   644       p2i(this), p2i((void *)class_loader()),
   703       p2i(this), p2i((void *)class_loader()),
   645       p2i(class_loader() != NULL ? class_loader()->klass() : NULL), loader_name());
   704       p2i(class_loader() != NULL ? class_loader()->klass() : NULL), loader_name());
   646   if (claimed()) out->print(" claimed ");
   705   if (claimed()) out->print(" claimed ");
   647   if (is_unloading()) out->print(" unloading ");
   706   if (is_unloading()) out->print(" unloading ");
   648   out->print(" handles " INTPTR_FORMAT, p2i(handles()));
       
   649   out->cr();
   707   out->cr();
   650   if (metaspace_or_null() != NULL) {
   708   if (metaspace_or_null() != NULL) {
   651     out->print_cr("metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));
   709     out->print_cr("metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));
   652     metaspace_or_null()->dump(out);
   710     metaspace_or_null()->dump(out);
   653   } else {
   711   } else {