Merge "Added CloseGuard for BaseObj" into nyc-dev

This commit is contained in:
Yang Ni
2016-04-01 00:33:57 +00:00
committed by Android (Google) Code Review
11 changed files with 42 additions and 2 deletions

View File

@@ -380,6 +380,7 @@ public class Allocation extends BaseObj {
Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e); Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e); throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
} }
guard.open("destroy");
} }
Allocation(long id, RenderScript rs, Type t, int usage, MipmapControl mips) { Allocation(long id, RenderScript rs, Type t, int usage, MipmapControl mips) {
@@ -1915,6 +1916,7 @@ public class Allocation extends BaseObj {
if (type.getID(rs) == 0) { if (type.getID(rs) == 0) {
throw new RSInvalidStateException("Bad Type"); throw new RSInvalidStateException("Bad Type");
} }
// TODO: What if there is an exception after this? The native allocation would leak.
long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0); long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
if (id == 0) { if (id == 0) {
throw new RSRuntimeException("Allocation creation failed."); throw new RSRuntimeException("Allocation creation failed.");

View File

@@ -16,6 +16,7 @@
package android.renderscript; package android.renderscript;
import dalvik.system.CloseGuard;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
/** /**
@@ -69,6 +70,7 @@ public class BaseObj {
} }
private long mID; private long mID;
final CloseGuard guard = CloseGuard.get();
private boolean mDestroyed; private boolean mDestroyed;
private String mName; private String mName;
RenderScript mRS; RenderScript mRS;
@@ -119,6 +121,7 @@ public class BaseObj {
} }
if (shouldDestroy) { if (shouldDestroy) {
guard.close();
// must include nObjDestroy in the critical section // must include nObjDestroy in the critical section
ReentrantReadWriteLock.ReadLock rlock = mRS.mRWLock.readLock(); ReentrantReadWriteLock.ReadLock rlock = mRS.mRWLock.readLock();
rlock.lock(); rlock.lock();
@@ -133,8 +136,14 @@ public class BaseObj {
} }
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
helpDestroy(); try {
super.finalize(); if (guard != null) {
guard.warnIfOpen();
}
helpDestroy();
} finally {
super.finalize();
}
} }
/** /**

View File

@@ -808,6 +808,7 @@ public class Element extends BaseObj {
mSize += mElements[ct].mSize * mArraySizes[ct]; mSize += mElements[ct].mSize * mArraySizes[ct];
} }
updateVisibleSubElements(); updateVisibleSubElements();
guard.open("destroy");
} }
Element(long id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) { Element(long id, RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
@@ -827,6 +828,7 @@ public class Element extends BaseObj {
mKind = dk; mKind = dk;
mNormalized = norm; mNormalized = norm;
mVectorSize = size; mVectorSize = size;
guard.open("destroy");
} }
Element(long id, RenderScript rs) { Element(long id, RenderScript rs) {

View File

@@ -170,6 +170,7 @@ public class FileA3D extends BaseObj {
FileA3D(long id, RenderScript rs, InputStream stream) { FileA3D(long id, RenderScript rs, InputStream stream) {
super(id, rs); super(id, rs);
mInputStream = stream; mInputStream = stream;
guard.open("destroy");
} }
private void initEntries() { private void initEntries() {

View File

@@ -150,6 +150,7 @@ public class Font extends BaseObj {
Font(long id, RenderScript rs) { Font(long id, RenderScript rs) {
super(id, rs); super(id, rs);
guard.open("destroy");
} }
/** /**

View File

@@ -91,6 +91,7 @@ public class Mesh extends BaseObj {
Mesh(long id, RenderScript rs) { Mesh(long id, RenderScript rs) {
super(id, rs); super(id, rs);
guard.open("destroy");
} }
/** /**

View File

@@ -76,6 +76,7 @@ public class Program extends BaseObj {
Program(long id, RenderScript rs) { Program(long id, RenderScript rs) {
super(id, rs); super(id, rs);
guard.open("destroy");
} }
/** /**

View File

@@ -51,6 +51,7 @@ public class Sampler extends BaseObj {
Sampler(long id, RenderScript rs) { Sampler(long id, RenderScript rs) {
super(id, rs); super(id, rs);
guard.open("destroy");
} }
/** /**

View File

@@ -41,6 +41,7 @@ public class Script extends BaseObj {
mScript = s; mScript = s;
mSlot = slot; mSlot = slot;
mSig = sig; mSig = sig;
guard.open("destroy");
} }
} }
@@ -118,6 +119,7 @@ public class Script extends BaseObj {
super(id, rs); super(id, rs);
mScript = s; mScript = s;
mSlot = slot; mSlot = slot;
guard.open("destroy");
} }
} }
@@ -357,6 +359,19 @@ public class Script extends BaseObj {
super(id, rs); super(id, rs);
mInIdsBuffer = new long[1]; mInIdsBuffer = new long[1];
/* The constructors for the derived classes (including ScriptIntrinsic
* derived classes and ScriptC derived classes generated by Slang
* reflection) seem to be simple enough, so we just put the guard.open()
* call here, rather than in the end of the constructor for the derived
* class. This, of course, assumes the derived constructor would not
* throw any exception after calling this constructor.
*
* If new derived classes are added with more complicated constructors
* that throw exceptions, this call has to be (duplicated and) moved
* to the end of each derived class constructor.
*/
guard.open("destroy");
} }
/** /**

View File

@@ -148,6 +148,8 @@ public final class ScriptGroup extends BaseObj {
fieldIDs, values, sizes, depClosures, depFieldIDs); fieldIDs, values, sizes, depClosures, depFieldIDs);
setID(id); setID(id);
guard.open("destroy");
} }
Closure(RenderScript rs, Script.InvokeID invokeID, Closure(RenderScript rs, Script.InvokeID invokeID,
@@ -181,6 +183,8 @@ public final class ScriptGroup extends BaseObj {
values, sizes); values, sizes);
setID(id); setID(id);
guard.open("destroy");
} }
private void retrieveValueAndDependenceInfo(RenderScript rs, private void retrieveValueAndDependenceInfo(RenderScript rs,
@@ -382,6 +386,7 @@ public final class ScriptGroup extends BaseObj {
ScriptGroup(long id, RenderScript rs) { ScriptGroup(long id, RenderScript rs) {
super(id, rs); super(id, rs);
guard.open("destroy");
} }
ScriptGroup(RenderScript rs, String name, List<Closure> closures, ScriptGroup(RenderScript rs, String name, List<Closure> closures,
@@ -398,6 +403,7 @@ public final class ScriptGroup extends BaseObj {
} }
long id = rs.nScriptGroup2Create(name, RenderScript.getCachePath(), closureIDs); long id = rs.nScriptGroup2Create(name, RenderScript.getCachePath(), closureIDs);
setID(id); setID(id);
guard.open("destroy");
} }
/** /**

View File

@@ -227,6 +227,7 @@ public class Type extends BaseObj {
Type(long id, RenderScript rs) { Type(long id, RenderScript rs) {
super(id, rs); super(id, rs);
guard.open("destroy");
} }
@Override @Override