1 /*
   2  * Copyright (c) 2010, 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 #ifndef SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_PROMOTIONINFO_INLINE_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_PROMOTIONINFO_INLINE_HPP
  27 
  28 #include "gc_implementation/concurrentMarkSweep/promotionInfo.hpp"
  29 #include "oops/oop.inline.hpp"
  30 
  31 //////////////////////////////////////////////////////////////////////////////
  32 // We go over the list of promoted objects, removing each from the list,
  33 // and applying the closure (this may, in turn, add more elements to
  34 // the tail of the promoted list, and these newly added objects will
  35 // also be processed) until the list is empty.
  36 // To aid verification and debugging, in the non-product builds
  37 // we actually forward _promoHead each time we process a promoted oop.
  38 // Note that this is not necessary in general (i.e. when we don't need to
  39 // call PromotionInfo::verify()) because oop_iterate can only add to the
  40 // end of _promoTail, and never needs to look at _promoHead.
  41 
  42 // Return the next displaced header, incrementing the pointer and
  43 // recycling spool area as necessary.
  44 inline markOop PromotionInfo::nextDisplacedHeader() {
  45   assert(_spoolHead != NULL, "promotionInfo inconsistency");
  46   assert(_spoolHead != _spoolTail || _firstIndex < _nextIndex,
  47          "Empty spool space: no displaced header can be fetched");
  48   assert(_spoolHead->bufferSize > _firstIndex, "Off by one error at head?");
  49   markOop hdr = _spoolHead->displacedHdr[_firstIndex];
  50   // Spool forward
  51   if (++_firstIndex == _spoolHead->bufferSize) { // last location in this block
  52     // forward to next block, recycling this block into spare spool buffer
  53     SpoolBlock* tmp = _spoolHead->nextSpoolBlock;
  54     assert(_spoolHead != _spoolTail, "Spooling storage mix-up");
  55     _spoolHead->nextSpoolBlock = _spareSpool;
  56     _spareSpool = _spoolHead;
  57     _spoolHead = tmp;
  58     _firstIndex = 1;
  59     NOT_PRODUCT(
  60       if (_spoolHead == NULL) {  // all buffers fully consumed
  61         assert(_spoolTail == NULL && _nextIndex == 1,
  62                "spool buffers processing inconsistency");
  63       }
  64     )
  65   }
  66   return hdr;
  67 }
  68 
  69 template <bool nv, typename OopClosureType>
  70 void PromotionInfo::promoted_oops_iterate(OopClosureType* cl) {
  71   NOT_PRODUCT(verify());
  72   PromotedObject *curObj, *nextObj;
  73   for (curObj = _promoHead; curObj != NULL; curObj = nextObj) {
  74     if ((nextObj = curObj->next()) == NULL) {
  75       /* protect ourselves against additions due to closure application
  76          below by resetting the list.  */
  77       assert(_promoTail == curObj, "Should have been the tail");
  78       _promoHead = _promoTail = NULL;
  79     }
  80     if (curObj->hasDisplacedMark()) {
  81       /* restore displaced header */
  82       oop(curObj)->set_mark(nextDisplacedHeader());
  83     } else {
  84       /* restore prototypical header */
  85       oop(curObj)->init_mark();
  86     }
  87     /* The "promoted_mark" should now not be set */
  88     assert(!curObj->hasPromotedMark(),
  89            "Should have been cleared by restoring displaced mark-word");
  90     NOT_PRODUCT(_promoHead = nextObj);
  91     if (cl != NULL) oop(curObj)->oop_iterate<nv>(cl);
  92     if (nextObj == NULL) { /* start at head of list reset above */
  93       nextObj = _promoHead;
  94     }
  95   }
  96   assert(noPromotions(), "post-condition violation");
  97   assert(_promoHead == NULL && _promoTail == NULL, "emptied promoted list");
  98   assert(_spoolHead == _spoolTail, "emptied spooling buffers");
  99   assert(_firstIndex == _nextIndex, "empty buffer");
 100 }
 101 
 102 #endif // SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_PROMOTIONINFO_INLINE_HPP