2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
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
10 * http://www.apache.org/licenses/LICENSE-2.0.
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.
18 package gov.nasa.jpf.vm;
20 import java.util.Iterator;
22 import gov.nasa.jpf.Config;
23 import gov.nasa.jpf.util.IntTable;
24 import gov.nasa.jpf.util.ObjVector;
27 * Statics implementation that uses a simple ObjVector as the underlying container.
29 * The ids used to retrieve ElementInfos are dense and search global, computation is based
30 * on the assumption that each ClassLoader can only define one class per binary class name
32 public class OVStatics implements Statics {
34 static class OVMemento implements Memento<Statics> {
35 ObjVector.Snapshot<ElementInfo> eiSnap;
37 OVMemento (OVStatics statics){
38 statics.elementInfos.process( ElementInfo.storer);
39 eiSnap = statics.elementInfos.getSnapshot();
43 public Statics restore(Statics inSitu) {
44 OVStatics statics = (OVStatics) inSitu;
45 statics.elementInfos.restore(eiSnap);
46 statics.elementInfos.process( ElementInfo.restorer);
52 protected ObjVector<ElementInfo> elementInfos;
54 // search global class ids (for this ClassLoader only)
55 // NOTE this is per instance so that each one is as dense as possible, but since
56 // it is search global it does NOT have to be restored and we can copy the reference when cloning
58 protected IntTable<String> ids;
63 public OVStatics (Config conf) {
64 elementInfos = new ObjVector<ElementInfo>();
67 ids = new IntTable<String>();
70 protected int computeId (ClassInfo ci) {
71 String clsName = ci.getName();
72 IntTable.Entry<String> e = ids.get(clsName);
75 ids.put( clsName, id);
83 protected StaticElementInfo createStaticElementInfo (int id, ClassInfo ci, ThreadInfo ti, ElementInfo eiClsObj) {
84 Fields f = ci.createStaticFields();
85 Monitor m = new Monitor();
87 StaticElementInfo ei = new StaticElementInfo( id, ci, f, m, ti, eiClsObj);
89 ci.initializeStaticData(ei, ti);
95 public StaticElementInfo newClass (ClassInfo ci, ThreadInfo ti, ElementInfo eiClsObj) {
96 assert (eiClsObj != null);
98 int id = computeId( ci);
100 StaticElementInfo ei = createStaticElementInfo( id, ci, ti, eiClsObj);
101 elementInfos.set(id, ei);
107 public StaticElementInfo newStartupClass (ClassInfo ci, ThreadInfo ti) {
108 int id = computeId( ci);
110 StaticElementInfo ei = createStaticElementInfo( id, ci, ti, null);
111 elementInfos.set(id, ei);
120 public StaticElementInfo get(int id) {
121 // the cast sucks, but otherwise we run into the Processor covariance problem
122 return (StaticElementInfo)elementInfos.get(id);
126 public StaticElementInfo getModifiable(int id) {
127 StaticElementInfo ei = (StaticElementInfo)elementInfos.get(id);
130 ei = (StaticElementInfo)ei.deepClone();
131 // freshly created ElementInfos are not frozen, so we don't have to defreeze
132 elementInfos.set(id, ei);
141 public void cleanUpDanglingReferences (Heap heap) {
142 ThreadInfo ti = ThreadInfo.getCurrentThread();
143 int tid = ti.getId();
144 boolean isThreadTermination = ti.isTerminated();
146 for (ElementInfo e : this) {
147 e.cleanUp( heap, isThreadTermination, tid);
151 //--- state restoration
154 public Memento<Statics> getMemento(MementoFactory factory) {
155 return factory.getMemento(this);
159 public Memento<Statics> getMemento() {
160 return new OVMemento(this);
163 @SuppressWarnings("rawtypes")
165 public Iterator<ElementInfo> iterator(){
166 return ((ObjVector)elementInfos).nonNullIterator();
170 public void markRoots(Heap heap) {
171 for (StaticElementInfo ei : liveStatics()){
172 ei.markStaticRoot(heap);
176 @SuppressWarnings("rawtypes")
178 public Iterable<StaticElementInfo> liveStatics() {
179 return (Iterable)elementInfos.elements();
184 return elementInfos.length();