Adding ParameterizedTypeImpl to getGenericSuperclass method.
[jpf-core.git] / src / main / gov / nasa / jpf / vm / GenericSGOIDHeap.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.IntTable;
22
23 /**
24  * abstract Heap trait that implements SGOIDs by means of a search global
25  * Allocation map and a state managed allocCount map
26  * 
27  * NOTE - a reference value of 0 represents null and therefore is not a valid SGOID
28  */
29 public abstract class GenericSGOIDHeap extends GenericHeap {
30
31   static class GenericSGOIDHeapMemento extends GenericHeapMemento {
32     IntTable.Snapshot<AllocationContext> ctxSnap;
33     
34     GenericSGOIDHeapMemento (GenericSGOIDHeap heap) {
35       super(heap);
36       
37       ctxSnap = heap.allocCounts.getSnapshot();
38     }
39
40     @Override
41     public Heap restore(Heap inSitu) {
42       super.restore( inSitu);
43       
44       GenericSGOIDHeap heap = (GenericSGOIDHeap) inSitu;
45       heap.allocCounts.restore(ctxSnap);
46       
47       return heap;
48     }
49   }
50   
51   // these are search global
52   protected int nextSgoid;
53   protected IntTable<Allocation> sgoids;
54   
55   // this is state managed 
56   // NOTE - this has to be included in the mementos of concrete Heap implementations 
57   protected IntTable<AllocationContext> allocCounts;
58   
59   protected GenericSGOIDHeap (Config config, KernelState ks){
60     super(config, ks);
61     
62     // static inits
63     initAllocationContext(config);
64     sgoids = new IntTable<Allocation>();
65     nextSgoid = 0;
66     
67     allocCounts = new IntTable<AllocationContext>();
68   }
69   
70   
71   //--- to be overridden by subclasses that use different AllocationContext implementations
72   
73   protected void initAllocationContext(Config config) {
74     HashedAllocationContext.init(config);
75     //PreciseAllocationContext.init(config);
76   }
77   
78   // these are always called directly from the allocation primitive, i.e. the allocating site is at a fixed
79   // stack offset (callers caller)
80   @Override
81   protected AllocationContext getSUTAllocationContext (ClassInfo ci, ThreadInfo ti) {
82     return HashedAllocationContext.getSUTAllocationContext(ci, ti);
83     //return PreciseAllocationContext.getSUTAllocationContext(ci, ti);
84   }
85   @Override
86   protected AllocationContext getSystemAllocationContext (ClassInfo ci, ThreadInfo ti, int anchor) {
87     return HashedAllocationContext.getSystemAllocationContext(ci, ti, anchor);
88     //return PreciseAllocationContext.getSystemAllocationContext(ci, ti, anchor);
89   }
90   
91
92   @Override
93   protected int getNewElementInfoIndex (AllocationContext ctx) {
94     int idx;
95     int cnt;
96     
97     IntTable.Entry<AllocationContext> cntEntry = allocCounts.getInc(ctx);
98     cnt = cntEntry.val;
99     
100     Allocation alloc = new Allocation(ctx, cnt);
101     
102     IntTable.Entry<Allocation> sgoidEntry = sgoids.get(alloc);
103     if (sgoidEntry != null) { // we already had this one
104       idx = sgoidEntry.val;
105       
106     } else { // new entry
107       idx = ++nextSgoid;
108       sgoids.put(alloc, idx);
109     }
110     
111     // sanity check - we do this here (and not in our super class) since we know how elements are stored
112 //    assert get(idx) == null;
113     
114     return idx;
115   }
116
117 }