Fixing a few bugs in the statistics printout.
[jpf-core.git] / src / main / gov / nasa / jpf / vm / OVHeap.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package gov.nasa.jpf.vm;
19
20 import gov.nasa.jpf.Config;
21 import gov.nasa.jpf.util.ObjVector;
22
23 import java.util.Iterator;
24
25 /**
26  * a heap that implements search global object ids (SGOIDs) and uses
27  * a simple ObjVector to store ElementInfos. This is only efficient
28  * for small heaps with low fragmentation
29  * 
30  * SGOID computation uses HashedAllocationContext, which means there
31  * is a chance of collisions, in which case a different heap type
32  * has to be used (we don't try to resolve collisions here)
33  * 
34  * NOTE - a reference value of 0 represents NULL, but we rather waste one
35  * unused element than doing a -1 on all gets/sets
36  */
37 public class OVHeap extends GenericSGOIDHeap {
38   
39   //--- state management
40   static class OVMemento extends GenericSGOIDHeapMemento {
41     ObjVector.Snapshot<ElementInfo> eiSnap;
42     
43     OVMemento(OVHeap heap) {
44       super(heap);
45       
46       heap.elementInfos.process(ElementInfo.storer);      
47       eiSnap = heap.elementInfos.getSnapshot();
48     }
49
50     @Override
51     public Heap restore(Heap inSitu) {
52       super.restore( inSitu);
53       
54       OVHeap heap = (OVHeap)inSitu;
55       heap.elementInfos.restore(eiSnap);      
56       heap.elementInfos.process(ElementInfo.restorer);
57       
58       return heap;
59     }
60   }
61   
62   //--- instance data
63   
64   ObjVector<ElementInfo> elementInfos;
65   
66   
67   //--- constructors
68   
69   public OVHeap (Config config, KernelState ks){
70     super(config, ks);
71     
72     elementInfos = new ObjVector<ElementInfo>();
73   }
74       
75   //--- the container interface
76
77   /**
78    * return number of non-null elements
79    */
80   @Override
81   public int size() {
82     return nLiveObjects;
83   }
84   
85   @Override
86   protected void set (int index, ElementInfo ei) {
87     elementInfos.set(index, ei);
88   }
89
90   /**
91    * we treat ref <= 0 as NULL reference instead of throwing an exception
92    */
93   @Override
94   public ElementInfo get (int ref) {
95     if (ref <= 0) {
96       return null;
97     } else {
98       return elementInfos.get(ref);
99     }
100   }
101
102   @Override
103   public ElementInfo getModifiable (int ref) {
104     if (ref <= 0) {
105       return null;
106     } else {
107       ElementInfo ei = elementInfos.get(ref);
108
109       if (ei != null && ei.isFrozen()) {
110         ei = ei.deepClone(); 
111         // freshly created ElementInfos are not frozen, so we don't have to defreeze
112         elementInfos.set(ref, ei);
113       }
114
115       return ei;
116     }
117   }
118     
119   @Override
120   protected void remove(int ref) {
121     elementInfos.remove(ref);
122   }
123
124   @Override
125   public Iterator<ElementInfo> iterator() {
126     return elementInfos.nonNullIterator();
127   }
128
129   @Override
130   public Iterable<ElementInfo> liveObjects() {
131     return elementInfos.elements();
132   }
133
134   @Override
135   public void resetVolatiles() {
136     // we don't have any
137   }
138
139   @Override
140   public void restoreVolatiles() {
141     // we don't have any
142   }
143
144   @Override
145   public Memento<Heap> getMemento(MementoFactory factory) {
146     return factory.getMemento(this);
147   }
148
149   @Override
150   public Memento<Heap> getMemento(){
151     return new OVMemento(this);
152   }
153
154
155 }