1 /*
   2  * Copyright (c) 2016, 2018, 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 class PreservedMarksSet;
  34 class WorkGang;
  35 
  36 class PreservedMarks {
  37 private:
  38   class OopAndMarkOop {
  39   private:
  40     oop _o;
  41     markOop _m;
  42 
  43   public:
  44     OopAndMarkOop(oop obj, markOop m) : _o(obj), _m(m) { }
  45 
  46     oop get_oop() { return _o; }
  47     inline void set_mark() const;
  48     void set_oop(oop obj) { _o = obj; }
  49   };
  50   typedef Stack<OopAndMarkOop, mtGC> OopAndMarkOopStack;
  51 
  52   OopAndMarkOopStack _stack;
  53 
  54   inline bool should_preserve_mark(oop obj, markOop m) const;
  55 
  56 public:
  57   size_t size() const { return _stack.size(); }
  58   inline void push(oop obj, markOop m);
  59   inline void push_if_necessary(oop obj, markOop m);
  60   // Iterate over the stack, restore all preserved marks, and
  61   // reclaim the memory taken up by the stack segments.
  62   void restore();
  63   // Iterate over the stack, adjust all preserved marks according
  64   // to their forwarding location stored in the mark.
  65   void adjust_during_full_gc();
  66 
  67   void restore_and_increment(volatile size_t* const _total_size_addr);
  68   inline static void init_forwarded_mark(oop obj);
  69 
  70   // Assert the stack is empty and has no cached segments.
  71   void assert_empty() PRODUCT_RETURN;
  72 
  73   inline PreservedMarks();
  74   ~PreservedMarks() { assert_empty(); }
  75 };
  76 
  77 class RemoveForwardedPointerClosure: public ObjectClosure {
  78 public:
  79   virtual void do_object(oop obj);
  80 };
  81 
  82 class RestorePreservedMarksTaskExecutor {
  83 public:
  84   void virtual restore(PreservedMarksSet* preserved_marks_set,
  85                        volatile size_t* total_size_addr) = 0;
  86 };
  87 
  88 class SharedRestorePreservedMarksTaskExecutor : public RestorePreservedMarksTaskExecutor {
  89 private:
  90     WorkGang* _workers;
  91 
  92 public:
  93     SharedRestorePreservedMarksTaskExecutor(WorkGang* workers) : _workers(workers) { }
  94 
  95     void restore(PreservedMarksSet* preserved_marks_set,
  96                  volatile size_t* total_size_addr);
  97 
  98 };
  99 
 100 class PreservedMarksSet : public CHeapObj<mtGC> {
 101 private:
 102   // true -> _stacks will be allocated in the C heap
 103   // false -> _stacks will be allocated in the resource arena
 104   const bool _in_c_heap;
 105 
 106   // Number of stacks we have allocated (typically, one stack per GC worker).
 107   // This should be >= 1 if the stacks have been initialized,
 108   // or == 0 if they have not.
 109   uint _num;
 110 
 111   // Stack array (typically, one stack per GC worker) of length _num.
 112   // This should be != NULL if the stacks have been initialized,
 113   // or == NULL if they have not.
 114   Padded<PreservedMarks>* _stacks;
 115 
 116 public:
 117   uint num() const { return _num; }
 118 
 119   // Return the i'th stack.
 120   PreservedMarks* get(uint i = 0) const {
 121     assert(_num > 0 && _stacks != NULL, "stacks should have been initialized");
 122     assert(i < _num, "pre-condition");
 123     return (_stacks + i);
 124   }
 125 
 126   // Allocate stack array.
 127   void init(uint num);
 128 
 129   // Iterate over all stacks, restore all preserved marks, and reclaim
 130   // the memory taken up by the stack segments.
 131   // Supported executors: SharedRestorePreservedMarksTaskExecutor (Serial, CMS, G1),
 132   // PSRestorePreservedMarksTaskExecutor (PS).
 133   inline void restore(RestorePreservedMarksTaskExecutor* executor);
 134 
 135   // Reclaim stack array.
 136   void reclaim();
 137 
 138   // Assert all the stacks are empty and have no cached segments.
 139   void assert_empty() PRODUCT_RETURN;
 140 
 141   PreservedMarksSet(bool in_c_heap)
 142       : _in_c_heap(in_c_heap), _num(0), _stacks(NULL) { }
 143 
 144   ~PreservedMarksSet() {
 145     assert(_stacks == NULL && _num == 0, "stacks should have been reclaimed");
 146   }
 147 };
 148 
 149 #endif // SHARE_VM_GC_SHARED_PRESERVEDMARKS_HPP