1 /* 2 * Copyright (c) 2020, 2025, 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 #include "classfile/classFileParser.hpp" 26 #include "classfile/fieldLayoutBuilder.hpp" 27 #include "jvm.h" 28 #include "memory/resourceArea.hpp" 29 #include "oops/array.hpp" 30 #include "oops/fieldStreams.inline.hpp" 31 #include "oops/instanceMirrorKlass.hpp" 32 #include "oops/instanceKlass.inline.hpp" 33 #include "oops/klass.inline.hpp" 34 #include "runtime/fieldDescriptor.inline.hpp" 35 36 37 LayoutRawBlock::LayoutRawBlock(Kind kind, int size) : 38 _next_block(nullptr), 39 _prev_block(nullptr), 40 _kind(kind), 41 _offset(-1), 42 _alignment(1), 43 _size(size), 44 _field_index(-1), 45 _is_reference(false) { 46 assert(kind == EMPTY || kind == RESERVED || kind == PADDING || kind == INHERITED, 47 "Otherwise, should use the constructor with a field index argument"); 48 assert(size > 0, "Sanity check"); 49 } 50 51 52 LayoutRawBlock::LayoutRawBlock(int index, Kind kind, int size, int alignment, bool is_reference) : 53 _next_block(nullptr), 54 _prev_block(nullptr), 55 _kind(kind), 56 _offset(-1), 57 _alignment(alignment), 58 _size(size), 59 _field_index(index), 60 _is_reference(is_reference) { 61 assert(kind == REGULAR || kind == FLATTENED || kind == INHERITED, 62 "Other kind do not have a field index"); 63 assert(size > 0, "Sanity check"); 64 assert(alignment > 0, "Sanity check"); 65 } 66 67 bool LayoutRawBlock::fit(int size, int alignment) { 68 int adjustment = 0; 69 if ((_offset % alignment) != 0) { 70 adjustment = alignment - (_offset % alignment); 71 } 72 return _size >= size + adjustment; 73 } 74 75 FieldGroup::FieldGroup(int contended_group) : 76 _next(nullptr), 77 _primitive_fields(nullptr), 78 _oop_fields(nullptr), 79 _contended_group(contended_group), // -1 means no contended group, 0 means default contended group 80 _oop_count(0) {} 81 82 void FieldGroup::add_primitive_field(int idx, BasicType type) { 83 int size = type2aelembytes(type); 84 LayoutRawBlock* block = new LayoutRawBlock(idx, LayoutRawBlock::REGULAR, size, size /* alignment == size for primitive types */, false); 85 if (_primitive_fields == nullptr) { 86 _primitive_fields = new GrowableArray<LayoutRawBlock*>(INITIAL_LIST_SIZE); 87 } 88 _primitive_fields->append(block); 89 } 90 91 void FieldGroup::add_oop_field(int idx) { 92 int size = type2aelembytes(T_OBJECT); 93 LayoutRawBlock* block = new LayoutRawBlock(idx, LayoutRawBlock::REGULAR, size, size /* alignment == size for oops */, true); 94 if (_oop_fields == nullptr) { 95 _oop_fields = new GrowableArray<LayoutRawBlock*>(INITIAL_LIST_SIZE); 96 } 97 _oop_fields->append(block); 98 _oop_count++; 99 } 100 101 void FieldGroup::sort_by_size() { 102 if (_primitive_fields != nullptr) { 103 _primitive_fields->sort(LayoutRawBlock::compare_size_inverted); 104 } 105 } 106 107 FieldLayout::FieldLayout(GrowableArray<FieldInfo>* field_info, ConstantPool* cp) : 108 _field_info(field_info), 109 _cp(cp), 110 _blocks(nullptr), 111 _start(_blocks), 112 _last(_blocks) {} 113 114 void FieldLayout::initialize_static_layout() { 115 _blocks = new LayoutRawBlock(LayoutRawBlock::EMPTY, INT_MAX); 116 _blocks->set_offset(0); 117 _last = _blocks; 118 _start = _blocks; 119 // Note: at this stage, InstanceMirrorKlass::offset_of_static_fields() could be zero, because 120 // during bootstrapping, the size of the java.lang.Class is still not known when layout 121 // of static field is computed. Field offsets are fixed later when the size is known 122 // (see java_lang_Class::fixup_mirror()) 123 if (InstanceMirrorKlass::offset_of_static_fields() > 0) { 124 insert(first_empty_block(), new LayoutRawBlock(LayoutRawBlock::RESERVED, InstanceMirrorKlass::offset_of_static_fields())); 125 _blocks->set_offset(0); 126 } 127 } 128 129 void FieldLayout::initialize_instance_layout(const InstanceKlass* super_klass) { 130 if (super_klass == nullptr) { 131 _blocks = new LayoutRawBlock(LayoutRawBlock::EMPTY, INT_MAX); 132 _blocks->set_offset(0); 133 _last = _blocks; 134 _start = _blocks; 135 insert(first_empty_block(), new LayoutRawBlock(LayoutRawBlock::RESERVED, instanceOopDesc::base_offset_in_bytes())); 136 } else { 137 bool has_fields = reconstruct_layout(super_klass); 138 fill_holes(super_klass); 139 if (!super_klass->has_contended_annotations() || !has_fields) { 140 _start = _blocks; // start allocating fields from the first empty block 141 } else { 142 _start = _last; // append fields at the end of the reconstructed layout 143 } 144 } 145 } 146 147 LayoutRawBlock* FieldLayout::first_field_block() { 148 LayoutRawBlock* block = _start; 149 while (block->kind() != LayoutRawBlock::INHERITED && block->kind() != LayoutRawBlock::REGULAR 150 && block->kind() != LayoutRawBlock::FLATTENED && block->kind() != LayoutRawBlock::PADDING) { 151 block = block->next_block(); 152 } 153 return block; 154 } 155 156 157 // Insert a set of fields into a layout using a best-fit strategy. 158 // For each field, search for the smallest empty slot able to fit the field 159 // (satisfying both size and alignment requirements), if none is found, 160 // add the field at the end of the layout. 161 // Fields cannot be inserted before the block specified in the "start" argument 162 void FieldLayout::add(GrowableArray<LayoutRawBlock*>* list, LayoutRawBlock* start) { 163 if (list == nullptr) return; 164 if (start == nullptr) start = this->_start; 165 bool last_search_success = false; 166 int last_size = 0; 167 int last_alignment = 0; 168 for (int i = 0; i < list->length(); i ++) { 169 LayoutRawBlock* b = list->at(i); 170 LayoutRawBlock* cursor = nullptr; 171 LayoutRawBlock* candidate = nullptr; 172 173 // if start is the last block, just append the field 174 if (start == last_block()) { 175 candidate = last_block(); 176 } 177 // Before iterating over the layout to find an empty slot fitting the field's requirements, 178 // check if the previous field had the same requirements and if the search for a fitting slot 179 // was successful. If the requirements were the same but the search failed, a new search will 180 // fail the same way, so just append the field at the of the layout. 181 else if (b->size() == last_size && b->alignment() == last_alignment && !last_search_success) { 182 candidate = last_block(); 183 } else { 184 // Iterate over the layout to find an empty slot fitting the field's requirements 185 last_size = b->size(); 186 last_alignment = b->alignment(); 187 cursor = last_block()->prev_block(); 188 assert(cursor != nullptr, "Sanity check"); 189 last_search_success = true; 190 while (cursor != start) { 191 if (cursor->kind() == LayoutRawBlock::EMPTY && cursor->fit(b->size(), b->alignment())) { 192 if (candidate == nullptr || cursor->size() < candidate->size()) { 193 candidate = cursor; 194 } 195 } 196 cursor = cursor->prev_block(); 197 } 198 if (candidate == nullptr) { 199 candidate = last_block(); 200 last_search_success = false; 201 } 202 assert(candidate != nullptr, "Candidate must not be null"); 203 assert(candidate->kind() == LayoutRawBlock::EMPTY, "Candidate must be an empty block"); 204 assert(candidate->fit(b->size(), b->alignment()), "Candidate must be able to store the block"); 205 } 206 207 insert_field_block(candidate, b); 208 } 209 } 210 211 // Finds a slot for the identity hash-code. 212 // Same basic algorithm as above add() method, but simplified 213 // and does not actually insert the field. 214 int FieldLayout::find_hash_offset() { 215 LayoutRawBlock* start = this->_start; 216 LayoutRawBlock* last = last_block(); 217 LayoutRawBlock* cursor = start; 218 while (cursor != last) { 219 assert(cursor != nullptr, "Sanity check"); 220 if (cursor->kind() == LayoutRawBlock::EMPTY && cursor->fit(4, 1)) { 221 break; 222 } 223 cursor = cursor->next_block(); 224 } 225 return cursor->offset(); 226 } 227 228 // Used for classes with hard coded field offsets, insert a field at the specified offset */ 229 void FieldLayout::add_field_at_offset(LayoutRawBlock* block, int offset, LayoutRawBlock* start) { 230 assert(block != nullptr, "Sanity check"); 231 block->set_offset(offset); 232 if (start == nullptr) { 233 start = this->_start; 234 } 235 LayoutRawBlock* slot = start; 236 while (slot != nullptr) { 237 if ((slot->offset() <= block->offset() && (slot->offset() + slot->size()) > block->offset()) || 238 slot == _last){ 239 assert(slot->kind() == LayoutRawBlock::EMPTY, "Matching slot must be an empty slot"); 240 assert(slot->size() >= block->offset() + block->size() ,"Matching slot must be big enough"); 241 if (slot->offset() < block->offset()) { 242 int adjustment = block->offset() - slot->offset(); 243 LayoutRawBlock* adj = new LayoutRawBlock(LayoutRawBlock::EMPTY, adjustment); 244 insert(slot, adj); 245 } 246 insert(slot, block); 247 if (slot->size() == 0) { 248 remove(slot); 249 } 250 _field_info->adr_at(block->field_index())->set_offset(block->offset()); 251 return; 252 } 253 slot = slot->next_block(); 254 } 255 fatal("Should have found a matching slot above, corrupted layout or invalid offset"); 256 } 257 258 // The allocation logic uses a best fit strategy: the set of fields is allocated 259 // in the first empty slot big enough to contain the whole set ((including padding 260 // to fit alignment constraints). 261 void FieldLayout::add_contiguously(GrowableArray<LayoutRawBlock*>* list, LayoutRawBlock* start) { 262 if (list == nullptr) return; 263 if (start == nullptr) { 264 start = _start; 265 } 266 // This code assumes that if the first block is well aligned, the following 267 // blocks would naturally be well aligned (no need for adjustment) 268 int size = 0; 269 for (int i = 0; i < list->length(); i++) { 270 size += list->at(i)->size(); 271 } 272 273 LayoutRawBlock* candidate = nullptr; 274 if (start == last_block()) { 275 candidate = last_block(); 276 } else { 277 LayoutRawBlock* first = list->at(0); 278 candidate = last_block()->prev_block(); 279 while (candidate->kind() != LayoutRawBlock::EMPTY || !candidate->fit(size, first->alignment())) { 280 if (candidate == start) { 281 candidate = last_block(); 282 break; 283 } 284 candidate = candidate->prev_block(); 285 } 286 assert(candidate != nullptr, "Candidate must not be null"); 287 assert(candidate->kind() == LayoutRawBlock::EMPTY, "Candidate must be an empty block"); 288 assert(candidate->fit(size, first->alignment()), "Candidate must be able to store the whole contiguous block"); 289 } 290 291 for (int i = 0; i < list->length(); i++) { 292 LayoutRawBlock* b = list->at(i); 293 insert_field_block(candidate, b); 294 assert((candidate->offset() % b->alignment() == 0), "Contiguous blocks must be naturally well aligned"); 295 } 296 } 297 298 LayoutRawBlock* FieldLayout::insert_field_block(LayoutRawBlock* slot, LayoutRawBlock* block) { 299 assert(slot->kind() == LayoutRawBlock::EMPTY, "Blocks can only be inserted in empty blocks"); 300 if (slot->offset() % block->alignment() != 0) { 301 int adjustment = block->alignment() - (slot->offset() % block->alignment()); 302 LayoutRawBlock* adj = new LayoutRawBlock(LayoutRawBlock::EMPTY, adjustment); 303 insert(slot, adj); 304 } 305 insert(slot, block); 306 if (slot->size() == 0) { 307 remove(slot); 308 } 309 _field_info->adr_at(block->field_index())->set_offset(block->offset()); 310 return block; 311 } 312 313 bool FieldLayout::reconstruct_layout(const InstanceKlass* ik) { 314 bool has_instance_fields = false; 315 GrowableArray<LayoutRawBlock*>* all_fields = new GrowableArray<LayoutRawBlock*>(32); 316 while (ik != nullptr) { 317 for (AllFieldStream fs(ik->fieldinfo_stream(), ik->constants()); !fs.done(); fs.next()) { 318 BasicType type = Signature::basic_type(fs.signature()); 319 // distinction between static and non-static fields is missing 320 if (fs.access_flags().is_static()) continue; 321 has_instance_fields = true; 322 int size = type2aelembytes(type); 323 // INHERITED blocks are marked as non-reference because oop_maps are handled by their holder class 324 LayoutRawBlock* block = new LayoutRawBlock(fs.index(), LayoutRawBlock::INHERITED, size, size, false); 325 block->set_offset(fs.offset()); 326 all_fields->append(block); 327 } 328 ik = ik->super() == nullptr ? nullptr : InstanceKlass::cast(ik->super()); 329 } 330 331 all_fields->sort(LayoutRawBlock::compare_offset); 332 _blocks = new LayoutRawBlock(LayoutRawBlock::RESERVED, instanceOopDesc::base_offset_in_bytes()); 333 _blocks->set_offset(0); 334 _last = _blocks; 335 336 for(int i = 0; i < all_fields->length(); i++) { 337 LayoutRawBlock* b = all_fields->at(i); 338 _last->set_next_block(b); 339 b->set_prev_block(_last); 340 _last = b; 341 } 342 _start = _blocks; 343 return has_instance_fields; 344 } 345 346 // Called during the reconstruction of a layout, after fields from super 347 // classes have been inserted. It fills unused slots between inserted fields 348 // with EMPTY blocks, so the regular field insertion methods would work. 349 // This method handles classes with @Contended annotations differently 350 // by inserting PADDING blocks instead of EMPTY block to prevent subclasses' 351 // fields to interfere with contended fields/classes. 352 void FieldLayout::fill_holes(const InstanceKlass* super_klass) { 353 assert(_blocks != nullptr, "Sanity check"); 354 assert(_blocks->offset() == 0, "first block must be at offset zero"); 355 LayoutRawBlock::Kind filling_type = super_klass->has_contended_annotations() ? LayoutRawBlock::PADDING: LayoutRawBlock::EMPTY; 356 LayoutRawBlock* b = _blocks; 357 while (b->next_block() != nullptr) { 358 if (b->next_block()->offset() > (b->offset() + b->size())) { 359 int size = b->next_block()->offset() - (b->offset() + b->size()); 360 LayoutRawBlock* empty = new LayoutRawBlock(filling_type, size); 361 empty->set_offset(b->offset() + b->size()); 362 empty->set_next_block(b->next_block()); 363 b->next_block()->set_prev_block(empty); 364 b->set_next_block(empty); 365 empty->set_prev_block(b); 366 } 367 b = b->next_block(); 368 } 369 assert(b->next_block() == nullptr, "Invariant at this point"); 370 assert(b->kind() != LayoutRawBlock::EMPTY, "Sanity check"); 371 372 // If the super class has @Contended annotation, a padding block is 373 // inserted at the end to ensure that fields from the subclasses won't share 374 // the cache line of the last field of the contended class 375 if (super_klass->has_contended_annotations() && ContendedPaddingWidth > 0) { 376 LayoutRawBlock* p = new LayoutRawBlock(LayoutRawBlock::PADDING, ContendedPaddingWidth); 377 p->set_offset(b->offset() + b->size()); 378 b->set_next_block(p); 379 p->set_prev_block(b); 380 b = p; 381 } 382 383 LayoutRawBlock* last = new LayoutRawBlock(LayoutRawBlock::EMPTY, INT_MAX); 384 last->set_offset(b->offset() + b->size()); 385 assert(last->offset() > 0, "Sanity check"); 386 b->set_next_block(last); 387 last->set_prev_block(b); 388 _last = last; 389 } 390 391 LayoutRawBlock* FieldLayout::insert(LayoutRawBlock* slot, LayoutRawBlock* block) { 392 assert(slot->kind() == LayoutRawBlock::EMPTY, "Blocks can only be inserted in empty blocks"); 393 assert(slot->offset() % block->alignment() == 0, "Incompatible alignment"); 394 block->set_offset(slot->offset()); 395 slot->set_offset(slot->offset() + block->size()); 396 assert((slot->size() - block->size()) < slot->size(), "underflow checking"); 397 assert(slot->size() - block->size() >= 0, "no negative size allowed"); 398 slot->set_size(slot->size() - block->size()); 399 block->set_prev_block(slot->prev_block()); 400 block->set_next_block(slot); 401 slot->set_prev_block(block); 402 if (block->prev_block() != nullptr) { 403 block->prev_block()->set_next_block(block); 404 } 405 if (_blocks == slot) { 406 _blocks = block; 407 } 408 return block; 409 } 410 411 void FieldLayout::remove(LayoutRawBlock* block) { 412 assert(block != nullptr, "Sanity check"); 413 assert(block != _last, "Sanity check"); 414 if (_blocks == block) { 415 _blocks = block->next_block(); 416 if (_blocks != nullptr) { 417 _blocks->set_prev_block(nullptr); 418 } 419 } else { 420 assert(block->prev_block() != nullptr, "_prev should be set for non-head blocks"); 421 block->prev_block()->set_next_block(block->next_block()); 422 block->next_block()->set_prev_block(block->prev_block()); 423 } 424 if (block == _start) { 425 _start = block->prev_block(); 426 } 427 } 428 429 void FieldLayout::print(outputStream* output, bool is_static, const InstanceKlass* super) { 430 ResourceMark rm; 431 LayoutRawBlock* b = _blocks; 432 while(b != _last) { 433 switch(b->kind()) { 434 case LayoutRawBlock::REGULAR: { 435 FieldInfo* fi = _field_info->adr_at(b->field_index()); 436 output->print_cr(" @%d \"%s\" %s %d/%d %s", 437 b->offset(), 438 fi->name(_cp)->as_C_string(), 439 fi->signature(_cp)->as_C_string(), 440 b->size(), 441 b->alignment(), 442 "REGULAR"); 443 break; 444 } 445 case LayoutRawBlock::FLATTENED: { 446 FieldInfo* fi = _field_info->adr_at(b->field_index()); 447 output->print_cr(" @%d \"%s\" %s %d/%d %s", 448 b->offset(), 449 fi->name(_cp)->as_C_string(), 450 fi->signature(_cp)->as_C_string(), 451 b->size(), 452 b->alignment(), 453 "FLATTENED"); 454 break; 455 } 456 case LayoutRawBlock::RESERVED: { 457 output->print_cr(" @%d %d/- %s", 458 b->offset(), 459 b->size(), 460 "RESERVED"); 461 break; 462 } 463 case LayoutRawBlock::INHERITED: { 464 assert(!is_static, "Static fields are not inherited in layouts"); 465 assert(super != nullptr, "super klass must be provided to retrieve inherited fields info"); 466 bool found = false; 467 const InstanceKlass* ik = super; 468 while (!found && ik != nullptr) { 469 for (AllFieldStream fs(ik->fieldinfo_stream(), ik->constants()); !fs.done(); fs.next()) { 470 if (fs.offset() == b->offset()) { 471 output->print_cr(" @%d \"%s\" %s %d/%d %s", 472 b->offset(), 473 fs.name()->as_C_string(), 474 fs.signature()->as_C_string(), 475 b->size(), 476 b->size(), // so far, alignment constraint == size, will change with Valhalla 477 "INHERITED"); 478 found = true; 479 break; 480 } 481 } 482 ik = ik->java_super(); 483 } 484 break; 485 } 486 case LayoutRawBlock::EMPTY: 487 output->print_cr(" @%d %d/1 %s", 488 b->offset(), 489 b->size(), 490 "EMPTY"); 491 break; 492 case LayoutRawBlock::PADDING: 493 output->print_cr(" @%d %d/1 %s", 494 b->offset(), 495 b->size(), 496 "PADDING"); 497 break; 498 } 499 b = b->next_block(); 500 } 501 } 502 503 FieldLayoutBuilder::FieldLayoutBuilder(const Symbol* classname, const InstanceKlass* super_klass, ConstantPool* constant_pool, 504 GrowableArray<FieldInfo>* field_info, bool is_contended, FieldLayoutInfo* info) : 505 _classname(classname), 506 _super_klass(super_klass), 507 _constant_pool(constant_pool), 508 _field_info(field_info), 509 _info(info), 510 _root_group(nullptr), 511 _contended_groups(GrowableArray<FieldGroup*>(8)), 512 _static_fields(nullptr), 513 _layout(nullptr), 514 _static_layout(nullptr), 515 _nonstatic_oopmap_count(0), 516 _alignment(-1), 517 _has_nonstatic_fields(false), 518 _is_contended(is_contended) {} 519 520 521 FieldGroup* FieldLayoutBuilder::get_or_create_contended_group(int g) { 522 assert(g > 0, "must only be called for named contended groups"); 523 FieldGroup* fg = nullptr; 524 for (int i = 0; i < _contended_groups.length(); i++) { 525 fg = _contended_groups.at(i); 526 if (fg->contended_group() == g) return fg; 527 } 528 fg = new FieldGroup(g); 529 _contended_groups.append(fg); 530 return fg; 531 } 532 533 void FieldLayoutBuilder::prologue() { 534 _layout = new FieldLayout(_field_info, _constant_pool); 535 const InstanceKlass* super_klass = _super_klass; 536 _layout->initialize_instance_layout(super_klass); 537 if (super_klass != nullptr) { 538 _has_nonstatic_fields = super_klass->has_nonstatic_fields(); 539 } 540 _static_layout = new FieldLayout(_field_info, _constant_pool); 541 _static_layout->initialize_static_layout(); 542 _static_fields = new FieldGroup(); 543 _root_group = new FieldGroup(); 544 } 545 546 // Field sorting for regular classes: 547 // - fields are sorted in static and non-static fields 548 // - non-static fields are also sorted according to their contention group 549 // (support of the @Contended annotation) 550 // - @Contended annotation is ignored for static fields 551 void FieldLayoutBuilder::regular_field_sorting() { 552 int idx = 0; 553 for (GrowableArrayIterator<FieldInfo> it = _field_info->begin(); it != _field_info->end(); ++it, ++idx) { 554 FieldInfo ctrl = _field_info->at(0); 555 FieldGroup* group = nullptr; 556 FieldInfo fieldinfo = *it; 557 if (fieldinfo.access_flags().is_static()) { 558 group = _static_fields; 559 } else { 560 _has_nonstatic_fields = true; 561 if (fieldinfo.field_flags().is_contended()) { 562 int g = fieldinfo.contended_group(); 563 if (g == 0) { 564 group = new FieldGroup(true); 565 _contended_groups.append(group); 566 } else { 567 group = get_or_create_contended_group(g); 568 } 569 } else { 570 group = _root_group; 571 } 572 } 573 assert(group != nullptr, "invariant"); 574 BasicType type = Signature::basic_type(fieldinfo.signature(_constant_pool)); 575 switch(type) { 576 case T_BYTE: 577 case T_CHAR: 578 case T_DOUBLE: 579 case T_FLOAT: 580 case T_INT: 581 case T_LONG: 582 case T_SHORT: 583 case T_BOOLEAN: 584 group->add_primitive_field(idx, type); 585 break; 586 case T_OBJECT: 587 case T_ARRAY: 588 if (group != _static_fields) _nonstatic_oopmap_count++; 589 group->add_oop_field(idx); 590 break; 591 default: 592 fatal("Something wrong?"); 593 } 594 } 595 _root_group->sort_by_size(); 596 _static_fields->sort_by_size(); 597 if (!_contended_groups.is_empty()) { 598 for (int i = 0; i < _contended_groups.length(); i++) { 599 _contended_groups.at(i)->sort_by_size(); 600 } 601 } 602 } 603 604 void FieldLayoutBuilder::insert_contended_padding(LayoutRawBlock* slot) { 605 if (ContendedPaddingWidth > 0) { 606 LayoutRawBlock* padding = new LayoutRawBlock(LayoutRawBlock::PADDING, ContendedPaddingWidth); 607 _layout->insert(slot, padding); 608 } 609 } 610 611 // Computation of regular classes layout is an evolution of the previous default layout 612 // (FieldAllocationStyle 1): 613 // - primitive fields are allocated first (from the biggest to the smallest) 614 // - then oop fields are allocated, either in existing gaps or at the end of 615 // the layout 616 void FieldLayoutBuilder::compute_regular_layout() { 617 bool need_tail_padding = false; 618 prologue(); 619 regular_field_sorting(); 620 621 if (_is_contended) { 622 _layout->set_start(_layout->last_block()); 623 // insertion is currently easy because the current strategy doesn't try to fill holes 624 // in super classes layouts => the _start block is by consequence the _last_block 625 insert_contended_padding(_layout->start()); 626 need_tail_padding = true; 627 } 628 _layout->add(_root_group->primitive_fields()); 629 _layout->add(_root_group->oop_fields()); 630 631 if (!_contended_groups.is_empty()) { 632 for (int i = 0; i < _contended_groups.length(); i++) { 633 FieldGroup* cg = _contended_groups.at(i); 634 LayoutRawBlock* start = _layout->last_block(); 635 insert_contended_padding(start); 636 _layout->add(cg->primitive_fields(), start); 637 _layout->add(cg->oop_fields(), start); 638 need_tail_padding = true; 639 } 640 } 641 642 if (need_tail_padding) { 643 insert_contended_padding(_layout->last_block()); 644 } 645 646 _static_layout->add_contiguously(this->_static_fields->oop_fields()); 647 _static_layout->add(this->_static_fields->primitive_fields()); 648 649 epilogue(); 650 } 651 652 void FieldLayoutBuilder::epilogue() { 653 // Computing oopmaps 654 int super_oop_map_count = (_super_klass == nullptr) ? 0 :_super_klass->nonstatic_oop_map_count(); 655 int max_oop_map_count = super_oop_map_count + _nonstatic_oopmap_count; 656 657 OopMapBlocksBuilder* nonstatic_oop_maps = 658 new OopMapBlocksBuilder(max_oop_map_count); 659 if (super_oop_map_count > 0) { 660 nonstatic_oop_maps->initialize_inherited_blocks(_super_klass->start_of_nonstatic_oop_maps(), 661 _super_klass->nonstatic_oop_map_count()); 662 } 663 664 if (_root_group->oop_fields() != nullptr) { 665 for (int i = 0; i < _root_group->oop_fields()->length(); i++) { 666 LayoutRawBlock* b = _root_group->oop_fields()->at(i); 667 nonstatic_oop_maps->add(b->offset(), 1); 668 } 669 } 670 671 if (!_contended_groups.is_empty()) { 672 for (int i = 0; i < _contended_groups.length(); i++) { 673 FieldGroup* cg = _contended_groups.at(i); 674 if (cg->oop_count() > 0) { 675 assert(cg->oop_fields() != nullptr && cg->oop_fields()->at(0) != nullptr, "oop_count > 0 but no oop fields found"); 676 nonstatic_oop_maps->add(cg->oop_fields()->at(0)->offset(), cg->oop_count()); 677 } 678 } 679 } 680 681 nonstatic_oop_maps->compact(); 682 683 int instance_end = align_up(_layout->last_block()->offset(), wordSize); 684 int static_fields_end = align_up(_static_layout->last_block()->offset(), wordSize); 685 int static_fields_size = (static_fields_end - 686 InstanceMirrorKlass::offset_of_static_fields()) / wordSize; 687 int nonstatic_field_end = align_up(_layout->last_block()->offset(), heapOopSize); 688 689 // Pass back information needed for InstanceKlass creation 690 691 _info->oop_map_blocks = nonstatic_oop_maps; 692 _info->_instance_size = align_object_size(instance_end / wordSize); 693 if (UseCompactObjectHeaders) { 694 _info->_hash_offset = _layout->find_hash_offset(); 695 } 696 _info->_static_field_size = static_fields_size; 697 _info->_nonstatic_field_size = (nonstatic_field_end - instanceOopDesc::base_offset_in_bytes()) / heapOopSize; 698 _info->_has_nonstatic_fields = _has_nonstatic_fields; 699 700 if (PrintFieldLayout) { 701 ResourceMark rm; 702 tty->print_cr("Layout of class %s", _classname->as_C_string()); 703 tty->print_cr("Instance fields:"); 704 _layout->print(tty, false, _super_klass); 705 tty->print_cr("Static fields:"); 706 _static_layout->print(tty, true, nullptr); 707 tty->print_cr("Instance size = %d bytes", _info->_instance_size * wordSize); 708 tty->print_cr("---"); 709 } 710 } 711 712 void FieldLayoutBuilder::build_layout() { 713 compute_regular_layout(); 714 }