src/share/vm/memory/space.cpp

Wed, 12 Mar 2014 17:13:48 +0100

author
mgerdin
date
Wed, 12 Mar 2014 17:13:48 +0100
changeset 6980
6c523f5d5440
parent 6979
5255b195f828
child 6981
ff1e37e7eb83
permissions
-rw-r--r--

8038412: Move object_iterate_careful down from Space to ContigousSpace and CFLSpace
Summary: Only declare the functions where they are actually needed.
Reviewed-by: tschatzl, stefank

     1 /*
     2  * Copyright (c) 1997, 2014, 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  */
    25 #include "precompiled.hpp"
    26 #include "classfile/systemDictionary.hpp"
    27 #include "classfile/vmSymbols.hpp"
    28 #include "gc_implementation/shared/liveRange.hpp"
    29 #include "gc_implementation/shared/markSweep.hpp"
    30 #include "gc_implementation/shared/spaceDecorator.hpp"
    31 #include "memory/blockOffsetTable.inline.hpp"
    32 #include "memory/defNewGeneration.hpp"
    33 #include "memory/genCollectedHeap.hpp"
    34 #include "memory/space.hpp"
    35 #include "memory/space.inline.hpp"
    36 #include "memory/universe.inline.hpp"
    37 #include "oops/oop.inline.hpp"
    38 #include "oops/oop.inline2.hpp"
    39 #include "runtime/java.hpp"
    40 #include "runtime/prefetch.inline.hpp"
    41 #include "runtime/orderAccess.inline.hpp"
    42 #include "runtime/safepoint.hpp"
    43 #include "utilities/copy.hpp"
    44 #include "utilities/globalDefinitions.hpp"
    45 #include "utilities/macros.hpp"
    47 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
    49 HeapWord* DirtyCardToOopClosure::get_actual_top(HeapWord* top,
    50                                                 HeapWord* top_obj) {
    51   if (top_obj != NULL) {
    52     if (_sp->block_is_obj(top_obj)) {
    53       if (_precision == CardTableModRefBS::ObjHeadPreciseArray) {
    54         if (oop(top_obj)->is_objArray() || oop(top_obj)->is_typeArray()) {
    55           // An arrayOop is starting on the dirty card - since we do exact
    56           // store checks for objArrays we are done.
    57         } else {
    58           // Otherwise, it is possible that the object starting on the dirty
    59           // card spans the entire card, and that the store happened on a
    60           // later card.  Figure out where the object ends.
    61           // Use the block_size() method of the space over which
    62           // the iteration is being done.  That space (e.g. CMS) may have
    63           // specific requirements on object sizes which will
    64           // be reflected in the block_size() method.
    65           top = top_obj + oop(top_obj)->size();
    66         }
    67       }
    68     } else {
    69       top = top_obj;
    70     }
    71   } else {
    72     assert(top == _sp->end(), "only case where top_obj == NULL");
    73   }
    74   return top;
    75 }
    77 void DirtyCardToOopClosure::walk_mem_region(MemRegion mr,
    78                                             HeapWord* bottom,
    79                                             HeapWord* top) {
    80   // 1. Blocks may or may not be objects.
    81   // 2. Even when a block_is_obj(), it may not entirely
    82   //    occupy the block if the block quantum is larger than
    83   //    the object size.
    84   // We can and should try to optimize by calling the non-MemRegion
    85   // version of oop_iterate() for all but the extremal objects
    86   // (for which we need to call the MemRegion version of
    87   // oop_iterate()) To be done post-beta XXX
    88   for (; bottom < top; bottom += _sp->block_size(bottom)) {
    89     // As in the case of contiguous space above, we'd like to
    90     // just use the value returned by oop_iterate to increment the
    91     // current pointer; unfortunately, that won't work in CMS because
    92     // we'd need an interface change (it seems) to have the space
    93     // "adjust the object size" (for instance pad it up to its
    94     // block alignment or minimum block size restrictions. XXX
    95     if (_sp->block_is_obj(bottom) &&
    96         !_sp->obj_allocated_since_save_marks(oop(bottom))) {
    97       oop(bottom)->oop_iterate(_cl, mr);
    98     }
    99   }
   100 }
   102 // We get called with "mr" representing the dirty region
   103 // that we want to process. Because of imprecise marking,
   104 // we may need to extend the incoming "mr" to the right,
   105 // and scan more. However, because we may already have
   106 // scanned some of that extended region, we may need to
   107 // trim its right-end back some so we do not scan what
   108 // we (or another worker thread) may already have scanned
   109 // or planning to scan.
   110 void DirtyCardToOopClosure::do_MemRegion(MemRegion mr) {
   112   // Some collectors need to do special things whenever their dirty
   113   // cards are processed. For instance, CMS must remember mutator updates
   114   // (i.e. dirty cards) so as to re-scan mutated objects.
   115   // Such work can be piggy-backed here on dirty card scanning, so as to make
   116   // it slightly more efficient than doing a complete non-detructive pre-scan
   117   // of the card table.
   118   MemRegionClosure* pCl = _sp->preconsumptionDirtyCardClosure();
   119   if (pCl != NULL) {
   120     pCl->do_MemRegion(mr);
   121   }
   123   HeapWord* bottom = mr.start();
   124   HeapWord* last = mr.last();
   125   HeapWord* top = mr.end();
   126   HeapWord* bottom_obj;
   127   HeapWord* top_obj;
   129   assert(_precision == CardTableModRefBS::ObjHeadPreciseArray ||
   130          _precision == CardTableModRefBS::Precise,
   131          "Only ones we deal with for now.");
   133   assert(_precision != CardTableModRefBS::ObjHeadPreciseArray ||
   134          _cl->idempotent() || _last_bottom == NULL ||
   135          top <= _last_bottom,
   136          "Not decreasing");
   137   NOT_PRODUCT(_last_bottom = mr.start());
   139   bottom_obj = _sp->block_start(bottom);
   140   top_obj    = _sp->block_start(last);
   142   assert(bottom_obj <= bottom, "just checking");
   143   assert(top_obj    <= top,    "just checking");
   145   // Given what we think is the top of the memory region and
   146   // the start of the object at the top, get the actual
   147   // value of the top.
   148   top = get_actual_top(top, top_obj);
   150   // If the previous call did some part of this region, don't redo.
   151   if (_precision == CardTableModRefBS::ObjHeadPreciseArray &&
   152       _min_done != NULL &&
   153       _min_done < top) {
   154     top = _min_done;
   155   }
   157   // Top may have been reset, and in fact may be below bottom,
   158   // e.g. the dirty card region is entirely in a now free object
   159   // -- something that could happen with a concurrent sweeper.
   160   bottom = MIN2(bottom, top);
   161   MemRegion extended_mr = MemRegion(bottom, top);
   162   assert(bottom <= top &&
   163          (_precision != CardTableModRefBS::ObjHeadPreciseArray ||
   164           _min_done == NULL ||
   165           top <= _min_done),
   166          "overlap!");
   168   // Walk the region if it is not empty; otherwise there is nothing to do.
   169   if (!extended_mr.is_empty()) {
   170     walk_mem_region(extended_mr, bottom_obj, top);
   171   }
   173   // An idempotent closure might be applied in any order, so we don't
   174   // record a _min_done for it.
   175   if (!_cl->idempotent()) {
   176     _min_done = bottom;
   177   } else {
   178     assert(_min_done == _last_explicit_min_done,
   179            "Don't update _min_done for idempotent cl");
   180   }
   181 }
   183 DirtyCardToOopClosure* Space::new_dcto_cl(ExtendedOopClosure* cl,
   184                                           CardTableModRefBS::PrecisionStyle precision,
   185                                           HeapWord* boundary) {
   186   return new DirtyCardToOopClosure(this, cl, precision, boundary);
   187 }
   189 HeapWord* ContiguousSpaceDCTOC::get_actual_top(HeapWord* top,
   190                                                HeapWord* top_obj) {
   191   if (top_obj != NULL && top_obj < (_sp->toContiguousSpace())->top()) {
   192     if (_precision == CardTableModRefBS::ObjHeadPreciseArray) {
   193       if (oop(top_obj)->is_objArray() || oop(top_obj)->is_typeArray()) {
   194         // An arrayOop is starting on the dirty card - since we do exact
   195         // store checks for objArrays we are done.
   196       } else {
   197         // Otherwise, it is possible that the object starting on the dirty
   198         // card spans the entire card, and that the store happened on a
   199         // later card.  Figure out where the object ends.
   200         assert(_sp->block_size(top_obj) == (size_t) oop(top_obj)->size(),
   201           "Block size and object size mismatch");
   202         top = top_obj + oop(top_obj)->size();
   203       }
   204     }
   205   } else {
   206     top = (_sp->toContiguousSpace())->top();
   207   }
   208   return top;
   209 }
   211 void Filtering_DCTOC::walk_mem_region(MemRegion mr,
   212                                       HeapWord* bottom,
   213                                       HeapWord* top) {
   214   // Note that this assumption won't hold if we have a concurrent
   215   // collector in this space, which may have freed up objects after
   216   // they were dirtied and before the stop-the-world GC that is
   217   // examining cards here.
   218   assert(bottom < top, "ought to be at least one obj on a dirty card.");
   220   if (_boundary != NULL) {
   221     // We have a boundary outside of which we don't want to look
   222     // at objects, so create a filtering closure around the
   223     // oop closure before walking the region.
   224     FilteringClosure filter(_boundary, _cl);
   225     walk_mem_region_with_cl(mr, bottom, top, &filter);
   226   } else {
   227     // No boundary, simply walk the heap with the oop closure.
   228     walk_mem_region_with_cl(mr, bottom, top, _cl);
   229   }
   231 }
   233 // We must replicate this so that the static type of "FilteringClosure"
   234 // (see above) is apparent at the oop_iterate calls.
   235 #define ContiguousSpaceDCTOC__walk_mem_region_with_cl_DEFN(ClosureType) \
   236 void ContiguousSpaceDCTOC::walk_mem_region_with_cl(MemRegion mr,        \
   237                                                    HeapWord* bottom,    \
   238                                                    HeapWord* top,       \
   239                                                    ClosureType* cl) {   \
   240   bottom += oop(bottom)->oop_iterate(cl, mr);                           \
   241   if (bottom < top) {                                                   \
   242     HeapWord* next_obj = bottom + oop(bottom)->size();                  \
   243     while (next_obj < top) {                                            \
   244       /* Bottom lies entirely below top, so we can call the */          \
   245       /* non-memRegion version of oop_iterate below. */                 \
   246       oop(bottom)->oop_iterate(cl);                                     \
   247       bottom = next_obj;                                                \
   248       next_obj = bottom + oop(bottom)->size();                          \
   249     }                                                                   \
   250     /* Last object. */                                                  \
   251     oop(bottom)->oop_iterate(cl, mr);                                   \
   252   }                                                                     \
   253 }
   255 // (There are only two of these, rather than N, because the split is due
   256 // only to the introduction of the FilteringClosure, a local part of the
   257 // impl of this abstraction.)
   258 ContiguousSpaceDCTOC__walk_mem_region_with_cl_DEFN(ExtendedOopClosure)
   259 ContiguousSpaceDCTOC__walk_mem_region_with_cl_DEFN(FilteringClosure)
   261 DirtyCardToOopClosure*
   262 ContiguousSpace::new_dcto_cl(ExtendedOopClosure* cl,
   263                              CardTableModRefBS::PrecisionStyle precision,
   264                              HeapWord* boundary) {
   265   return new ContiguousSpaceDCTOC(this, cl, precision, boundary);
   266 }
   268 void Space::initialize(MemRegion mr,
   269                        bool clear_space,
   270                        bool mangle_space) {
   271   HeapWord* bottom = mr.start();
   272   HeapWord* end    = mr.end();
   273   assert(Universe::on_page_boundary(bottom) && Universe::on_page_boundary(end),
   274          "invalid space boundaries");
   275   set_bottom(bottom);
   276   set_end(end);
   277   if (clear_space) clear(mangle_space);
   278 }
   280 void Space::clear(bool mangle_space) {
   281   if (ZapUnusedHeapArea && mangle_space) {
   282     mangle_unused_area();
   283   }
   284 }
   286 ContiguousSpace::ContiguousSpace(): CompactibleSpace(), _top(NULL),
   287     _concurrent_iteration_safe_limit(NULL) {
   288   _mangler = new GenSpaceMangler(this);
   289 }
   291 ContiguousSpace::~ContiguousSpace() {
   292   delete _mangler;
   293 }
   295 void ContiguousSpace::initialize(MemRegion mr,
   296                                  bool clear_space,
   297                                  bool mangle_space)
   298 {
   299   CompactibleSpace::initialize(mr, clear_space, mangle_space);
   300   set_concurrent_iteration_safe_limit(top());
   301 }
   303 void ContiguousSpace::clear(bool mangle_space) {
   304   set_top(bottom());
   305   set_saved_mark();
   306   CompactibleSpace::clear(mangle_space);
   307 }
   309 bool ContiguousSpace::is_in(const void* p) const {
   310   return _bottom <= p && p < _top;
   311 }
   313 bool ContiguousSpace::is_free_block(const HeapWord* p) const {
   314   return p >= _top;
   315 }
   317 void OffsetTableContigSpace::clear(bool mangle_space) {
   318   ContiguousSpace::clear(mangle_space);
   319   _offsets.initialize_threshold();
   320 }
   322 void OffsetTableContigSpace::set_bottom(HeapWord* new_bottom) {
   323   Space::set_bottom(new_bottom);
   324   _offsets.set_bottom(new_bottom);
   325 }
   327 void OffsetTableContigSpace::set_end(HeapWord* new_end) {
   328   // Space should not advertize an increase in size
   329   // until after the underlying offest table has been enlarged.
   330   _offsets.resize(pointer_delta(new_end, bottom()));
   331   Space::set_end(new_end);
   332 }
   334 #ifndef PRODUCT
   336 void ContiguousSpace::set_top_for_allocations(HeapWord* v) {
   337   mangler()->set_top_for_allocations(v);
   338 }
   339 void ContiguousSpace::set_top_for_allocations() {
   340   mangler()->set_top_for_allocations(top());
   341 }
   342 void ContiguousSpace::check_mangled_unused_area(HeapWord* limit) {
   343   mangler()->check_mangled_unused_area(limit);
   344 }
   346 void ContiguousSpace::check_mangled_unused_area_complete() {
   347   mangler()->check_mangled_unused_area_complete();
   348 }
   350 // Mangled only the unused space that has not previously
   351 // been mangled and that has not been allocated since being
   352 // mangled.
   353 void ContiguousSpace::mangle_unused_area() {
   354   mangler()->mangle_unused_area();
   355 }
   356 void ContiguousSpace::mangle_unused_area_complete() {
   357   mangler()->mangle_unused_area_complete();
   358 }
   359 void ContiguousSpace::mangle_region(MemRegion mr) {
   360   // Although this method uses SpaceMangler::mangle_region() which
   361   // is not specific to a space, the when the ContiguousSpace version
   362   // is called, it is always with regard to a space and this
   363   // bounds checking is appropriate.
   364   MemRegion space_mr(bottom(), end());
   365   assert(space_mr.contains(mr), "Mangling outside space");
   366   SpaceMangler::mangle_region(mr);
   367 }
   368 #endif  // NOT_PRODUCT
   370 void CompactibleSpace::initialize(MemRegion mr,
   371                                   bool clear_space,
   372                                   bool mangle_space) {
   373   Space::initialize(mr, clear_space, mangle_space);
   374   set_compaction_top(bottom());
   375   _next_compaction_space = NULL;
   376 }
   378 void CompactibleSpace::clear(bool mangle_space) {
   379   Space::clear(mangle_space);
   380   _compaction_top = bottom();
   381 }
   383 HeapWord* CompactibleSpace::forward(oop q, size_t size,
   384                                     CompactPoint* cp, HeapWord* compact_top) {
   385   // q is alive
   386   // First check if we should switch compaction space
   387   assert(this == cp->space, "'this' should be current compaction space.");
   388   size_t compaction_max_size = pointer_delta(end(), compact_top);
   389   while (size > compaction_max_size) {
   390     // switch to next compaction space
   391     cp->space->set_compaction_top(compact_top);
   392     cp->space = cp->space->next_compaction_space();
   393     if (cp->space == NULL) {
   394       cp->gen = GenCollectedHeap::heap()->prev_gen(cp->gen);
   395       assert(cp->gen != NULL, "compaction must succeed");
   396       cp->space = cp->gen->first_compaction_space();
   397       assert(cp->space != NULL, "generation must have a first compaction space");
   398     }
   399     compact_top = cp->space->bottom();
   400     cp->space->set_compaction_top(compact_top);
   401     cp->threshold = cp->space->initialize_threshold();
   402     compaction_max_size = pointer_delta(cp->space->end(), compact_top);
   403   }
   405   // store the forwarding pointer into the mark word
   406   if ((HeapWord*)q != compact_top) {
   407     q->forward_to(oop(compact_top));
   408     assert(q->is_gc_marked(), "encoding the pointer should preserve the mark");
   409   } else {
   410     // if the object isn't moving we can just set the mark to the default
   411     // mark and handle it specially later on.
   412     q->init_mark();
   413     assert(q->forwardee() == NULL, "should be forwarded to NULL");
   414   }
   416   compact_top += size;
   418   // we need to update the offset table so that the beginnings of objects can be
   419   // found during scavenge.  Note that we are updating the offset table based on
   420   // where the object will be once the compaction phase finishes.
   421   if (compact_top > cp->threshold)
   422     cp->threshold =
   423       cp->space->cross_threshold(compact_top - size, compact_top);
   424   return compact_top;
   425 }
   428 bool CompactibleSpace::insert_deadspace(size_t& allowed_deadspace_words,
   429                                         HeapWord* q, size_t deadlength) {
   430   if (allowed_deadspace_words >= deadlength) {
   431     allowed_deadspace_words -= deadlength;
   432     CollectedHeap::fill_with_object(q, deadlength);
   433     oop(q)->set_mark(oop(q)->mark()->set_marked());
   434     assert((int) deadlength == oop(q)->size(), "bad filler object size");
   435     // Recall that we required "q == compaction_top".
   436     return true;
   437   } else {
   438     allowed_deadspace_words = 0;
   439     return false;
   440   }
   441 }
   443 #define block_is_always_obj(q) true
   444 #define obj_size(q) oop(q)->size()
   445 #define adjust_obj_size(s) s
   447 void CompactibleSpace::prepare_for_compaction(CompactPoint* cp) {
   448   SCAN_AND_FORWARD(cp, end, block_is_obj, block_size);
   449 }
   451 // Faster object search.
   452 void ContiguousSpace::prepare_for_compaction(CompactPoint* cp) {
   453   SCAN_AND_FORWARD(cp, top, block_is_always_obj, obj_size);
   454 }
   456 void Space::adjust_pointers() {
   457   // adjust all the interior pointers to point at the new locations of objects
   458   // Used by MarkSweep::mark_sweep_phase3()
   460   // First check to see if there is any work to be done.
   461   if (used() == 0) {
   462     return;  // Nothing to do.
   463   }
   465   // Otherwise...
   466   HeapWord* q = bottom();
   467   HeapWord* t = end();
   469   debug_only(HeapWord* prev_q = NULL);
   470   while (q < t) {
   471     if (oop(q)->is_gc_marked()) {
   472       // q is alive
   474       // point all the oops to the new location
   475       size_t size = oop(q)->adjust_pointers();
   477       debug_only(prev_q = q);
   479       q += size;
   480     } else {
   481       // q is not a live object.  But we're not in a compactible space,
   482       // So we don't have live ranges.
   483       debug_only(prev_q = q);
   484       q += block_size(q);
   485       assert(q > prev_q, "we should be moving forward through memory");
   486     }
   487   }
   488   assert(q == t, "just checking");
   489 }
   491 void CompactibleSpace::adjust_pointers() {
   492   // Check first is there is any work to do.
   493   if (used() == 0) {
   494     return;   // Nothing to do.
   495   }
   497   SCAN_AND_ADJUST_POINTERS(adjust_obj_size);
   498 }
   500 void CompactibleSpace::compact() {
   501   SCAN_AND_COMPACT(obj_size);
   502 }
   504 void Space::print_short() const { print_short_on(tty); }
   506 void Space::print_short_on(outputStream* st) const {
   507   st->print(" space " SIZE_FORMAT "K, %3d%% used", capacity() / K,
   508               (int) ((double) used() * 100 / capacity()));
   509 }
   511 void Space::print() const { print_on(tty); }
   513 void Space::print_on(outputStream* st) const {
   514   print_short_on(st);
   515   st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ")",
   516                 bottom(), end());
   517 }
   519 void ContiguousSpace::print_on(outputStream* st) const {
   520   print_short_on(st);
   521   st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")",
   522                 bottom(), top(), end());
   523 }
   525 void OffsetTableContigSpace::print_on(outputStream* st) const {
   526   print_short_on(st);
   527   st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", "
   528                 INTPTR_FORMAT ", " INTPTR_FORMAT ")",
   529               bottom(), top(), _offsets.threshold(), end());
   530 }
   532 void ContiguousSpace::verify() const {
   533   HeapWord* p = bottom();
   534   HeapWord* t = top();
   535   HeapWord* prev_p = NULL;
   536   while (p < t) {
   537     oop(p)->verify();
   538     prev_p = p;
   539     p += 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 }
   549 void Space::oop_iterate(ExtendedOopClosure* blk) {
   550   ObjectToOopClosure blk2(blk);
   551   object_iterate(&blk2);
   552 }
   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 }
   559 #if INCLUDE_ALL_GCS
   560 #define ContigSpace_PAR_OOP_ITERATE_DEFN(OopClosureType, nv_suffix)         \
   561                                                                             \
   562   void ContiguousSpace::par_oop_iterate(MemRegion mr, OopClosureType* blk) {\
   563     HeapWord* obj_addr = mr.start();                                        \
   564     HeapWord* t = mr.end();                                                 \
   565     while (obj_addr < t) {                                                  \
   566       assert(oop(obj_addr)->is_oop(), "Should be an oop");                  \
   567       obj_addr += oop(obj_addr)->oop_iterate(blk);                          \
   568     }                                                                       \
   569   }
   571   ALL_PAR_OOP_ITERATE_CLOSURES(ContigSpace_PAR_OOP_ITERATE_DEFN)
   573 #undef ContigSpace_PAR_OOP_ITERATE_DEFN
   574 #endif // INCLUDE_ALL_GCS
   576 void ContiguousSpace::oop_iterate(ExtendedOopClosure* blk) {
   577   if (is_empty()) return;
   578   HeapWord* obj_addr = bottom();
   579   HeapWord* t = top();
   580   // Could call objects iterate, but this is easier.
   581   while (obj_addr < t) {
   582     obj_addr += oop(obj_addr)->oop_iterate(blk);
   583   }
   584 }
   586 void ContiguousSpace::object_iterate(ObjectClosure* blk) {
   587   if (is_empty()) return;
   588   WaterMark bm = bottom_mark();
   589   object_iterate_from(bm, blk);
   590 }
   592 // For a continguous space object_iterate() and safe_object_iterate()
   593 // are the same.
   594 void ContiguousSpace::safe_object_iterate(ObjectClosure* blk) {
   595   object_iterate(blk);
   596 }
   598 void ContiguousSpace::object_iterate_from(WaterMark mark, ObjectClosure* blk) {
   599   assert(mark.space() == this, "Mark does not match space");
   600   HeapWord* p = mark.point();
   601   while (p < top()) {
   602     blk->do_object(oop(p));
   603     p += oop(p)->size();
   604   }
   605 }
   607 HeapWord*
   608 ContiguousSpace::object_iterate_careful(ObjectClosureCareful* blk) {
   609   HeapWord * limit = concurrent_iteration_safe_limit();
   610   assert(limit <= top(), "sanity check");
   611   for (HeapWord* p = bottom(); p < limit;) {
   612     size_t size = blk->do_object_careful(oop(p));
   613     if (size == 0) {
   614       return p;  // failed at p
   615     } else {
   616       p += size;
   617     }
   618   }
   619   return NULL; // all done
   620 }
   622 #define ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN(OopClosureType, nv_suffix)  \
   623                                                                           \
   624 void ContiguousSpace::                                                    \
   625 oop_since_save_marks_iterate##nv_suffix(OopClosureType* blk) {            \
   626   HeapWord* t;                                                            \
   627   HeapWord* p = saved_mark_word();                                        \
   628   assert(p != NULL, "expected saved mark");                               \
   629                                                                           \
   630   const intx interval = PrefetchScanIntervalInBytes;                      \
   631   do {                                                                    \
   632     t = top();                                                            \
   633     while (p < t) {                                                       \
   634       Prefetch::write(p, interval);                                       \
   635       debug_only(HeapWord* prev = p);                                     \
   636       oop m = oop(p);                                                     \
   637       p += m->oop_iterate(blk);                                           \
   638     }                                                                     \
   639   } while (t < top());                                                    \
   640                                                                           \
   641   set_saved_mark_word(p);                                                 \
   642 }
   644 ALL_SINCE_SAVE_MARKS_CLOSURES(ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN)
   646 #undef ContigSpace_OOP_SINCE_SAVE_MARKS_DEFN
   648 // Very general, slow implementation.
   649 HeapWord* ContiguousSpace::block_start_const(const void* p) const {
   650   assert(MemRegion(bottom(), end()).contains(p),
   651          err_msg("p (" PTR_FORMAT ") not in space [" PTR_FORMAT ", " PTR_FORMAT ")",
   652                   p, bottom(), end()));
   653   if (p >= top()) {
   654     return top();
   655   } else {
   656     HeapWord* last = bottom();
   657     HeapWord* cur = last;
   658     while (cur <= p) {
   659       last = cur;
   660       cur += oop(cur)->size();
   661     }
   662     assert(oop(last)->is_oop(),
   663            err_msg(PTR_FORMAT " should be an object start", last));
   664     return last;
   665   }
   666 }
   668 size_t ContiguousSpace::block_size(const HeapWord* p) const {
   669   assert(MemRegion(bottom(), end()).contains(p),
   670          err_msg("p (" PTR_FORMAT ") not in space [" PTR_FORMAT ", " PTR_FORMAT ")",
   671                   p, bottom(), end()));
   672   HeapWord* current_top = top();
   673   assert(p <= current_top,
   674          err_msg("p > current top - p: " PTR_FORMAT ", current top: " PTR_FORMAT,
   675                   p, current_top));
   676   assert(p == current_top || oop(p)->is_oop(),
   677          err_msg("p (" PTR_FORMAT ") is not a block start - "
   678                  "current_top: " PTR_FORMAT ", is_oop: %s",
   679                  p, current_top, BOOL_TO_STR(oop(p)->is_oop())));
   680   if (p < current_top) {
   681     return oop(p)->size();
   682   } else {
   683     assert(p == current_top, "just checking");
   684     return pointer_delta(end(), (HeapWord*) p);
   685   }
   686 }
   688 // This version requires locking.
   689 inline HeapWord* ContiguousSpace::allocate_impl(size_t size,
   690                                                 HeapWord* const end_value) {
   691   // In G1 there are places where a GC worker can allocates into a
   692   // region using this serial allocation code without being prone to a
   693   // race with other GC workers (we ensure that no other GC worker can
   694   // access the same region at the same time). So the assert below is
   695   // too strong in the case of G1.
   696   assert(Heap_lock->owned_by_self() ||
   697          (SafepointSynchronize::is_at_safepoint() &&
   698                                (Thread::current()->is_VM_thread() || UseG1GC)),
   699          "not locked");
   700   HeapWord* obj = top();
   701   if (pointer_delta(end_value, 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 }
   711 // This version is lock-free.
   712 inline HeapWord* ContiguousSpace::par_allocate_impl(size_t size,
   713                                                     HeapWord* const end_value) {
   714   do {
   715     HeapWord* obj = top();
   716     if (pointer_delta(end_value, obj) >= size) {
   717       HeapWord* new_top = obj + size;
   718       HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj);
   719       // result can be one of two:
   720       //  the old top value: the exchange succeeded
   721       //  otherwise: the new value of the top is returned.
   722       if (result == obj) {
   723         assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
   724         return obj;
   725       }
   726     } else {
   727       return NULL;
   728     }
   729   } while (true);
   730 }
   732 // Requires locking.
   733 HeapWord* ContiguousSpace::allocate(size_t size) {
   734   return allocate_impl(size, end());
   735 }
   737 // Lock-free.
   738 HeapWord* ContiguousSpace::par_allocate(size_t size) {
   739   return par_allocate_impl(size, end());
   740 }
   742 void ContiguousSpace::allocate_temporary_filler(int factor) {
   743   // allocate temporary type array decreasing free size with factor 'factor'
   744   assert(factor >= 0, "just checking");
   745   size_t size = pointer_delta(end(), top());
   747   // if space is full, return
   748   if (size == 0) return;
   750   if (factor > 0) {
   751     size -= size/factor;
   752   }
   753   size = align_object_size(size);
   755   const size_t array_header_size = typeArrayOopDesc::header_size(T_INT);
   756   if (size >= (size_t)align_object_size(array_header_size)) {
   757     size_t length = (size - array_header_size) * (HeapWordSize / sizeof(jint));
   758     // allocate uninitialized int array
   759     typeArrayOop t = (typeArrayOop) allocate(size);
   760     assert(t != NULL, "allocation should succeed");
   761     t->set_mark(markOopDesc::prototype());
   762     t->set_klass(Universe::intArrayKlassObj());
   763     t->set_length((int)length);
   764   } else {
   765     assert(size == CollectedHeap::min_fill_size(),
   766            "size for smallest fake object doesn't match");
   767     instanceOop obj = (instanceOop) allocate(size);
   768     obj->set_mark(markOopDesc::prototype());
   769     obj->set_klass_gap(0);
   770     obj->set_klass(SystemDictionary::Object_klass());
   771   }
   772 }
   774 void EdenSpace::clear(bool mangle_space) {
   775   ContiguousSpace::clear(mangle_space);
   776   set_soft_end(end());
   777 }
   779 // Requires locking.
   780 HeapWord* EdenSpace::allocate(size_t size) {
   781   return allocate_impl(size, soft_end());
   782 }
   784 // Lock-free.
   785 HeapWord* EdenSpace::par_allocate(size_t size) {
   786   return par_allocate_impl(size, soft_end());
   787 }
   789 HeapWord* ConcEdenSpace::par_allocate(size_t size)
   790 {
   791   do {
   792     // The invariant is top() should be read before end() because
   793     // top() can't be greater than end(), so if an update of _soft_end
   794     // occurs between 'end_val = end();' and 'top_val = top();' top()
   795     // also can grow up to the new end() and the condition
   796     // 'top_val > end_val' is true. To ensure the loading order
   797     // OrderAccess::loadload() is required after top() read.
   798     HeapWord* obj = top();
   799     OrderAccess::loadload();
   800     if (pointer_delta(*soft_end_addr(), obj) >= size) {
   801       HeapWord* new_top = obj + size;
   802       HeapWord* result = (HeapWord*)Atomic::cmpxchg_ptr(new_top, top_addr(), obj);
   803       // result can be one of two:
   804       //  the old top value: the exchange succeeded
   805       //  otherwise: the new value of the top is returned.
   806       if (result == obj) {
   807         assert(is_aligned(obj) && is_aligned(new_top), "checking alignment");
   808         return obj;
   809       }
   810     } else {
   811       return NULL;
   812     }
   813   } while (true);
   814 }
   817 HeapWord* OffsetTableContigSpace::initialize_threshold() {
   818   return _offsets.initialize_threshold();
   819 }
   821 HeapWord* OffsetTableContigSpace::cross_threshold(HeapWord* start, HeapWord* end) {
   822   _offsets.alloc_block(start, end);
   823   return _offsets.threshold();
   824 }
   826 OffsetTableContigSpace::OffsetTableContigSpace(BlockOffsetSharedArray* sharedOffsetArray,
   827                                                MemRegion mr) :
   828   _offsets(sharedOffsetArray, mr),
   829   _par_alloc_lock(Mutex::leaf, "OffsetTableContigSpace par alloc lock", true)
   830 {
   831   _offsets.set_contig_space(this);
   832   initialize(mr, SpaceDecorator::Clear, SpaceDecorator::Mangle);
   833 }
   835 #define OBJ_SAMPLE_INTERVAL 0
   836 #define BLOCK_SAMPLE_INTERVAL 100
   838 void OffsetTableContigSpace::verify() const {
   839   HeapWord* p = bottom();
   840   HeapWord* prev_p = NULL;
   841   int objs = 0;
   842   int blocks = 0;
   844   if (VerifyObjectStartArray) {
   845     _offsets.verify();
   846   }
   848   while (p < top()) {
   849     size_t size = oop(p)->size();
   850     // For a sampling of objects in the space, find it using the
   851     // block offset table.
   852     if (blocks == BLOCK_SAMPLE_INTERVAL) {
   853       guarantee(p == block_start_const(p + (size/2)),
   854                 "check offset computation");
   855       blocks = 0;
   856     } else {
   857       blocks++;
   858     }
   860     if (objs == OBJ_SAMPLE_INTERVAL) {
   861       oop(p)->verify();
   862       objs = 0;
   863     } else {
   864       objs++;
   865     }
   866     prev_p = p;
   867     p += size;
   868   }
   869   guarantee(p == top(), "end of last object must match end of space");
   870 }
   873 size_t TenuredSpace::allowed_dead_ratio() const {
   874   return MarkSweepDeadRatio;
   875 }

mercurial