3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-15 13:28:47 +00:00

Java bindings with no finalizers

Replacing finalizers with PhantomReferences, required quite a lot of
changes to the codebase.
This commit is contained in:
George Karpenkov 2016-06-12 14:18:13 +02:00
parent dfc80d3b69
commit 495ef0f055
48 changed files with 368 additions and 939 deletions

View file

@ -187,34 +187,15 @@ public class AST extends Z3Object implements Comparable<AST>
return Native.astToString(getContext().nCtx(), getNativeObject()); return Native.astToString(getContext().nCtx(), getNativeObject());
} }
AST(Context ctx) AST(Context ctx, long obj) {
{
super(ctx);
}
AST(Context ctx, long obj)
{
super(ctx, obj); super(ctx, obj);
} }
@Override @Override
void incRef(long o) void incRef(long o)
{ {
// Console.WriteLine("AST IncRef()"); Native.incRef(getContext().nCtx(), o);
if (getContext() == null || o == 0) getContext().getASTDRQ().storeReference(getContext(), this);
return;
getContext().getASTDRQ().incAndClear(getContext(), o);
super.incRef(o);
}
@Override
void decRef(long o)
{
// Console.WriteLine("AST DecRef()");
if (getContext() == null || o == 0)
return;
getContext().getASTDRQ().add(o);
super.decRef(o);
} }
static AST create(Context ctx, long obj) static AST create(Context ctx, long obj)

View file

@ -17,7 +17,7 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
class ASTDecRefQueue extends IDecRefQueue class ASTDecRefQueue extends IDecRefQueue<AST>
{ {
public ASTDecRefQueue() public ASTDecRefQueue()
{ {
@ -30,26 +30,7 @@ class ASTDecRefQueue extends IDecRefQueue
} }
@Override @Override
protected void incRef(Context ctx, long obj) protected void decRef(Context ctx, long obj) {
{
try
{
Native.incRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
}
@Override
protected void decRef(Context ctx, long obj)
{
try
{
Native.decRef(ctx.nCtx(), obj); Native.decRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
} }
}; };

View file

@ -120,14 +120,7 @@ class ASTMap extends Z3Object
@Override @Override
void incRef(long o) void incRef(long o)
{ {
getContext().getASTMapDRQ().incAndClear(getContext(), o); Native.astMapIncRef(getContext().nCtx(), o);
super.incRef(o); getContext().getASTMapDRQ().storeReference(getContext(), this);
}
@Override
void decRef(long o)
{
getContext().getASTMapDRQ().add(o);
super.decRef(o);
} }
} }

View file

@ -105,15 +105,8 @@ public class ASTVector extends Z3Object
@Override @Override
void incRef(long o) void incRef(long o)
{ {
getContext().getASTVectorDRQ().incAndClear(getContext(), o); Native.astVectorIncRef(getContext().nCtx(), o);
super.incRef(o); getContext().getASTVectorDRQ().storeReference(getContext(), this);
}
@Override
void decRef(long o)
{
getContext().getASTVectorDRQ().add(o);
super.decRef(o);
} }
/** /**

View file

@ -74,16 +74,8 @@ public class ApplyResult extends Z3Object
} }
@Override @Override
void incRef(long o) void incRef(long o) {
{ Native.applyResultIncRef(getContext().nCtx(), o);
getContext().getApplyResultDRQ().incAndClear(getContext(), o); getContext().getApplyResultDRQ().storeReference(getContext(), this);
super.incRef(o);
}
@Override
void decRef(long o)
{
getContext().getApplyResultDRQ().add(o);
super.decRef(o);
} }
} }

View file

@ -17,7 +17,7 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
class ApplyResultDecRefQueue extends IDecRefQueue class ApplyResultDecRefQueue extends IDecRefQueue<ApplyResult>
{ {
public ApplyResultDecRefQueue() public ApplyResultDecRefQueue()
{ {
@ -30,26 +30,7 @@ class ApplyResultDecRefQueue extends IDecRefQueue
} }
@Override @Override
protected void incRef(Context ctx, long obj) protected void decRef(Context ctx, long obj) {
{
try
{
Native.applyResultIncRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
}
@Override
protected void decRef(Context ctx, long obj)
{
try
{
Native.applyResultDecRef(ctx.nCtx(), obj); Native.applyResultDecRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
} }
}; };

View file

@ -17,7 +17,7 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
class ASTMapDecRefQueue extends IDecRefQueue class ASTMapDecRefQueue extends IDecRefQueue<ASTMap>
{ {
public ASTMapDecRefQueue() public ASTMapDecRefQueue()
{ {
@ -30,26 +30,7 @@ class ASTMapDecRefQueue extends IDecRefQueue
} }
@Override @Override
protected void incRef(Context ctx, long obj) protected void decRef(Context ctx, long obj) {
{
try
{
Native.astMapIncRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
}
@Override
protected void decRef(Context ctx, long obj)
{
try
{
Native.astMapDecRef(ctx.nCtx(), obj); Native.astMapDecRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
} }
}; };

View file

@ -17,7 +17,7 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
class ASTVectorDecRefQueue extends IDecRefQueue class ASTVectorDecRefQueue extends IDecRefQueue<ASTVector>
{ {
public ASTVectorDecRefQueue() public ASTVectorDecRefQueue()
{ {
@ -30,26 +30,7 @@ class ASTVectorDecRefQueue extends IDecRefQueue
} }
@Override @Override
protected void incRef(Context ctx, long obj) protected void decRef(Context ctx, long obj) {
{
try
{
Native.astVectorIncRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
}
@Override
protected void decRef(Context ctx, long obj)
{
try
{
Native.astVectorDecRef(ctx.nCtx(), obj); Native.astVectorDecRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
} }
}; };

View file

@ -20,15 +20,7 @@ package com.microsoft.z3;
/** /**
* Boolean expressions * Boolean expressions
**/ **/
public class BoolExpr extends Expr public class BoolExpr extends Expr {
{
/**
* Constructor for BoolExpr
**/
protected BoolExpr(Context ctx)
{
super(ctx);
}
/** /**
* Constructor for BoolExpr * Constructor for BoolExpr

View file

@ -20,8 +20,14 @@ package com.microsoft.z3;
/** /**
* Constructors are used for datatype sorts. * Constructors are used for datatype sorts.
**/ **/
public class Constructor extends Z3Object public class Constructor extends Z3Object {
{ private final int n;
Constructor(Context ctx, int n, long nativeObj) {
super(ctx, nativeObj);
this.n = n;
}
/** /**
* The number of fields of the constructor. * The number of fields of the constructor.
* @throws Z3Exception * @throws Z3Exception
@ -78,29 +84,16 @@ public class Constructor extends Z3Object
return t; return t;
} }
/** @Override
* Destructor. void incRef(long o) {
* @throws Throwable
* @throws Z3Exception on error // Datatype constructors are not reference counted.
**/ getContext().getConstructorDRQ().storeReference(getContext(), this);
protected void finalize() throws Throwable
{
try {
Native.delConstructor(getContext().nCtx(), getNativeObject());
} finally {
super.finalize();
}
} }
private int n = 0; static Constructor of(Context ctx, Symbol name, Symbol recognizer,
Symbol[] fieldNames, Sort[] sorts, int[] sortRefs) {
Constructor(Context ctx, Symbol name, Symbol recognizer, int n = AST.arrayLength(fieldNames);
Symbol[] fieldNames, Sort[] sorts, int[] sortRefs)
{
super(ctx);
n = AST.arrayLength(fieldNames);
if (n != AST.arrayLength(sorts)) if (n != AST.arrayLength(sorts))
throw new Z3Exception( throw new Z3Exception(
@ -112,9 +105,10 @@ public class Constructor extends Z3Object
if (sortRefs == null) if (sortRefs == null)
sortRefs = new int[n]; sortRefs = new int[n];
setNativeObject(Native.mkConstructor(ctx.nCtx(), name.getNativeObject(), long nativeObj = Native.mkConstructor(ctx.nCtx(), name.getNativeObject(),
recognizer.getNativeObject(), n, Symbol.arrayToNative(fieldNames), recognizer.getNativeObject(), n, Symbol.arrayToNative(fieldNames),
Sort.arrayToNative(sorts), sortRefs)); Sort.arrayToNative(sorts), sortRefs);
return new Constructor(ctx, n, nativeObj);
} }
} }

View file

@ -20,32 +20,21 @@ package com.microsoft.z3;
/** /**
* Lists of constructors * Lists of constructors
**/ **/
public class ConstructorList extends Z3Object public class ConstructorList extends Z3Object {
{
/**
* Destructor.
* @throws Throwable
* @throws Z3Exception on error
**/
protected void finalize() throws Throwable
{
try {
Native.delConstructorList(getContext().nCtx(), getNativeObject());
} finally {
super.finalize();
}
}
ConstructorList(Context ctx, long obj) ConstructorList(Context ctx, long obj)
{ {
super(ctx, obj); super(ctx, obj);
} }
@Override
void incRef(long o) {
getContext().getConstructorListDRQ().storeReference(getContext(), this);
}
ConstructorList(Context ctx, Constructor[] constructors) ConstructorList(Context ctx, Constructor[] constructors)
{ {
super(ctx); super(ctx, Native.mkConstructorList(ctx.nCtx(),
setNativeObject(Native.mkConstructorList(getContext().nCtx(),
constructors.length, constructors.length,
Constructor.arrayToNative(constructors))); Constructor.arrayToNative(constructors)));
} }

View file

@ -17,28 +17,29 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
import java.util.Map; import static com.microsoft.z3.Constructor.of;
import java.util.concurrent.atomic.AtomicInteger;
import com.microsoft.z3.enumerations.Z3_ast_print_mode; import com.microsoft.z3.enumerations.Z3_ast_print_mode;
import java.util.Map;
/** /**
* The main interaction with Z3 happens via the Context. * The main interaction with Z3 happens via the Context.
**/ **/
public class Context extends IDisposable public class Context implements AutoCloseable {
{ private final long m_ctx;
/** static final Object creation_lock = new Object();
* Constructor.
**/ public static Context mkContext() {
public Context() long m_ctx;
{
super();
synchronized (creation_lock) { synchronized (creation_lock) {
m_ctx = Native.mkContextRc(0); m_ctx = Native.mkContextRc(0);
initContext(); // TODO: then adding settings will not be under the lock.
} }
return new Context(m_ctx);
} }
/** /**
* Constructor. * Constructor.
* Remarks: * Remarks:
@ -56,17 +57,29 @@ public class Context extends IDisposable
* Note that in previous versions of Z3, this constructor was also used to set global and * Note that in previous versions of Z3, this constructor was also used to set global and
* module parameters. For this purpose we should now use {@code Global.setParameter} * module parameters. For this purpose we should now use {@code Global.setParameter}
**/ **/
public Context(Map<String, String> settings) public static Context mkContext(Map<String, String> settings)
{ {
super(); long m_ctx;
synchronized (creation_lock) { synchronized (creation_lock) {
long cfg = Native.mkConfig(); long cfg = Native.mkConfig();
for (Map.Entry<String, String> kv : settings.entrySet()) for (Map.Entry<String, String> kv : settings.entrySet())
Native.setParamValue(cfg, kv.getKey(), kv.getValue()); Native.setParamValue(cfg, kv.getKey(), kv.getValue());
m_ctx = Native.mkContextRc(cfg); m_ctx = Native.mkContextRc(cfg);
Native.delConfig(cfg); Native.delConfig(cfg);
initContext();
} }
return new Context(m_ctx);
}
/**
* Constructor.
**/
protected Context(long m_ctx)
{
this.m_ctx = m_ctx;
// Code which used to be in "initContext".
setPrintMode(Z3_ast_print_mode.Z3_PRINT_SMTLIB2_COMPLIANT);
Native.setInternalErrorHandler(m_ctx);
} }
/** /**
@ -242,7 +255,7 @@ public class Context extends IDisposable
checkContextMatch(name); checkContextMatch(name);
checkContextMatch(fieldNames); checkContextMatch(fieldNames);
checkContextMatch(fieldSorts); checkContextMatch(fieldSorts);
return new TupleSort(this, name, (int) fieldNames.length, fieldNames, return new TupleSort(this, name, fieldNames.length, fieldNames,
fieldSorts); fieldSorts);
} }
@ -319,8 +332,7 @@ public class Context extends IDisposable
Symbol[] fieldNames, Sort[] sorts, int[] sortRefs) Symbol[] fieldNames, Sort[] sorts, int[] sortRefs)
{ {
return of(this, name, recognizer, fieldNames, sorts,
return new Constructor(this, name, recognizer, fieldNames, sorts,
sortRefs); sortRefs);
} }
@ -329,10 +341,8 @@ public class Context extends IDisposable
**/ **/
public Constructor mkConstructor(String name, String recognizer, public Constructor mkConstructor(String name, String recognizer,
String[] fieldNames, Sort[] sorts, int[] sortRefs) String[] fieldNames, Sort[] sorts, int[] sortRefs)
{ {
return of(this, mkSymbol(name), mkSymbol(recognizer),
return new Constructor(this, mkSymbol(name), mkSymbol(recognizer),
mkSymbols(fieldNames), sorts, sortRefs); mkSymbols(fieldNames), sorts, sortRefs);
} }
@ -525,7 +535,7 @@ public class Context extends IDisposable
throw new Z3Exception("Cannot create a pattern from zero terms"); throw new Z3Exception("Cannot create a pattern from zero terms");
long[] termsNative = AST.arrayToNative(terms); long[] termsNative = AST.arrayToNative(terms);
return new Pattern(this, Native.mkPattern(nCtx(), (int) terms.length, return new Pattern(this, Native.mkPattern(nCtx(), terms.length,
termsNative)); termsNative));
} }
@ -688,7 +698,7 @@ public class Context extends IDisposable
public BoolExpr mkDistinct(Expr... args) public BoolExpr mkDistinct(Expr... args)
{ {
checkContextMatch(args); checkContextMatch(args);
return new BoolExpr(this, Native.mkDistinct(nCtx(), (int) args.length, return new BoolExpr(this, Native.mkDistinct(nCtx(), args.length,
AST.arrayToNative(args))); AST.arrayToNative(args)));
} }
@ -756,7 +766,7 @@ public class Context extends IDisposable
public BoolExpr mkAnd(BoolExpr... t) public BoolExpr mkAnd(BoolExpr... t)
{ {
checkContextMatch(t); checkContextMatch(t);
return new BoolExpr(this, Native.mkAnd(nCtx(), (int) t.length, return new BoolExpr(this, Native.mkAnd(nCtx(), t.length,
AST.arrayToNative(t))); AST.arrayToNative(t)));
} }
@ -766,7 +776,7 @@ public class Context extends IDisposable
public BoolExpr mkOr(BoolExpr... t) public BoolExpr mkOr(BoolExpr... t)
{ {
checkContextMatch(t); checkContextMatch(t);
return new BoolExpr(this, Native.mkOr(nCtx(), (int) t.length, return new BoolExpr(this, Native.mkOr(nCtx(), t.length,
AST.arrayToNative(t))); AST.arrayToNative(t)));
} }
@ -777,7 +787,7 @@ public class Context extends IDisposable
{ {
checkContextMatch(t); checkContextMatch(t);
return (ArithExpr) Expr.create(this, return (ArithExpr) Expr.create(this,
Native.mkAdd(nCtx(), (int) t.length, AST.arrayToNative(t))); Native.mkAdd(nCtx(), t.length, AST.arrayToNative(t)));
} }
/** /**
@ -787,7 +797,7 @@ public class Context extends IDisposable
{ {
checkContextMatch(t); checkContextMatch(t);
return (ArithExpr) Expr.create(this, return (ArithExpr) Expr.create(this,
Native.mkMul(nCtx(), (int) t.length, AST.arrayToNative(t))); Native.mkMul(nCtx(), t.length, AST.arrayToNative(t)));
} }
/** /**
@ -797,7 +807,7 @@ public class Context extends IDisposable
{ {
checkContextMatch(t); checkContextMatch(t);
return (ArithExpr) Expr.create(this, return (ArithExpr) Expr.create(this,
Native.mkSub(nCtx(), (int) t.length, AST.arrayToNative(t))); Native.mkSub(nCtx(), t.length, AST.arrayToNative(t)));
} }
/** /**
@ -1814,7 +1824,7 @@ public class Context extends IDisposable
{ {
checkContextMatch(args); checkContextMatch(args);
return (ArrayExpr)Expr.create(this, return (ArrayExpr)Expr.create(this,
Native.mkSetUnion(nCtx(), (int) args.length, Native.mkSetUnion(nCtx(), args.length,
AST.arrayToNative(args))); AST.arrayToNative(args)));
} }
@ -1825,7 +1835,7 @@ public class Context extends IDisposable
{ {
checkContextMatch(args); checkContextMatch(args);
return (ArrayExpr)Expr.create(this, return (ArrayExpr)Expr.create(this,
Native.mkSetIntersect(nCtx(), (int) args.length, Native.mkSetIntersect(nCtx(), args.length,
AST.arrayToNative(args))); AST.arrayToNative(args)));
} }
@ -1912,7 +1922,7 @@ public class Context extends IDisposable
public SeqExpr MkConcat(SeqExpr... t) public SeqExpr MkConcat(SeqExpr... t)
{ {
checkContextMatch(t); checkContextMatch(t);
return new SeqExpr(this, Native.mkSeqConcat(nCtx(), (int)t.length, AST.arrayToNative(t))); return new SeqExpr(this, Native.mkSeqConcat(nCtx(), t.length, AST.arrayToNative(t)));
} }
@ -2040,7 +2050,7 @@ public class Context extends IDisposable
public ReExpr MkConcat(ReExpr... t) public ReExpr MkConcat(ReExpr... t)
{ {
checkContextMatch(t); checkContextMatch(t);
return new ReExpr(this, Native.mkReConcat(nCtx(), (int)t.length, AST.arrayToNative(t))); return new ReExpr(this, Native.mkReConcat(nCtx(), t.length, AST.arrayToNative(t)));
} }
/** /**
@ -2049,7 +2059,7 @@ public class Context extends IDisposable
public ReExpr MkUnion(ReExpr... t) public ReExpr MkUnion(ReExpr... t)
{ {
checkContextMatch(t); checkContextMatch(t);
return new ReExpr(this, Native.mkReUnion(nCtx(), (int)t.length, AST.arrayToNative(t))); return new ReExpr(this, Native.mkReUnion(nCtx(), t.length, AST.arrayToNative(t)));
} }
@ -2258,7 +2268,7 @@ public class Context extends IDisposable
Symbol quantifierID, Symbol skolemID) Symbol quantifierID, Symbol skolemID)
{ {
return new Quantifier(this, true, sorts, names, body, weight, patterns, return Quantifier.of(this, true, sorts, names, body, weight, patterns,
noPatterns, quantifierID, skolemID); noPatterns, quantifierID, skolemID);
} }
@ -2271,7 +2281,7 @@ public class Context extends IDisposable
Symbol skolemID) Symbol skolemID)
{ {
return new Quantifier(this, true, boundConstants, body, weight, return Quantifier.of(this, true, boundConstants, body, weight,
patterns, noPatterns, quantifierID, skolemID); patterns, noPatterns, quantifierID, skolemID);
} }
@ -2284,7 +2294,7 @@ public class Context extends IDisposable
Symbol quantifierID, Symbol skolemID) Symbol quantifierID, Symbol skolemID)
{ {
return new Quantifier(this, false, sorts, names, body, weight, return Quantifier.of(this, false, sorts, names, body, weight,
patterns, noPatterns, quantifierID, skolemID); patterns, noPatterns, quantifierID, skolemID);
} }
@ -2297,7 +2307,7 @@ public class Context extends IDisposable
Symbol skolemID) Symbol skolemID)
{ {
return new Quantifier(this, false, boundConstants, body, weight, return Quantifier.of(this, false, boundConstants, body, weight,
patterns, noPatterns, quantifierID, skolemID); patterns, noPatterns, quantifierID, skolemID);
} }
@ -3814,7 +3824,7 @@ public class Context extends IDisposable
* must be a native object obtained from Z3 (e.g., through * must be a native object obtained from Z3 (e.g., through
* {@code UnwrapAST}) and that it must have a correct reference count. * {@code UnwrapAST}) and that it must have a correct reference count.
* @see Native#incRef * @see Native#incRef
* @see unwrapAST * @see #unwrapAST
* @param nativeObject The native pointer to wrap. * @param nativeObject The native pointer to wrap.
**/ **/
public AST wrapAST(long nativeObject) public AST wrapAST(long nativeObject)
@ -3869,19 +3879,12 @@ public class Context extends IDisposable
Native.updateParamValue(nCtx(), id, value); Native.updateParamValue(nCtx(), id, value);
} }
protected long m_ctx = 0;
protected static final Object creation_lock = new Object();
long nCtx() long nCtx()
{ {
return m_ctx; return m_ctx;
} }
void initContext()
{
setPrintMode(Z3_ast_print_mode.Z3_PRINT_SMTLIB2_COMPLIANT);
Native.setInternalErrorHandler(nCtx());
}
void checkContextMatch(Z3Object other) void checkContextMatch(Z3Object other)
{ {
@ -3925,110 +3928,103 @@ public class Context extends IDisposable
private TacticDecRefQueue m_Tactic_DRQ = new TacticDecRefQueue(10); private TacticDecRefQueue m_Tactic_DRQ = new TacticDecRefQueue(10);
private FixedpointDecRefQueue m_Fixedpoint_DRQ = new FixedpointDecRefQueue(10); private FixedpointDecRefQueue m_Fixedpoint_DRQ = new FixedpointDecRefQueue(10);
private OptimizeDecRefQueue m_Optimize_DRQ = new OptimizeDecRefQueue(10); private OptimizeDecRefQueue m_Optimize_DRQ = new OptimizeDecRefQueue(10);
private ConstructorDecRefQueue m_Constructor_DRQ = new ConstructorDecRefQueue(10);
private ConstructorListDecRefQueue m_ConstructorList_DRQ =
new ConstructorListDecRefQueue(10);
public IDecRefQueue getASTDRQ() public IDecRefQueue<Constructor> getConstructorDRQ() {
return m_Constructor_DRQ;
}
public IDecRefQueue<ConstructorList> getConstructorListDRQ() {
return m_ConstructorList_DRQ;
}
public IDecRefQueue<AST> getASTDRQ()
{ {
return m_AST_DRQ; return m_AST_DRQ;
} }
public IDecRefQueue getASTMapDRQ() public IDecRefQueue<ASTMap> getASTMapDRQ()
{ {
return m_ASTMap_DRQ; return m_ASTMap_DRQ;
} }
public IDecRefQueue getASTVectorDRQ() public IDecRefQueue<ASTVector> getASTVectorDRQ()
{ {
return m_ASTVector_DRQ; return m_ASTVector_DRQ;
} }
public IDecRefQueue getApplyResultDRQ() public IDecRefQueue<ApplyResult> getApplyResultDRQ()
{ {
return m_ApplyResult_DRQ; return m_ApplyResult_DRQ;
} }
public IDecRefQueue getFuncEntryDRQ() public IDecRefQueue<FuncInterp.Entry> getFuncEntryDRQ()
{ {
return m_FuncEntry_DRQ; return m_FuncEntry_DRQ;
} }
public IDecRefQueue getFuncInterpDRQ() public IDecRefQueue<FuncInterp> getFuncInterpDRQ()
{ {
return m_FuncInterp_DRQ; return m_FuncInterp_DRQ;
} }
public IDecRefQueue getGoalDRQ() public IDecRefQueue<Goal> getGoalDRQ()
{ {
return m_Goal_DRQ; return m_Goal_DRQ;
} }
public IDecRefQueue getModelDRQ() public IDecRefQueue<Model> getModelDRQ()
{ {
return m_Model_DRQ; return m_Model_DRQ;
} }
public IDecRefQueue getParamsDRQ() public IDecRefQueue<Params> getParamsDRQ()
{ {
return m_Params_DRQ; return m_Params_DRQ;
} }
public IDecRefQueue getParamDescrsDRQ() public IDecRefQueue<ParamDescrs> getParamDescrsDRQ()
{ {
return m_ParamDescrs_DRQ; return m_ParamDescrs_DRQ;
} }
public IDecRefQueue getProbeDRQ() public IDecRefQueue<Probe> getProbeDRQ()
{ {
return m_Probe_DRQ; return m_Probe_DRQ;
} }
public IDecRefQueue getSolverDRQ() public IDecRefQueue<Solver> getSolverDRQ()
{ {
return m_Solver_DRQ; return m_Solver_DRQ;
} }
public IDecRefQueue getStatisticsDRQ() public IDecRefQueue<Statistics> getStatisticsDRQ()
{ {
return m_Statistics_DRQ; return m_Statistics_DRQ;
} }
public IDecRefQueue getTacticDRQ() public IDecRefQueue<Tactic> getTacticDRQ()
{ {
return m_Tactic_DRQ; return m_Tactic_DRQ;
} }
public IDecRefQueue getFixedpointDRQ() public IDecRefQueue<Fixedpoint> getFixedpointDRQ()
{ {
return m_Fixedpoint_DRQ; return m_Fixedpoint_DRQ;
} }
public IDecRefQueue getOptimizeDRQ() public IDecRefQueue<Optimize> getOptimizeDRQ()
{ {
return m_Optimize_DRQ; return m_Optimize_DRQ;
} }
protected AtomicInteger m_refCount = new AtomicInteger(0);
/**
* Finalizer.
* @throws Throwable
**/
protected void finalize() throws Throwable
{
try {
dispose();
}
catch (Throwable t) {
throw t;
}
finally {
super.finalize();
}
}
/** /**
* Disposes of the context. * Disposes of the context.
**/ **/
public void dispose() @Override
public void close()
{ {
m_AST_DRQ.clear(this); m_AST_DRQ.clear(this);
m_ASTMap_DRQ.clear(this); m_ASTMap_DRQ.clear(this);
@ -4052,15 +4048,7 @@ public class Context extends IDisposable
m_stringSort = null; m_stringSort = null;
synchronized (creation_lock) { synchronized (creation_lock) {
if (m_refCount.get() == 0 && m_ctx != 0) {
try {
Native.delContext(m_ctx); Native.delContext(m_ctx);
} catch (Z3Exception e) {
// OK?
System.out.println("Context deletion failed; memory leak possible.");
}
m_ctx = 0;
}
} }
} }
} }

View file

@ -92,13 +92,9 @@ public class EnumSort extends Sort
EnumSort(Context ctx, Symbol name, Symbol[] enumNames) EnumSort(Context ctx, Symbol name, Symbol[] enumNames)
{ {
super(ctx, 0); super(ctx, Native.mkEnumerationSort(ctx.nCtx(),
name.getNativeObject(), enumNames.length,
int n = enumNames.length; Symbol.arrayToNative(enumNames),
long[] n_constdecls = new long[n]; new long[enumNames.length], new long[enumNames.length]));
long[] n_testers = new long[n];
setNativeObject(Native.mkEnumerationSort(ctx.nCtx(),
name.getNativeObject(), n, Symbol.arrayToNative(enumNames),
n_constdecls, n_testers));
} }
}; };

View file

@ -120,13 +120,13 @@ public class Expr extends AST
* @param args arguments * @param args arguments
* @throws Z3Exception on error * @throws Z3Exception on error
**/ **/
public void update(Expr[] args) public Expr update(Expr[] args)
{ {
getContext().checkContextMatch(args); getContext().checkContextMatch(args);
if (isApp() && args.length != getNumArgs()) { if (isApp() && args.length != getNumArgs()) {
throw new Z3Exception("Number of arguments does not match"); throw new Z3Exception("Number of arguments does not match");
} }
setNativeObject(Native.updateTerm(getContext().nCtx(), getNativeObject(), return new Expr(getContext(), Native.updateTerm(getContext().nCtx(), getNativeObject(),
args.length, Expr.arrayToNative(args))); args.length, Expr.arrayToNative(args)));
} }
@ -2091,36 +2091,23 @@ public class Expr extends AST
**/ **/
public int getIndex() public int getIndex()
{ {
if (!isVar()) if (!isVar()) {
throw new Z3Exception("Term is not a bound variable."); throw new Z3Exception("Term is not a bound variable.");
}
return Native.getIndexValue(getContext().nCtx(), getNativeObject()); return Native.getIndexValue(getContext().nCtx(), getNativeObject());
} }
/**
* Constructor for Expr
**/
protected Expr(Context ctx)
{
super(ctx);
{
}
}
/** /**
* Constructor for Expr * Constructor for Expr
* @throws Z3Exception on error * @throws Z3Exception on error
**/ **/
protected Expr(Context ctx, long obj) protected Expr(Context ctx, long obj) {
{
super(ctx, obj); super(ctx, obj);
{
}
} }
@Override @Override
void checkNativeObject(long obj) void checkNativeObject(long obj) {
{
if (!Native.isApp(getContext().nCtx(), obj) && if (!Native.isApp(getContext().nCtx(), obj) &&
Native.getAstKind(getContext().nCtx(), obj) != Z3_ast_kind.Z3_VAR_AST.toInt() && Native.getAstKind(getContext().nCtx(), obj) != Z3_ast_kind.Z3_VAR_AST.toInt() &&
Native.getAstKind(getContext().nCtx(), obj) != Z3_ast_kind.Z3_QUANTIFIER_AST.toInt()) { Native.getAstKind(getContext().nCtx(), obj) != Z3_ast_kind.Z3_QUANTIFIER_AST.toInt()) {

View file

@ -28,7 +28,7 @@ public class FPNum extends FPExpr
*/ */
public boolean getSign() { public boolean getSign() {
Native.IntPtr res = new Native.IntPtr(); Native.IntPtr res = new Native.IntPtr();
if (Native.fpaGetNumeralSign(getContext().nCtx(), getNativeObject(), res) ^ true) if (!Native.fpaGetNumeralSign(getContext().nCtx(), getNativeObject(), res))
throw new Z3Exception("Sign is not a Boolean value"); throw new Z3Exception("Sign is not a Boolean value");
return res.value != 0; return res.value != 0;
} }
@ -53,7 +53,7 @@ public class FPNum extends FPExpr
public long getSignificandUInt64() public long getSignificandUInt64()
{ {
Native.LongPtr res = new Native.LongPtr(); Native.LongPtr res = new Native.LongPtr();
if (Native.fpaGetNumeralSignificandUint64(getContext().nCtx(), getNativeObject(), res) ^ true) if (!Native.fpaGetNumeralSignificandUint64(getContext().nCtx(), getNativeObject(), res))
throw new Z3Exception("Significand is not a 64 bit unsigned integer"); throw new Z3Exception("Significand is not a 64 bit unsigned integer");
return res.value; return res.value;
} }
@ -72,7 +72,7 @@ public class FPNum extends FPExpr
*/ */
public long getExponentInt64() { public long getExponentInt64() {
Native.LongPtr res = new Native.LongPtr(); Native.LongPtr res = new Native.LongPtr();
if (Native.fpaGetNumeralExponentInt64(getContext().nCtx(), getNativeObject(), res) ^ true) if (!Native.fpaGetNumeralExponentInt64(getContext().nCtx(), getNativeObject(), res))
throw new Z3Exception("Exponent is not a 64 bit integer"); throw new Z3Exception("Exponent is not a 64 bit integer");
return res.value; return res.value;
} }

View file

@ -349,16 +349,11 @@ public class Fixedpoint extends Z3Object
} }
@Override @Override
void incRef(long o) void incRef(long o) {
{ Native.fixedpointIncRef(getContext().nCtx(), o);
getContext().getFixedpointDRQ().incAndClear(getContext(), o); getContext().getFixedpointDRQ().storeReference(getContext(), this);
super.incRef(o);
} }
@Override @Override
void decRef(long o) void checkNativeObject(long obj) { }
{
getContext().getFixedpointDRQ().add(o);
super.decRef(o);
}
} }

View file

@ -17,7 +17,7 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
class FixedpointDecRefQueue extends IDecRefQueue class FixedpointDecRefQueue extends IDecRefQueue<Fixedpoint>
{ {
public FixedpointDecRefQueue() public FixedpointDecRefQueue()
{ {
@ -29,27 +29,10 @@ class FixedpointDecRefQueue extends IDecRefQueue
super(move_limit); super(move_limit);
} }
@Override
protected void incRef(Context ctx, long obj)
{
try
{
Native.fixedpointIncRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
}
@Override @Override
protected void decRef(Context ctx, long obj) protected void decRef(Context ctx, long obj)
{
try
{ {
Native.fixedpointDecRef(ctx.nCtx(), obj); Native.fixedpointDecRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
} }
}; };

View file

@ -72,8 +72,6 @@ public class FuncInterp extends Z3Object
**/ **/
@Override @Override
public String toString() public String toString()
{
try
{ {
int n = getNumArgs(); int n = getNumArgs();
String res = "["; String res = "[";
@ -81,10 +79,6 @@ public class FuncInterp extends Z3Object
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
res += args[i] + ", "; res += args[i] + ", ";
return res + getValue() + "]"; return res + getValue() + "]";
} catch (Z3Exception e)
{
return "Z3Exception: " + e.getMessage();
}
} }
Entry(Context ctx, long obj) Entry(Context ctx, long obj)
@ -93,17 +87,9 @@ public class FuncInterp extends Z3Object
} }
@Override @Override
void incRef(long o) void incRef(long o) {
{ Native.funcEntryIncRef(getContext().nCtx(), o);;
getContext().getFuncEntryDRQ().incAndClear(getContext(), o); getContext().getFuncEntryDRQ().storeReference(getContext(), this);
super.incRef(o);
}
@Override
void decRef(long o)
{
getContext().getFuncEntryDRQ().add(o);
super.decRef(o);
} }
} }
@ -160,8 +146,6 @@ public class FuncInterp extends Z3Object
* A string representation of the function interpretation. * A string representation of the function interpretation.
**/ **/
public String toString() public String toString()
{
try
{ {
String res = ""; String res = "";
res += "["; res += "[";
@ -184,10 +168,6 @@ public class FuncInterp extends Z3Object
res += "else -> " + getElse(); res += "else -> " + getElse();
res += "]"; res += "]";
return res; return res;
} catch (Z3Exception e)
{
return "Z3Exception: " + e.getMessage();
}
} }
FuncInterp(Context ctx, long obj) FuncInterp(Context ctx, long obj)
@ -196,16 +176,8 @@ public class FuncInterp extends Z3Object
} }
@Override @Override
void incRef(long o) void incRef(long o) {
{ Native.funcInterpIncRef(getContext().nCtx(), o);
getContext().getFuncInterpDRQ().incAndClear(getContext(), o); getContext().getFuncInterpDRQ().storeReference(getContext(), this);
super.incRef(o);
}
@Override
void decRef(long o)
{
getContext().getFuncInterpDRQ().add(o);
super.decRef(o);
} }
} }

View file

@ -17,7 +17,7 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
class FuncInterpDecRefQueue extends IDecRefQueue class FuncInterpDecRefQueue extends IDecRefQueue<FuncInterp>
{ {
public FuncInterpDecRefQueue() public FuncInterpDecRefQueue()
{ {
@ -30,26 +30,7 @@ class FuncInterpDecRefQueue extends IDecRefQueue
} }
@Override @Override
protected void incRef(Context ctx, long obj) protected void decRef(Context ctx, long obj) {
{
try
{
Native.funcInterpIncRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
}
@Override
protected void decRef(Context ctx, long obj)
{
try
{
Native.funcInterpDecRef(ctx.nCtx(), obj); Native.funcInterpDecRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
} }
}; };

View file

@ -17,7 +17,7 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
class FuncInterpEntryDecRefQueue extends IDecRefQueue class FuncInterpEntryDecRefQueue extends IDecRefQueue<FuncInterp.Entry>
{ {
public FuncInterpEntryDecRefQueue() public FuncInterpEntryDecRefQueue()
{ {
@ -30,26 +30,7 @@ class FuncInterpEntryDecRefQueue extends IDecRefQueue
} }
@Override @Override
protected void incRef(Context ctx, long obj) protected void decRef(Context ctx, long obj) {
{
try
{
Native.funcEntryIncRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
}
@Override
protected void decRef(Context ctx, long obj)
{
try
{
Native.funcEntryDecRef(ctx.nCtx(), obj); Native.funcEntryDecRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
} }
}; };

View file

@ -222,11 +222,11 @@ public class Goal extends Z3Object
**/ **/
public BoolExpr AsBoolExpr() { public BoolExpr AsBoolExpr() {
int n = size(); int n = size();
if (n == 0) if (n == 0) {
return getContext().mkTrue(); return getContext().mkTrue();
else if (n == 1) } else if (n == 1) {
return getFormulas()[0]; return getFormulas()[0];
else { } else {
return getContext().mkAnd(getFormulas()); return getContext().mkAnd(getFormulas());
} }
} }
@ -236,23 +236,14 @@ public class Goal extends Z3Object
super(ctx, obj); super(ctx, obj);
} }
Goal(Context ctx, boolean models, boolean unsatCores, boolean proofs) Goal(Context ctx, boolean models, boolean unsatCores, boolean proofs) {
{
super(ctx, Native.mkGoal(ctx.nCtx(), (models), super(ctx, Native.mkGoal(ctx.nCtx(), (models),
(unsatCores), (proofs))); (unsatCores), (proofs)));
} }
void incRef(long o) @Override
{ void incRef(long o) {
getContext().getGoalDRQ().incAndClear(getContext(), o); Native.goalIncRef(getContext().nCtx(), o);
super.incRef(o); getContext().getGoalDRQ().storeReference(getContext(), this);
} }
void decRef(long o)
{
getContext().getGoalDRQ().add(o);
super.decRef(o);
}
} }

View file

@ -17,7 +17,7 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
class GoalDecRefQueue extends IDecRefQueue class GoalDecRefQueue extends IDecRefQueue<Goal>
{ {
public GoalDecRefQueue() public GoalDecRefQueue()
{ {
@ -30,26 +30,7 @@ class GoalDecRefQueue extends IDecRefQueue
} }
@Override @Override
protected void incRef(Context ctx, long obj) protected void decRef(Context ctx, long obj) {
{
try
{
Native.goalIncRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
}
@Override
protected void decRef(Context ctx, long obj)
{
try
{
Native.goalDecRef(ctx.nCtx(), obj); Native.goalDecRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
} }
}; };

View file

@ -17,13 +17,21 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
import java.util.LinkedList; import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.util.IdentityHashMap;
import java.util.Map;
public abstract class IDecRefQueue public abstract class IDecRefQueue<T extends Z3Object>
{ {
protected final Object m_lock = new Object(); private final int m_move_limit;
protected LinkedList<Long> m_queue = new LinkedList<Long>();
protected int m_move_limit; // TODO: problem: ReferenceQueue has no API to return length.
private final ReferenceQueue<T> referenceQueue = new ReferenceQueue<>();
private int queueSize = 0;
private final Map<PhantomReference<T>, Long> referenceMap =
new IdentityHashMap<>();
protected IDecRefQueue() protected IDecRefQueue()
{ {
@ -35,37 +43,30 @@ public abstract class IDecRefQueue
m_move_limit = move_limit; m_move_limit = move_limit;
} }
public void setLimit(int l) { m_move_limit = l; } /**
* An implementation of this method should decrement the reference on a
protected abstract void incRef(Context ctx, long obj); * given native object.
* This function should be always called on the {@code ctx} thread.
*
* @param ctx Z3 context.
* @param obj Pointer to a Z3 object.
*/
protected abstract void decRef(Context ctx, long obj); protected abstract void decRef(Context ctx, long obj);
protected void incAndClear(Context ctx, long o) public void storeReference(Context ctx, T obj) {
{ PhantomReference<T> ref = new PhantomReference<>(obj, referenceQueue);
incRef(ctx, o); referenceMap.put(ref, obj.getNativeObject());
if (m_queue.size() >= m_move_limit)
// TODO: use move_limit, somehow get the size of referenceQueue.
clear(ctx); clear(ctx);
} }
protected void add(long o)
{
if (o == 0)
return;
synchronized (m_lock)
{
m_queue.add(o);
}
}
protected void clear(Context ctx) protected void clear(Context ctx)
{ {
synchronized (m_lock) Reference<? extends T> ref;
{ while ((ref = referenceQueue.poll()) != null) {
for (Long o : m_queue) long z3ast = referenceMap.remove(ref);
decRef(ctx, o); decRef(ctx, z3ast);
m_queue.clear();
} }
} }
} }

View file

@ -1,25 +0,0 @@
/*++
Copyright (c) 2012 Microsoft Corporation
Module Name:
IDisposable.java
Abstract:
Compatability interface (C# -> Java)
Author:
Christoph Wintersteiger (cwinter) 2012-03-16
Notes:
--*/
package com.microsoft.z3;
public abstract class IDisposable
{
public abstract void dispose();
}

View file

@ -17,11 +17,10 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
import java.util.Map;
import java.lang.String;
import com.microsoft.z3.enumerations.Z3_lbool; import com.microsoft.z3.enumerations.Z3_lbool;
import java.util.Map;
/** /**
* The InterpolationContext is suitable for generation of interpolants. * The InterpolationContext is suitable for generation of interpolants.
* *
@ -33,13 +32,13 @@ public class InterpolationContext extends Context
/** /**
* Constructor. * Constructor.
**/ **/
public InterpolationContext() public static InterpolationContext mkContext()
{ {
super(); long m_ctx;
synchronized(creation_lock) { synchronized(creation_lock) {
m_ctx = Native.mkInterpolationContext(0); m_ctx = Native.mkInterpolationContext(0);
initContext();
} }
return new InterpolationContext(m_ctx);
} }
/** /**
@ -49,17 +48,21 @@ public class InterpolationContext extends Context
* Remarks: * Remarks:
* @see Context#Context * @see Context#Context
**/ **/
public InterpolationContext(Map<String, String> settings) public static InterpolationContext mkContext(Map<String, String> settings)
{ {
super(); long m_ctx;
synchronized(creation_lock) { synchronized(creation_lock) {
long cfg = Native.mkConfig(); long cfg = Native.mkConfig();
for (Map.Entry<String, String> kv : settings.entrySet()) for (Map.Entry<String, String> kv : settings.entrySet())
Native.setParamValue(cfg, kv.getKey(), kv.getValue()); Native.setParamValue(cfg, kv.getKey(), kv.getValue());
m_ctx = Native.mkInterpolationContext(cfg); m_ctx = Native.mkInterpolationContext(cfg);
Native.delConfig(cfg); Native.delConfig(cfg);
initContext();
} }
return new InterpolationContext(m_ctx);
}
private InterpolationContext(long m_ctx) {
super(m_ctx);
} }
/** /**

View file

@ -17,6 +17,8 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
import com.microsoft.z3.Native.LongPtr;
/** /**
* List sorts. * List sorts.
**/ **/
@ -88,14 +90,9 @@ public class ListSort extends Sort
ListSort(Context ctx, Symbol name, Sort elemSort) ListSort(Context ctx, Symbol name, Sort elemSort)
{ {
super(ctx, 0); super(ctx, Native.mkListSort(ctx.nCtx(), name.getNativeObject(),
elemSort.getNativeObject(),
Native.LongPtr inil = new Native.LongPtr(), iisnil = new Native.LongPtr(); new LongPtr(), new Native.LongPtr(), new LongPtr(),
Native.LongPtr icons = new Native.LongPtr(), iiscons = new Native.LongPtr(); new LongPtr(), new LongPtr(), new LongPtr()));
Native.LongPtr ihead = new Native.LongPtr(), itail = new Native.LongPtr();
setNativeObject(Native.mkListSort(ctx.nCtx(), name.getNativeObject(),
elemSort.getNativeObject(), inil, iisnil, icons, iiscons, ihead,
itail));
} }
}; };

View file

@ -293,16 +293,8 @@ public class Model extends Z3Object
} }
@Override @Override
void incRef(long o) void incRef(long o) {
{ Native.modelIncRef(getContext().nCtx(), o);
getContext().getModelDRQ().incAndClear(getContext(), o); getContext().getModelDRQ().storeReference(getContext(), this);
super.incRef(o);
}
@Override
void decRef(long o)
{
getContext().getModelDRQ().add(o);
super.decRef(o);
} }
} }

View file

@ -17,7 +17,7 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
class ModelDecRefQueue extends IDecRefQueue class ModelDecRefQueue extends IDecRefQueue<Model>
{ {
public ModelDecRefQueue() public ModelDecRefQueue()
{ {
@ -30,26 +30,7 @@ class ModelDecRefQueue extends IDecRefQueue
} }
@Override @Override
protected void incRef(Context ctx, long obj) protected void decRef(Context ctx, long obj) {
{
try
{
Native.modelIncRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
}
@Override
protected void decRef(Context ctx, long obj)
{
try
{
Native.modelDecRef(ctx.nCtx(), obj); Native.modelDecRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
} }
}; };

View file

@ -266,17 +266,8 @@ public class Optimize extends Z3Object
} }
@Override @Override
void incRef(long o) void incRef(long o) {
{ Native.optimizeIncRef(getContext().nCtx(), o);
getContext().getOptimizeDRQ().incAndClear(getContext(), o); getContext().getOptimizeDRQ().storeReference(getContext(), this);
super.incRef(o);
} }
@Override
void decRef(long o)
{
getContext().getOptimizeDRQ().add(o);
super.decRef(o);
}
} }

View file

@ -17,8 +17,7 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
class OptimizeDecRefQueue extends IDecRefQueue class OptimizeDecRefQueue extends IDecRefQueue<Optimize> {
{
public OptimizeDecRefQueue() public OptimizeDecRefQueue()
{ {
super(); super();
@ -30,26 +29,7 @@ class OptimizeDecRefQueue extends IDecRefQueue
} }
@Override @Override
protected void incRef(Context ctx, long obj) protected void decRef(Context ctx, long obj) {
{ Native.optimizeDecRef(ctx.nCtx(), obj);
try
{
Native.fixedpointIncRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
}
@Override
protected void decRef(Context ctx, long obj)
{
try
{
Native.fixedpointDecRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
} }
}; };

View file

@ -92,16 +92,8 @@ public class ParamDescrs extends Z3Object
} }
@Override @Override
void incRef(long o) void incRef(long o) {
{ Native.paramDescrsIncRef(getContext().nCtx(), o);
getContext().getParamDescrsDRQ().incAndClear(getContext(), o); getContext().getParamDescrsDRQ().storeReference(getContext(), this);
super.incRef(o);
}
@Override
void decRef(long o)
{
getContext().getParamDescrsDRQ().add(o);
super.decRef(o);
} }
} }

View file

@ -17,39 +17,20 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
class ParamDescrsDecRefQueue extends IDecRefQueue class ParamDescrsDecRefQueue extends IDecRefQueue<ParamDescrs>
{ {
public ParamDescrsDecRefQueue() public ParamDescrsDecRefQueue()
{ {
super(); super();
} }
public ParamDescrsDecRefQueue(int move_limit) public ParamDescrsDecRefQueue(int move_limit) {
{
super(move_limit); super(move_limit);
} }
@Override
protected void incRef(Context ctx, long obj)
{
try
{
Native.paramDescrsIncRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
}
@Override @Override
protected void decRef(Context ctx, long obj) protected void decRef(Context ctx, long obj)
{
try
{ {
Native.paramDescrsDecRef(ctx.nCtx(), obj); Native.paramDescrsDecRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
} }
}; };

View file

@ -124,16 +124,8 @@ public class Params extends Z3Object
} }
@Override @Override
void incRef(long o) void incRef(long o) {
{ Native.paramsIncRef(getContext().nCtx(), o);
getContext().getParamsDRQ().incAndClear(getContext(), o); getContext().getParamsDRQ().storeReference(getContext(), this);
super.incRef(o);
}
@Override
void decRef(long o)
{
getContext().getParamsDRQ().add(o);
super.decRef(o);
} }
} }

View file

@ -17,7 +17,7 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
class ParamsDecRefQueue extends IDecRefQueue class ParamsDecRefQueue extends IDecRefQueue<Params>
{ {
public ParamsDecRefQueue() public ParamsDecRefQueue()
{ {
@ -30,26 +30,7 @@ class ParamsDecRefQueue extends IDecRefQueue
} }
@Override @Override
protected void incRef(Context ctx, long obj) protected void decRef(Context ctx, long obj) {
{
try
{
Native.paramsIncRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
}
@Override
protected void decRef(Context ctx, long obj)
{
try
{
Native.paramsDecRef(ctx.nCtx(), obj); Native.paramsDecRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
} }
}; };

View file

@ -54,14 +54,7 @@ public class Probe extends Z3Object
@Override @Override
void incRef(long o) void incRef(long o)
{ {
getContext().getProbeDRQ().incAndClear(getContext(), o); Native.probeIncRef(getContext().nCtx(), o);
super.incRef(o); getContext().getProbeDRQ().storeReference(getContext(), this);
}
@Override
void decRef(long o)
{
getContext().getProbeDRQ().add(o);
super.decRef(o);
} }
} }

View file

@ -17,7 +17,7 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
class ProbeDecRefQueue extends IDecRefQueue class ProbeDecRefQueue extends IDecRefQueue<Probe>
{ {
public ProbeDecRefQueue() public ProbeDecRefQueue()
{ {
@ -29,27 +29,9 @@ class ProbeDecRefQueue extends IDecRefQueue
super(move_limit); super(move_limit);
} }
@Override
protected void incRef(Context ctx, long obj)
{
try
{
Native.probeIncRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
}
@Override @Override
protected void decRef(Context ctx, long obj) protected void decRef(Context ctx, long obj)
{
try
{ {
Native.probeDecRef(ctx.nCtx(), obj); Native.probeDecRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
} }
}; };

View file

@ -145,68 +145,67 @@ public class Quantifier extends BoolExpr
.nCtx(), getNativeObject())); .nCtx(), getNativeObject()));
} }
Quantifier(Context ctx, boolean isForall, Sort[] sorts, Symbol[] names, public static Quantifier of(
Context ctx, boolean isForall, Sort[] sorts, Symbol[] names,
Expr body, int weight, Pattern[] patterns, Expr[] noPatterns, Expr body, int weight, Pattern[] patterns, Expr[] noPatterns,
Symbol quantifierID, Symbol skolemID) Symbol quantifierID, Symbol skolemID
{ ) {
super(ctx, 0); ctx.checkContextMatch(patterns);
ctx.checkContextMatch(noPatterns);
getContext().checkContextMatch(patterns); ctx.checkContextMatch(sorts);
getContext().checkContextMatch(noPatterns); ctx.checkContextMatch(names);
getContext().checkContextMatch(sorts); ctx.checkContextMatch(body);
getContext().checkContextMatch(names);
getContext().checkContextMatch(body);
if (sorts.length != names.length) if (sorts.length != names.length)
throw new Z3Exception( throw new Z3Exception(
"Number of sorts does not match number of names"); "Number of sorts does not match number of names");
if (noPatterns == null && quantifierID == null && skolemID == null) long nativeObj;
{ if (noPatterns == null && quantifierID == null && skolemID == null) {
setNativeObject(Native.mkQuantifier(ctx.nCtx(), (isForall), weight, AST.arrayLength(patterns), AST nativeObj = Native.mkQuantifier(ctx.nCtx(), (isForall), weight, AST.arrayLength(patterns), AST
.arrayToNative(patterns), AST.arrayLength(sorts), AST .arrayToNative(patterns), AST.arrayLength(sorts), AST
.arrayToNative(sorts), Symbol.arrayToNative(names), body .arrayToNative(sorts), Symbol.arrayToNative(names), body
.getNativeObject())); .getNativeObject());
} else } else {
{ nativeObj = Native.mkQuantifierEx(ctx.nCtx(),
setNativeObject(Native.mkQuantifierEx(ctx.nCtx(),
(isForall), weight, AST.getNativeObject(quantifierID), (isForall), weight, AST.getNativeObject(quantifierID),
AST.getNativeObject(skolemID), AST.getNativeObject(skolemID),
AST.arrayLength(patterns), AST.arrayToNative(patterns), AST.arrayLength(patterns), AST.arrayToNative(patterns),
AST.arrayLength(noPatterns), AST.arrayToNative(noPatterns), AST.arrayLength(noPatterns), AST.arrayToNative(noPatterns),
AST.arrayLength(sorts), AST.arrayToNative(sorts), AST.arrayLength(sorts), AST.arrayToNative(sorts),
Symbol.arrayToNative(names), Symbol.arrayToNative(names),
body.getNativeObject())); body.getNativeObject());
} }
return new Quantifier(ctx, nativeObj);
} }
Quantifier(Context ctx, boolean isForall, Expr[] bound, Expr body,
public static Quantifier of(Context ctx, boolean isForall, Expr[] bound, Expr body,
int weight, Pattern[] patterns, Expr[] noPatterns, int weight, Pattern[] patterns, Expr[] noPatterns,
Symbol quantifierID, Symbol skolemID) Symbol quantifierID, Symbol skolemID)
{ {
super(ctx, 0); ctx.checkContextMatch(noPatterns);
ctx.checkContextMatch(patterns);
getContext().checkContextMatch(noPatterns); // ctx.CheckContextMatch(bound);
getContext().checkContextMatch(patterns); ctx.checkContextMatch(body);
// Context().CheckContextMatch(bound);
getContext().checkContextMatch(body);
long nativeObj;
if (noPatterns == null && quantifierID == null && skolemID == null) if (noPatterns == null && quantifierID == null && skolemID == null)
{ {
setNativeObject(Native.mkQuantifierConst(ctx.nCtx(), nativeObj = Native.mkQuantifierConst(ctx.nCtx(),
isForall, weight, AST.arrayLength(bound), isForall, weight, AST.arrayLength(bound),
AST.arrayToNative(bound), AST.arrayLength(patterns), AST.arrayToNative(bound), AST.arrayLength(patterns),
AST.arrayToNative(patterns), body.getNativeObject())); AST.arrayToNative(patterns), body.getNativeObject());
} else } else {
{ nativeObj = Native.mkQuantifierConstEx(ctx.nCtx(),
setNativeObject(Native.mkQuantifierConstEx(ctx.nCtx(),
isForall, weight, isForall, weight,
AST.getNativeObject(quantifierID), AST.getNativeObject(quantifierID),
AST.getNativeObject(skolemID), AST.arrayLength(bound), AST.getNativeObject(skolemID), AST.arrayLength(bound),
AST.arrayToNative(bound), AST.arrayLength(patterns), AST.arrayToNative(bound), AST.arrayLength(patterns),
AST.arrayToNative(patterns), AST.arrayLength(noPatterns), AST.arrayToNative(patterns), AST.arrayLength(noPatterns),
AST.arrayToNative(noPatterns), body.getNativeObject())); AST.arrayToNative(noPatterns), body.getNativeObject());
} }
return new Quantifier(ctx, nativeObj);
} }
Quantifier(Context ctx, long obj) Quantifier(Context ctx, long obj)

View file

@ -127,13 +127,13 @@ public class Solver extends Z3Object
* using the Boolean constants in ps. * using the Boolean constants in ps.
* *
* Remarks: * Remarks:
* This API is an alternative to {@link check} with assumptions for * This API is an alternative to {@link #check()} with assumptions for
* extracting unsat cores. * extracting unsat cores.
* Both APIs can be used in the same solver. The unsat core will contain a * Both APIs can be used in the same solver. The unsat core will contain a
* combination * combination
* of the Boolean variables provided using {@link assertAndTrack} * of the Boolean variables provided using {@code #assertAndTrack}
* and the Boolean literals * and the Boolean literals
* provided using {@link check} with assumptions. * provided using {@link #check()} with assumptions.
**/ **/
public void assertAndTrack(BoolExpr[] constraints, BoolExpr[] ps) public void assertAndTrack(BoolExpr[] constraints, BoolExpr[] ps)
{ {
@ -333,16 +333,8 @@ public class Solver extends Z3Object
} }
@Override @Override
void incRef(long o) void incRef(long o) {
{ Native.solverIncRef(getContext().nCtx(), o);
getContext().getSolverDRQ().incAndClear(getContext(), o); getContext().getSolverDRQ().storeReference(getContext(), this);
super.incRef(o);
}
@Override
void decRef(long o)
{
getContext().getSolverDRQ().add(o);
super.decRef(o);
} }
} }

View file

@ -17,7 +17,7 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
class SolverDecRefQueue extends IDecRefQueue class SolverDecRefQueue extends IDecRefQueue<Solver>
{ {
public SolverDecRefQueue() { super(); } public SolverDecRefQueue() { super(); }
@ -27,26 +27,7 @@ class SolverDecRefQueue extends IDecRefQueue
} }
@Override @Override
protected void incRef(Context ctx, long obj) protected void decRef(Context ctx, long obj) {
{
try
{
Native.solverIncRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
}
@Override
protected void decRef(Context ctx, long obj)
{
try
{
Native.solverDecRef(ctx.nCtx(), obj); Native.solverDecRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
} }
}; };

View file

@ -95,6 +95,7 @@ public class Sort extends AST
super(ctx, obj); super(ctx, obj);
} }
@Override
void checkNativeObject(long obj) void checkNativeObject(long obj)
{ {
if (Native.getAstKind(getContext().nCtx(), obj) != Z3_ast_kind.Z3_SORT_AST if (Native.getAstKind(getContext().nCtx(), obj) != Z3_ast_kind.Z3_SORT_AST

View file

@ -190,15 +190,9 @@ public class Statistics extends Z3Object
super(ctx, obj); super(ctx, obj);
} }
void incRef(long o) @Override
{ void incRef(long o) {
getContext().getStatisticsDRQ().incAndClear(getContext(), o); Native.statsIncRef(getContext().nCtx(), o);
super.incRef(o); getContext().getStatisticsDRQ().storeReference(getContext(), this);
}
void decRef(long o)
{
getContext().getStatisticsDRQ().add(o);
super.decRef(o);
} }
} }

View file

@ -17,7 +17,7 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
class StatisticsDecRefQueue extends IDecRefQueue class StatisticsDecRefQueue extends IDecRefQueue<Statistics>
{ {
public StatisticsDecRefQueue() public StatisticsDecRefQueue()
{ {
@ -29,25 +29,8 @@ class StatisticsDecRefQueue extends IDecRefQueue
super(move_limit); super(move_limit);
} }
protected void incRef(Context ctx, long obj) @Override
{ protected void decRef(Context ctx, long obj) {
try
{
Native.statsIncRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
}
protected void decRef(Context ctx, long obj)
{
try
{
Native.statsDecRef(ctx.nCtx(), obj); Native.statsDecRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
} }
}; };

View file

@ -48,9 +48,9 @@ public class StringSymbol extends Symbol
void checkNativeObject(long obj) void checkNativeObject(long obj)
{ {
if (Native.getSymbolKind(getContext().nCtx(), obj) != Z3_symbol_kind.Z3_STRING_SYMBOL if (Native.getSymbolKind(getContext().nCtx(), obj) != Z3_symbol_kind.Z3_STRING_SYMBOL
.toInt()) .toInt()) {
throw new Z3Exception("Symbol is not of String kind"); throw new Z3Exception("Symbol is not of String kind");
}
super.checkNativeObject(obj); super.checkNativeObject(obj);
} }
} }

View file

@ -62,19 +62,13 @@ public class Symbol extends Z3Object
* A string representation of the symbol. * A string representation of the symbol.
**/ **/
@Override @Override
public String toString() public String toString() {
{ if (isIntSymbol()) {
try
{
if (isIntSymbol())
return Integer.toString(((IntSymbol) this).getInt()); return Integer.toString(((IntSymbol) this).getInt());
else if (isStringSymbol()) } else if (isStringSymbol()) {
return ((StringSymbol) this).getString(); return ((StringSymbol) this).getString();
else } else {
return "Z3Exception: Unknown symbol kind encountered."; return "Z3Exception: Unknown symbol kind encountered.";
} catch (Z3Exception ex)
{
return "Z3Exception: " + ex.getMessage();
} }
} }
@ -86,6 +80,11 @@ public class Symbol extends Z3Object
super(ctx, obj); super(ctx, obj);
} }
@Override
void incRef(long o) {
// Symbol does not require tracking.
}
static Symbol create(Context ctx, long obj) static Symbol create(Context ctx, long obj)
{ {
switch (Z3_symbol_kind.fromInt(Native.getSymbolKind(ctx.nCtx(), obj))) switch (Z3_symbol_kind.fromInt(Native.getSymbolKind(ctx.nCtx(), obj)))

View file

@ -92,15 +92,10 @@ public class Tactic extends Z3Object
super(ctx, Native.mkTactic(ctx.nCtx(), name)); super(ctx, Native.mkTactic(ctx.nCtx(), name));
} }
@Override
void incRef(long o) void incRef(long o)
{ {
getContext().getTacticDRQ().incAndClear(getContext(), o); Native.tacticIncRef(getContext().nCtx(), o);
super.incRef(o); getContext().getTacticDRQ().storeReference(getContext(), this);
}
void decRef(long o)
{
getContext().getTacticDRQ().add(o);
super.decRef(o);
} }
} }

View file

@ -17,8 +17,7 @@ Notes:
package com.microsoft.z3; package com.microsoft.z3;
class TacticDecRefQueue extends IDecRefQueue class TacticDecRefQueue extends IDecRefQueue<Tactic> {
{
public TacticDecRefQueue() public TacticDecRefQueue()
{ {
super(); super();
@ -29,25 +28,9 @@ class TacticDecRefQueue extends IDecRefQueue
super(move_limit); super(move_limit);
} }
protected void incRef(Context ctx, long obj) @Override
{
try
{
Native.tacticIncRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
}
}
protected void decRef(Context ctx, long obj) protected void decRef(Context ctx, long obj)
{
try
{ {
Native.tacticDecRef(ctx.nCtx(), obj); Native.tacticDecRef(ctx.nCtx(), obj);
} catch (Z3Exception e)
{
// OK.
} }
} }
};

View file

@ -59,11 +59,9 @@ public class TupleSort extends Sort
TupleSort(Context ctx, Symbol name, int numFields, Symbol[] fieldNames, TupleSort(Context ctx, Symbol name, int numFields, Symbol[] fieldNames,
Sort[] fieldSorts) Sort[] fieldSorts)
{ {
super(ctx, 0); super(ctx, Native.mkTupleSort(ctx.nCtx(), name.getNativeObject(),
Native.LongPtr t = new Native.LongPtr();
setNativeObject(Native.mkTupleSort(ctx.nCtx(), name.getNativeObject(),
numFields, Symbol.arrayToNative(fieldNames), numFields, Symbol.arrayToNative(fieldNames),
AST.arrayToNative(fieldSorts), t, new long[numFields])); AST.arrayToNative(fieldSorts), new Native.LongPtr(),
new long[numFields]));
} }
}; };

View file

@ -21,88 +21,38 @@ package com.microsoft.z3;
* Internal base class for interfacing with native Z3 objects. Should not be * Internal base class for interfacing with native Z3 objects. Should not be
* used externally. * used externally.
**/ **/
public class Z3Object extends IDisposable public abstract class Z3Object {
{
/**
* Finalizer.
* @throws Throwable
**/
protected void finalize() throws Throwable
{
try {
dispose();
} finally {
super.finalize();
}
}
/** private final Context m_ctx;
* Disposes of the underlying native Z3 object. private final long m_n_obj;
**/
public void dispose()
{
if (m_n_obj != 0)
{
decRef(m_n_obj);
m_n_obj = 0;
}
if (m_ctx != null) Z3Object(Context ctx, long obj) {
{
if (m_ctx.m_refCount.decrementAndGet() == 0)
m_ctx.dispose();
m_ctx = null;
}
}
private Context m_ctx = null;
private long m_n_obj = 0;
Z3Object(Context ctx)
{
ctx.m_refCount.incrementAndGet();
m_ctx = ctx;
}
Z3Object(Context ctx, long obj)
{
ctx.m_refCount.incrementAndGet();
m_ctx = ctx; m_ctx = ctx;
checkNativeObject(obj);
incRef(obj); incRef(obj);
m_n_obj = obj; m_n_obj = obj;
} }
void incRef(long o) /**
{ * Increment reference count on {@code o}.
} *
* @param o Z3 object.
*/
abstract void incRef(long o);
void decRef(long o) /**
{ * This function is provided for overriding, and a child class
} * can insert consistency checks on {@code obj}.
*
void checkNativeObject(long obj) * @param obj Z3 native object.
{ */
} void checkNativeObject(long obj) {}
long getNativeObject() long getNativeObject()
{ {
return m_n_obj; return m_n_obj;
} }
void setNativeObject(long value)
{
if (value != 0)
{
checkNativeObject(value);
incRef(value);
}
if (m_n_obj != 0)
{
decRef(m_n_obj);
}
m_n_obj = value;
}
static long getNativeObject(Z3Object s) static long getNativeObject(Z3Object s)
{ {
if (s == null) if (s == null)