1 /*
   2  * Copyright (c) 2014, 2018, 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 #include "precompiled.hpp"
  25 #include "classfile/javaClasses.hpp"
  26 #include "jfr/leakprofiler/chains/edge.hpp"
  27 #include "jfr/leakprofiler/utilities/unifiedOop.hpp"
  28 
  29 Edge::Edge() : _parent(NULL), _reference(NULL) {}
  30 
  31 Edge::Edge(const Edge* parent, const oop* reference) : _parent(parent),
  32                                                        _reference(reference) {}
  33 
  34 const oop Edge::pointee() const {
  35   return UnifiedOop::dereference(_reference);
  36 }
  37 
  38 const oop Edge::reference_owner() const {
  39   return is_root() ? (oop)NULL : UnifiedOop::dereference(_parent->reference());
  40 }
  41 
  42 static const Klass* resolve_klass(const oop obj) {
  43   assert(obj != NULL, "invariant");
  44   return java_lang_Class::is_instance(obj) ?
  45     java_lang_Class::as_Klass(obj) : obj->klass();
  46 }
  47 
  48 const Klass* Edge::pointee_klass() const {
  49   return resolve_klass(pointee());
  50 }
  51 
  52 const Klass* Edge::reference_owner_klass() const {
  53   const oop ref_owner = reference_owner();
  54   return ref_owner != NULL ? resolve_klass(ref_owner) : NULL;
  55 }
  56 
  57 size_t Edge::distance_to_root() const {
  58   size_t depth = 0;
  59   const Edge* current = _parent;
  60   while (current != NULL) {
  61     depth++;
  62     current = current->parent();
  63   }
  64   return depth;
  65 }