Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

DynamicArea.java

00001 package gov.nasa.arc.ase.jpf.jvm;
00002 
00003 import java.util.BitSet;
00004 import gov.nasa.arc.ase.util.Debug;
00005 import gov.nasa.arc.ase.jpf.Engine;
00006 import gov.nasa.arc.ase.jpf.jvm.bytecode.Instruction;
00007 //#ifdef RACE
00008 
00009 
00010 
00011 //#endif RACE
00012 //#ifdef MARK_N_SWEEP || REFERENCE_COUNT
00013 import gov.nasa.arc.ase.util.BSet;
00014 import gov.nasa.arc.ase.util.FalseBSet;
00015 import gov.nasa.arc.ase.util.TrueBSet;
00016 //#endif MARK_N_SWEEP || REFERENCE_COUNT
00017 
00018 public class DynamicArea extends Area {
00019 //#ifdef MARK_N_SWEEP
00020   
00021   /**
00022    * Used to store which objects are currently referenced.
00023    */
00024   public BitSet isUsed = null;
00025   
00026   public DynamicArea(DynamicArea da) {
00027     super(da);
00028     isUsed = da.isUsed;
00029   }  
00030 //#endif MARK_N_SWEEP
00031 
00032   /**
00033    * Creates a new empty dynamic area.
00034    */
00035   public DynamicArea(KernelState ks) {
00036     super(ks, false);
00037   }  
00038   public Object clone() {
00039     return new DynamicArea(this);
00040   }  
00041 //#ifdef MARK_N_SWEEP
00042   /**
00043    * Executes the mark and sweep garbage collection algorithm.
00044    */
00045   public void gc() {
00046     int length = elements.length;
00047 
00048     isUsed = new BitSet(length);
00049 
00050     ks.tl.mark();
00051     ks.sa.mark();
00052 
00053     int count = 0;
00054 
00055     for(int i = 0; i < length; i++)
00056       if(!isUsed.get(i)) {
00057     if(elements[i] != null) {
00058       count++;
00059 
00060       remove(i);
00061     }
00062       }
00063 
00064     Engine.getJPF().status.GCs += count;
00065 
00066     isUsed = null;
00067   }  
00068   public ElementInfo get(int index) {
00069     if(index == -1) return null;
00070 
00071     return super.get(index);
00072   }  
00073 //#endif MARK_N_SWEEP
00074 
00075 //#ifdef NO_DYNAMIC_SYMMETRY
00076 
00077 
00078 
00079 
00080 
00081 
00082 
00083 
00084 
00085 
00086 //#else NO_DYNAMIC_SYMMETRY
00087 
00088   private int indexFor(ThreadInfo th) {
00089     Instruction pc = null;
00090     if(th != null) pc = th.getPC();
00091 
00092     DynamicMapIndex i = new DynamicMapIndex(pc,
00093 //#ifdef THREAD_SYMMETRY
00094     th == null ? 0 : th.index, 
00095 //#endif THREAD_SYMMETRY
00096     0);
00097 
00098     synchronized(DynamicMap.class) {
00099       if(!DynamicMap.hasEntry(i)) return DynamicMap.addEntry(i);
00100 
00101       int index = DynamicMap.getEntry(i);
00102       int length = elements.length;
00103 
00104       if(index < length)
00105     while(elements[index] != null) {
00106       i.next();
00107 
00108       if(!DynamicMap.hasEntry(i)) return DynamicMap.addEntry(i);
00109 
00110       index = DynamicMap.getEntry(i);
00111 
00112       if(index >= length) return index;
00113     }
00114 
00115       return index;
00116     }
00117   }  
00118 //#endif NO_DYNAMIC_SYMMETRY
00119 
00120   public void log() {
00121     Debug.println(Debug.MESSAGE, "DA");
00122     for(int i = 0; i < elements.length; i++)
00123       if(elements[i] != null)
00124     elements[i].log();
00125   }  
00126   public void mark(int objref) {
00127     if(objref == -1) return;
00128 
00129     if(isUsed.get(objref)) return;
00130 
00131     isUsed.set(objref);
00132 
00133     elements[objref].mark();
00134   }  
00135   /**
00136    * Creates a new array object at a given address.
00137    */
00138   private int newArray(int index, String type, int size) {
00139     int ts = Types.getTypeSize(type);
00140     boolean ir = Types.isReference(type);
00141 
00142     Fields f = new Fields("["+type, ClassInfo.getClassInfo("java.lang.Object"),
00143     size * ts, size, false
00144 //#ifdef MARK_N_SWEEP || REFERENCE_COUNT
00145     , ir ? (BSet)new TrueBSet() : (BSet)new FalseBSet()
00146 //#endif MARK_N_SWEEP || REFERENCE_COUNT
00147     );
00148     Monitor m = new Monitor();
00149 //#ifdef RACE
00150 
00151 
00152 
00153 
00154 
00155 
00156 
00157 //#endif RACE
00158 
00159     ElementInfo e = new ElementInfo(f, m 
00160 //#ifdef RACE
00161 
00162 //#endif RACE
00163       );
00164     add(index, e);
00165     
00166     return index;
00167   }  
00168   /**
00169    * Creates a new array of the given type.
00170    */
00171   public int newArray(String type, int size, ThreadInfo th) {
00172     return newArray(indexFor(th), type, size);
00173   }  
00174   /**
00175    * Creates a new class object.
00176    */
00177   //** missing: this code does not exist yet.               **//
00178   public int newClass(String cname, ThreadInfo th) {
00179     return -1;
00180   }  
00181   /**
00182    * Creates a new object at the given address.
00183    */ 
00184   private int newObject(int index, ClassInfo ci) {
00185     ks.sa.get(ci.getName());
00186 
00187     Fields f = ci.createObjectFields();
00188     Monitor m = new Monitor();
00189 //#ifdef RACE
00190 
00191 
00192 
00193 //#endif RACE
00194 
00195     add(index, new ElementInfo(f, m
00196 //#ifdef RACE
00197 
00198 //#endif RACE
00199       ));
00200 
00201     return index;
00202   }  
00203   /**
00204    * Creates a new object of the given class.
00205    */
00206   public int newObject(ClassInfo ci, ThreadInfo th) {
00207     return newObject(indexFor(th), ci);
00208   }  
00209   /**
00210    * Creates a new string constant.
00211    */
00212   public int newString(String str, ThreadInfo th) {
00213     int length = str.length();
00214     int index = newObject(ClassInfo.getClassInfo("java.lang.String"), th);
00215     int value = newArray("C", length, th);
00216 
00217     ElementInfo e = get(index);
00218     e.setField("value", value);
00219     e.setField("offset", 0);
00220     e.setField("count", length);
00221 
00222     e = get(value);
00223     for(int i = 0; i < length; i++)
00224       e.setField(i, str.charAt(i));
00225 
00226     return index;
00227   }  
00228 }

Generated at Thu Feb 7 06:44:58 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001