Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/classfile/classLoaderData.cpp
40949 views
1
/*
2
* Copyright (c) 2012, 2021, 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
// 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
// platform, 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().
48
49
#include "precompiled.hpp"
50
#include "classfile/classLoaderData.inline.hpp"
51
#include "classfile/classLoaderDataGraph.inline.hpp"
52
#include "classfile/dictionary.hpp"
53
#include "classfile/javaClasses.hpp"
54
#include "classfile/moduleEntry.hpp"
55
#include "classfile/packageEntry.hpp"
56
#include "classfile/symbolTable.hpp"
57
#include "classfile/systemDictionary.hpp"
58
#include "classfile/vmClasses.hpp"
59
#include "logging/log.hpp"
60
#include "logging/logStream.hpp"
61
#include "memory/allocation.inline.hpp"
62
#include "memory/classLoaderMetaspace.hpp"
63
#include "memory/metadataFactory.hpp"
64
#include "memory/metaspace.hpp"
65
#include "memory/resourceArea.hpp"
66
#include "memory/universe.hpp"
67
#include "oops/access.inline.hpp"
68
#include "oops/klass.inline.hpp"
69
#include "oops/oop.inline.hpp"
70
#include "oops/oopHandle.inline.hpp"
71
#include "oops/weakHandle.inline.hpp"
72
#include "runtime/arguments.hpp"
73
#include "runtime/atomic.hpp"
74
#include "runtime/handles.inline.hpp"
75
#include "runtime/mutex.hpp"
76
#include "runtime/safepoint.hpp"
77
#include "utilities/growableArray.hpp"
78
#include "utilities/macros.hpp"
79
#include "utilities/ostream.hpp"
80
81
ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
82
83
void ClassLoaderData::init_null_class_loader_data() {
84
assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
85
assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
86
87
_the_null_class_loader_data = new ClassLoaderData(Handle(), false);
88
ClassLoaderDataGraph::_head = _the_null_class_loader_data;
89
assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
90
91
LogTarget(Trace, class, loader, data) lt;
92
if (lt.is_enabled()) {
93
ResourceMark rm;
94
LogStream ls(lt);
95
ls.print("create ");
96
_the_null_class_loader_data->print_value_on(&ls);
97
ls.cr();
98
}
99
}
100
101
// Obtain and set the class loader's name within the ClassLoaderData so
102
// it will be available for error messages, logging, JFR, etc. The name
103
// and klass are available after the class_loader oop is no longer alive,
104
// during unloading.
105
void ClassLoaderData::initialize_name(Handle class_loader) {
106
ResourceMark rm;
107
108
// Obtain the class loader's name. If the class loader's name was not
109
// explicitly set during construction, the CLD's _name field will be null.
110
oop cl_name = java_lang_ClassLoader::name(class_loader());
111
if (cl_name != NULL) {
112
const char* cl_instance_name = java_lang_String::as_utf8_string(cl_name);
113
114
if (cl_instance_name != NULL && cl_instance_name[0] != '\0') {
115
_name = SymbolTable::new_symbol(cl_instance_name);
116
}
117
}
118
119
// Obtain the class loader's name and identity hash. If the class loader's
120
// name was not explicitly set during construction, the class loader's name and id
121
// will be set to the qualified class name of the class loader along with its
122
// identity hash.
123
// If for some reason the ClassLoader's constructor has not been run, instead of
124
// leaving the _name_and_id field null, fall back to the external qualified class
125
// name. Thus CLD's _name_and_id field should never have a null value.
126
oop cl_name_and_id = java_lang_ClassLoader::nameAndId(class_loader());
127
const char* cl_instance_name_and_id =
128
(cl_name_and_id == NULL) ? _class_loader_klass->external_name() :
129
java_lang_String::as_utf8_string(cl_name_and_id);
130
assert(cl_instance_name_and_id != NULL && cl_instance_name_and_id[0] != '\0', "class loader has no name and id");
131
_name_and_id = SymbolTable::new_symbol(cl_instance_name_and_id);
132
}
133
134
ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool has_class_mirror_holder) :
135
_metaspace(NULL),
136
_metaspace_lock(new Mutex(Mutex::leaf+1, "Metaspace allocation lock", true,
137
Mutex::_safepoint_check_never)),
138
_unloading(false), _has_class_mirror_holder(has_class_mirror_holder),
139
_modified_oops(true),
140
// A non-strong hidden class loader data doesn't have anything to keep
141
// it from being unloaded during parsing of the non-strong hidden class.
142
// The null-class-loader should always be kept alive.
143
_keep_alive((has_class_mirror_holder || h_class_loader.is_null()) ? 1 : 0),
144
_claim(0),
145
_handles(),
146
_klasses(NULL), _packages(NULL), _modules(NULL), _unnamed_module(NULL), _dictionary(NULL),
147
_jmethod_ids(NULL),
148
_deallocate_list(NULL),
149
_next(NULL),
150
_class_loader_klass(NULL), _name(NULL), _name_and_id(NULL) {
151
152
if (!h_class_loader.is_null()) {
153
_class_loader = _handles.add(h_class_loader());
154
_class_loader_klass = h_class_loader->klass();
155
initialize_name(h_class_loader);
156
}
157
158
if (!has_class_mirror_holder) {
159
// The holder is initialized later for non-strong hidden classes,
160
// and before calling anything that call class_loader().
161
initialize_holder(h_class_loader);
162
163
// A ClassLoaderData created solely for a non-strong hidden class should never
164
// have a ModuleEntryTable or PackageEntryTable created for it.
165
_packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
166
if (h_class_loader.is_null()) {
167
// Create unnamed module for boot loader
168
_unnamed_module = ModuleEntry::create_boot_unnamed_module(this);
169
} else {
170
// Create unnamed module for all other loaders
171
_unnamed_module = ModuleEntry::create_unnamed_module(this);
172
}
173
_dictionary = create_dictionary();
174
}
175
176
NOT_PRODUCT(_dependency_count = 0); // number of class loader dependencies
177
178
JFR_ONLY(INIT_ID(this);)
179
}
180
181
ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() {
182
Chunk* c = _head;
183
while (c != NULL) {
184
Chunk* next = c->_next;
185
delete c;
186
c = next;
187
}
188
}
189
190
OopHandle ClassLoaderData::ChunkedHandleList::add(oop o) {
191
if (_head == NULL || _head->_size == Chunk::CAPACITY) {
192
Chunk* next = new Chunk(_head);
193
Atomic::release_store(&_head, next);
194
}
195
oop* handle = &_head->_data[_head->_size];
196
NativeAccess<IS_DEST_UNINITIALIZED>::oop_store(handle, o);
197
Atomic::release_store(&_head->_size, _head->_size + 1);
198
return OopHandle(handle);
199
}
200
201
int ClassLoaderData::ChunkedHandleList::count() const {
202
int count = 0;
203
Chunk* chunk = _head;
204
while (chunk != NULL) {
205
count += chunk->_size;
206
chunk = chunk->_next;
207
}
208
return count;
209
}
210
211
inline void ClassLoaderData::ChunkedHandleList::oops_do_chunk(OopClosure* f, Chunk* c, const juint size) {
212
for (juint i = 0; i < size; i++) {
213
if (c->_data[i] != NULL) {
214
f->do_oop(&c->_data[i]);
215
}
216
}
217
}
218
219
void ClassLoaderData::ChunkedHandleList::oops_do(OopClosure* f) {
220
Chunk* head = Atomic::load_acquire(&_head);
221
if (head != NULL) {
222
// Must be careful when reading size of head
223
oops_do_chunk(f, head, Atomic::load_acquire(&head->_size));
224
for (Chunk* c = head->_next; c != NULL; c = c->_next) {
225
oops_do_chunk(f, c, c->_size);
226
}
227
}
228
}
229
230
class VerifyContainsOopClosure : public OopClosure {
231
oop _target;
232
bool _found;
233
234
public:
235
VerifyContainsOopClosure(oop target) : _target(target), _found(false) {}
236
237
void do_oop(oop* p) {
238
if (p != NULL && NativeAccess<AS_NO_KEEPALIVE>::oop_load(p) == _target) {
239
_found = true;
240
}
241
}
242
243
void do_oop(narrowOop* p) {
244
// The ChunkedHandleList should not contain any narrowOop
245
ShouldNotReachHere();
246
}
247
248
bool found() const {
249
return _found;
250
}
251
};
252
253
bool ClassLoaderData::ChunkedHandleList::contains(oop p) {
254
VerifyContainsOopClosure cl(p);
255
oops_do(&cl);
256
return cl.found();
257
}
258
259
#ifndef PRODUCT
260
bool ClassLoaderData::ChunkedHandleList::owner_of(oop* oop_handle) {
261
Chunk* chunk = _head;
262
while (chunk != NULL) {
263
if (&(chunk->_data[0]) <= oop_handle && oop_handle < &(chunk->_data[chunk->_size])) {
264
return true;
265
}
266
chunk = chunk->_next;
267
}
268
return false;
269
}
270
#endif // PRODUCT
271
272
void ClassLoaderData::clear_claim(int claim) {
273
for (;;) {
274
int old_claim = Atomic::load(&_claim);
275
if ((old_claim & claim) == 0) {
276
return;
277
}
278
int new_claim = old_claim & ~claim;
279
if (Atomic::cmpxchg(&_claim, old_claim, new_claim) == old_claim) {
280
return;
281
}
282
}
283
}
284
285
bool ClassLoaderData::try_claim(int claim) {
286
for (;;) {
287
int old_claim = Atomic::load(&_claim);
288
if ((old_claim & claim) == claim) {
289
return false;
290
}
291
int new_claim = old_claim | claim;
292
if (Atomic::cmpxchg(&_claim, old_claim, new_claim) == old_claim) {
293
return true;
294
}
295
}
296
}
297
298
// Non-strong hidden classes have their own ClassLoaderData that is marked to keep alive
299
// while the class is being parsed, and if the class appears on the module fixup list.
300
// Due to the uniqueness that no other class shares the hidden class' name or
301
// ClassLoaderData, no other non-GC thread has knowledge of the hidden class while
302
// it is being defined, therefore _keep_alive is not volatile or atomic.
303
void ClassLoaderData::inc_keep_alive() {
304
if (has_class_mirror_holder()) {
305
if (!Arguments::is_dumping_archive()) {
306
assert(_keep_alive > 0, "Invalid keep alive increment count");
307
}
308
_keep_alive++;
309
}
310
}
311
312
void ClassLoaderData::dec_keep_alive() {
313
if (has_class_mirror_holder()) {
314
assert(_keep_alive > 0, "Invalid keep alive decrement count");
315
_keep_alive--;
316
}
317
}
318
319
void ClassLoaderData::oops_do(OopClosure* f, int claim_value, bool clear_mod_oops) {
320
if (claim_value != ClassLoaderData::_claim_none && !try_claim(claim_value)) {
321
return;
322
}
323
324
// Only clear modified_oops after the ClassLoaderData is claimed.
325
if (clear_mod_oops) {
326
clear_modified_oops();
327
}
328
329
_handles.oops_do(f);
330
}
331
332
void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
333
// Lock-free access requires load_acquire
334
for (Klass* k = Atomic::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
335
klass_closure->do_klass(k);
336
assert(k != k->next_link(), "no loops!");
337
}
338
}
339
340
void ClassLoaderData::classes_do(void f(Klass * const)) {
341
// Lock-free access requires load_acquire
342
for (Klass* k = Atomic::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
343
f(k);
344
assert(k != k->next_link(), "no loops!");
345
}
346
}
347
348
void ClassLoaderData::methods_do(void f(Method*)) {
349
// Lock-free access requires load_acquire
350
for (Klass* k = Atomic::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
351
if (k->is_instance_klass() && InstanceKlass::cast(k)->is_loaded()) {
352
InstanceKlass::cast(k)->methods_do(f);
353
}
354
}
355
}
356
357
void ClassLoaderData::loaded_classes_do(KlassClosure* klass_closure) {
358
// Lock-free access requires load_acquire
359
for (Klass* k = Atomic::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
360
// Do not filter ArrayKlass oops here...
361
if (k->is_array_klass() || (k->is_instance_klass() && InstanceKlass::cast(k)->is_loaded())) {
362
#ifdef ASSERT
363
oop m = k->java_mirror();
364
assert(m != NULL, "NULL mirror");
365
assert(m->is_a(vmClasses::Class_klass()), "invalid mirror");
366
#endif
367
klass_closure->do_klass(k);
368
}
369
}
370
}
371
372
void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
373
// Lock-free access requires load_acquire
374
for (Klass* k = Atomic::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
375
if (k->is_instance_klass()) {
376
f(InstanceKlass::cast(k));
377
}
378
assert(k != k->next_link(), "no loops!");
379
}
380
}
381
382
void ClassLoaderData::modules_do(void f(ModuleEntry*)) {
383
assert_locked_or_safepoint(Module_lock);
384
if (_unnamed_module != NULL) {
385
f(_unnamed_module);
386
}
387
if (_modules != NULL) {
388
for (int i = 0; i < _modules->table_size(); i++) {
389
for (ModuleEntry* entry = _modules->bucket(i);
390
entry != NULL;
391
entry = entry->next()) {
392
f(entry);
393
}
394
}
395
}
396
}
397
398
void ClassLoaderData::packages_do(void f(PackageEntry*)) {
399
assert_locked_or_safepoint(Module_lock);
400
if (_packages != NULL) {
401
for (int i = 0; i < _packages->table_size(); i++) {
402
for (PackageEntry* entry = _packages->bucket(i);
403
entry != NULL;
404
entry = entry->next()) {
405
f(entry);
406
}
407
}
408
}
409
}
410
411
void ClassLoaderData::record_dependency(const Klass* k) {
412
assert(k != NULL, "invariant");
413
414
ClassLoaderData * const from_cld = this;
415
ClassLoaderData * const to_cld = k->class_loader_data();
416
417
// Do not need to record dependency if the dependency is to a class whose
418
// class loader data is never freed. (i.e. the dependency's class loader
419
// is one of the three builtin class loaders and the dependency's class
420
// loader data has a ClassLoader holder, not a Class holder.)
421
if (to_cld->is_permanent_class_loader_data()) {
422
return;
423
}
424
425
oop to;
426
if (to_cld->has_class_mirror_holder()) {
427
// Just return if a non-strong hidden class class is attempting to record a dependency
428
// to itself. (Note that every non-strong hidden class has its own unique class
429
// loader data.)
430
if (to_cld == from_cld) {
431
return;
432
}
433
// Hidden class dependencies are through the mirror.
434
to = k->java_mirror();
435
} else {
436
to = to_cld->class_loader();
437
oop from = from_cld->class_loader();
438
439
// Just return if this dependency is to a class with the same or a parent
440
// class_loader.
441
if (from == to || java_lang_ClassLoader::isAncestor(from, to)) {
442
return; // this class loader is in the parent list, no need to add it.
443
}
444
}
445
446
// It's a dependency we won't find through GC, add it.
447
if (!_handles.contains(to)) {
448
NOT_PRODUCT(Atomic::inc(&_dependency_count));
449
LogTarget(Trace, class, loader, data) lt;
450
if (lt.is_enabled()) {
451
ResourceMark rm;
452
LogStream ls(lt);
453
ls.print("adding dependency from ");
454
print_value_on(&ls);
455
ls.print(" to ");
456
to_cld->print_value_on(&ls);
457
ls.cr();
458
}
459
Handle dependency(Thread::current(), to);
460
add_handle(dependency);
461
// Added a potentially young gen oop to the ClassLoaderData
462
record_modified_oops();
463
}
464
}
465
466
void ClassLoaderData::add_class(Klass* k, bool publicize /* true */) {
467
{
468
MutexLocker ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
469
Klass* old_value = _klasses;
470
k->set_next_link(old_value);
471
// Link the new item into the list, making sure the linked class is stable
472
// since the list can be walked without a lock
473
Atomic::release_store(&_klasses, k);
474
if (k->is_array_klass()) {
475
ClassLoaderDataGraph::inc_array_classes(1);
476
} else {
477
ClassLoaderDataGraph::inc_instance_classes(1);
478
}
479
}
480
481
if (publicize) {
482
LogTarget(Trace, class, loader, data) lt;
483
if (lt.is_enabled()) {
484
ResourceMark rm;
485
LogStream ls(lt);
486
ls.print("Adding k: " PTR_FORMAT " %s to ", p2i(k), k->external_name());
487
print_value_on(&ls);
488
ls.cr();
489
}
490
}
491
}
492
493
void ClassLoaderData::initialize_holder(Handle loader_or_mirror) {
494
if (loader_or_mirror() != NULL) {
495
assert(_holder.is_null(), "never replace holders");
496
_holder = WeakHandle(Universe::vm_weak(), loader_or_mirror);
497
}
498
}
499
500
// Remove a klass from the _klasses list for scratch_class during redefinition
501
// or parsed class in the case of an error.
502
void ClassLoaderData::remove_class(Klass* scratch_class) {
503
assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
504
505
// Adjust global class iterator.
506
ClassLoaderDataGraph::adjust_saved_class(scratch_class);
507
508
Klass* prev = NULL;
509
for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
510
if (k == scratch_class) {
511
if (prev == NULL) {
512
_klasses = k->next_link();
513
} else {
514
Klass* next = k->next_link();
515
prev->set_next_link(next);
516
}
517
518
if (k->is_array_klass()) {
519
ClassLoaderDataGraph::dec_array_classes(1);
520
} else {
521
ClassLoaderDataGraph::dec_instance_classes(1);
522
}
523
524
return;
525
}
526
prev = k;
527
assert(k != k->next_link(), "no loops!");
528
}
529
ShouldNotReachHere(); // should have found this class!!
530
}
531
532
void ClassLoaderData::unload() {
533
_unloading = true;
534
535
LogTarget(Trace, class, loader, data) lt;
536
if (lt.is_enabled()) {
537
ResourceMark rm;
538
LogStream ls(lt);
539
ls.print("unload");
540
print_value_on(&ls);
541
ls.cr();
542
}
543
544
// Some items on the _deallocate_list need to free their C heap structures
545
// if they are not already on the _klasses list.
546
free_deallocate_list_C_heap_structures();
547
548
// Clean up class dependencies and tell serviceability tools
549
// these classes are unloading. Must be called
550
// after erroneous classes are released.
551
classes_do(InstanceKlass::unload_class);
552
553
// Clean up global class iterator for compiler
554
ClassLoaderDataGraph::adjust_saved_class(this);
555
}
556
557
ModuleEntryTable* ClassLoaderData::modules() {
558
// Lazily create the module entry table at first request.
559
// Lock-free access requires load_acquire.
560
ModuleEntryTable* modules = Atomic::load_acquire(&_modules);
561
if (modules == NULL) {
562
MutexLocker m1(Module_lock);
563
// Check if _modules got allocated while we were waiting for this lock.
564
if ((modules = _modules) == NULL) {
565
modules = new ModuleEntryTable(ModuleEntryTable::_moduletable_entry_size);
566
567
{
568
MutexLocker m1(metaspace_lock(), Mutex::_no_safepoint_check_flag);
569
// Ensure _modules is stable, since it is examined without a lock
570
Atomic::release_store(&_modules, modules);
571
}
572
}
573
}
574
return modules;
575
}
576
577
const int _boot_loader_dictionary_size = 1009;
578
const int _default_loader_dictionary_size = 107;
579
580
Dictionary* ClassLoaderData::create_dictionary() {
581
assert(!has_class_mirror_holder(), "class mirror holder cld does not have a dictionary");
582
int size;
583
bool resizable = false;
584
if (_the_null_class_loader_data == NULL) {
585
size = _boot_loader_dictionary_size;
586
resizable = true;
587
} else if (class_loader()->is_a(vmClasses::reflect_DelegatingClassLoader_klass())) {
588
size = 1; // there's only one class in relection class loader and no initiated classes
589
} else if (is_system_class_loader_data()) {
590
size = _boot_loader_dictionary_size;
591
resizable = true;
592
} else {
593
size = _default_loader_dictionary_size;
594
resizable = true;
595
}
596
if (!DynamicallyResizeSystemDictionaries || DumpSharedSpaces) {
597
resizable = false;
598
}
599
return new Dictionary(this, size, resizable);
600
}
601
602
// Tell the GC to keep this klass alive while iterating ClassLoaderDataGraph
603
oop ClassLoaderData::holder_phantom() const {
604
// A klass that was previously considered dead can be looked up in the
605
// CLD/SD, and its _java_mirror or _class_loader can be stored in a root
606
// or a reachable object making it alive again. The SATB part of G1 needs
607
// to get notified about this potential resurrection, otherwise the marking
608
// might not find the object.
609
if (!_holder.is_null()) { // NULL class_loader
610
return _holder.resolve();
611
} else {
612
return NULL;
613
}
614
}
615
616
// Let the GC read the holder without keeping it alive.
617
oop ClassLoaderData::holder_no_keepalive() const {
618
if (!_holder.is_null()) { // NULL class_loader
619
return _holder.peek();
620
} else {
621
return NULL;
622
}
623
}
624
625
// Unloading support
626
bool ClassLoaderData::is_alive() const {
627
bool alive = keep_alive() // null class loader and incomplete non-strong hidden class.
628
|| (_holder.peek() != NULL); // and not cleaned by the GC weak handle processing.
629
630
return alive;
631
}
632
633
class ReleaseKlassClosure: public KlassClosure {
634
private:
635
size_t _instance_class_released;
636
size_t _array_class_released;
637
public:
638
ReleaseKlassClosure() : _instance_class_released(0), _array_class_released(0) { }
639
640
size_t instance_class_released() const { return _instance_class_released; }
641
size_t array_class_released() const { return _array_class_released; }
642
643
void do_klass(Klass* k) {
644
if (k->is_array_klass()) {
645
_array_class_released ++;
646
} else {
647
assert(k->is_instance_klass(), "Must be");
648
_instance_class_released ++;
649
}
650
k->release_C_heap_structures();
651
}
652
};
653
654
ClassLoaderData::~ClassLoaderData() {
655
// Release C heap structures for all the classes.
656
ReleaseKlassClosure cl;
657
classes_do(&cl);
658
659
ClassLoaderDataGraph::dec_array_classes(cl.array_class_released());
660
ClassLoaderDataGraph::dec_instance_classes(cl.instance_class_released());
661
662
// Release the WeakHandle
663
_holder.release(Universe::vm_weak());
664
665
// Release C heap allocated hashtable for all the packages.
666
if (_packages != NULL) {
667
// Destroy the table itself
668
delete _packages;
669
_packages = NULL;
670
}
671
672
// Release C heap allocated hashtable for all the modules.
673
if (_modules != NULL) {
674
// Destroy the table itself
675
delete _modules;
676
_modules = NULL;
677
}
678
679
// Release C heap allocated hashtable for the dictionary
680
if (_dictionary != NULL) {
681
// Destroy the table itself
682
delete _dictionary;
683
_dictionary = NULL;
684
}
685
686
if (_unnamed_module != NULL) {
687
_unnamed_module->delete_unnamed_module();
688
_unnamed_module = NULL;
689
}
690
691
// release the metaspace
692
ClassLoaderMetaspace *m = _metaspace;
693
if (m != NULL) {
694
_metaspace = NULL;
695
delete m;
696
}
697
// Clear all the JNI handles for methods
698
// These aren't deallocated and are going to look like a leak, but that's
699
// needed because we can't really get rid of jmethodIDs because we don't
700
// know when native code is going to stop using them. The spec says that
701
// they're "invalid" but existing programs likely rely on their being
702
// NULL after class unloading.
703
if (_jmethod_ids != NULL) {
704
Method::clear_jmethod_ids(this);
705
}
706
// Delete lock
707
delete _metaspace_lock;
708
709
// Delete free list
710
if (_deallocate_list != NULL) {
711
delete _deallocate_list;
712
}
713
714
// Decrement refcounts of Symbols if created.
715
if (_name != NULL) {
716
_name->decrement_refcount();
717
}
718
if (_name_and_id != NULL) {
719
_name_and_id->decrement_refcount();
720
}
721
}
722
723
// Returns true if this class loader data is for the app class loader
724
// or a user defined system class loader. (Note that the class loader
725
// data may have a Class holder.)
726
bool ClassLoaderData::is_system_class_loader_data() const {
727
return SystemDictionary::is_system_class_loader(class_loader());
728
}
729
730
// Returns true if this class loader data is for the platform class loader.
731
// (Note that the class loader data may have a Class holder.)
732
bool ClassLoaderData::is_platform_class_loader_data() const {
733
return SystemDictionary::is_platform_class_loader(class_loader());
734
}
735
736
// Returns true if the class loader for this class loader data is one of
737
// the 3 builtin (boot application/system or platform) class loaders,
738
// including a user-defined system class loader. Note that if the class
739
// loader data is for a non-strong hidden class then it may
740
// get freed by a GC even if its class loader is one of these loaders.
741
bool ClassLoaderData::is_builtin_class_loader_data() const {
742
return (is_boot_class_loader_data() ||
743
SystemDictionary::is_system_class_loader(class_loader()) ||
744
SystemDictionary::is_platform_class_loader(class_loader()));
745
}
746
747
// Returns true if this class loader data is a class loader data
748
// that is not ever freed by a GC. It must be the CLD for one of the builtin
749
// class loaders and not the CLD for a non-strong hidden class.
750
bool ClassLoaderData::is_permanent_class_loader_data() const {
751
return is_builtin_class_loader_data() && !has_class_mirror_holder();
752
}
753
754
ClassLoaderMetaspace* ClassLoaderData::metaspace_non_null() {
755
// If the metaspace has not been allocated, create a new one. Might want
756
// to create smaller arena for Reflection class loaders also.
757
// The reason for the delayed allocation is because some class loaders are
758
// simply for delegating with no metadata of their own.
759
// Lock-free access requires load_acquire.
760
ClassLoaderMetaspace* metaspace = Atomic::load_acquire(&_metaspace);
761
if (metaspace == NULL) {
762
MutexLocker ml(_metaspace_lock, Mutex::_no_safepoint_check_flag);
763
// Check if _metaspace got allocated while we were waiting for this lock.
764
if ((metaspace = _metaspace) == NULL) {
765
if (this == the_null_class_loader_data()) {
766
assert (class_loader() == NULL, "Must be");
767
metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::BootMetaspaceType);
768
} else if (has_class_mirror_holder()) {
769
metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::ClassMirrorHolderMetaspaceType);
770
} else if (class_loader()->is_a(vmClasses::reflect_DelegatingClassLoader_klass())) {
771
metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType);
772
} else {
773
metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::StandardMetaspaceType);
774
}
775
// Ensure _metaspace is stable, since it is examined without a lock
776
Atomic::release_store(&_metaspace, metaspace);
777
}
778
}
779
return metaspace;
780
}
781
782
OopHandle ClassLoaderData::add_handle(Handle h) {
783
MutexLocker ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
784
record_modified_oops();
785
return _handles.add(h());
786
}
787
788
void ClassLoaderData::remove_handle(OopHandle h) {
789
assert(!is_unloading(), "Do not remove a handle for a CLD that is unloading");
790
oop* ptr = h.ptr_raw();
791
if (ptr != NULL) {
792
assert(_handles.owner_of(ptr), "Got unexpected handle " PTR_FORMAT, p2i(ptr));
793
NativeAccess<>::oop_store(ptr, oop(NULL));
794
}
795
}
796
797
void ClassLoaderData::init_handle_locked(OopHandle& dest, Handle h) {
798
MutexLocker ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
799
if (dest.resolve() != NULL) {
800
return;
801
} else {
802
dest = _handles.add(h());
803
}
804
}
805
806
// Add this metadata pointer to be freed when it's safe. This is only during
807
// a safepoint which checks if handles point to this metadata field.
808
void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
809
// Metadata in shared region isn't deleted.
810
if (!m->is_shared()) {
811
MutexLocker ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
812
if (_deallocate_list == NULL) {
813
_deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, mtClass);
814
}
815
_deallocate_list->append_if_missing(m);
816
log_debug(class, loader, data)("deallocate added for %s", m->print_value_string());
817
ClassLoaderDataGraph::set_should_clean_deallocate_lists();
818
}
819
}
820
821
// Deallocate free metadata on the free list. How useful the PermGen was!
822
void ClassLoaderData::free_deallocate_list() {
823
// This must be called at a safepoint because it depends on metadata walking at
824
// safepoint cleanup time.
825
assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
826
assert(!is_unloading(), "only called for ClassLoaderData that are not unloading");
827
if (_deallocate_list == NULL) {
828
return;
829
}
830
// Go backwards because this removes entries that are freed.
831
for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
832
Metadata* m = _deallocate_list->at(i);
833
if (!m->on_stack()) {
834
_deallocate_list->remove_at(i);
835
// There are only three types of metadata that we deallocate directly.
836
// Cast them so they can be used by the template function.
837
if (m->is_method()) {
838
MetadataFactory::free_metadata(this, (Method*)m);
839
} else if (m->is_constantPool()) {
840
MetadataFactory::free_metadata(this, (ConstantPool*)m);
841
} else if (m->is_klass()) {
842
MetadataFactory::free_metadata(this, (InstanceKlass*)m);
843
} else {
844
ShouldNotReachHere();
845
}
846
} else {
847
// Metadata is alive.
848
// If scratch_class is on stack then it shouldn't be on this list!
849
assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
850
"scratch classes on this list should be dead");
851
// Also should assert that other metadata on the list was found in handles.
852
// Some cleaning remains.
853
ClassLoaderDataGraph::set_should_clean_deallocate_lists();
854
}
855
}
856
}
857
858
// This is distinct from free_deallocate_list. For class loader data that are
859
// unloading, this frees the C heap memory for items on the list, and unlinks
860
// scratch or error classes so that unloading events aren't triggered for these
861
// classes. The metadata is removed with the unloading metaspace.
862
// There isn't C heap memory allocated for methods, so nothing is done for them.
863
void ClassLoaderData::free_deallocate_list_C_heap_structures() {
864
assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
865
assert(is_unloading(), "only called for ClassLoaderData that are unloading");
866
if (_deallocate_list == NULL) {
867
return;
868
}
869
// Go backwards because this removes entries that are freed.
870
for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
871
Metadata* m = _deallocate_list->at(i);
872
_deallocate_list->remove_at(i);
873
if (m->is_constantPool()) {
874
((ConstantPool*)m)->release_C_heap_structures();
875
} else if (m->is_klass()) {
876
InstanceKlass* ik = (InstanceKlass*)m;
877
// also releases ik->constants() C heap memory
878
ik->release_C_heap_structures();
879
// Remove the class so unloading events aren't triggered for
880
// this class (scratch or error class) in do_unloading().
881
remove_class(ik);
882
}
883
}
884
}
885
886
// Caller needs ResourceMark
887
// If the class loader's _name has not been explicitly set, the class loader's
888
// qualified class name is returned.
889
const char* ClassLoaderData::loader_name() const {
890
if (_class_loader_klass == NULL) {
891
return BOOTSTRAP_LOADER_NAME;
892
} else if (_name != NULL) {
893
return _name->as_C_string();
894
} else {
895
return _class_loader_klass->external_name();
896
}
897
}
898
899
// Caller needs ResourceMark
900
// Format of the _name_and_id is as follows:
901
// If the defining loader has a name explicitly set then '<loader-name>' @<id>
902
// If the defining loader has no name then <qualified-class-name> @<id>
903
// If built-in loader, then omit '@<id>' as there is only one instance.
904
const char* ClassLoaderData::loader_name_and_id() const {
905
if (_class_loader_klass == NULL) {
906
return "'" BOOTSTRAP_LOADER_NAME "'";
907
} else if (_name_and_id != NULL) {
908
return _name_and_id->as_C_string();
909
} else {
910
// May be called in a race before _name_and_id is initialized.
911
return _class_loader_klass->external_name();
912
}
913
}
914
915
void ClassLoaderData::print_value_on(outputStream* out) const {
916
if (!is_unloading() && class_loader() != NULL) {
917
out->print("loader data: " INTPTR_FORMAT " for instance ", p2i(this));
918
class_loader()->print_value_on(out); // includes loader_name_and_id() and address of class loader instance
919
} else {
920
// loader data: 0xsomeaddr of 'bootstrap'
921
out->print("loader data: " INTPTR_FORMAT " of %s", p2i(this), loader_name_and_id());
922
}
923
if (_has_class_mirror_holder) {
924
out->print(" has a class holder");
925
}
926
}
927
928
void ClassLoaderData::print_value() const { print_value_on(tty); }
929
930
#ifndef PRODUCT
931
class PrintKlassClosure: public KlassClosure {
932
outputStream* _out;
933
public:
934
PrintKlassClosure(outputStream* out): _out(out) { }
935
936
void do_klass(Klass* k) {
937
ResourceMark rm;
938
_out->print("%s,", k->external_name());
939
}
940
};
941
942
void ClassLoaderData::print_on(outputStream* out) const {
943
ResourceMark rm;
944
out->print_cr("ClassLoaderData(" INTPTR_FORMAT ")", p2i(this));
945
out->print_cr(" - name %s", loader_name_and_id());
946
if (!_holder.is_null()) {
947
out->print (" - holder ");
948
_holder.print_on(out);
949
out->print_cr("");
950
}
951
out->print_cr(" - class loader " INTPTR_FORMAT, p2i(_class_loader.ptr_raw()));
952
out->print_cr(" - metaspace " INTPTR_FORMAT, p2i(_metaspace));
953
out->print_cr(" - unloading %s", _unloading ? "true" : "false");
954
out->print_cr(" - class mirror holder %s", _has_class_mirror_holder ? "true" : "false");
955
out->print_cr(" - modified oops %s", _modified_oops ? "true" : "false");
956
out->print_cr(" - keep alive %d", _keep_alive);
957
out->print (" - claim ");
958
switch(_claim) {
959
case _claim_none: out->print_cr("none"); break;
960
case _claim_finalizable:out->print_cr("finalizable"); break;
961
case _claim_strong: out->print_cr("strong"); break;
962
case _claim_other: out->print_cr("other"); break;
963
default: ShouldNotReachHere();
964
}
965
out->print_cr(" - handles %d", _handles.count());
966
out->print_cr(" - dependency count %d", _dependency_count);
967
out->print (" - klasses {");
968
PrintKlassClosure closure(out);
969
((ClassLoaderData*)this)->classes_do(&closure);
970
out->print_cr(" }");
971
out->print_cr(" - packages " INTPTR_FORMAT, p2i(_packages));
972
out->print_cr(" - module " INTPTR_FORMAT, p2i(_modules));
973
out->print_cr(" - unnamed module " INTPTR_FORMAT, p2i(_unnamed_module));
974
out->print_cr(" - dictionary " INTPTR_FORMAT, p2i(_dictionary));
975
if (_jmethod_ids != NULL) {
976
out->print (" - jmethod count ");
977
Method::print_jmethod_ids_count(this, out);
978
out->print_cr("");
979
}
980
out->print_cr(" - deallocate list " INTPTR_FORMAT, p2i(_deallocate_list));
981
out->print_cr(" - next CLD " INTPTR_FORMAT, p2i(_next));
982
}
983
#endif // PRODUCT
984
985
void ClassLoaderData::print() const { print_on(tty); }
986
987
void ClassLoaderData::verify() {
988
assert_locked_or_safepoint(_metaspace_lock);
989
oop cl = class_loader();
990
991
guarantee(this == class_loader_data(cl) || has_class_mirror_holder(), "Must be the same");
992
guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || has_class_mirror_holder(), "must be");
993
994
// Verify the integrity of the allocated space.
995
#ifdef ASSERT
996
if (metaspace_or_null() != NULL) {
997
metaspace_or_null()->verify();
998
}
999
#endif
1000
1001
for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
1002
guarantee(k->class_loader_data() == this, "Must be the same");
1003
k->verify();
1004
assert(k != k->next_link(), "no loops!");
1005
}
1006
}
1007
1008
bool ClassLoaderData::contains_klass(Klass* klass) {
1009
// Lock-free access requires load_acquire
1010
for (Klass* k = Atomic::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
1011
if (k == klass) return true;
1012
}
1013
return false;
1014
}
1015
1016