1 /*
2  * Copyright (c) 2001, 2006, 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 package sun.jvm.hotspot.utilities;
26 
27 import java.io.*;
28 import java.util.*;
29 import sun.jvm.hotspot.oops.*;
30 
31 /** Describes a path from an object back to the root which is keeping
32     it alive. Elements of the path are (object, field) pairs, where
33     the object is expressed as a @link{sun.jvm.hotspot.oops.Oop}, and
34     where the field is expressed as a
35     @link{sun.jvm.hotspot.oops.FieldIdentifier}. If the element
36     reflects a root, the Oop will be null. If the element is the end
37     of the path, the FieldIdentifier will be null. */
38 
39 public class LivenessPath {
LivenessPath()40   LivenessPath() {
41     stack = new Stack();
42   }
43 
44   /** Number of elements in the path */
size()45   public int size() {
46     return stack.size();
47   }
48 
49   /** Fetch the element at the given index; 0-based */
get(int index)50   public LivenessPathElement get(int index) throws ArrayIndexOutOfBoundsException {
51     return (LivenessPathElement) stack.get(index);
52   }
53 
printOn(PrintStream tty)54   public void printOn(PrintStream tty) {
55     for (int j = 0; j < size(); j++) {
56       LivenessPathElement el = get(j);
57       tty.print("  - ");
58       if (el.getObj() != null) {
59         Oop.printOopValueOn(el.getObj(), tty);
60       }
61       if (el.getField() != null) {
62         if (el.getObj() != null) {
63           tty.print(", field ");
64         }
65         tty.print(el.getField().getName());
66       }
67       tty.println();
68     }
69   }
70 
71   /** Indicates whether this path is "complete", i.e., whether the
72       last element is a root. Convenience routine for LivenessAnalysis. */
isComplete()73   boolean isComplete() {
74     if (size() == 0)
75       return false;
76     return peek().isRoot();
77   }
78 
79   // Convenience routine for LivenessAnalysis
peek()80   LivenessPathElement peek() {
81     return (LivenessPathElement) stack.peek();
82   }
83 
84   // Convenience routine for LivenessAnalysis
push(LivenessPathElement el)85   void push(LivenessPathElement el) {
86     stack.push(el);
87   }
88 
89   // Convenience routine for LivenessAnalysis
pop()90   void pop() {
91     stack.pop();
92   }
93 
94   // Make a copy of the contents of the path -- the
95   // LivenessPathElements are not duplicated, only the containing path
copy()96   LivenessPath copy() {
97     LivenessPath dup = new LivenessPath();
98     for (int i = 0; i < stack.size(); i++) {
99       dup.stack.push(stack.get(i));
100     }
101     return dup;
102   }
103 
104   //---------------------------------------------------------------------------
105   // Internals only below this point
106   //
107   private Stack stack;
108 }
109