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 gov.nasa.jpf.Config;
21 import gov.nasa.jpf.util.ObjVector;
23 import java.util.Iterator;
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
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)
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
37 public class OVHeap extends GenericSGOIDHeap {
39 //--- state management
40 static class OVMemento extends GenericSGOIDHeapMemento {
41 ObjVector.Snapshot<ElementInfo> eiSnap;
43 OVMemento(OVHeap heap) {
46 heap.elementInfos.process(ElementInfo.storer);
47 eiSnap = heap.elementInfos.getSnapshot();
51 public Heap restore(Heap inSitu) {
52 super.restore( inSitu);
54 OVHeap heap = (OVHeap)inSitu;
55 heap.elementInfos.restore(eiSnap);
56 heap.elementInfos.process(ElementInfo.restorer);
64 ObjVector<ElementInfo> elementInfos;
69 public OVHeap (Config config, KernelState ks){
72 elementInfos = new ObjVector<ElementInfo>();
75 //--- the container interface
78 * return number of non-null elements
86 protected void set (int index, ElementInfo ei) {
87 elementInfos.set(index, ei);
91 * we treat ref <= 0 as NULL reference instead of throwing an exception
94 public ElementInfo get (int ref) {
98 return elementInfos.get(ref);
103 public ElementInfo getModifiable (int ref) {
107 ElementInfo ei = elementInfos.get(ref);
109 if (ei != null && ei.isFrozen()) {
111 // freshly created ElementInfos are not frozen, so we don't have to defreeze
112 elementInfos.set(ref, ei);
120 protected void remove(int ref) {
121 elementInfos.remove(ref);
125 public Iterator<ElementInfo> iterator() {
126 return elementInfos.nonNullIterator();
130 public Iterable<ElementInfo> liveObjects() {
131 return elementInfos.elements();
135 public void resetVolatiles() {
140 public void restoreVolatiles() {
145 public Memento<Heap> getMemento(MementoFactory factory) {
146 return factory.getMemento(this);
150 public Memento<Heap> getMemento(){
151 return new OVMemento(this);