3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-01-24 19:14:00 +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

@ -21,88 +21,38 @@ package com.microsoft.z3;
* Internal base class for interfacing with native Z3 objects. Should not be
* used externally.
**/
public class Z3Object extends IDisposable
{
/**
* Finalizer.
* @throws Throwable
**/
protected void finalize() throws Throwable
{
try {
dispose();
} finally {
super.finalize();
}
}
public abstract class Z3Object {
/**
* Disposes of the underlying native Z3 object.
**/
public void dispose()
{
if (m_n_obj != 0)
{
decRef(m_n_obj);
m_n_obj = 0;
}
private final Context m_ctx;
private final long m_n_obj;
if (m_ctx != null)
{
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();
Z3Object(Context ctx, long obj) {
m_ctx = ctx;
checkNativeObject(obj);
incRef(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)
{
}
void checkNativeObject(long obj)
{
}
/**
* This function is provided for overriding, and a child class
* can insert consistency checks on {@code obj}.
*
* @param obj Z3 native object.
*/
void checkNativeObject(long obj) {}
long getNativeObject()
{
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)
{
if (s == null)
@ -121,7 +71,7 @@ public class Z3Object extends IDisposable
return null;
long[] an = new long[a.length];
for (int i = 0; i < a.length; i++)
an[i] = (a[i] == null) ? 0 : a[i].getNativeObject();
an[i] = (a[i] == null) ? 0 : a[i].getNativeObject();
return an;
}