1 /*
2 * Copyright (c) 1997, 2023, 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 "precompiled.hpp"
26 #include "classfile/vmClasses.hpp"
27 #include "classfile/vmSymbols.hpp"
28 #include "gc/shared/collectedHeap.inline.hpp"
29 #include "gc/shared/genCollectedHeap.hpp"
30 #include "gc/shared/slidingForwarding.inline.hpp"
31 #include "gc/shared/space.hpp"
32 #include "gc/shared/space.inline.hpp"
33 #include "gc/shared/spaceDecorator.inline.hpp"
34 #include "memory/iterator.inline.hpp"
35 #include "memory/universe.hpp"
36 #include "oops/oop.inline.hpp"
37 #include "runtime/atomic.hpp"
38 #include "runtime/java.hpp"
39 #include "runtime/prefetch.inline.hpp"
40 #include "runtime/safepoint.hpp"
41 #include "utilities/align.hpp"
42 #include "utilities/copy.hpp"
43 #include "utilities/globalDefinitions.hpp"
44 #include "utilities/macros.hpp"
45 #if INCLUDE_SERIALGC
46 #include "gc/serial/serialBlockOffsetTable.inline.hpp"
47 #include "gc/serial/defNewGeneration.hpp"
48 #endif
49
50 HeapWord* DirtyCardToOopClosure::get_actual_top(HeapWord* top,
51 HeapWord* top_obj) {
52 if (top_obj != nullptr && top_obj < (_sp->toContiguousSpace())->top()) {
53 if (cast_to_oop(top_obj)->is_objArray() || cast_to_oop(top_obj)->is_typeArray()) {
54 // An arrayOop is starting on the dirty card - since we do exact
55 // store checks for objArrays we are done.
56 } else {
57 // Otherwise, it is possible that the object starting on the dirty
58 // card spans the entire card, and that the store happened on a
59 // later card. Figure out where the object ends.
60 assert(_sp->block_size(top_obj) == cast_to_oop(top_obj)->size(),
61 "Block size and object size mismatch");
62 top = top_obj + cast_to_oop(top_obj)->size();
63 }
64 } else {
65 top = (_sp->toContiguousSpace())->top();
66 }
67 return top;
68 }
69
70 void DirtyCardToOopClosure::walk_mem_region(MemRegion mr,
71 HeapWord* bottom,
72 HeapWord* top) {
73 // Note that this assumption won't hold if we have a concurrent
74 // collector in this space, which may have freed up objects after
75 // they were dirtied and before the stop-the-world GC that is
76 // examining cards here.
77 assert(bottom < top, "ought to be at least one obj on a dirty card.");
78
79 walk_mem_region_with_cl(mr, bottom, top, _cl);
80 }
81
82 // We get called with "mr" representing the dirty region
83 // that we want to process. Because of imprecise marking,
84 // we may need to extend the incoming "mr" to the right,
85 // and scan more. However, because we may already have
86 // scanned some of that extended region, we may need to
87 // trim its right-end back some so we do not scan what
88 // we (or another worker thread) may already have scanned
89 // or planning to scan.
90 void DirtyCardToOopClosure::do_MemRegion(MemRegion mr) {
91 HeapWord* bottom = mr.start();
92 HeapWord* last = mr.last();
93 HeapWord* top = mr.end();
94 HeapWord* bottom_obj;
95 HeapWord* top_obj;
96
97 assert(_last_bottom == nullptr || top <= _last_bottom,
98 "Not decreasing");
99 NOT_PRODUCT(_last_bottom = mr.start());
100
101 bottom_obj = _sp->block_start(bottom);
102 top_obj = _sp->block_start(last);
103
104 assert(bottom_obj <= bottom, "just checking");
105 assert(top_obj <= top, "just checking");
106
107 // Given what we think is the top of the memory region and
108 // the start of the object at the top, get the actual
109 // value of the top.
110 top = get_actual_top(top, top_obj);
111
112 // If the previous call did some part of this region, don't redo.
113 if (_min_done != nullptr && _min_done < top) {
114 top = _min_done;
115 }
116
117 // Top may have been reset, and in fact may be below bottom,
118 // e.g. the dirty card region is entirely in a now free object
119 // -- something that could happen with a concurrent sweeper.
120 bottom = MIN2(bottom, top);
121 MemRegion extended_mr = MemRegion(bottom, top);
122 assert(bottom <= top &&
123 (_min_done == nullptr || top <= _min_done),
124 "overlap!");
125
126 // Walk the region if it is not empty; otherwise there is nothing to do.
127 if (!extended_mr.is_empty()) {
128 walk_mem_region(extended_mr, bottom_obj, top);
129 }
130
131 _min_done = bottom;
132 }
133
134 void DirtyCardToOopClosure::walk_mem_region_with_cl(MemRegion mr,
135 HeapWord* bottom,
136 HeapWord* top,
137 OopIterateClosure* cl) {
138 bottom += cast_to_oop(bottom)->oop_iterate_size(cl, mr);
139 if (bottom < top) {
140 HeapWord* next_obj = bottom + cast_to_oop(bottom)->size();
141 while (next_obj < top) {
142 /* Bottom lies entirely below top, so we can call the */
143 /* non-memRegion version of oop_iterate below. */
144 cast_to_oop(bottom)->oop_iterate(cl);
145 bottom = next_obj;
146 next_obj = bottom + cast_to_oop(bottom)->size();
147 }
148 /* Last object. */
149 cast_to_oop(bottom)->oop_iterate(cl, mr);
150 }
151 }
152
153 void Space::initialize(MemRegion mr,
154 bool clear_space,
155 bool mangle_space) {
156 HeapWord* bottom = mr.start();
157 HeapWord* end = mr.end();
158 assert(Universe::on_page_boundary(bottom) && Universe::on_page_boundary(end),
159 "invalid space boundaries");
160 set_bottom(bottom);
161 set_end(end);
162 if (clear_space) clear(mangle_space);
163 }
164
165 void Space::clear(bool mangle_space) {
166 if (ZapUnusedHeapArea && mangle_space) {
167 mangle_unused_area();
168 }
169 }
170
171 ContiguousSpace::ContiguousSpace(): Space(),
172 _compaction_top(nullptr),
173 _next_compaction_space(nullptr),
174 _top(nullptr) {
175 _mangler = new GenSpaceMangler(this);
176 }
177
178 ContiguousSpace::~ContiguousSpace() {
179 delete _mangler;
180 }
181
182 void ContiguousSpace::initialize(MemRegion mr,
183 bool clear_space,
184 bool mangle_space)
185 {
186 Space::initialize(mr, clear_space, mangle_space);
187 set_compaction_top(bottom());
188 _next_compaction_space = nullptr;
189 }
190
191 void ContiguousSpace::clear(bool mangle_space) {
192 set_top(bottom());
193 set_saved_mark();
194 Space::clear(mangle_space);
195 _compaction_top = bottom();
196 }
197
198 bool ContiguousSpace::is_free_block(const HeapWord* p) const {
199 return p >= _top;
200 }
201
202 #if INCLUDE_SERIALGC
203 void TenuredSpace::clear(bool mangle_space) {
204 ContiguousSpace::clear(mangle_space);
205 _offsets.initialize_threshold();
206 }
207
208 void TenuredSpace::set_bottom(HeapWord* new_bottom) {
209 Space::set_bottom(new_bottom);
210 _offsets.set_bottom(new_bottom);
211 }
212
213 void TenuredSpace::set_end(HeapWord* new_end) {
214 // Space should not advertise an increase in size
215 // until after the underlying offset table has been enlarged.
216 _offsets.resize(pointer_delta(new_end, bottom()));
217 Space::set_end(new_end);
218 }
219 #endif // INCLUDE_SERIALGC
220
221 #ifndef PRODUCT
222
223 void ContiguousSpace::set_top_for_allocations(HeapWord* v) {
224 mangler()->set_top_for_allocations(v);
225 }
226 void ContiguousSpace::set_top_for_allocations() {
227 mangler()->set_top_for_allocations(top());
228 }
229 void ContiguousSpace::check_mangled_unused_area(HeapWord* limit) {
230 mangler()->check_mangled_unused_area(limit);
231 }
232
233 void ContiguousSpace::check_mangled_unused_area_complete() {
234 mangler()->check_mangled_unused_area_complete();
235 }
236
237 // Mangled only the unused space that has not previously
238 // been mangled and that has not been allocated since being
239 // mangled.
240 void ContiguousSpace::mangle_unused_area() {
241 mangler()->mangle_unused_area();
242 }
243 void ContiguousSpace::mangle_unused_area_complete() {
244 mangler()->mangle_unused_area_complete();
245 }
246 #endif // NOT_PRODUCT
247
248 template <bool ALT_FWD>
249 HeapWord* ContiguousSpace::forward(oop q, size_t size,
250 CompactPoint* cp, HeapWord* compact_top) {
251 // q is alive
252 // First check if we should switch compaction space
253 assert(this == cp->space, "'this' should be current compaction space.");
254 size_t compaction_max_size = pointer_delta(end(), compact_top);
255 while (size > compaction_max_size) {
256 // switch to next compaction space
257 cp->space->set_compaction_top(compact_top);
258 cp->space = cp->space->next_compaction_space();
259 if (cp->space == nullptr) {
260 cp->gen = GenCollectedHeap::heap()->young_gen();
261 assert(cp->gen != nullptr, "compaction must succeed");
262 cp->space = cp->gen->first_compaction_space();
263 assert(cp->space != nullptr, "generation must have a first compaction space");
264 }
265 compact_top = cp->space->bottom();
266 cp->space->set_compaction_top(compact_top);
267 cp->space->initialize_threshold();
268 compaction_max_size = pointer_delta(cp->space->end(), compact_top);
269 }
270
271 // store the forwarding pointer into the mark word
272 if (cast_from_oop<HeapWord*>(q) != compact_top) {
273 SlidingForwarding::forward_to<ALT_FWD>(q, cast_to_oop(compact_top));
274 assert(q->is_gc_marked(), "encoding the pointer should preserve the mark");
275 } else {
276 // if the object isn't moving we can just set the mark to the default
277 // mark and handle it specially later on.
278 q->init_mark();
279 assert(SlidingForwarding::is_not_forwarded(q), "should not be forwarded");
280 }
281
282 compact_top += size;
283
284 // We need to update the offset table so that the beginnings of objects can be
285 // found during scavenge. Note that we are updating the offset table based on
286 // where the object will be once the compaction phase finishes.
287 cp->space->alloc_block(compact_top - size, compact_top);
288 return compact_top;
289 }
290
291 #if INCLUDE_SERIALGC
292
293 template <bool ALT_FWD>
294 void ContiguousSpace::prepare_for_compaction_impl(CompactPoint* cp) {
295 // Compute the new addresses for the live objects and store it in the mark
296 // Used by universe::mark_sweep_phase2()
297
298 // We're sure to be here before any objects are compacted into this
299 // space, so this is a good time to initialize this:
300 set_compaction_top(bottom());
301
302 if (cp->space == nullptr) {
303 assert(cp->gen != nullptr, "need a generation");
304 assert(cp->gen->first_compaction_space() == this, "just checking");
305 cp->space = cp->gen->first_compaction_space();
306 cp->space->initialize_threshold();
307 cp->space->set_compaction_top(cp->space->bottom());
308 }
309
310 HeapWord* compact_top = cp->space->compaction_top(); // This is where we are currently compacting to.
311
312 DeadSpacer dead_spacer(this);
313
314 HeapWord* end_of_live = bottom(); // One byte beyond the last byte of the last live object.
315 HeapWord* first_dead = nullptr; // The first dead object.
316
317 const intx interval = PrefetchScanIntervalInBytes;
318
319 HeapWord* cur_obj = bottom();
320 HeapWord* scan_limit = top();
321
322 while (cur_obj < scan_limit) {
323 if (cast_to_oop(cur_obj)->is_gc_marked()) {
324 // prefetch beyond cur_obj
325 Prefetch::write(cur_obj, interval);
326 size_t size = cast_to_oop(cur_obj)->size();
327 compact_top = cp->space->forward<ALT_FWD>(cast_to_oop(cur_obj), size, cp, compact_top);
328 cur_obj += size;
329 end_of_live = cur_obj;
330 } else {
331 // run over all the contiguous dead objects
332 HeapWord* end = cur_obj;
333 do {
334 // prefetch beyond end
335 Prefetch::write(end, interval);
336 end += cast_to_oop(end)->size();
337 } while (end < scan_limit && !cast_to_oop(end)->is_gc_marked());
338
339 // see if we might want to pretend this object is alive so that
340 // we don't have to compact quite as often.
341 if (cur_obj == compact_top && dead_spacer.insert_deadspace(cur_obj, end)) {
342 oop obj = cast_to_oop(cur_obj);
343 compact_top = cp->space->forward<ALT_FWD>(obj, obj->size(), cp, compact_top);
344 end_of_live = end;
345 } else {
346 // otherwise, it really is a free region.
347
348 // cur_obj is a pointer to a dead object. Use this dead memory to store a pointer to the next live object.
349 *(HeapWord**)cur_obj = end;
350
351 // see if this is the first dead region.
352 if (first_dead == nullptr) {
353 first_dead = cur_obj;
354 }
355 }
356
357 // move on to the next object
358 cur_obj = end;
359 }
360 }
361
362 assert(cur_obj == scan_limit, "just checking");
363 _end_of_live = end_of_live;
364 if (first_dead != nullptr) {
365 _first_dead = first_dead;
366 } else {
367 _first_dead = end_of_live;
368 }
369
370 // save the compaction_top of the compaction space.
371 cp->space->set_compaction_top(compact_top);
372 }
373
374 void ContiguousSpace::prepare_for_compaction(CompactPoint* cp) {
375 if (UseAltGCForwarding) {
376 prepare_for_compaction_impl<true>(cp);
377 } else {
378 prepare_for_compaction_impl<false>(cp);
379 }
380 }
381
382 template <bool ALT_FWD>
383 void ContiguousSpace::adjust_pointers_impl() {
384 // Check first is there is any work to do.
385 if (used() == 0) {
386 return; // Nothing to do.
387 }
388
389 // adjust all the interior pointers to point at the new locations of objects
390 // Used by MarkSweep::mark_sweep_phase3()
391
392 HeapWord* cur_obj = bottom();
393 HeapWord* const end_of_live = _end_of_live; // Established by prepare_for_compaction().
394 HeapWord* const first_dead = _first_dead; // Established by prepare_for_compaction().
395
396 assert(first_dead <= end_of_live, "Stands to reason, no?");
397
398 const intx interval = PrefetchScanIntervalInBytes;
399
400 debug_only(HeapWord* prev_obj = nullptr);
401 while (cur_obj < end_of_live) {
402 Prefetch::write(cur_obj, interval);
403 if (cur_obj < first_dead || cast_to_oop(cur_obj)->is_gc_marked()) {
404 // cur_obj is alive
405 // point all the oops to the new location
406 size_t size = MarkSweep::adjust_pointers<ALT_FWD>(cast_to_oop(cur_obj));
407 debug_only(prev_obj = cur_obj);
408 cur_obj += size;
409 } else {
410 debug_only(prev_obj = cur_obj);
411 // cur_obj is not a live object, instead it points at the next live object
412 cur_obj = *(HeapWord**)cur_obj;
413 assert(cur_obj > prev_obj, "we should be moving forward through memory, cur_obj: " PTR_FORMAT ", prev_obj: " PTR_FORMAT, p2i(cur_obj), p2i(prev_obj));
414 }
415 }
416
417 assert(cur_obj == end_of_live, "just checking");
418 }
419
420 void ContiguousSpace::adjust_pointers() {
421 if (UseAltGCForwarding) {
422 adjust_pointers_impl<true>();
423 } else {
424 adjust_pointers_impl<false>();
425 }
426 }
427
428 template <bool ALT_FWD>
429 void ContiguousSpace::compact_impl() {
430 // Copy all live objects to their new location
431 // Used by MarkSweep::mark_sweep_phase4()
432
433 verify_up_to_first_dead(this);
434
435 HeapWord* const start = bottom();
436 HeapWord* const end_of_live = _end_of_live;
437
438 assert(_first_dead <= end_of_live, "Invariant. _first_dead: " PTR_FORMAT " <= end_of_live: " PTR_FORMAT, p2i(_first_dead), p2i(end_of_live));
439 if (_first_dead == end_of_live && (start == end_of_live || !cast_to_oop(start)->is_gc_marked())) {
440 // Nothing to compact. The space is either empty or all live object should be left in place.
441 clear_empty_region(this);
442 return;
443 }
444
445 const intx scan_interval = PrefetchScanIntervalInBytes;
446 const intx copy_interval = PrefetchCopyIntervalInBytes;
447
448 assert(start < end_of_live, "bottom: " PTR_FORMAT " should be < end_of_live: " PTR_FORMAT, p2i(start), p2i(end_of_live));
449 HeapWord* cur_obj = start;
450 if (_first_dead > cur_obj && !cast_to_oop(cur_obj)->is_gc_marked()) {
451 // All object before _first_dead can be skipped. They should not be moved.
452 // A pointer to the first live object is stored at the memory location for _first_dead.
453 cur_obj = *(HeapWord**)(_first_dead);
454 }
455
456 debug_only(HeapWord* prev_obj = nullptr);
457 while (cur_obj < end_of_live) {
458 if (SlidingForwarding::is_not_forwarded(cast_to_oop(cur_obj))) {
459 debug_only(prev_obj = cur_obj);
460 // The first word of the dead object contains a pointer to the next live object or end of space.
461 cur_obj = *(HeapWord**)cur_obj;
462 assert(cur_obj > prev_obj, "we should be moving forward through memory");
463 } else {
464 // prefetch beyond q
465 Prefetch::read(cur_obj, scan_interval);
466
467 // size and destination
468 size_t size = cast_to_oop(cur_obj)->size();
469 HeapWord* compaction_top = cast_from_oop<HeapWord*>(SlidingForwarding::forwardee<ALT_FWD>(cast_to_oop(cur_obj)));
470
471 // prefetch beyond compaction_top
472 Prefetch::write(compaction_top, copy_interval);
473
474 // copy object and reinit its mark
475 assert(cur_obj != compaction_top, "everything in this pass should be moving");
476 Copy::aligned_conjoint_words(cur_obj, compaction_top, size);
477 oop new_obj = cast_to_oop(compaction_top);
478
479 ContinuationGCSupport::transform_stack_chunk(new_obj);
480
481 new_obj->init_mark();
482 assert(new_obj->klass() != nullptr, "should have a class");
483
484 debug_only(prev_obj = cur_obj);
485 cur_obj += size;
486 }
487 }
488
489 clear_empty_region(this);
490 }
491
492 void ContiguousSpace::compact() {
493 if (UseAltGCForwarding) {
494 compact_impl<true>();
495 } else {
496 compact_impl<false>();
497 }
498 }
499
500 #endif // INCLUDE_SERIALGC
501
502 void Space::print_short() const { print_short_on(tty); }
503
504 void Space::print_short_on(outputStream* st) const {
505 st->print(" space " SIZE_FORMAT "K, %3d%% used", capacity() / K,
506 (int) ((double) used() * 100 / capacity()));
507 }
508
509 void Space::print() const { print_on(tty); }
510
511 void Space::print_on(outputStream* st) const {
512 print_short_on(st);
513 st->print_cr(" [" PTR_FORMAT ", " PTR_FORMAT ")",
514 p2i(bottom()), p2i(end()));
515 }
516
517 void ContiguousSpace::print_on(outputStream* st) const {
518 print_short_on(st);
519 st->print_cr(" [" PTR_FORMAT ", " PTR_FORMAT ", " PTR_FORMAT ")",
520 p2i(bottom()), p2i(top()), p2i(end()));
521 }
522
523 #if INCLUDE_SERIALGC
524 void TenuredSpace::print_on(outputStream* st) const {
525 print_short_on(st);
526 st->print_cr(" [" PTR_FORMAT ", " PTR_FORMAT ", "
527 PTR_FORMAT ", " PTR_FORMAT ")",
528 p2i(bottom()), p2i(top()), p2i(_offsets.threshold()), p2i(end()));
529 }
530 #endif
531
532 void ContiguousSpace::verify() const {
533 HeapWord* p = bottom();
534 HeapWord* t = top();
535 HeapWord* prev_p = nullptr;
536 while (p < t) {
537 oopDesc::verify(cast_to_oop(p));
538 prev_p = p;
539 p += cast_to_oop(p)->size();
540 }
541 guarantee(p == top(), "end of last object must match end of space");
542 if (top() != end()) {
543 guarantee(top() == block_start_const(end()-1) &&
544 top() == block_start_const(top()),
545 "top should be start of unallocated block, if it exists");
546 }
547 }
548
549 void Space::oop_iterate(OopIterateClosure* blk) {
550 ObjectToOopClosure blk2(blk);
551 object_iterate(&blk2);
552 }
553
554 bool Space::obj_is_alive(const HeapWord* p) const {
555 assert (block_is_obj(p), "The address should point to an object");
556 return true;
557 }
558
559 void ContiguousSpace::oop_iterate(OopIterateClosure* blk) {
560 if (is_empty()) return;
561 HeapWord* obj_addr = bottom();
562 HeapWord* t = top();
563 // Could call objects iterate, but this is easier.
564 while (obj_addr < t) {
565 obj_addr += cast_to_oop(obj_addr)->oop_iterate_size(blk);
566 }
567 }
568
569 void ContiguousSpace::object_iterate(ObjectClosure* blk) {
570 if (is_empty()) return;
571 object_iterate_from(bottom(), blk);
572 }
573
574 void ContiguousSpace::object_iterate_from(HeapWord* mark, ObjectClosure* blk) {
575 while (mark < top()) {
576 blk->do_object(cast_to_oop(mark));
577 mark += cast_to_oop(mark)->size();
578 }
579 }
580
581 // Very general, slow implementation.
582 HeapWord* ContiguousSpace::block_start_const(const void* p) const {
583 assert(MemRegion(bottom(), end()).contains(p),
584 "p (" PTR_FORMAT ") not in space [" PTR_FORMAT ", " PTR_FORMAT ")",
585 p2i(p), p2i(bottom()), p2i(end()));
586 if (p >= top()) {
587 return top();
588 } else {
589 HeapWord* last = bottom();
590 HeapWord* cur = last;
591 while (cur <= p) {
592 last = cur;
593 cur += cast_to_oop(cur)->size();
594 }
595 assert(oopDesc::is_oop(cast_to_oop(last)), PTR_FORMAT " should be an object start", p2i(last));
596 return last;
597 }
598 }
599
600 size_t ContiguousSpace::block_size(const HeapWord* p) const {
601 assert(MemRegion(bottom(), end()).contains(p),
602 "p (" PTR_FORMAT ") not in space [" PTR_FORMAT ", " PTR_FORMAT ")",
603 p2i(p), p2i(bottom()), p2i(end()));
604 HeapWord* current_top = top();
605 assert(p <= current_top,
606 "p > current top - p: " PTR_FORMAT ", current top: " PTR_FORMAT,
607 p2i(p), p2i(current_top));
608 assert(p == current_top || oopDesc::is_oop(cast_to_oop(p)),
609 "p (" PTR_FORMAT ") is not a block start - "
610 "current_top: " PTR_FORMAT ", is_oop: %s",
611 p2i(p), p2i(current_top), BOOL_TO_STR(oopDesc::is_oop(cast_to_oop(p))));
612 if (p < current_top) {
613 return cast_to_oop(p)->size();
614 } else {
615 assert(p == current_top, "just checking");
616 return pointer_delta(end(), (HeapWord*) p);
617 }
618 }
619
620 // This version requires locking.
621 inline HeapWord* ContiguousSpace::allocate_impl(size_t size) {
622 assert(Heap_lock->owned_by_self() ||
623 (SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread()),
624 "not locked");
625 HeapWord* obj = top();
626 if (pointer_delta(end(), obj) >= size) {
627 HeapWord* new_top = obj + size;
628 set_top(new_top);
629 assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
630 return obj;
631 } else {
632 return nullptr;
633 }
634 }
635
636 // This version is lock-free.
637 inline HeapWord* ContiguousSpace::par_allocate_impl(size_t size) {
638 do {
639 HeapWord* obj = top();
640 if (pointer_delta(end(), obj) >= size) {
641 HeapWord* new_top = obj + size;
642 HeapWord* result = Atomic::cmpxchg(top_addr(), obj, new_top);
643 // result can be one of two:
644 // the old top value: the exchange succeeded
645 // otherwise: the new value of the top is returned.
646 if (result == obj) {
647 assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
648 return obj;
649 }
650 } else {
651 return nullptr;
652 }
653 } while (true);
654 }
655
656 // Requires locking.
657 HeapWord* ContiguousSpace::allocate(size_t size) {
658 return allocate_impl(size);
659 }
660
661 // Lock-free.
662 HeapWord* ContiguousSpace::par_allocate(size_t size) {
663 return par_allocate_impl(size);
664 }
665
666 #if INCLUDE_SERIALGC
667 void TenuredSpace::initialize_threshold() {
668 _offsets.initialize_threshold();
669 }
670
671 void TenuredSpace::alloc_block(HeapWord* start, HeapWord* end) {
672 _offsets.alloc_block(start, end);
673 }
674
675 TenuredSpace::TenuredSpace(BlockOffsetSharedArray* sharedOffsetArray,
676 MemRegion mr) :
677 _offsets(sharedOffsetArray, mr),
678 _par_alloc_lock(Mutex::safepoint, "TenuredSpaceParAlloc_lock", true)
679 {
680 _offsets.set_contig_space(this);
681 initialize(mr, SpaceDecorator::Clear, SpaceDecorator::Mangle);
682 }
683
684 #define OBJ_SAMPLE_INTERVAL 0
685 #define BLOCK_SAMPLE_INTERVAL 100
686
687 void TenuredSpace::verify() const {
688 HeapWord* p = bottom();
689 HeapWord* prev_p = nullptr;
690 int objs = 0;
691 int blocks = 0;
692
693 if (VerifyObjectStartArray) {
694 _offsets.verify();
695 }
696
697 while (p < top()) {
698 size_t size = cast_to_oop(p)->size();
699 // For a sampling of objects in the space, find it using the
700 // block offset table.
701 if (blocks == BLOCK_SAMPLE_INTERVAL) {
702 guarantee(p == block_start_const(p + (size/2)),
703 "check offset computation");
704 blocks = 0;
705 } else {
706 blocks++;
707 }
708
709 if (objs == OBJ_SAMPLE_INTERVAL) {
710 oopDesc::verify(cast_to_oop(p));
711 objs = 0;
712 } else {
713 objs++;
714 }
715 prev_p = p;
716 p += size;
717 }
718 guarantee(p == top(), "end of last object must match end of space");
719 }
720
721
722 size_t TenuredSpace::allowed_dead_ratio() const {
723 return MarkSweepDeadRatio;
724 }
725 #endif // INCLUDE_SERIALGC