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