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