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