87 public:
88 // Byte offset of the first oop mapped by this block.
89 int offset() const { return _offset; }
90 void set_offset(int offset) { _offset = offset; }
91
92 // Number of oops in this block.
93 uint count() const { return _count; }
94 void set_count(uint count) { _count = count; }
95
96 // sizeof(OopMapBlock) in HeapWords.
97 static const int size_in_words() {
98 return align_size_up(int(sizeof(OopMapBlock)), HeapWordSize) >>
99 LogHeapWordSize;
100 }
101
102 private:
103 int _offset;
104 uint _count;
105 };
106
107 struct JvmtiCachedClassFileData;
108
109 class InstanceKlass: public Klass {
110 friend class VMStructs;
111 friend class ClassFileParser;
112 friend class CompileReplay;
113
114 protected:
115 // Constructor
116 InstanceKlass(int vtable_len,
117 int itable_len,
118 int static_field_size,
119 int nonstatic_oop_map_size,
120 unsigned kind,
121 ReferenceType rt,
122 AccessFlags access_flags,
123 bool is_anonymous);
124 public:
125 static InstanceKlass* allocate_instance_klass(
126 ClassLoaderData* loader_data,
181 // if this class is unloaded.
182 Symbol* _array_name;
183
184 // Number of heapOopSize words used by non-static fields in this klass
185 // (including inherited fields but after header_size()).
186 int _nonstatic_field_size;
187 int _static_field_size; // number words used by static fields (oop and non-oop) in this klass
188 // Constant pool index to the utf8 entry of the Generic signature,
189 // or 0 if none.
190 u2 _generic_signature_index;
191 // Constant pool index to the utf8 entry for the name of source file
192 // containing this klass, 0 if not specified.
193 u2 _source_file_name_index;
194 u2 _static_oop_field_count;// number of static oop fields in this klass
195 u2 _java_fields_count; // The number of declared Java fields
196 int _nonstatic_oop_map_size;// size in words of nonstatic oop map blocks
197
198 // _is_marked_dependent can be set concurrently, thus cannot be part of the
199 // _misc_flags.
200 bool _is_marked_dependent; // used for marking during flushing and deoptimization
201 bool _has_unloaded_dependent;
202
203 // The low two bits of _misc_flags contains the kind field.
204 // This can be used to quickly discriminate among the four kinds of
205 // InstanceKlass.
206
207 static const unsigned _misc_kind_field_size = 2;
208 static const unsigned _misc_kind_field_pos = 0;
209 static const unsigned _misc_kind_field_mask = (1u << _misc_kind_field_size) - 1u;
210
211 static const unsigned _misc_kind_other = 0; // concrete InstanceKlass
212 static const unsigned _misc_kind_reference = 1; // InstanceRefKlass
213 static const unsigned _misc_kind_class_loader = 2; // InstanceClassLoaderKlass
214 static const unsigned _misc_kind_mirror = 3; // InstanceMirrorKlass
215
216 // Start after _misc_kind field.
217 enum {
218 _misc_rewritten = 1 << 2, // methods rewritten.
219 _misc_has_nonstatic_fields = 1 << 3, // for sizing with UseCompressedOops
220 _misc_should_verify_class = 1 << 4, // allow caching of preverification
221 _misc_is_anonymous = 1 << 5, // has embedded _host_klass field
222 _misc_is_contended = 1 << 6, // marked with contended annotation
223 _misc_has_default_methods = 1 << 7, // class/superclass/implemented interfaces has default methods
224 _misc_declares_default_methods = 1 << 8, // directly declares default methods (any access)
225 _misc_has_been_redefined = 1 << 9, // class has been redefined
226 _misc_is_scratch_class = 1 << 10 // class is the redefined scratch class
227 };
228 u2 _misc_flags;
229 u2 _minor_version; // minor version number of class file
230 u2 _major_version; // major version number of class file
231 Thread* _init_thread; // Pointer to current thread doing initialization (to handle recusive initialization)
232 int _vtable_len; // length of Java vtable (in words)
233 int _itable_len; // length of Java itable (in words)
234 OopMapCache* volatile _oop_map_cache; // OopMapCache for all methods in the klass (allocated lazily)
235 MemberNameTable* _member_names; // Member names
236 JNIid* _jni_ids; // First JNI identifier for static fields in this class
237 jmethodID* _methods_jmethod_ids; // jmethodIDs corresponding to method_idnum, or NULL if none
238 nmethodBucket* _dependencies; // list of dependent nmethods
239 nmethod* _osr_nmethods_head; // Head of list of on-stack replacement nmethods for this class
240 BreakpointInfo* _breakpoints; // bpt lists, managed by Method*
241 // Linked instanceKlasses of previous versions
242 InstanceKlass* _previous_versions;
243 // JVMTI fields can be moved to their own structure - see 6315920
244 // JVMTI: cached class file, before retransformable agent modified it in CFLH
245 JvmtiCachedClassFileData* _cached_class_file;
246
247 volatile u2 _idnum_allocated_count; // JNI/JVMTI: increments with the addition of methods, old ids don't change
248
249 // Class states are defined as ClassState (see above).
250 // Place the _init_state here to utilize the unused 2-byte after
251 // _idnum_allocated_count.
252 u1 _init_state; // state of class
253 u1 _reference_type; // reference type
254
255 JvmtiCachedClassFieldMap* _jvmti_cached_class_field_map; // JVMTI: used during heap iteration
256
257 NOT_PRODUCT(int _verify_count;) // to avoid redundant verifies
258
451 bool is_reentrant_initialization(Thread *thread) { return thread == _init_thread; }
452 ClassState init_state() { return (ClassState)_init_state; }
453 bool is_rewritten() const { return (_misc_flags & _misc_rewritten) != 0; }
454
455 // defineClass specified verification
456 bool should_verify_class() const {
457 return (_misc_flags & _misc_should_verify_class) != 0;
458 }
459 void set_should_verify_class(bool value) {
460 if (value) {
461 _misc_flags |= _misc_should_verify_class;
462 } else {
463 _misc_flags &= ~_misc_should_verify_class;
464 }
465 }
466
467 // marking
468 bool is_marked_dependent() const { return _is_marked_dependent; }
469 void set_is_marked_dependent(bool value) { _is_marked_dependent = value; }
470
471 bool has_unloaded_dependent() const { return _has_unloaded_dependent; }
472 void set_has_unloaded_dependent(bool value) { _has_unloaded_dependent = value; }
473
474 // initialization (virtuals from Klass)
475 bool should_be_initialized() const; // means that initialize should be called
476 void initialize(TRAPS);
477 void link_class(TRAPS);
478 bool link_class_or_fail(TRAPS); // returns false on failure
479 void unlink_class();
480 void rewrite_class(TRAPS);
481 void link_methods(TRAPS);
482 Method* class_initializer();
483
484 // set the class to initialized if no static initializer is present
485 void eager_initialize(Thread *thread);
486
487 // reference type
488 ReferenceType reference_type() const { return (ReferenceType)_reference_type; }
489 void set_reference_type(ReferenceType t) {
490 assert(t == (u1)t, "overflow");
491 _reference_type = (u1)t;
492 }
493
1007 // - the class has a finalizer (if !RegisterFinalizersAtInit)
1008 // - the class size is larger than FastAllocateSizeLimit
1009 // - the class is java/lang/Class, which cannot be allocated directly
1010 bool can_be_fastpath_allocated() const {
1011 return !layout_helper_needs_slow_path(layout_helper());
1012 }
1013
1014 // Java vtable/itable
1015 klassVtable* vtable() const; // return new klassVtable wrapper
1016 inline Method* method_at_vtable(int index);
1017 klassItable* itable() const; // return new klassItable wrapper
1018 Method* method_at_itable(Klass* holder, int index, TRAPS);
1019
1020 #if INCLUDE_JVMTI
1021 void adjust_default_methods(InstanceKlass* holder, bool* trace_name_printed);
1022 #endif // INCLUDE_JVMTI
1023
1024 void clean_weak_instanceklass_links(BoolObjectClosure* is_alive);
1025 void clean_implementors_list(BoolObjectClosure* is_alive);
1026 void clean_method_data(BoolObjectClosure* is_alive);
1027 void clean_dependent_nmethods();
1028
1029 // Explicit metaspace deallocation of fields
1030 // For RedefineClasses and class file parsing errors, we need to deallocate
1031 // instanceKlasses and the metadata they point to.
1032 void deallocate_contents(ClassLoaderData* loader_data);
1033 static void deallocate_methods(ClassLoaderData* loader_data,
1034 Array<Method*>* methods);
1035 void static deallocate_interfaces(ClassLoaderData* loader_data,
1036 Klass* super_klass,
1037 Array<Klass*>* local_interfaces,
1038 Array<Klass*>* transitive_interfaces);
1039
1040 // The constant pool is on stack if any of the methods are executing or
1041 // referenced by handles.
1042 bool on_stack() const { return _constants->on_stack(); }
1043
1044 // callbacks for actions during class unloading
1045 static void notify_unload_class(InstanceKlass* ik);
1046 static void release_C_heap_structures(InstanceKlass* ik);
1047
1319
1320
1321 //
1322 // nmethodBucket is used to record dependent nmethods for
1323 // deoptimization. nmethod dependencies are actually <klass, method>
1324 // pairs but we really only care about the klass part for purposes of
1325 // finding nmethods which might need to be deoptimized. Instead of
1326 // recording the method, a count of how many times a particular nmethod
1327 // was recorded is kept. This ensures that any recording errors are
1328 // noticed since an nmethod should be removed as many times are it's
1329 // added.
1330 //
1331 class nmethodBucket: public CHeapObj<mtClass> {
1332 friend class VMStructs;
1333 private:
1334 nmethod* _nmethod;
1335 int _count;
1336 nmethodBucket* _next;
1337
1338 public:
1339 nmethodBucket(nmethod* nmethod, nmethodBucket* next) {
1340 _nmethod = nmethod;
1341 _next = next;
1342 _count = 1;
1343 }
1344 int count() { return _count; }
1345 int increment() { _count += 1; return _count; }
1346 int decrement();
1347 nmethodBucket* next() { return _next; }
1348 void set_next(nmethodBucket* b) { _next = b; }
1349 nmethod* get_nmethod() { return _nmethod; }
1350
1351 static int mark_dependent_nmethods(nmethodBucket* deps, DepChange& changes);
1352 static nmethodBucket* add_dependent_nmethod(nmethodBucket* deps, nmethod* nm);
1353 static bool remove_dependent_nmethod(nmethodBucket** deps, nmethod* nm, bool delete_immediately);
1354 static bool remove_dependent_nmethod(nmethodBucket* deps, nmethod* nm);
1355 static nmethodBucket* clean_dependent_nmethods(nmethodBucket* deps);
1356 #ifndef PRODUCT
1357 static void print_dependent_nmethods(nmethodBucket* deps, bool verbose);
1358 static bool is_dependent_nmethod(nmethodBucket* deps, nmethod* nm);
1359 #endif //PRODUCT
1360 };
1361
1362 // An iterator that's used to access the inner classes indices in the
1363 // InstanceKlass::_inner_classes array.
1364 class InnerClassesIterator : public StackObj {
1365 private:
1366 Array<jushort>* _inner_classes;
1367 int _length;
1368 int _idx;
1369 public:
1370
1371 InnerClassesIterator(instanceKlassHandle k) {
1372 _inner_classes = k->inner_classes();
1373 if (k->inner_classes() != NULL) {
1374 _length = _inner_classes->length();
1375 // The inner class array's length should be the multiple of
1376 // inner_class_next_offset if it only contains the InnerClasses
1377 // attribute data, or it should be
1378 // n*inner_class_next_offset+enclosing_method_attribute_size
1379 // if it also contains the EnclosingMethod data.
|
87 public:
88 // Byte offset of the first oop mapped by this block.
89 int offset() const { return _offset; }
90 void set_offset(int offset) { _offset = offset; }
91
92 // Number of oops in this block.
93 uint count() const { return _count; }
94 void set_count(uint count) { _count = count; }
95
96 // sizeof(OopMapBlock) in HeapWords.
97 static const int size_in_words() {
98 return align_size_up(int(sizeof(OopMapBlock)), HeapWordSize) >>
99 LogHeapWordSize;
100 }
101
102 private:
103 int _offset;
104 uint _count;
105 };
106
107 // Utility class to manipulate nmethod dependency context.
108 // The context consists of nmethodBucket* (a head of a linked list)
109 // and a boolean flag (does the list contains stale entries). The structure is
110 // encoded as an intptr_t: lower bit is used for the flag. It is possible since
111 // nmethodBucket* is aligned - the structure is malloc'ed in C heap.
112 // Dependency context can be attached either to an InstanceKlass (_dep_context field)
113 // or CallSiteContext oop for call_site_target dependencies (see javaClasses.hpp).
114 // DependencyContext class operates on some location which holds a intptr_t value.
115 class DependencyContext : public StackObj {
116 friend class VMStructs;
117 friend class TestDependencyContext;
118 private:
119 enum TagBits { _has_stale_entries_bit = 1, _has_stale_entries_mask = 1 };
120
121 intptr_t* _dependency_context_addr;
122
123 void set_dependencies(nmethodBucket* b) {
124 assert((intptr_t(b) & _has_stale_entries_mask) == 0, "should be aligned");
125 if (has_stale_entries()) {
126 *_dependency_context_addr = intptr_t(b) | _has_stale_entries_mask;
127 } else {
128 *_dependency_context_addr = intptr_t(b);
129 }
130 }
131
132 void set_has_stale_entries(bool b) {
133 if (b) {
134 *_dependency_context_addr |= _has_stale_entries_mask;
135 } else {
136 *_dependency_context_addr &= ~_has_stale_entries_mask;
137 }
138 }
139
140 nmethodBucket* dependencies() {
141 intptr_t value = *_dependency_context_addr;
142 return (nmethodBucket*) (value & ~_has_stale_entries_mask);
143 }
144
145 bool has_stale_entries() const {
146 intptr_t value = *_dependency_context_addr;
147 return (value & _has_stale_entries_mask) != 0;
148 };
149
150 public:
151 DependencyContext(intptr_t* addr) : _dependency_context_addr(addr) {}
152
153 static const intptr_t EMPTY = 0; // dependencies = NULL, has_stale_entries = false
154
155 int mark_dependent_nmethods(DepChange& changes);
156 void add_dependent_nmethod(nmethod* nm, bool expunge_stale_entries = false);
157 void remove_dependent_nmethod(nmethod* nm, bool expunge_stale_entries = false);
158 int remove_all_dependents();
159
160 void expunge_stale_entries();
161
162 #ifndef PRODUCT
163 void print_dependent_nmethods(bool verbose);
164 bool is_dependent_nmethod(nmethod* nm);
165 bool find_stale_entries();
166 #endif //PRODUCT
167 };
168
169 struct JvmtiCachedClassFileData;
170
171 class InstanceKlass: public Klass {
172 friend class VMStructs;
173 friend class ClassFileParser;
174 friend class CompileReplay;
175
176 protected:
177 // Constructor
178 InstanceKlass(int vtable_len,
179 int itable_len,
180 int static_field_size,
181 int nonstatic_oop_map_size,
182 unsigned kind,
183 ReferenceType rt,
184 AccessFlags access_flags,
185 bool is_anonymous);
186 public:
187 static InstanceKlass* allocate_instance_klass(
188 ClassLoaderData* loader_data,
243 // if this class is unloaded.
244 Symbol* _array_name;
245
246 // Number of heapOopSize words used by non-static fields in this klass
247 // (including inherited fields but after header_size()).
248 int _nonstatic_field_size;
249 int _static_field_size; // number words used by static fields (oop and non-oop) in this klass
250 // Constant pool index to the utf8 entry of the Generic signature,
251 // or 0 if none.
252 u2 _generic_signature_index;
253 // Constant pool index to the utf8 entry for the name of source file
254 // containing this klass, 0 if not specified.
255 u2 _source_file_name_index;
256 u2 _static_oop_field_count;// number of static oop fields in this klass
257 u2 _java_fields_count; // The number of declared Java fields
258 int _nonstatic_oop_map_size;// size in words of nonstatic oop map blocks
259
260 // _is_marked_dependent can be set concurrently, thus cannot be part of the
261 // _misc_flags.
262 bool _is_marked_dependent; // used for marking during flushing and deoptimization
263
264 // The low two bits of _misc_flags contains the kind field.
265 // This can be used to quickly discriminate among the four kinds of
266 // InstanceKlass.
267
268 static const unsigned _misc_kind_field_size = 2;
269 static const unsigned _misc_kind_field_pos = 0;
270 static const unsigned _misc_kind_field_mask = (1u << _misc_kind_field_size) - 1u;
271
272 static const unsigned _misc_kind_other = 0; // concrete InstanceKlass
273 static const unsigned _misc_kind_reference = 1; // InstanceRefKlass
274 static const unsigned _misc_kind_class_loader = 2; // InstanceClassLoaderKlass
275 static const unsigned _misc_kind_mirror = 3; // InstanceMirrorKlass
276
277 // Start after _misc_kind field.
278 enum {
279 _misc_rewritten = 1 << 2, // methods rewritten.
280 _misc_has_nonstatic_fields = 1 << 3, // for sizing with UseCompressedOops
281 _misc_should_verify_class = 1 << 4, // allow caching of preverification
282 _misc_is_anonymous = 1 << 5, // has embedded _host_klass field
283 _misc_is_contended = 1 << 6, // marked with contended annotation
284 _misc_has_default_methods = 1 << 7, // class/superclass/implemented interfaces has default methods
285 _misc_declares_default_methods = 1 << 8, // directly declares default methods (any access)
286 _misc_has_been_redefined = 1 << 9, // class has been redefined
287 _misc_is_scratch_class = 1 << 10 // class is the redefined scratch class
288 };
289 u2 _misc_flags;
290 u2 _minor_version; // minor version number of class file
291 u2 _major_version; // major version number of class file
292 Thread* _init_thread; // Pointer to current thread doing initialization (to handle recusive initialization)
293 int _vtable_len; // length of Java vtable (in words)
294 int _itable_len; // length of Java itable (in words)
295 OopMapCache* volatile _oop_map_cache; // OopMapCache for all methods in the klass (allocated lazily)
296 MemberNameTable* _member_names; // Member names
297 JNIid* _jni_ids; // First JNI identifier for static fields in this class
298 jmethodID* _methods_jmethod_ids; // jmethodIDs corresponding to method_idnum, or NULL if none
299 intptr_t _dep_context; // packed DependencyContext structure
300 nmethod* _osr_nmethods_head; // Head of list of on-stack replacement nmethods for this class
301 BreakpointInfo* _breakpoints; // bpt lists, managed by Method*
302 // Linked instanceKlasses of previous versions
303 InstanceKlass* _previous_versions;
304 // JVMTI fields can be moved to their own structure - see 6315920
305 // JVMTI: cached class file, before retransformable agent modified it in CFLH
306 JvmtiCachedClassFileData* _cached_class_file;
307
308 volatile u2 _idnum_allocated_count; // JNI/JVMTI: increments with the addition of methods, old ids don't change
309
310 // Class states are defined as ClassState (see above).
311 // Place the _init_state here to utilize the unused 2-byte after
312 // _idnum_allocated_count.
313 u1 _init_state; // state of class
314 u1 _reference_type; // reference type
315
316 JvmtiCachedClassFieldMap* _jvmti_cached_class_field_map; // JVMTI: used during heap iteration
317
318 NOT_PRODUCT(int _verify_count;) // to avoid redundant verifies
319
512 bool is_reentrant_initialization(Thread *thread) { return thread == _init_thread; }
513 ClassState init_state() { return (ClassState)_init_state; }
514 bool is_rewritten() const { return (_misc_flags & _misc_rewritten) != 0; }
515
516 // defineClass specified verification
517 bool should_verify_class() const {
518 return (_misc_flags & _misc_should_verify_class) != 0;
519 }
520 void set_should_verify_class(bool value) {
521 if (value) {
522 _misc_flags |= _misc_should_verify_class;
523 } else {
524 _misc_flags &= ~_misc_should_verify_class;
525 }
526 }
527
528 // marking
529 bool is_marked_dependent() const { return _is_marked_dependent; }
530 void set_is_marked_dependent(bool value) { _is_marked_dependent = value; }
531
532 // initialization (virtuals from Klass)
533 bool should_be_initialized() const; // means that initialize should be called
534 void initialize(TRAPS);
535 void link_class(TRAPS);
536 bool link_class_or_fail(TRAPS); // returns false on failure
537 void unlink_class();
538 void rewrite_class(TRAPS);
539 void link_methods(TRAPS);
540 Method* class_initializer();
541
542 // set the class to initialized if no static initializer is present
543 void eager_initialize(Thread *thread);
544
545 // reference type
546 ReferenceType reference_type() const { return (ReferenceType)_reference_type; }
547 void set_reference_type(ReferenceType t) {
548 assert(t == (u1)t, "overflow");
549 _reference_type = (u1)t;
550 }
551
1065 // - the class has a finalizer (if !RegisterFinalizersAtInit)
1066 // - the class size is larger than FastAllocateSizeLimit
1067 // - the class is java/lang/Class, which cannot be allocated directly
1068 bool can_be_fastpath_allocated() const {
1069 return !layout_helper_needs_slow_path(layout_helper());
1070 }
1071
1072 // Java vtable/itable
1073 klassVtable* vtable() const; // return new klassVtable wrapper
1074 inline Method* method_at_vtable(int index);
1075 klassItable* itable() const; // return new klassItable wrapper
1076 Method* method_at_itable(Klass* holder, int index, TRAPS);
1077
1078 #if INCLUDE_JVMTI
1079 void adjust_default_methods(InstanceKlass* holder, bool* trace_name_printed);
1080 #endif // INCLUDE_JVMTI
1081
1082 void clean_weak_instanceklass_links(BoolObjectClosure* is_alive);
1083 void clean_implementors_list(BoolObjectClosure* is_alive);
1084 void clean_method_data(BoolObjectClosure* is_alive);
1085
1086 // Explicit metaspace deallocation of fields
1087 // For RedefineClasses and class file parsing errors, we need to deallocate
1088 // instanceKlasses and the metadata they point to.
1089 void deallocate_contents(ClassLoaderData* loader_data);
1090 static void deallocate_methods(ClassLoaderData* loader_data,
1091 Array<Method*>* methods);
1092 void static deallocate_interfaces(ClassLoaderData* loader_data,
1093 Klass* super_klass,
1094 Array<Klass*>* local_interfaces,
1095 Array<Klass*>* transitive_interfaces);
1096
1097 // The constant pool is on stack if any of the methods are executing or
1098 // referenced by handles.
1099 bool on_stack() const { return _constants->on_stack(); }
1100
1101 // callbacks for actions during class unloading
1102 static void notify_unload_class(InstanceKlass* ik);
1103 static void release_C_heap_structures(InstanceKlass* ik);
1104
1376
1377
1378 //
1379 // nmethodBucket is used to record dependent nmethods for
1380 // deoptimization. nmethod dependencies are actually <klass, method>
1381 // pairs but we really only care about the klass part for purposes of
1382 // finding nmethods which might need to be deoptimized. Instead of
1383 // recording the method, a count of how many times a particular nmethod
1384 // was recorded is kept. This ensures that any recording errors are
1385 // noticed since an nmethod should be removed as many times are it's
1386 // added.
1387 //
1388 class nmethodBucket: public CHeapObj<mtClass> {
1389 friend class VMStructs;
1390 private:
1391 nmethod* _nmethod;
1392 int _count;
1393 nmethodBucket* _next;
1394
1395 public:
1396 nmethodBucket(nmethod* nmethod, nmethodBucket* next) :
1397 _nmethod(nmethod), _next(next), _count(1) {}
1398
1399 int count() { return _count; }
1400 int increment() { _count += 1; return _count; }
1401 int decrement();
1402 nmethodBucket* next() { return _next; }
1403 void set_next(nmethodBucket* b) { _next = b; }
1404 nmethod* get_nmethod() { return _nmethod; }
1405 };
1406
1407 // An iterator that's used to access the inner classes indices in the
1408 // InstanceKlass::_inner_classes array.
1409 class InnerClassesIterator : public StackObj {
1410 private:
1411 Array<jushort>* _inner_classes;
1412 int _length;
1413 int _idx;
1414 public:
1415
1416 InnerClassesIterator(instanceKlassHandle k) {
1417 _inner_classes = k->inner_classes();
1418 if (k->inner_classes() != NULL) {
1419 _length = _inner_classes->length();
1420 // The inner class array's length should be the multiple of
1421 // inner_class_next_offset if it only contains the InnerClasses
1422 // attribute data, or it should be
1423 // n*inner_class_next_offset+enclosing_method_attribute_size
1424 // if it also contains the EnclosingMethod data.
|