1 /*
   2  * Copyright (c) 2017, 2019, 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 #include "precompiled.hpp"
  26 #include "classfile/classLoaderData.hpp"
  27 #include "jfr/leakprofiler/utilities/saveRestore.hpp"
  28 
  29 MarkOopContext::MarkOopContext() : _obj(NULL), _mark_oop(NULL) {}
  30 
  31 MarkOopContext::MarkOopContext(const oop obj) : _obj(obj), _mark_oop(obj->mark()) {
  32   assert(_obj->mark() == _mark_oop, "invariant");
  33   // now we will "poison" the mark word of the object
  34   // to the intermediate monitor INFLATING state.
  35   // This is an "impossible" state during a safepoint,
  36   // hence we will use it to quickly identify objects
  37   // during the reachability search from gc roots.
  38   assert(NULL == markOopDesc::INFLATING(), "invariant");
  39   _obj->set_mark(markOopDesc::INFLATING());
  40   assert(NULL == obj->mark(), "invariant");
  41 }
  42 
  43 MarkOopContext::~MarkOopContext() {
  44   if (_obj != NULL) {
  45     _obj->set_mark(_mark_oop);
  46     assert(_obj->mark() == _mark_oop, "invariant");
  47   }
  48 }
  49 
  50 MarkOopContext::MarkOopContext(const MarkOopContext& rhs) : _obj(NULL), _mark_oop(NULL) {
  51   swap(const_cast<MarkOopContext&>(rhs));
  52 }
  53 
  54 void MarkOopContext::operator=(MarkOopContext rhs) {
  55   swap(rhs);
  56 }
  57 
  58 void MarkOopContext::swap(MarkOopContext& rhs) {
  59   oop temp_obj = rhs._obj;
  60   markOop temp_mark_oop = rhs._mark_oop;
  61   rhs._obj = _obj;
  62   rhs._mark_oop = _mark_oop;
  63   _obj = temp_obj;
  64   _mark_oop = temp_mark_oop;
  65 }
  66 
  67 CLDClaimContext::CLDClaimContext() : _cld(NULL) {}
  68 
  69 CLDClaimContext::CLDClaimContext(ClassLoaderData* cld) : _cld(cld) {
  70   assert(_cld->claimed(), "invariant");
  71   _cld->clear_claimed();
  72 }
  73 
  74 CLDClaimContext::~CLDClaimContext() {
  75   if (_cld != NULL) {
  76     assert(!_cld->claimed(), "invariant");
  77     _cld->claim();
  78     assert(_cld->claimed(), "invariant");
  79   }
  80 }
  81 
  82 CLDClaimContext::CLDClaimContext(const CLDClaimContext& rhs) : _cld(NULL) {
  83   swap(const_cast<CLDClaimContext&>(rhs));
  84 }
  85 
  86 void CLDClaimContext::operator=(CLDClaimContext rhs) {
  87   swap(rhs);
  88 }
  89 
  90 void CLDClaimContext::swap(CLDClaimContext& rhs) {
  91   ClassLoaderData* temp_cld = rhs._cld;
  92   rhs._cld = _cld;
  93   _cld = temp_cld;
  94 }
  95 
  96 CLDClaimStateClosure::CLDClaimStateClosure() : CLDClosure(), _state() {}
  97 
  98 void CLDClaimStateClosure::do_cld(ClassLoaderData* cld) {
  99   assert(cld != NULL, "invariant");
 100   if (cld->claimed()) {
 101     _state.save(cld);
 102   }
 103 }
 104 
 105 SaveRestoreCLDClaimBits::SaveRestoreCLDClaimBits() : _claim_state_closure() {
 106   ClassLoaderDataGraph::cld_do(&_claim_state_closure);
 107 }
 108 
 109 SaveRestoreCLDClaimBits::~SaveRestoreCLDClaimBits() {
 110   ClassLoaderDataGraph::clear_claimed_marks();
 111 }