1 /*
   2  * Copyright (c) 2016, 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_SHARED_PRESERVEDMARKS_HPP
  26 #define SHARE_VM_GC_SHARED_PRESERVEDMARKS_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "memory/padded.hpp"
  30 #include "oops/oop.hpp"
  31 #include "utilities/stack.hpp"
  32 
  33 #if INCLUDE_ALL_GCS
  34 class GCTaskManager;
  35 #endif
  36 class WorkGang;
  37 
  38 class PreservedMarks VALUE_OBJ_CLASS_SPEC {
  39 private:
  40   class OopAndMarkOop {
  41   private:
  42     oop _o;
  43     markOop _m;
  44 
  45   public:
  46     OopAndMarkOop(oop obj, markOop m) : _o(obj), _m(m) { }
  47 
  48     void set_mark() const {
  49       _o->set_mark(_m);
  50     }
  51   };
  52   typedef Stack<OopAndMarkOop, mtGC> OopAndMarkOopStack;
  53 
  54   OopAndMarkOopStack _stack;
  55 
  56   inline bool should_preserve_mark(oop obj, markOop m) const;
  57   inline void push(oop obj, markOop m);
  58 
  59 public:
  60   size_t size() const { return _stack.size(); }
  61   inline void push_if_necessary(oop obj, markOop m);
  62   // Iterate over the stack, restore all preserved marks, and
  63   // reclaim the memory taken up by the stack segments.
  64   void restore();
  65 
  66   inline static void init_forwarded_mark(oop obj);
  67 
  68   // Assert the stack is empty and has no cached segments.
  69   void assert_empty() PRODUCT_RETURN;
  70 
  71   inline PreservedMarks();
  72   ~PreservedMarks() { assert_empty(); }
  73 };
  74 
  75 class RemoveForwardedPointerClosure: public ObjectClosure {
  76 public:
  77   virtual void do_object(oop obj);
  78 };
  79 
  80 class PreservedMarksSet : public CHeapObj<mtGC> {
  81 private:
  82   // true -> _stacks will be allocated in the C heap
  83   // false -> _stacks will be allocated in the resource arena
  84   const bool _in_c_heap;
  85 
  86   // Number of stacks we have allocated (typically, one stack per GC worker).
  87   // This should be >= 1 if the stacks have been initialized,
  88   // or == 0 if they have not.
  89   uint _num;
  90 
  91   // Stack array (typically, one stack per GC worker) of length _num.
  92   // This should be != NULL if the stacks have been initialized,
  93   // or == NULL if they have not.
  94   Padded<PreservedMarks>* _stacks;
  95 
  96   // Internal version of restore() that uses a WorkGang for parallelism.
  97   void restore_internal(WorkGang* workers, volatile size_t* total_size_addr);
  98 
  99   #if INCLUDE_ALL_GCS
 100   // Internal version of restore() that uses a GCTaskManager for parallelism.
 101   void restore_internal(GCTaskManager* gc_task_manager,
 102                         volatile size_t* total_size_addr);
 103   #endif
 104 
 105 public:
 106   uint num() const { return _num; }
 107 
 108   // Return the i'th stack.
 109   PreservedMarks* get(uint i = 0) const {
 110     assert(_num > 0 && _stacks != NULL, "stacks should have been initialized");
 111     assert(i < _num, "pre-condition");
 112     return (_stacks + i);
 113   }
 114 
 115   // Allocate stack array.
 116   void init(uint num);
 117 
 118   // Itrerate over all stacks, restore all presered marks, and reclaim
 119   // the memory taken up by the stack segments. If the executor is
 120   // NULL, restoration will be done serially. If the executor is not
 121   // NULL, restoration could be done in parallel (when it makes
 122   // sense). Supported executors: WorkGang (Serial, CMS, G1),
 123   // GCTaskManager (PS).
 124   template <class E>
 125   inline void restore(E* executor);
 126 
 127   // Reclaim stack array.
 128   void reclaim();
 129 
 130   // Assert all the stacks are empty and have no cached segments.
 131   void assert_empty() PRODUCT_RETURN;
 132 
 133   PreservedMarksSet(bool in_c_heap)
 134       : _in_c_heap(in_c_heap), _num(0), _stacks(NULL) { }
 135 
 136   ~PreservedMarksSet() {
 137     assert(_stacks == NULL && _num == 0, "stacks should have been reclaimed");
 138   }
 139 };
 140 
 141 #endif // SHARE_VM_GC_SHARED_PRESERVEDMARKS_HPP