summaryrefslogtreecommitdiff
path: root/core/java/android
diff options
context:
space:
mode:
authorPhilip P. Moltmann <moltmann@google.com>2018-01-25 13:35:31 -0800
committerPhilip P. Moltmann <moltmann@google.com>2018-01-25 13:38:38 -0800
commit6d8f30bee76f5f9cd97697a7aa5108c73df12057 (patch)
tree1137aba22cd410c54c5ddffd9c35b70498d31d78 /core/java/android
parentf88fdc995fbd23ecc3e9b6b8a94186cd96969085 (diff)
Add logging to MemoryIntArray
Test: Booted and saw logging Bug: 72415980 Change-Id: I828c74b2e703a8bd3cce9f325e5747f9243fe485
Diffstat (limited to 'core/java/android')
-rw-r--r--core/java/android/util/MemoryIntArray.java51
1 files changed, 44 insertions, 7 deletions
diff --git a/core/java/android/util/MemoryIntArray.java b/core/java/android/util/MemoryIntArray.java
index bf335196edef..597089235e6b 100644
--- a/core/java/android/util/MemoryIntArray.java
+++ b/core/java/android/util/MemoryIntArray.java
@@ -16,13 +16,19 @@
package android.util;
+import static android.os.Process.FIRST_APPLICATION_UID;
+
import android.os.Parcel;
import android.os.ParcelFileDescriptor;
import android.os.Parcelable;
+import android.os.Process;
+
+import com.android.internal.annotations.GuardedBy;
-import libcore.io.IoUtils;
import dalvik.system.CloseGuard;
+import libcore.io.IoUtils;
+
import java.io.Closeable;
import java.io.IOException;
import java.util.UUID;
@@ -49,13 +55,18 @@ import java.util.UUID;
*/
public final class MemoryIntArray implements Parcelable, Closeable {
private static final String TAG = "MemoryIntArray";
+ private static final boolean DEBUG = Process.myUid() < FIRST_APPLICATION_UID;
private static final int MAX_SIZE = 1024;
+ private final Object mLock = new Object();
private final CloseGuard mCloseGuard = CloseGuard.get();
private final boolean mIsOwner;
private final long mMemoryAddr;
+
+ /** Fd for the shared memory object, -1 when closed */
+ @GuardedBy("mLock")
private int mFd = -1;
/**
@@ -74,6 +85,7 @@ public final class MemoryIntArray implements Parcelable, Closeable {
mFd = nativeCreate(name, size);
mMemoryAddr = nativeOpen(mFd, mIsOwner);
mCloseGuard.open("close");
+ if (DEBUG) Log.i(TAG, "created " + getString());
}
private MemoryIntArray(Parcel parcel) throws IOException {
@@ -85,6 +97,8 @@ public final class MemoryIntArray implements Parcelable, Closeable {
mFd = pfd.detachFd();
mMemoryAddr = nativeOpen(mFd, mIsOwner);
mCloseGuard.open("close");
+
+ if (DEBUG) Log.i(TAG, "created from parcel " + getString());
}
/**
@@ -141,13 +155,33 @@ public final class MemoryIntArray implements Parcelable, Closeable {
*/
@Override
public void close() throws IOException {
- if (!isClosed()) {
- nativeClose(mFd, mMemoryAddr, mIsOwner);
- mFd = -1;
- mCloseGuard.close();
+ synchronized (mLock) {
+ if (!isClosed()) {
+ if (DEBUG) {
+ try {
+ throw new Exception();
+ } catch (Exception here) {
+ Log.i(TAG, "closing " + getString(), here);
+ }
+ }
+ nativeClose(mFd, mMemoryAddr, mIsOwner);
+ mFd = -1;
+ mCloseGuard.close();
+ } else {
+ try {
+ throw new Exception();
+ } catch (Exception here) {
+ if (DEBUG) Log.i(TAG, getString() + " already closed", here);
+ }
+ }
}
}
+ private String getString() {
+ return this.getClass().getSimpleName() + "@" + System.identityHashCode(this)
+ + " mMemoryAddr=" + mMemoryAddr + " mFd=" + mFd;
+ }
+
/**
* @return Whether this array is closed and shouldn't be used.
*/
@@ -162,7 +196,9 @@ public final class MemoryIntArray implements Parcelable, Closeable {
mCloseGuard.warnIfOpen();
}
- IoUtils.closeQuietly(this);
+ if (!isClosed()) {
+ IoUtils.closeQuietly(this);
+ }
} finally {
super.finalize();
}
@@ -206,7 +242,8 @@ public final class MemoryIntArray implements Parcelable, Closeable {
private void enforceNotClosed() {
if (isClosed()) {
- throw new IllegalStateException("cannot interact with a closed instance");
+ throw new IllegalStateException("cannot interact with a closed instance "
+ + getString());
}
}