1 /*
  2  * Copyright (c) 2021, 2024, 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 #ifndef SHARE_GC_Z_ZITERATOR_INLINE_HPP
 25 #define SHARE_GC_Z_ZITERATOR_INLINE_HPP
 26 
 27 #include "gc/z/zIterator.hpp"
 28 
 29 #include "gc/z/zVerify.hpp"
 30 #include "memory/iterator.inline.hpp"
 31 #include "oops/objArrayOop.hpp"
 32 #include "oops/oop.inline.hpp"
 33 
 34 inline bool ZIterator::is_invisible_object(oop obj) {
 35   // This is a good place to make sure that we can't concurrently iterate over
 36   // objects while VMThread operations think they have exclusive access to the
 37   // object graph.
 38   //
 39   // One example that have caused problems is the JFR Leak Profiler, which
 40   // sets the mark word to a value that makes the object arrays look like
 41   // invisible objects.
 42   z_verify_safepoints_are_blocked();
 43 
 44   return obj->mark_acquire().is_marked();
 45 }
 46 
 47 inline bool ZIterator::is_invisible_object_array(oop obj) {
 48   return obj->klass()->is_objArray_klass() && is_invisible_object(obj);
 49 }
 50 
 51 // This iterator skips invisible object arrays
 52 template <typename OopClosureT>
 53 void ZIterator::oop_iterate_safe(oop obj, OopClosureT* cl) {
 54   // Skip invisible object arrays - we only filter out *object* arrays,
 55   // because that check is arguably faster than the is_invisible_object
 56   // check, and primitive arrays are cheap to call oop_iterate on.
 57   if (!is_invisible_object_array(obj)) {
 58     obj->oop_iterate(cl);
 59   }
 60 }
 61 
 62 template <typename OopClosureT>
 63 void ZIterator::oop_iterate(oop obj, OopClosureT* cl) {
 64   assert(!is_invisible_object_array(obj), "not safe");
 65   obj->oop_iterate(cl);
 66 }
 67 
 68 template <typename OopClosureT>
 69 void ZIterator::oop_iterate_range(objArrayOop obj, OopClosureT* cl, int start, int end) {
 70   assert(!is_invisible_object_array(obj), "not safe");
 71   obj->oop_iterate_range(cl, start, end);
 72 }
 73 
 74 template <typename Function>
 75 class ZBasicOopIterateClosure : public BasicOopIterateClosure {
 76 private:
 77   Function _function;
 78 
 79 public:
 80   ZBasicOopIterateClosure(Function function)
 81     : _function(function) {}
 82 
 83   virtual void do_oop(oop* p) {
 84     _function((volatile zpointer*)p);
 85   }
 86 
 87   virtual void do_oop(narrowOop* p_) {
 88     ShouldNotReachHere();
 89   }
 90 };
 91 
 92 // This function skips invisible roots
 93 template <typename Function>
 94 void ZIterator::basic_oop_iterate_safe(oop obj, Function function) {
 95   ZBasicOopIterateClosure<Function> cl(function);
 96   oop_iterate_safe(obj, &cl);
 97 }
 98 
 99 template <typename Function>
100 void ZIterator::basic_oop_iterate(oop obj, Function function) {
101   ZBasicOopIterateClosure<Function> cl(function);
102   oop_iterate(obj, &cl);
103 }
104 
105 template <typename Function>
106 ZObjectClosure<Function>::ZObjectClosure(Function function)
107   : _function(function) {}
108 
109 template <typename Function>
110 void ZObjectClosure<Function>::do_object(oop obj) {
111   _function(obj);
112 }
113 
114 #endif // SHARE_GC_Z_ZITERATOR_INLINE_HPP