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.
19 package gov.nasa.jpf.vm;
21 import gov.nasa.jpf.ListenerAdapter;
22 import gov.nasa.jpf.annotation.MJI;
23 import gov.nasa.jpf.jvm.bytecode.JVMReturnInstruction;
24 import gov.nasa.jpf.vm.ElementInfo;
25 import gov.nasa.jpf.vm.Instruction;
26 import gov.nasa.jpf.vm.VM;
27 import gov.nasa.jpf.vm.MJIEnv;
28 import gov.nasa.jpf.vm.MethodInfo;
29 import gov.nasa.jpf.vm.NativePeer;
32 * native peer for MemoryGoal tests
34 public class JPF_gov_nasa_jpf_test_MemoryGoal extends NativePeer {
38 // <2do> that's too simple, because we should only measure what is
39 // allocated from the invoked method, not the MethodTester. Needs a listener
41 static class Listener extends ListenerAdapter {
51 Listener (MethodInfo mi){
56 public void objectCreated (VM vm, ThreadInfo ti, ElementInfo ei){
59 nAllocBytes += ei.getHeapSize(); // just an approximation
64 public void objectReleased (VM vm, ThreadInfo ti, ElementInfo ei){
67 nFreeBytes += ei.getHeapSize(); // just an approximation
72 public void instructionExecuted (VM vm, ThreadInfo ti, Instruction nextInsn, Instruction executedInsn){
74 if (executedInsn.getMethodInfo() == mi){
78 if ((executedInsn instanceof JVMReturnInstruction) && (executedInsn.getMethodInfo() == mi)){
84 long totalAllocBytes() {
85 return nAllocBytes - nFreeBytes;
90 public boolean preCheck__Lgov_nasa_jpf_test_TestContext_2Ljava_lang_reflect_Method_2__Z
91 (MJIEnv env, int objRef, int testContextRef, int methodRef){
92 MethodInfo mi = JPF_java_lang_reflect_Method.getMethodInfo(env, methodRef);
94 listener = new Listener(mi);
95 env.addListener(listener);
99 // what a terrible name!
101 public boolean postCheck__Lgov_nasa_jpf_test_TestContext_2Ljava_lang_reflect_Method_2Ljava_lang_Object_2Ljava_lang_Throwable_2__Z
102 (MJIEnv env, int objRef, int testContextRef, int methdRef, int resultRef, int exRef){
104 long nMax = env.getLongField(objRef, "maxGrowth");
106 Listener l = listener;
107 env.removeListener(l);
110 return (l.totalAllocBytes() <= nMax);