1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ahat.heapdump;
18 
19 public class AhatHeap implements Diffable<AhatHeap> {
20   private String mName;
21   private long mSize = 0;
22   private int mIndex;
23   private AhatHeap mBaseline;
24   private boolean mIsPlaceHolder = false;
25 
AhatHeap(String name, int index)26   AhatHeap(String name, int index) {
27     mName = name;
28     mIndex = index;
29     mBaseline = this;
30   }
31 
32   /**
33    * Construct a place holder heap.
34    */
AhatHeap(String name, AhatHeap baseline)35   private AhatHeap(String name, AhatHeap baseline) {
36     mName = name;
37     mIndex = -1;
38     mBaseline = baseline;
39     baseline.setBaseline(this);
40     mIsPlaceHolder = true;
41   }
42 
43   /**
44    * Construct a new place holder heap that has the given baseline heap.
45    */
newPlaceHolderHeap(String name, AhatHeap baseline)46   static AhatHeap newPlaceHolderHeap(String name, AhatHeap baseline) {
47     return new AhatHeap(name, baseline);
48   }
49 
addToSize(long increment)50   void addToSize(long increment) {
51     mSize += increment;
52   }
53 
54   /**
55    * Returns a unique instance for this heap between 0 and the total number of
56    * heaps in this snapshot, or -1 if this is a placeholder heap.
57    */
getIndex()58   int getIndex() {
59     return mIndex;
60   }
61 
62   /**
63    * Returns the name of this heap.
64    */
getName()65   public String getName() {
66     return mName;
67   }
68 
69   /**
70    * Returns the total number of bytes allocated on this heap.
71    */
getSize()72   public long getSize() {
73     return mSize;
74   }
75 
setBaseline(AhatHeap baseline)76   void setBaseline(AhatHeap baseline) {
77     mBaseline = baseline;
78   }
79 
80   @Override
getBaseline()81   public AhatHeap getBaseline() {
82     return mBaseline;
83   }
84 
85   @Override
isPlaceHolder()86   public boolean isPlaceHolder() {
87     return mIsPlaceHolder;
88   }
89 }
90