Wed, 13 Mar 2013 15:15:56 -0400
8009829: CDS: JDK JPRT test fails crash in Symbol::equals()
Summary: -Xshare:dump was creating a Symbol in C_heap. There's an assert there that jdk jprt wasn't hitting because it was only done in product
Reviewed-by: dholmes, hseigel, iklam
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 */
25 // A ClassLoaderData identifies the full set of class types that a class
26 // loader's name resolution strategy produces for a given configuration of the
27 // class loader.
28 // Class types in the ClassLoaderData may be defined by from class file binaries
29 // provided by the class loader, or from other class loader it interacts with
30 // according to its name resolution strategy.
31 //
32 // Class loaders that implement a deterministic name resolution strategy
33 // (including with respect to their delegation behavior), such as the boot, the
34 // extension, and the system loaders of the JDK's built-in class loader
35 // hierarchy, always produce the same linkset for a given configuration.
36 //
37 // ClassLoaderData carries information related to a linkset (e.g.,
38 // metaspace holding its klass definitions).
39 // The System Dictionary and related data structures (e.g., placeholder table,
40 // loader constraints table) as well as the runtime representation of classes
41 // only reference ClassLoaderData.
42 //
43 // Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that
44 // that represent the loader's "linking domain" in the JVM.
45 //
46 // The bootstrap loader (represented by NULL) also has a ClassLoaderData,
47 // the singleton class the_null_class_loader_data().
49 #include "precompiled.hpp"
50 #include "classfile/classLoaderData.hpp"
51 #include "classfile/classLoaderData.inline.hpp"
52 #include "classfile/javaClasses.hpp"
53 #include "classfile/metadataOnStackMark.hpp"
54 #include "classfile/systemDictionary.hpp"
55 #include "code/codeCache.hpp"
56 #include "memory/metadataFactory.hpp"
57 #include "memory/metaspaceShared.hpp"
58 #include "memory/oopFactory.hpp"
59 #include "runtime/jniHandles.hpp"
60 #include "runtime/mutex.hpp"
61 #include "runtime/safepoint.hpp"
62 #include "runtime/synchronizer.hpp"
63 #include "utilities/growableArray.hpp"
64 #include "utilities/ostream.hpp"
66 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
68 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous) :
69 _class_loader(h_class_loader()),
70 _is_anonymous(is_anonymous), _keep_alive(is_anonymous), // initially
71 _metaspace(NULL), _unloading(false), _klasses(NULL),
72 _claimed(0), _jmethod_ids(NULL), _handles(NULL), _deallocate_list(NULL),
73 _next(NULL), _dependencies(NULL),
74 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
75 // empty
76 }
78 void ClassLoaderData::init_dependencies(TRAPS) {
79 // Create empty dependencies array to add to. CMS requires this to be
80 // an oop so that it can track additions via card marks. We think.
81 _dependencies = (oop)oopFactory::new_objectArray(2, CHECK);
82 }
84 bool ClassLoaderData::claim() {
85 if (_claimed == 1) {
86 return false;
87 }
89 return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0;
90 }
92 void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
93 if (must_claim && !claim()) {
94 return;
95 }
97 f->do_oop(&_class_loader);
98 f->do_oop(&_dependencies);
99 _handles->oops_do(f);
100 if (klass_closure != NULL) {
101 classes_do(klass_closure);
102 }
103 }
105 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
106 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
107 klass_closure->do_klass(k);
108 }
109 }
111 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
112 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
113 if (k->oop_is_instance()) {
114 f(InstanceKlass::cast(k));
115 }
116 }
117 }
119 void ClassLoaderData::record_dependency(Klass* k, TRAPS) {
120 ClassLoaderData * const from_cld = this;
121 ClassLoaderData * const to_cld = k->class_loader_data();
123 // Dependency to the null class loader data doesn't need to be recorded
124 // because the null class loader data never goes away.
125 if (to_cld->is_the_null_class_loader_data()) {
126 return;
127 }
129 oop to;
130 if (to_cld->is_anonymous()) {
131 // Anonymous class dependencies are through the mirror.
132 to = k->java_mirror();
133 } else {
134 to = to_cld->class_loader();
136 // If from_cld is anonymous, even if it's class_loader is a parent of 'to'
137 // we still have to add it. The class_loader won't keep from_cld alive.
138 if (!from_cld->is_anonymous()) {
139 // Check that this dependency isn't from the same or parent class_loader
140 oop from = from_cld->class_loader();
142 oop curr = from;
143 while (curr != NULL) {
144 if (curr == to) {
145 return; // this class loader is in the parent list, no need to add it.
146 }
147 curr = java_lang_ClassLoader::parent(curr);
148 }
149 }
150 }
152 // It's a dependency we won't find through GC, add it. This is relatively rare
153 // Must handle over GC point.
154 Handle dependency(THREAD, to);
155 from_cld->add_dependency(dependency, CHECK);
156 }
159 void ClassLoaderData::add_dependency(Handle dependency, TRAPS) {
160 // Check first if this dependency is already in the list.
161 // Save a pointer to the last to add to under the lock.
162 objArrayOop ok = (objArrayOop)_dependencies;
163 objArrayOop last = NULL;
164 while (ok != NULL) {
165 last = ok;
166 if (ok->obj_at(0) == dependency()) {
167 // Don't need to add it
168 return;
169 }
170 ok = (objArrayOop)ok->obj_at(1);
171 }
173 // Must handle over GC points
174 assert (last != NULL, "dependencies should be initialized");
175 objArrayHandle last_handle(THREAD, last);
177 // Create a new dependency node with fields for (class_loader or mirror, next)
178 objArrayOop deps = oopFactory::new_objectArray(2, CHECK);
179 deps->obj_at_put(0, dependency());
181 // Must handle over GC points
182 objArrayHandle new_dependency(THREAD, deps);
184 // Add the dependency under lock
185 locked_add_dependency(last_handle, new_dependency);
186 }
188 void ClassLoaderData::locked_add_dependency(objArrayHandle last_handle,
189 objArrayHandle new_dependency) {
191 // Have to lock and put the new dependency on the end of the dependency
192 // array so the card mark for CMS sees that this dependency is new.
193 // Can probably do this lock free with some effort.
194 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
196 oop loader_or_mirror = new_dependency->obj_at(0);
198 // Since the dependencies are only added, add to the end.
199 objArrayOop end = last_handle();
200 objArrayOop last = NULL;
201 while (end != NULL) {
202 last = end;
203 // check again if another thread added it to the end.
204 if (end->obj_at(0) == loader_or_mirror) {
205 // Don't need to add it
206 return;
207 }
208 end = (objArrayOop)end->obj_at(1);
209 }
210 assert (last != NULL, "dependencies should be initialized");
211 // fill in the first element with the oop in new_dependency.
212 if (last->obj_at(0) == NULL) {
213 last->obj_at_put(0, new_dependency->obj_at(0));
214 } else {
215 last->obj_at_put(1, new_dependency());
216 }
217 }
219 void ClassLoaderDataGraph::clear_claimed_marks() {
220 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
221 cld->clear_claimed();
222 }
223 }
225 void ClassLoaderData::add_class(Klass* k) {
226 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
227 Klass* old_value = _klasses;
228 k->set_next_link(old_value);
229 // link the new item into the list
230 _klasses = k;
232 if (TraceClassLoaderData && Verbose && k->class_loader_data() != NULL) {
233 ResourceMark rm;
234 tty->print_cr("[TraceClassLoaderData] Adding k: " PTR_FORMAT " %s to CLD: "
235 PTR_FORMAT " loader: " PTR_FORMAT " %s",
236 k,
237 k->external_name(),
238 k->class_loader_data(),
239 k->class_loader(),
240 loader_name());
241 }
242 }
244 // This is called by InstanceKlass::deallocate_contents() to remove the
245 // scratch_class for redefine classes. We need a lock because there it may not
246 // be called at a safepoint if there's an error.
247 void ClassLoaderData::remove_class(Klass* scratch_class) {
248 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
249 Klass* prev = NULL;
250 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
251 if (k == scratch_class) {
252 if (prev == NULL) {
253 _klasses = k->next_link();
254 } else {
255 Klass* next = k->next_link();
256 prev->set_next_link(next);
257 }
258 return;
259 }
260 prev = k;
261 }
262 ShouldNotReachHere(); // should have found this class!!
263 }
265 void ClassLoaderData::unload() {
266 _unloading = true;
268 if (TraceClassLoaderData) {
269 ResourceMark rm;
270 tty->print("[ClassLoaderData: unload loader data "PTR_FORMAT, this);
271 tty->print(" for instance "PTR_FORMAT" of %s", class_loader(),
272 loader_name());
273 if (is_anonymous()) {
274 tty->print(" for anonymous class "PTR_FORMAT " ", _klasses);
275 }
276 tty->print_cr("]");
277 }
278 }
280 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
281 bool alive =
282 is_anonymous() ?
283 is_alive_closure->do_object_b(_klasses->java_mirror()) :
284 class_loader() == NULL || is_alive_closure->do_object_b(class_loader());
285 assert(!alive || claimed(), "must be claimed");
286 return alive;
287 }
290 ClassLoaderData::~ClassLoaderData() {
291 Metaspace *m = _metaspace;
292 if (m != NULL) {
293 _metaspace = NULL;
294 // release the metaspace
295 delete m;
296 // release the handles
297 if (_handles != NULL) {
298 JNIHandleBlock::release_block(_handles);
299 _handles = NULL;
300 }
301 }
303 // Clear all the JNI handles for methods
304 // These aren't deallocated and are going to look like a leak, but that's
305 // needed because we can't really get rid of jmethodIDs because we don't
306 // know when native code is going to stop using them. The spec says that
307 // they're "invalid" but existing programs likely rely on their being
308 // NULL after class unloading.
309 if (_jmethod_ids != NULL) {
310 Method::clear_jmethod_ids(this);
311 }
312 // Delete lock
313 delete _metaspace_lock;
315 // Delete free list
316 if (_deallocate_list != NULL) {
317 delete _deallocate_list;
318 }
319 }
321 Metaspace* ClassLoaderData::metaspace_non_null() {
322 assert(!DumpSharedSpaces, "wrong metaspace!");
323 // If the metaspace has not been allocated, create a new one. Might want
324 // to create smaller arena for Reflection class loaders also.
325 // The reason for the delayed allocation is because some class loaders are
326 // simply for delegating with no metadata of their own.
327 if (_metaspace == NULL) {
328 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
329 // Check again if metaspace has been allocated while we were getting this lock.
330 if (_metaspace != NULL) {
331 return _metaspace;
332 }
333 if (this == the_null_class_loader_data()) {
334 assert (class_loader() == NULL, "Must be");
335 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType));
336 } else if (is_anonymous()) {
337 if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
338 tty->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name());
339 }
340 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType));
341 } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
342 if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
343 tty->print_cr("is_reflection: %s", class_loader()->klass()->internal_name());
344 }
345 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType));
346 } else {
347 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType));
348 }
349 }
350 return _metaspace;
351 }
353 JNIHandleBlock* ClassLoaderData::handles() const { return _handles; }
354 void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; }
356 jobject ClassLoaderData::add_handle(Handle h) {
357 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
358 if (handles() == NULL) {
359 set_handles(JNIHandleBlock::allocate_block());
360 }
361 return handles()->allocate_handle(h());
362 }
364 // Add this metadata pointer to be freed when it's safe. This is only during
365 // class unloading because Handles might point to this metadata field.
366 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
367 // Metadata in shared region isn't deleted.
368 if (!m->is_shared()) {
369 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
370 if (_deallocate_list == NULL) {
371 _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
372 }
373 _deallocate_list->append_if_missing(m);
374 }
375 }
377 // Deallocate free metadata on the free list. How useful the PermGen was!
378 void ClassLoaderData::free_deallocate_list() {
379 // Don't need lock, at safepoint
380 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
381 if (_deallocate_list == NULL) {
382 return;
383 }
384 // Go backwards because this removes entries that are freed.
385 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
386 Metadata* m = _deallocate_list->at(i);
387 if (!m->on_stack()) {
388 _deallocate_list->remove_at(i);
389 // There are only three types of metadata that we deallocate directly.
390 // Cast them so they can be used by the template function.
391 if (m->is_method()) {
392 MetadataFactory::free_metadata(this, (Method*)m);
393 } else if (m->is_constantPool()) {
394 MetadataFactory::free_metadata(this, (ConstantPool*)m);
395 } else if (m->is_klass()) {
396 MetadataFactory::free_metadata(this, (InstanceKlass*)m);
397 } else {
398 ShouldNotReachHere();
399 }
400 }
401 }
402 }
404 // These anonymous class loaders are to contain classes used for JSR292
405 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) {
406 // Add a new class loader data to the graph.
407 return ClassLoaderDataGraph::add(NULL, loader, CHECK_NULL);
408 }
410 const char* ClassLoaderData::loader_name() {
411 // Handles null class loader
412 return SystemDictionary::loader_name(class_loader());
413 }
415 #ifndef PRODUCT
416 // Define to dump klasses
417 #undef CLD_DUMP_KLASSES
419 void ClassLoaderData::dump(outputStream * const out) {
420 ResourceMark rm;
421 out->print("ClassLoaderData CLD: "PTR_FORMAT", loader: "PTR_FORMAT", loader_klass: "PTR_FORMAT" %s {",
422 this, class_loader(),
423 class_loader() != NULL ? class_loader()->klass() : NULL, loader_name());
424 if (claimed()) out->print(" claimed ");
425 if (is_unloading()) out->print(" unloading ");
426 out->print(" handles " INTPTR_FORMAT, handles());
427 out->cr();
428 if (metaspace_or_null() != NULL) {
429 out->print_cr("metaspace: " PTR_FORMAT, metaspace_or_null());
430 metaspace_or_null()->dump(out);
431 } else {
432 out->print_cr("metaspace: NULL");
433 }
435 #ifdef CLD_DUMP_KLASSES
436 if (Verbose) {
437 ResourceMark rm;
438 Klass* k = _klasses;
439 while (k != NULL) {
440 out->print_cr("klass "PTR_FORMAT", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(),
441 k->has_modified_oops(), k->has_accumulated_modified_oops());
442 k = k->next_link();
443 }
444 }
445 #endif // CLD_DUMP_KLASSES
446 #undef CLD_DUMP_KLASSES
447 if (_jmethod_ids != NULL) {
448 Method::print_jmethod_ids(this, out);
449 }
450 out->print_cr("}");
451 }
452 #endif // PRODUCT
454 void ClassLoaderData::verify() {
455 oop cl = class_loader();
457 guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same");
458 guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be");
460 // Verify the integrity of the allocated space.
461 if (metaspace_or_null() != NULL) {
462 metaspace_or_null()->verify();
463 }
465 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
466 guarantee(k->class_loader_data() == this, "Must be the same");
467 k->verify();
468 }
469 }
472 // GC root of class loader data created.
473 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
474 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
475 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
478 // Add a new class loader data node to the list. Assign the newly created
479 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
480 ClassLoaderData* ClassLoaderDataGraph::add(ClassLoaderData** cld_addr, Handle loader, TRAPS) {
481 // Not assigned a class loader data yet.
482 // Create one.
483 ClassLoaderData* *list_head = &_head;
484 ClassLoaderData* next = _head;
486 bool is_anonymous = (cld_addr == NULL);
487 ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous);
489 if (cld_addr != NULL) {
490 // First, Atomically set it
491 ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);
492 if (old != NULL) {
493 delete cld;
494 // Returns the data.
495 return old;
496 }
497 }
499 // We won the race, and therefore the task of adding the data to the list of
500 // class loader data
501 do {
502 cld->set_next(next);
503 ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);
504 if (exchanged == next) {
505 if (TraceClassLoaderData) {
506 ResourceMark rm;
507 tty->print("[ClassLoaderData: ");
508 tty->print("create class loader data "PTR_FORMAT, cld);
509 tty->print(" for instance "PTR_FORMAT" of %s", cld->class_loader(),
510 cld->loader_name());
511 tty->print_cr("]");
512 }
513 // Create dependencies after the CLD is added to the list. Otherwise,
514 // the GC GC will not find the CLD and the _class_loader field will
515 // not be updated.
516 cld->init_dependencies(CHECK_NULL);
517 return cld;
518 }
519 next = exchanged;
520 } while (true);
522 }
524 void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
525 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
526 cld->oops_do(f, klass_closure, must_claim);
527 }
528 }
530 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
531 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
532 if (cld->keep_alive()) {
533 cld->oops_do(f, klass_closure, must_claim);
534 }
535 }
536 }
538 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
539 if (ClassUnloading) {
540 ClassLoaderData::the_null_class_loader_data()->oops_do(f, klass_closure, must_claim);
541 // keep any special CLDs alive.
542 ClassLoaderDataGraph::keep_alive_oops_do(f, klass_closure, must_claim);
543 } else {
544 ClassLoaderDataGraph::oops_do(f, klass_closure, must_claim);
545 }
546 }
548 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {
549 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
550 cld->classes_do(klass_closure);
551 }
552 }
554 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
555 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
557 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
559 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
560 ClassLoaderData* curr = _head;
561 while (curr != _saved_head) {
562 if (!curr->claimed()) {
563 array->push(curr);
565 if (TraceClassLoaderData) {
566 tty->print("[ClassLoaderData] found new CLD: ");
567 curr->print_value_on(tty);
568 tty->cr();
569 }
570 }
572 curr = curr->_next;
573 }
575 return array;
576 }
578 #ifndef PRODUCT
579 // for debugging and hsfind(x)
580 bool ClassLoaderDataGraph::contains(address x) {
581 // I think we need the _metaspace_lock taken here because the class loader
582 // data graph could be changing while we are walking it (new entries added,
583 // new entries being unloaded, etc).
584 if (DumpSharedSpaces) {
585 // There are only two metaspaces to worry about.
586 ClassLoaderData* ncld = ClassLoaderData::the_null_class_loader_data();
587 return (ncld->ro_metaspace()->contains(x) || ncld->rw_metaspace()->contains(x));
588 }
590 if (UseSharedSpaces && MetaspaceShared::is_in_shared_space(x)) {
591 return true;
592 }
594 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
595 if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
596 return true;
597 }
598 }
600 // Could also be on an unloading list which is okay, ie. still allocated
601 // for a little while.
602 for (ClassLoaderData* ucld = _unloading; ucld != NULL; ucld = ucld->next()) {
603 if (ucld->metaspace_or_null() != NULL && ucld->metaspace_or_null()->contains(x)) {
604 return true;
605 }
606 }
607 return false;
608 }
610 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
611 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
612 if (loader_data == data) {
613 return true;
614 }
615 }
617 return false;
618 }
619 #endif // PRODUCT
622 // Move class loader data from main list to the unloaded list for unloading
623 // and deallocation later.
624 bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive_closure) {
625 ClassLoaderData* data = _head;
626 ClassLoaderData* prev = NULL;
627 bool seen_dead_loader = false;
628 // mark metadata seen on the stack and code cache so we can delete
629 // unneeded entries.
630 bool has_redefined_a_class = JvmtiExport::has_redefined_a_class();
631 MetadataOnStackMark md_on_stack;
632 while (data != NULL) {
633 if (data->keep_alive() || data->is_alive(is_alive_closure)) {
634 if (has_redefined_a_class) {
635 data->classes_do(InstanceKlass::purge_previous_versions);
636 }
637 data->free_deallocate_list();
638 prev = data;
639 data = data->next();
640 continue;
641 }
642 seen_dead_loader = true;
643 ClassLoaderData* dead = data;
644 dead->unload();
645 data = data->next();
646 // Remove from loader list.
647 if (prev != NULL) {
648 prev->set_next(data);
649 } else {
650 assert(dead == _head, "sanity check");
651 _head = data;
652 }
653 dead->set_next(_unloading);
654 _unloading = dead;
655 }
656 return seen_dead_loader;
657 }
659 void ClassLoaderDataGraph::purge() {
660 ClassLoaderData* list = _unloading;
661 _unloading = NULL;
662 ClassLoaderData* next = list;
663 while (next != NULL) {
664 ClassLoaderData* purge_me = next;
665 next = purge_me->next();
666 delete purge_me;
667 }
668 }
670 // CDS support
672 // Global metaspaces for writing information to the shared archive. When
673 // application CDS is supported, we may need one per metaspace, so this
674 // sort of looks like it.
675 Metaspace* ClassLoaderData::_ro_metaspace = NULL;
676 Metaspace* ClassLoaderData::_rw_metaspace = NULL;
677 static bool _shared_metaspaces_initialized = false;
679 // Initialize shared metaspaces (change to call from somewhere not lazily)
680 void ClassLoaderData::initialize_shared_metaspaces() {
681 assert(DumpSharedSpaces, "only use this for dumping shared spaces");
682 assert(this == ClassLoaderData::the_null_class_loader_data(),
683 "only supported for null loader data for now");
684 assert (!_shared_metaspaces_initialized, "only initialize once");
685 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
686 _ro_metaspace = new Metaspace(_metaspace_lock, Metaspace::ROMetaspaceType);
687 _rw_metaspace = new Metaspace(_metaspace_lock, Metaspace::ReadWriteMetaspaceType);
688 _shared_metaspaces_initialized = true;
689 }
691 Metaspace* ClassLoaderData::ro_metaspace() {
692 assert(_ro_metaspace != NULL, "should already be initialized");
693 return _ro_metaspace;
694 }
696 Metaspace* ClassLoaderData::rw_metaspace() {
697 assert(_rw_metaspace != NULL, "should already be initialized");
698 return _rw_metaspace;
699 }
702 ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() {
703 _data = ClassLoaderDataGraph::_head;
704 }
706 ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {}
708 #ifndef PRODUCT
709 // callable from debugger
710 extern "C" int print_loader_data_graph() {
711 ClassLoaderDataGraph::dump_on(tty);
712 return 0;
713 }
715 void ClassLoaderDataGraph::verify() {
716 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
717 data->verify();
718 }
719 }
721 void ClassLoaderDataGraph::dump_on(outputStream * const out) {
722 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
723 data->dump(out);
724 }
725 MetaspaceAux::dump(out);
726 }
727 #endif // PRODUCT
729 void ClassLoaderData::print_value_on(outputStream* out) const {
730 if (class_loader() == NULL) {
731 out->print("NULL class_loader");
732 } else {
733 out->print("class loader "PTR_FORMAT, this);
734 class_loader()->print_value_on(out);
735 }
736 }