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/gcForwarding.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 
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     GCForwarding::forward_to(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(GCForwarding::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 void ContiguousSpace::prepare_for_compaction(CompactPoint* cp) {
294   // Compute the new addresses for the live objects and store it in the mark
295   // Used by universe::mark_sweep_phase2()
296 
297   // We're sure to be here before any objects are compacted into this
298   // space, so this is a good time to initialize this:
299   set_compaction_top(bottom());
300 
301   if (cp->space == nullptr) {
302     assert(cp->gen != nullptr, "need a generation");
303     assert(cp->gen->first_compaction_space() == this, "just checking");
304     cp->space = cp->gen->first_compaction_space();
305     cp->space->initialize_threshold();
306     cp->space->set_compaction_top(cp->space->bottom());
307   }
308 
309   HeapWord* compact_top = cp->space->compaction_top(); // This is where we are currently compacting to.
310 
311   DeadSpacer dead_spacer(this);
312 
313   HeapWord*  end_of_live = bottom();  // One byte beyond the last byte of the last live object.
314   HeapWord*  first_dead = nullptr; // The first dead object.
315 
316   const intx interval = PrefetchScanIntervalInBytes;
317 
318   HeapWord* cur_obj = bottom();
319   HeapWord* scan_limit = top();
320 
321   while (cur_obj < scan_limit) {
322     if (cast_to_oop(cur_obj)->is_gc_marked()) {
323       // prefetch beyond cur_obj
324       Prefetch::write(cur_obj, interval);
325       size_t size = cast_to_oop(cur_obj)->size();
326       compact_top = cp->space->forward(cast_to_oop(cur_obj), size, cp, compact_top);
327       cur_obj += size;
328       end_of_live = cur_obj;
329     } else {
330       // run over all the contiguous dead objects
331       HeapWord* end = cur_obj;
332       do {
333         // prefetch beyond end
334         Prefetch::write(end, interval);
335         end += cast_to_oop(end)->size();
336       } while (end < scan_limit && !cast_to_oop(end)->is_gc_marked());
337 
338       // see if we might want to pretend this object is alive so that
339       // we don't have to compact quite as often.
340       if (cur_obj == compact_top && dead_spacer.insert_deadspace(cur_obj, end)) {
341         oop obj = cast_to_oop(cur_obj);
342         compact_top = cp->space->forward(obj, obj->size(), cp, compact_top);
343         end_of_live = end;
344       } else {
345         // otherwise, it really is a free region.
346 
347         // cur_obj is a pointer to a dead object. Use this dead memory to store a pointer to the next live object.
348         *(HeapWord**)cur_obj = end;
349 
350         // see if this is the first dead region.
351         if (first_dead == nullptr) {
352           first_dead = cur_obj;
353         }
354       }
355 
356       // move on to the next object
357       cur_obj = end;
358     }
359   }
360 
361   assert(cur_obj == scan_limit, "just checking");
362   _end_of_live = end_of_live;
363   if (first_dead != nullptr) {
364     _first_dead = first_dead;
365   } else {
366     _first_dead = end_of_live;
367   }
368 
369   // save the compaction_top of the compaction space.
370   cp->space->set_compaction_top(compact_top);
371 }
372 
373 void ContiguousSpace::adjust_pointers() {
374   // Check first is there is any work to do.
375   if (used() == 0) {
376     return;   // Nothing to do.
377   }
378 
379   // adjust all the interior pointers to point at the new locations of objects
380   // Used by MarkSweep::mark_sweep_phase3()
381 
382   HeapWord* cur_obj = bottom();
383   HeapWord* const end_of_live = _end_of_live;  // Established by prepare_for_compaction().
384   HeapWord* const first_dead = _first_dead;    // Established by prepare_for_compaction().
385 
386   assert(first_dead <= end_of_live, "Stands to reason, no?");
387 
388   const intx interval = PrefetchScanIntervalInBytes;
389 
390   debug_only(HeapWord* prev_obj = nullptr);
391   while (cur_obj < end_of_live) {
392     Prefetch::write(cur_obj, interval);
393     if (cur_obj < first_dead || cast_to_oop(cur_obj)->is_gc_marked()) {
394       // cur_obj is alive
395       // point all the oops to the new location
396       size_t size = MarkSweep::adjust_pointers(cast_to_oop(cur_obj));
397       debug_only(prev_obj = cur_obj);
398       cur_obj += size;
399     } else {
400       debug_only(prev_obj = cur_obj);
401       // cur_obj is not a live object, instead it points at the next live object
402       cur_obj = *(HeapWord**)cur_obj;
403       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));
404     }
405   }
406 
407   assert(cur_obj == end_of_live, "just checking");
408 }
409 
410 void ContiguousSpace::compact() {
411   // Copy all live objects to their new location
412   // Used by MarkSweep::mark_sweep_phase4()
413 
414   verify_up_to_first_dead(this);
415 
416   HeapWord* const start = bottom();
417   HeapWord* const end_of_live = _end_of_live;
418 
419   assert(_first_dead <= end_of_live, "Invariant. _first_dead: " PTR_FORMAT " <= end_of_live: " PTR_FORMAT, p2i(_first_dead), p2i(end_of_live));
420   if (_first_dead == end_of_live && (start == end_of_live || !cast_to_oop(start)->is_gc_marked())) {
421     // Nothing to compact. The space is either empty or all live object should be left in place.
422     clear_empty_region(this);
423     return;
424   }
425 
426   const intx scan_interval = PrefetchScanIntervalInBytes;
427   const intx copy_interval = PrefetchCopyIntervalInBytes;
428 
429   assert(start < end_of_live, "bottom: " PTR_FORMAT " should be < end_of_live: " PTR_FORMAT, p2i(start), p2i(end_of_live));
430   HeapWord* cur_obj = start;
431   if (_first_dead > cur_obj && !cast_to_oop(cur_obj)->is_gc_marked()) {
432     // All object before _first_dead can be skipped. They should not be moved.
433     // A pointer to the first live object is stored at the memory location for _first_dead.
434     cur_obj = *(HeapWord**)(_first_dead);
435   }
436 
437   debug_only(HeapWord* prev_obj = nullptr);
438   while (cur_obj < end_of_live) {
439     if (GCForwarding::is_not_forwarded(cast_to_oop(cur_obj))) {
440       debug_only(prev_obj = cur_obj);
441       // The first word of the dead object contains a pointer to the next live object or end of space.
442       cur_obj = *(HeapWord**)cur_obj;
443       assert(cur_obj > prev_obj, "we should be moving forward through memory");
444     } else {
445       // prefetch beyond q
446       Prefetch::read(cur_obj, scan_interval);
447 
448       // size and destination
449       size_t size = cast_to_oop(cur_obj)->size();
450       HeapWord* compaction_top = cast_from_oop<HeapWord*>(GCForwarding::forwardee(cast_to_oop(cur_obj)));
451 
452       // prefetch beyond compaction_top
453       Prefetch::write(compaction_top, copy_interval);
454 
455       // copy object and reinit its mark
456       assert(cur_obj != compaction_top, "everything in this pass should be moving");
457       Copy::aligned_conjoint_words(cur_obj, compaction_top, size);
458       oop new_obj = cast_to_oop(compaction_top);
459 
460       ContinuationGCSupport::transform_stack_chunk(new_obj);
461 
462       new_obj->init_mark();
463       assert(new_obj->klass() != nullptr, "should have a class");
464 
465       debug_only(prev_obj = cur_obj);
466       cur_obj += size;
467     }
468   }
469 
470   clear_empty_region(this);
471 }
472 
473 #endif // INCLUDE_SERIALGC
474 
475 void Space::print_short() const { print_short_on(tty); }
476 
477 void Space::print_short_on(outputStream* st) const {
478   st->print(" space " SIZE_FORMAT "K, %3d%% used", capacity() / K,
479               (int) ((double) used() * 100 / capacity()));
480 }
481 
482 void Space::print() const { print_on(tty); }
483 
484 void Space::print_on(outputStream* st) const {
485   print_short_on(st);
486   st->print_cr(" [" PTR_FORMAT ", " PTR_FORMAT ")",
487                 p2i(bottom()), p2i(end()));
488 }
489 
490 void ContiguousSpace::print_on(outputStream* st) const {
491   print_short_on(st);
492   st->print_cr(" [" PTR_FORMAT ", " PTR_FORMAT ", " PTR_FORMAT ")",
493                 p2i(bottom()), p2i(top()), p2i(end()));
494 }
495 
496 #if INCLUDE_SERIALGC
497 void TenuredSpace::print_on(outputStream* st) const {
498   print_short_on(st);
499   st->print_cr(" [" PTR_FORMAT ", " PTR_FORMAT ", "
500                 PTR_FORMAT ", " PTR_FORMAT ")",
501               p2i(bottom()), p2i(top()), p2i(_offsets.threshold()), p2i(end()));
502 }
503 #endif
504 
505 void ContiguousSpace::verify() const {
506   HeapWord* p = bottom();
507   HeapWord* t = top();
508   HeapWord* prev_p = nullptr;
509   while (p < t) {
510     oopDesc::verify(cast_to_oop(p));
511     prev_p = p;
512     p += cast_to_oop(p)->size();
513   }
514   guarantee(p == top(), "end of last object must match end of space");
515   if (top() != end()) {
516     guarantee(top() == block_start_const(end()-1) &&
517               top() == block_start_const(top()),
518               "top should be start of unallocated block, if it exists");
519   }
520 }
521 
522 void Space::oop_iterate(OopIterateClosure* blk) {
523   ObjectToOopClosure blk2(blk);
524   object_iterate(&blk2);
525 }
526 
527 bool Space::obj_is_alive(const HeapWord* p) const {
528   assert (block_is_obj(p), "The address should point to an object");
529   return true;
530 }
531 
532 void ContiguousSpace::oop_iterate(OopIterateClosure* blk) {
533   if (is_empty()) return;
534   HeapWord* obj_addr = bottom();
535   HeapWord* t = top();
536   // Could call objects iterate, but this is easier.
537   while (obj_addr < t) {
538     obj_addr += cast_to_oop(obj_addr)->oop_iterate_size(blk);
539   }
540 }
541 
542 void ContiguousSpace::object_iterate(ObjectClosure* blk) {
543   if (is_empty()) return;
544   object_iterate_from(bottom(), blk);
545 }
546 
547 void ContiguousSpace::object_iterate_from(HeapWord* mark, ObjectClosure* blk) {
548   while (mark < top()) {
549     blk->do_object(cast_to_oop(mark));
550     mark += cast_to_oop(mark)->size();
551   }
552 }
553 
554 // Very general, slow implementation.
555 HeapWord* ContiguousSpace::block_start_const(const void* p) const {
556   assert(MemRegion(bottom(), end()).contains(p),
557          "p (" PTR_FORMAT ") not in space [" PTR_FORMAT ", " PTR_FORMAT ")",
558          p2i(p), p2i(bottom()), p2i(end()));
559   if (p >= top()) {
560     return top();
561   } else {
562     HeapWord* last = bottom();
563     HeapWord* cur = last;
564     while (cur <= p) {
565       last = cur;
566       cur += cast_to_oop(cur)->size();
567     }
568     assert(oopDesc::is_oop(cast_to_oop(last)), PTR_FORMAT " should be an object start", p2i(last));
569     return last;
570   }
571 }
572 
573 size_t ContiguousSpace::block_size(const HeapWord* p) const {
574   assert(MemRegion(bottom(), end()).contains(p),
575          "p (" PTR_FORMAT ") not in space [" PTR_FORMAT ", " PTR_FORMAT ")",
576          p2i(p), p2i(bottom()), p2i(end()));
577   HeapWord* current_top = top();
578   assert(p <= current_top,
579          "p > current top - p: " PTR_FORMAT ", current top: " PTR_FORMAT,
580          p2i(p), p2i(current_top));
581   assert(p == current_top || oopDesc::is_oop(cast_to_oop(p)),
582          "p (" PTR_FORMAT ") is not a block start - "
583          "current_top: " PTR_FORMAT ", is_oop: %s",
584          p2i(p), p2i(current_top), BOOL_TO_STR(oopDesc::is_oop(cast_to_oop(p))));
585   if (p < current_top) {
586     return cast_to_oop(p)->size();
587   } else {
588     assert(p == current_top, "just checking");
589     return pointer_delta(end(), (HeapWord*) p);
590   }
591 }
592 
593 // This version requires locking.
594 inline HeapWord* ContiguousSpace::allocate_impl(size_t size) {
595   assert(Heap_lock->owned_by_self() ||
596          (SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread()),
597          "not locked");
598   HeapWord* obj = top();
599   if (pointer_delta(end(), obj) >= size) {
600     HeapWord* new_top = obj + size;
601     set_top(new_top);
602     assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
603     return obj;
604   } else {
605     return nullptr;
606   }
607 }
608 
609 // This version is lock-free.
610 inline HeapWord* ContiguousSpace::par_allocate_impl(size_t size) {
611   do {
612     HeapWord* obj = top();
613     if (pointer_delta(end(), obj) >= size) {
614       HeapWord* new_top = obj + size;
615       HeapWord* result = Atomic::cmpxchg(top_addr(), obj, new_top);
616       // result can be one of two:
617       //  the old top value: the exchange succeeded
618       //  otherwise: the new value of the top is returned.
619       if (result == obj) {
620         assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
621         return obj;
622       }
623     } else {
624       return nullptr;
625     }
626   } while (true);
627 }
628 
629 // Requires locking.
630 HeapWord* ContiguousSpace::allocate(size_t size) {
631   return allocate_impl(size);
632 }
633 
634 // Lock-free.
635 HeapWord* ContiguousSpace::par_allocate(size_t size) {
636   return par_allocate_impl(size);
637 }
638 
639 #if INCLUDE_SERIALGC
640 void TenuredSpace::initialize_threshold() {
641   _offsets.initialize_threshold();
642 }
643 
644 void TenuredSpace::alloc_block(HeapWord* start, HeapWord* end) {
645   _offsets.alloc_block(start, end);
646 }
647 
648 TenuredSpace::TenuredSpace(BlockOffsetSharedArray* sharedOffsetArray,
649                            MemRegion mr) :
650   _offsets(sharedOffsetArray, mr),
651   _par_alloc_lock(Mutex::safepoint, "TenuredSpaceParAlloc_lock", true)
652 {
653   _offsets.set_contig_space(this);
654   initialize(mr, SpaceDecorator::Clear, SpaceDecorator::Mangle);
655 }
656 
657 #define OBJ_SAMPLE_INTERVAL 0
658 #define BLOCK_SAMPLE_INTERVAL 100
659 
660 void TenuredSpace::verify() const {
661   HeapWord* p = bottom();
662   HeapWord* prev_p = nullptr;
663   int objs = 0;
664   int blocks = 0;
665 
666   if (VerifyObjectStartArray) {
667     _offsets.verify();
668   }
669 
670   while (p < top()) {
671     size_t size = cast_to_oop(p)->size();
672     // For a sampling of objects in the space, find it using the
673     // block offset table.
674     if (blocks == BLOCK_SAMPLE_INTERVAL) {
675       guarantee(p == block_start_const(p + (size/2)),
676                 "check offset computation");
677       blocks = 0;
678     } else {
679       blocks++;
680     }
681 
682     if (objs == OBJ_SAMPLE_INTERVAL) {
683       oopDesc::verify(cast_to_oop(p));
684       objs = 0;
685     } else {
686       objs++;
687     }
688     prev_p = p;
689     p += size;
690   }
691   guarantee(p == top(), "end of last object must match end of space");
692 }
693 
694 
695 size_t TenuredSpace::allowed_dead_ratio() const {
696   return MarkSweepDeadRatio;
697 }
698 #endif // INCLUDE_SERIALGC