Skip to content

Commit a3d00ce

Browse files
authored
Improved Java phantom references (#7131)
* Reworked phantom reference handling. - Replaced IDecRefQueue with a new Z3ReferenceQueue class - Z3ReferenceQueue manages custom subclasses of phantom references in a doubly-linked list - Replaced all subclasses of IDecRefQueue with subclasses of Z3ReferenceQueue.Reference. These custom reference classes are embedded in the class they reference count. - Context now owns a single Z3ReferenceQueue for all types of references. * Made Statistics.Entry a static subclass * Made Context.close idempotent (as recommended) * Update CMakeLists.txt for building the Java API. * Updated CMakeLists.txt again. * Use correct SuppressWarning annotation to silence the compiler * Formatting
1 parent f7691d3 commit a3d00ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+448
-805
lines changed

src/api/java/AST.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import com.microsoft.z3.enumerations.Z3_ast_kind;
2121

22+
import java.lang.ref.ReferenceQueue;
23+
2224
/**
2325
* The abstract syntax tree (AST) class.
2426
**/
@@ -196,7 +198,7 @@ void incRef() {
196198

197199
@Override
198200
void addToReferenceQueue() {
199-
getContext().getASTDRQ().storeReference(getContext(), this);
201+
getContext().getReferenceQueue().storeReference(this, ASTRef::new);
200202
}
201203

202204
static AST create(Context ctx, long obj)
@@ -217,4 +219,16 @@ static AST create(Context ctx, long obj)
217219
throw new Z3Exception("Unknown AST kind");
218220
}
219221
}
222+
223+
private static class ASTRef extends Z3ReferenceQueue.Reference<AST> {
224+
225+
private ASTRef(AST referent, ReferenceQueue<Z3Object> q) {
226+
super(referent, q);
227+
}
228+
229+
@Override
230+
void decRef(Context ctx, long z3Obj) {
231+
Native.decRef(ctx.nCtx(), z3Obj);
232+
}
233+
}
220234
}

src/api/java/ASTDecRefQueue.java

-31
This file was deleted.

src/api/java/ASTMap.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package com.microsoft.z3;
1919

20+
import java.lang.ref.ReferenceQueue;
21+
2022
/**
2123
* Map from AST to AST
2224
**/
@@ -123,6 +125,18 @@ void incRef() {
123125

124126
@Override
125127
void addToReferenceQueue() {
126-
getContext().getASTMapDRQ().storeReference(getContext(), this);
128+
getContext().getReferenceQueue().storeReference(this, ASTMapRef::new);
129+
}
130+
131+
private static class ASTMapRef extends Z3ReferenceQueue.Reference<ASTMap> {
132+
133+
private ASTMapRef(ASTMap referent, ReferenceQueue<Z3Object> q) {
134+
super(referent, q);
135+
}
136+
137+
@Override
138+
void decRef(Context ctx, long z3Obj) {
139+
Native.astMapDecRef(ctx.nCtx(), z3Obj);
140+
}
127141
}
128142
}

src/api/java/ASTVector.java

+24-10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package com.microsoft.z3;
1919

20+
import java.lang.ref.ReferenceQueue;
21+
2022
/**
2123
* Vectors of ASTs.
2224
**/
@@ -101,16 +103,6 @@ public ASTVector(Context ctx)
101103
super(ctx, Native.mkAstVector(ctx.nCtx()));
102104
}
103105

104-
@Override
105-
void incRef() {
106-
Native.astVectorIncRef(getContext().nCtx(), getNativeObject());
107-
}
108-
109-
@Override
110-
void addToReferenceQueue() {
111-
getContext().getASTVectorDRQ().storeReference(getContext(), this);
112-
}
113-
114106
/**
115107
* Translates the AST vector into an AST[]
116108
* */
@@ -241,4 +233,26 @@ public RealExpr[] ToRealExprArray()
241233
res[i] = (RealExpr)Expr.create(getContext(), get(i).getNativeObject());
242234
return res;
243235
}
236+
237+
@Override
238+
void incRef() {
239+
Native.astVectorIncRef(getContext().nCtx(), getNativeObject());
240+
}
241+
242+
@Override
243+
void addToReferenceQueue() {
244+
getContext().getReferenceQueue().storeReference(this, ASTVectorRef::new);
245+
}
246+
247+
private static class ASTVectorRef extends Z3ReferenceQueue.Reference<ASTVector> {
248+
249+
private ASTVectorRef(ASTVector referent, ReferenceQueue<Z3Object> q) {
250+
super(referent, q);
251+
}
252+
253+
@Override
254+
void decRef(Context ctx, long z3Obj) {
255+
Native.astVectorDecRef(ctx.nCtx(), z3Obj);
256+
}
257+
}
244258
}

src/api/java/ApplyResult.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package com.microsoft.z3;
1919

20+
import java.lang.ref.ReferenceQueue;
21+
2022
/**
2123
* ApplyResult objects represent the result of an application of a tactic to a
2224
* goal. It contains the subgoals that were produced.
@@ -66,6 +68,18 @@ void incRef() {
6668

6769
@Override
6870
void addToReferenceQueue() {
69-
getContext().getApplyResultDRQ().storeReference(getContext(), this);
71+
getContext().getReferenceQueue().storeReference(this, ApplyResultRef::new);
72+
}
73+
74+
private static class ApplyResultRef extends Z3ReferenceQueue.Reference<ApplyResult> {
75+
76+
private ApplyResultRef(ApplyResult referent, ReferenceQueue<Z3Object> q) {
77+
super(referent, q);
78+
}
79+
80+
@Override
81+
void decRef(Context ctx, long z3Obj) {
82+
Native.applyResultDecRef(ctx.nCtx(), z3Obj);
83+
}
7084
}
7185
}

src/api/java/ApplyResultDecRefQueue.java

-31
This file was deleted.

src/api/java/AstMapDecRefQueue.java

-30
This file was deleted.

src/api/java/AstVectorDecRefQueue.java

-30
This file was deleted.

src/api/java/CMakeLists.txt

+1-20
Original file line numberDiff line numberDiff line change
@@ -91,27 +91,21 @@ add_custom_command(OUTPUT ${Z3_JAVA_ENUMERATION_PACKAGE_FILES_FULL_PATH}
9191

9292
set(Z3_JAVA_JAR_SOURCE_FILES
9393
AlgebraicNum.java
94-
ApplyResultDecRefQueue.java
9594
ApplyResult.java
9695
ArithExpr.java
9796
ArithSort.java
9897
ArrayExpr.java
9998
ArraySort.java
100-
ASTDecRefQueue.java
10199
AST.java
102-
AstMapDecRefQueue.java
103100
ASTMap.java
104-
AstVectorDecRefQueue.java
105101
ASTVector.java
106102
BitVecExpr.java
107103
BitVecNum.java
108104
BitVecSort.java
109105
BoolExpr.java
110106
BoolSort.java
111107
CharSort.java
112-
ConstructorDecRefQueue.java
113108
Constructor.java
114-
ConstructorListDecRefQueue.java
115109
ConstructorList.java
116110
Context.java
117111
DatatypeExpr.java
@@ -121,7 +115,6 @@ set(Z3_JAVA_JAR_SOURCE_FILES
121115
FiniteDomainExpr.java
122116
FiniteDomainNum.java
123117
FiniteDomainSort.java
124-
FixedpointDecRefQueue.java
125118
Fixedpoint.java
126119
FPExpr.java
127120
FPNum.java
@@ -130,30 +123,21 @@ set(Z3_JAVA_JAR_SOURCE_FILES
130123
FPRMSort.java
131124
FPSort.java
132125
FuncDecl.java
133-
FuncInterpDecRefQueue.java
134-
FuncInterpEntryDecRefQueue.java
135126
FuncInterp.java
136127
Global.java
137-
GoalDecRefQueue.java
138128
Goal.java
139-
IDecRefQueue.java
140129
IntExpr.java
141130
IntNum.java
142131
IntSort.java
143132
IntSymbol.java
144133
Lambda.java
145134
ListSort.java
146135
Log.java
147-
ModelDecRefQueue.java
148136
Model.java
149-
OptimizeDecRefQueue.java
150137
Optimize.java
151-
ParamDescrsDecRefQueue.java
152138
ParamDescrs.java
153-
ParamsDecRefQueue.java
154139
Params.java
155140
Pattern.java
156-
ProbeDecRefQueue.java
157141
Probe.java
158142
Quantifier.java
159143
RatNum.java
@@ -166,23 +150,20 @@ set(Z3_JAVA_JAR_SOURCE_FILES
166150
SeqSort.java
167151
SetSort.java
168152
Simplifier.java
169-
SimplifierDecRefQueue.java
170-
SolverDecRefQueue.java
171153
Solver.java
172154
Sort.java
173-
StatisticsDecRefQueue.java
174155
Statistics.java
175156
Status.java
176157
StringSymbol.java
177158
Symbol.java
178-
TacticDecRefQueue.java
179159
Tactic.java
180160
TupleSort.java
181161
UninterpretedSort.java
182162
UserPropagatorBase.java
183163
Version.java
184164
Z3Exception.java
185165
Z3Object.java
166+
Z3ReferenceQueue.java
186167
)
187168
set(Z3_JAVA_JAR_SOURCE_FILES_FULL_PATH "")
188169
foreach (java_src_file ${Z3_JAVA_JAR_SOURCE_FILES})

src/api/java/Constructor.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package com.microsoft.z3;
1919

20+
import java.lang.ref.ReferenceQueue;
21+
2022
/**
2123
* Constructors are used for datatype sorts.
2224
**/
@@ -91,7 +93,7 @@ void incRef() {
9193

9294
@Override
9395
void addToReferenceQueue() {
94-
getContext().getConstructorDRQ().storeReference(getContext(), this);
96+
getContext().getReferenceQueue().storeReference(this, ConstructorRef::new);
9597
}
9698

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

116118
}
119+
120+
private static class ConstructorRef extends Z3ReferenceQueue.Reference<Constructor<?>> {
121+
122+
private ConstructorRef(Constructor<?> referent, ReferenceQueue<Z3Object> q) {
123+
super(referent, q);
124+
}
125+
126+
@Override
127+
void decRef(Context ctx, long z3Obj) {
128+
Native.delConstructor(ctx.nCtx(), z3Obj);
129+
}
130+
}
117131
}

0 commit comments

Comments
 (0)