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