Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Java phantom references #7131

Merged
merged 7 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/api/java/AST.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import com.microsoft.z3.enumerations.Z3_ast_kind;

import java.lang.ref.ReferenceQueue;

/**
* The abstract syntax tree (AST) class.
**/
Expand Down Expand Up @@ -196,7 +198,7 @@ void incRef() {

@Override
void addToReferenceQueue() {
getContext().getASTDRQ().storeReference(getContext(), this);
getContext().getReferenceQueue().storeReference(this, ASTRef::new);
}

static AST create(Context ctx, long obj)
Expand All @@ -217,4 +219,16 @@ static AST create(Context ctx, long obj)
throw new Z3Exception("Unknown AST kind");
}
}

private static class ASTRef extends Z3ReferenceQueue.Reference<AST> {

private ASTRef(AST referent, ReferenceQueue<Z3Object> q) {
super(referent, q);
}

@Override
void decRef(Context ctx, long z3Obj) {
Native.decRef(ctx.nCtx(), z3Obj);
}
}
}
31 changes: 0 additions & 31 deletions src/api/java/ASTDecRefQueue.java

This file was deleted.

16 changes: 15 additions & 1 deletion src/api/java/ASTMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.microsoft.z3;

import java.lang.ref.ReferenceQueue;

/**
* Map from AST to AST
**/
Expand Down Expand Up @@ -123,6 +125,18 @@ void incRef() {

@Override
void addToReferenceQueue() {
getContext().getASTMapDRQ().storeReference(getContext(), this);
getContext().getReferenceQueue().storeReference(this, ASTMapRef::new);
}

private static class ASTMapRef extends Z3ReferenceQueue.Reference<ASTMap> {

private ASTMapRef(ASTMap referent, ReferenceQueue<Z3Object> q) {
super(referent, q);
}

@Override
void decRef(Context ctx, long z3Obj) {
Native.astMapDecRef(ctx.nCtx(), z3Obj);
}
}
}
34 changes: 24 additions & 10 deletions src/api/java/ASTVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.microsoft.z3;

import java.lang.ref.ReferenceQueue;

/**
* Vectors of ASTs.
**/
Expand Down Expand Up @@ -101,16 +103,6 @@ public ASTVector(Context ctx)
super(ctx, Native.mkAstVector(ctx.nCtx()));
}

@Override
void incRef() {
Native.astVectorIncRef(getContext().nCtx(), getNativeObject());
}

@Override
void addToReferenceQueue() {
getContext().getASTVectorDRQ().storeReference(getContext(), this);
}

/**
* Translates the AST vector into an AST[]
* */
Expand Down Expand Up @@ -241,4 +233,26 @@ public RealExpr[] ToRealExprArray()
res[i] = (RealExpr)Expr.create(getContext(), get(i).getNativeObject());
return res;
}

@Override
void incRef() {
Native.astVectorIncRef(getContext().nCtx(), getNativeObject());
}

@Override
void addToReferenceQueue() {
getContext().getReferenceQueue().storeReference(this, ASTVectorRef::new);
}

private static class ASTVectorRef extends Z3ReferenceQueue.Reference<ASTVector> {

private ASTVectorRef(ASTVector referent, ReferenceQueue<Z3Object> q) {
super(referent, q);
}

@Override
void decRef(Context ctx, long z3Obj) {
Native.astVectorDecRef(ctx.nCtx(), z3Obj);
}
}
}
16 changes: 15 additions & 1 deletion src/api/java/ApplyResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.microsoft.z3;

import java.lang.ref.ReferenceQueue;

/**
* ApplyResult objects represent the result of an application of a tactic to a
* goal. It contains the subgoals that were produced.
Expand Down Expand Up @@ -66,6 +68,18 @@ void incRef() {

@Override
void addToReferenceQueue() {
getContext().getApplyResultDRQ().storeReference(getContext(), this);
getContext().getReferenceQueue().storeReference(this, ApplyResultRef::new);
}

private static class ApplyResultRef extends Z3ReferenceQueue.Reference<ApplyResult> {

private ApplyResultRef(ApplyResult referent, ReferenceQueue<Z3Object> q) {
super(referent, q);
}

@Override
void decRef(Context ctx, long z3Obj) {
Native.applyResultDecRef(ctx.nCtx(), z3Obj);
}
}
}
31 changes: 0 additions & 31 deletions src/api/java/ApplyResultDecRefQueue.java

This file was deleted.

30 changes: 0 additions & 30 deletions src/api/java/AstMapDecRefQueue.java

This file was deleted.

30 changes: 0 additions & 30 deletions src/api/java/AstVectorDecRefQueue.java

This file was deleted.

21 changes: 1 addition & 20 deletions src/api/java/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,21 @@ add_custom_command(OUTPUT ${Z3_JAVA_ENUMERATION_PACKAGE_FILES_FULL_PATH}

set(Z3_JAVA_JAR_SOURCE_FILES
AlgebraicNum.java
ApplyResultDecRefQueue.java
ApplyResult.java
ArithExpr.java
ArithSort.java
ArrayExpr.java
ArraySort.java
ASTDecRefQueue.java
AST.java
AstMapDecRefQueue.java
ASTMap.java
AstVectorDecRefQueue.java
ASTVector.java
BitVecExpr.java
BitVecNum.java
BitVecSort.java
BoolExpr.java
BoolSort.java
CharSort.java
ConstructorDecRefQueue.java
Constructor.java
ConstructorListDecRefQueue.java
ConstructorList.java
Context.java
DatatypeExpr.java
Expand All @@ -121,7 +115,6 @@ set(Z3_JAVA_JAR_SOURCE_FILES
FiniteDomainExpr.java
FiniteDomainNum.java
FiniteDomainSort.java
FixedpointDecRefQueue.java
Fixedpoint.java
FPExpr.java
FPNum.java
Expand All @@ -130,30 +123,21 @@ set(Z3_JAVA_JAR_SOURCE_FILES
FPRMSort.java
FPSort.java
FuncDecl.java
FuncInterpDecRefQueue.java
FuncInterpEntryDecRefQueue.java
FuncInterp.java
Global.java
GoalDecRefQueue.java
Goal.java
IDecRefQueue.java
IntExpr.java
IntNum.java
IntSort.java
IntSymbol.java
Lambda.java
ListSort.java
Log.java
ModelDecRefQueue.java
Model.java
OptimizeDecRefQueue.java
Optimize.java
ParamDescrsDecRefQueue.java
ParamDescrs.java
ParamsDecRefQueue.java
Params.java
Pattern.java
ProbeDecRefQueue.java
Probe.java
Quantifier.java
RatNum.java
Expand All @@ -166,23 +150,20 @@ set(Z3_JAVA_JAR_SOURCE_FILES
SeqSort.java
SetSort.java
Simplifier.java
SimplifierDecRefQueue.java
SolverDecRefQueue.java
Solver.java
Sort.java
StatisticsDecRefQueue.java
Statistics.java
Status.java
StringSymbol.java
Symbol.java
TacticDecRefQueue.java
Tactic.java
TupleSort.java
UninterpretedSort.java
UserPropagatorBase.java
Version.java
Z3Exception.java
Z3Object.java
Z3ReferenceQueue.java
)
set(Z3_JAVA_JAR_SOURCE_FILES_FULL_PATH "")
foreach (java_src_file ${Z3_JAVA_JAR_SOURCE_FILES})
Expand Down
16 changes: 15 additions & 1 deletion src/api/java/Constructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.microsoft.z3;

import java.lang.ref.ReferenceQueue;

/**
* Constructors are used for datatype sorts.
**/
Expand Down Expand Up @@ -91,7 +93,7 @@ void incRef() {

@Override
void addToReferenceQueue() {
getContext().getConstructorDRQ().storeReference(getContext(), this);
getContext().getReferenceQueue().storeReference(this, ConstructorRef::new);
}

static <R> Constructor<R> of(Context ctx, Symbol name, Symbol recognizer,
Expand All @@ -114,4 +116,16 @@ static <R> Constructor<R> of(Context ctx, Symbol name, Symbol recognizer,
return new Constructor<>(ctx, n, nativeObj);

}

private static class ConstructorRef extends Z3ReferenceQueue.Reference<Constructor<?>> {

private ConstructorRef(Constructor<?> referent, ReferenceQueue<Z3Object> q) {
super(referent, q);
}

@Override
void decRef(Context ctx, long z3Obj) {
Native.delConstructor(ctx.nCtx(), z3Obj);
}
}
}
Loading
Loading