View Javadoc

1   /*
2    * Copyright 2008 Eric Caspole
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the License at
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under
10   * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11   * KIND, either express or implied. See the License for the specific language governing
12   * permissions and limitations under the License.
13   */
14   package net.sf.madmap;
15  
16  import java.util.*;
17  
18  /**
19   * HprofHeapCollectable is the base class for everything that is "collectable"
20   * in the target process heap, i.e. objects, arrays and classes. This class
21   * has methods to compute the liveness and retained size during the analysis.
22   * 
23   * @author ecaspole
24   *  
25   */
26  public abstract class HprofHeapCollectable extends HprofHeapElement {
27  
28    // highest bit is sign bit
29    static final long _liveBits     = 0x40000000;
30    static final long _claimedBits  = 0x20000000;
31    static final long _sizeBits     = 0x1fffffff;
32    
33    // support for iterative retained size accumulation
34    protected Stack  _visitedChildren;
35    long      _retainedSize = 0;
36  
37    public long getRetainedSize()         { return (_retainedSize << 3) >> 3; }
38    public void claim()                   { _retainedSize = (_retainedSize | (_claimedBits << 32)); }
39    public boolean isClaimed()            { return (_retainedSize & (_claimedBits << 32)) != 0; }
40    public void setClaimed( boolean x )   { 
41      if ( x ) {
42        claim(); 
43      } else {
44        _retainedSize = (_retainedSize & (~(_claimedBits<<32)));
45      }
46    }
47    
48    public boolean  isAlive()             { return (_retainedSize & (_liveBits << 32 )) != 0; }
49    public void     setLiveness( boolean lv )   { 
50      if ( lv ) {
51        _retainedSize = _retainedSize | (_liveBits << 32);
52      } else {
53        _retainedSize = _retainedSize & (~(_liveBits << 32));    
54      }
55    }
56    
57    /**
58     * @param newSize the object's retained size, computed in the analysis phase 
59     */
60    public void setRetainedSize( long newSize) { 
61      long oldBits = _retainedSize >> 61;
62  	  _retainedSize = newSize | (oldBits << 61); 
63    }
64  
65    /**
66     * @param addr address of the object
67     */
68    public HprofHeapCollectable( long addr ) {
69  	  super( addr );
70    }
71  
72    long[]  longArrayListtoLongArray( ArrayList in ) {
73      if ( in == null ) {
74        return null;
75      }      
76      if ( in.size() == 0 ) {
77        return null;
78      }
79      long[]  out = new long[ in.size() ];
80      for ( int i = 0; i < out.length; i++ ) {
81        out[ i ] = ((Long) in.get( i )).longValue();
82      }
83      return out;
84    }
85  
86  
87    public abstract String    className();
88    public abstract long      children_size();
89    public abstract long[]    children();
90    public abstract long      size();
91    
92    /**
93   * @param ref	the address to find in this object
94   * @return		true if ref is a member of this object
95   */
96  public boolean containsRef( long ref ) {
97      for ( int i = 0; i < children_size(); i ++ ) {
98        if ( children()[i] == ref ) {
99          return true;
100       }
101     }
102     return false;
103   }
104   
105   void visited( HprofHeapCollectable child ) {
106     if ( _visitedChildren == null ) {
107       _visitedChildren = new Stack();
108     }
109     _visitedChildren.push( child );
110   }
111   
112   Stack getVisitedStack() { return _visitedChildren; }
113   int getVisitedCount() { 
114     return _visitedChildren == null ? 0 : _visitedChildren.size();
115   }
116   
117   void resetVisited() { 
118     _visitedChildren = null;
119   }  
120 }