Z3
IDecRefQueue.java
Go to the documentation of this file.
1 
18 package com.microsoft.z3;
19 
20 import java.lang.ref.PhantomReference;
21 import java.lang.ref.Reference;
22 import java.lang.ref.ReferenceQueue;
23 import java.util.IdentityHashMap;
24 import java.util.Map;
25 
39 public 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 storeReference(Context ctx, T obj)
void forceClear(Context ctx)
abstract void decRef(Context ctx, long obj)