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