Z3
IDecRefQueue.java
Go to the documentation of this file.
1
18package com.microsoft.z3;
19
20import java.lang.ref.PhantomReference;
21import java.lang.ref.Reference;
22import java.lang.ref.ReferenceQueue;
23import java.util.IdentityHashMap;
24import java.util.Map;
25
39public abstract class IDecRefQueue<T extends Z3Object> {
40 private final ReferenceQueue<T> referenceQueue = new ReferenceQueue<>();
41 private final Map<PhantomReference<T>, Long> referenceMap =
42 new IdentityHashMap<>();
43
44 protected IDecRefQueue() {}
45
54 protected abstract void decRef(Context ctx, long obj);
55
56 public void storeReference(Context ctx, T obj) {
57 PhantomReference<T> ref = new PhantomReference<>(obj, referenceQueue);
58 referenceMap.put(ref, obj.getNativeObject());
59 clear(ctx);
60 }
61
65 protected void clear(Context ctx)
66 {
67 Reference<? extends T> ref;
68 while ((ref = referenceQueue.poll()) != null) {
69 long z3ast = referenceMap.remove(ref);
70 decRef(ctx, z3ast);
71 }
72 }
73
78 public void forceClear(Context ctx) {
79 for (long ref : referenceMap.values()) {
80 decRef(ctx, ref);
81 }
82 }
83}
void forceClear(Context ctx)
void storeReference(Context ctx, T obj)
abstract void decRef(Context ctx, long obj)