KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > StaticArea


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.jvm;
20
21 import gov.nasa.jpf.Config;
22 import gov.nasa.jpf.JPFException;
23 import gov.nasa.jpf.util.Debug;
24
25
26 /**
27  * memory area for static fields
28  */

29 public class StaticArea extends Area {
30
31   public static void init (Config config) {
32     StaticMap.init();
33   }
34   
35   /**
36    * Creates a new empty static area.
37    */

38   public StaticArea (Config config, KernelState ks) {
39     super(ks, StaticElementInfo.storingDataLength);
40   }
41
42   public StaticArea (StaticArea sa) {
43     super(sa);
44   }
45
46   public boolean isStatic () {
47     return true;
48   }
49
50   public Object JavaDoc clone () {
51     return new StaticArea(this);
52   }
53
54   public boolean containsClass (String JavaDoc cname) {
55     return indexOf(cname) != -1;
56   }
57
58   public StaticElementInfo get (String JavaDoc cname) {
59     int index = indexOf(cname);
60
61     if (index == -1) {
62       newClass(ClassInfo.getClassInfo(cname));
63     }
64
65     return (StaticElementInfo) get(indexOf(cname));
66   }
67
68   /**
69    * Returns the index of a given class.
70    */

71   public int indexOf (String JavaDoc cname) {
72     synchronized (StaticMap.class) {
73       if (!StaticMap.hasEntry(cname)) {
74         return -1;
75       }
76
77       int index = StaticMap.getEntry(cname);
78
79       if ((index >= 0) && (index < elements.length) &&
80               (elements[index] != null)) {
81         return index;
82       } else {
83         return -1;
84       }
85     }
86   }
87
88   public void initializeClasses () {
89     int length = elements.length;
90     ThreadInfo th = ks.ss.getRunningThread();
91
92     for (int i = 0; i < length; i++) {
93       if (elements[i] != null) {
94         StaticElementInfo ei = (StaticElementInfo) elements[i];
95         ClassInfo ci = ei.getClassInfo();
96
97         if (ei.classObjectRef == -1) {
98           // must have been a startup class
99
ei.classObjectRef = ci.createClassObject(th, i);
100         }
101
102         ci.initializeClass(th);
103       }
104     }
105   }
106
107   public void log () {
108     Debug.println(Debug.MESSAGE, "SA");
109
110     for (int i = 0; i < elements.length; i++) {
111       if (elements[i] != null) {
112         elements[i].log();
113       }
114     }
115   }
116
117   public void markRoots () {
118     int length = elements.length;
119
120     for (int i = 0; i < length; i++) {
121       StaticElementInfo ei = (StaticElementInfo)elements[i];
122       if (ei != null) {
123         ei.markStaticRoot();
124       }
125     }
126   }
127   
128   /**
129    * to deal with recursive loads/inits, we don't throw exceptions when encountering
130    * a class that's aleady in. Inclusion is checked by the caller, we just make
131    * sure here it doesn't get initialized twice. However, we cannot add early, silently
132    * step over recursive requests, and initialize when we get out of recursion - the
133    * recursive calls might rely on proper initialization being done, i.e. we
134    * have to initermost
135    *
136    * watch out - we can't use this for startup classes (would
137    * end up in infinite recursion)
138    */

139   public int newClass (ClassInfo ci) {
140     int index = indexOf(ci.getName());
141     
142     if (index == -1) {
143       // make sure the superclass is loaded and initialized
144
ClassInfo sci = ci.getSuperClass();
145
146       if ((sci != null) && !containsClass(sci.getName())) {
147         newClass(sci); // might get recursive here
148
}
149
150       index = indexOf(ci.getName());
151       if (index == -1) {
152         index = getNewIndexFor(ci.getName());
153
154         ThreadInfo th = ks.ss.getRunningThread();
155         int cref = ci.createClassObject(th, index);
156         StaticElementInfo ei = createElementInfo(ci, cref);
157
158         add(index, ei);
159
160         ci.initializeClass(th); // or here
161
}
162     }
163
164     return index;
165   }
166
167   ElementInfo createElementInfo () {
168     return new StaticElementInfo();
169   }
170
171   int newUninitializedClass (ClassInfo ci) {
172     int index = getNewIndexFor(ci.getName());
173
174     ThreadInfo th = ks.ss.getRunningThread();
175     int cref = ci.createClassObject(th, index);
176     StaticElementInfo ei = createElementInfo(ci, cref);
177
178     add(index, ei);
179
180     return index;
181   }
182   
183   StaticElementInfo createElementInfo (ClassInfo ci, int classObjRef) {
184     Fields f = ci.createStaticFields(this);
185     Monitor m = new Monitor();
186
187     StaticElementInfo ei = new StaticElementInfo(f, m, classObjRef);
188
189     return ei;
190   }
191
192   /**
193    * this is for the VM bootstrapping, i.e. we can't do clinit or
194    * create class objects yet, but we need to get elements entries
195    */

196   int newStartupClass (ClassInfo ci) {
197     StaticElementInfo ei = createElementInfo(ci, -1);
198     int index = getNewIndexFor(ci.getName());
199     
200     add(index, ei);
201
202     return index;
203   }
204
205   private static int getNewIndexFor (String JavaDoc cname) {
206     return StaticMap.addEntry(cname);
207   }
208 }
209
Popular Tags