Wed, 24 Apr 2013 16:19:35 -0400
8011803: release_C_heap_structures is never called for anonymous classes.
Summary: Call this function from the ClassLoaderData destructor instead of the system dictionary walk.
Reviewed-by: stefank, mgerdin
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 #ifndef SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
26 #define SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
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"
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.
48 class ClassLoaderData;
49 class JNIMethodBlock;
50 class JNIHandleBlock;
51 class Metadebug;
52 // GC root for walking class loader data created
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;
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);
76 // CMS support.
77 static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
78 static GrowableArray<ClassLoaderData*>* new_clds();
80 static void dump_on(outputStream * const out) PRODUCT_RETURN;
81 static void dump() { dump_on(tty); }
82 static void verify();
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 };
91 // ClassLoaderData class
93 class ClassLoaderData : public CHeapObj<mtClass> {
94 friend class VMStructs;
95 private:
96 class Dependencies VALUE_OBJ_CLASS_SPEC {
97 objArrayOop _list_head;
98 void locked_add(objArrayHandle last,
99 objArrayHandle new_dependency,
100 Thread* THREAD);
101 public:
102 Dependencies() : _list_head(NULL) {}
103 void add(Handle dependency, TRAPS);
104 void init(TRAPS);
105 void oops_do(OopClosure* f);
106 };
108 friend class ClassLoaderDataGraph;
109 friend class ClassLoaderDataGraphMetaspaceIterator;
110 friend class MetaDataFactory;
111 friend class Method;
113 static ClassLoaderData * _the_null_class_loader_data;
115 oop _class_loader; // oop used to uniquely identify a class loader
116 // class loader or a canonical class path
117 Dependencies _dependencies; // holds dependencies from this class loader
118 // data to others.
120 Metaspace * _metaspace; // Meta-space where meta-data defined by the
121 // classes in the class loader are allocated.
122 Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup.
123 bool _unloading; // true if this class loader goes away
124 bool _keep_alive; // if this CLD can be unloaded for anonymous loaders
125 bool _is_anonymous; // if this CLD is for an anonymous class
126 volatile int _claimed; // true if claimed, for example during GC traces.
127 // To avoid applying oop closure more than once.
128 // Has to be an int because we cas it.
129 Klass* _klasses; // The classes defined by the class loader.
131 JNIHandleBlock* _handles; // Handles to constant pool arrays
133 // These method IDs are created for the class loader and set to NULL when the
134 // class loader is unloaded. They are rarely freed, only for redefine classes
135 // and if they lose a data race in InstanceKlass.
136 JNIMethodBlock* _jmethod_ids;
138 // Metadata to be deallocated when it's safe at class unloading, when
139 // this class loader isn't unloaded itself.
140 GrowableArray<Metadata*>* _deallocate_list;
142 // Support for walking class loader data objects
143 ClassLoaderData* _next; /// Next loader_datas created
145 // ReadOnly and ReadWrite metaspaces (static because only on the null
146 // class loader for now).
147 static Metaspace* _ro_metaspace;
148 static Metaspace* _rw_metaspace;
150 void set_next(ClassLoaderData* next) { _next = next; }
151 ClassLoaderData* next() const { return _next; }
153 ClassLoaderData(Handle h_class_loader, bool is_anonymous);
154 ~ClassLoaderData();
156 void set_metaspace(Metaspace* m) { _metaspace = m; }
158 JNIHandleBlock* handles() const;
159 void set_handles(JNIHandleBlock* handles);
161 Mutex* metaspace_lock() const { return _metaspace_lock; }
163 // GC interface.
164 void clear_claimed() { _claimed = 0; }
165 bool claimed() const { return _claimed == 1; }
166 bool claim();
168 void unload();
169 bool keep_alive() const { return _keep_alive; }
170 bool is_alive(BoolObjectClosure* is_alive_closure) const;
172 void classes_do(void f(InstanceKlass*));
174 // Deallocate free list during class unloading.
175 void free_deallocate_list();
177 // Allocate out of this class loader data
178 MetaWord* allocate(size_t size);
180 public:
181 // Accessors
182 Metaspace* metaspace_or_null() const { return _metaspace; }
184 static ClassLoaderData* the_null_class_loader_data() {
185 return _the_null_class_loader_data;
186 }
188 bool is_anonymous() const { return _is_anonymous; }
190 static void init_null_class_loader_data() {
191 assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
192 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
193 _the_null_class_loader_data = new ClassLoaderData((oop)NULL, false);
194 ClassLoaderDataGraph::_head = _the_null_class_loader_data;
195 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
196 if (DumpSharedSpaces) {
197 _the_null_class_loader_data->initialize_shared_metaspaces();
198 }
199 }
201 bool is_the_null_class_loader_data() const {
202 return this == _the_null_class_loader_data;
203 }
204 bool is_ext_class_loader_data() const;
206 // The Metaspace is created lazily so may be NULL. This
207 // method will allocate a Metaspace if needed.
208 Metaspace* metaspace_non_null();
210 oop class_loader() const { return _class_loader; }
212 // Returns true if this class loader data is for a loader going away.
213 bool is_unloading() const {
214 assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");
215 return _unloading;
216 }
217 // Anonymous class loader data doesn't have anything to keep them from
218 // being unloaded during parsing the anonymous class.
219 void set_keep_alive(bool value) { _keep_alive = value; }
221 unsigned int identity_hash() {
222 return _class_loader == NULL ? 0 : _class_loader->identity_hash();
223 }
225 // Used when tracing from klasses.
226 void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
228 void classes_do(KlassClosure* klass_closure);
230 JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; }
231 void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; }
233 void print_value() { print_value_on(tty); }
234 void print_value_on(outputStream* out) const;
235 void dump(outputStream * const out) PRODUCT_RETURN;
236 void verify();
237 const char* loader_name();
239 jobject add_handle(Handle h);
240 void add_class(Klass* k);
241 void remove_class(Klass* k);
242 void record_dependency(Klass* to, TRAPS);
243 void init_dependencies(TRAPS);
245 void add_to_deallocate_list(Metadata* m);
247 static ClassLoaderData* class_loader_data(oop loader);
248 static ClassLoaderData* class_loader_data_or_null(oop loader);
249 static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
250 static void print_loader(ClassLoaderData *loader_data, outputStream *out);
252 // CDS support
253 Metaspace* ro_metaspace();
254 Metaspace* rw_metaspace();
255 void initialize_shared_metaspaces();
256 };
258 class ClassLoaderDataGraphMetaspaceIterator : public StackObj {
259 ClassLoaderData* _data;
260 public:
261 ClassLoaderDataGraphMetaspaceIterator();
262 ~ClassLoaderDataGraphMetaspaceIterator();
263 bool repeat() { return _data != NULL; }
264 Metaspace* get_next() {
265 assert(_data != NULL, "Should not be NULL in call to the iterator");
266 Metaspace* result = _data->metaspace_or_null();
267 _data = _data->next();
268 // This result might be NULL for class loaders without metaspace
269 // yet. It would be nice to return only non-null results but
270 // there is no guarantee that there will be a non-null result
271 // down the list so the caller is going to have to check.
272 return result;
273 }
274 };
275 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP