diff options
| author | Eugene Susla <eugenesusla@google.com> | 2019-05-13 22:21:48 +0000 |
|---|---|---|
| committer | Android (Google) Code Review <android-gerrit@google.com> | 2019-05-13 22:21:48 +0000 |
| commit | 1198fc4e7101db6cc7206594d489bb92b3d954ac (patch) | |
| tree | 8deff0ca01d81b6ffb316aab79f4c351be4584f0 /core/java/android | |
| parent | e7933dba63499c13bf8e74f0d9d4a0c49b521be1 (diff) | |
| parent | d2404405e77a1adbba7d17af30640374f6428308 (diff) | |
Merge "Revert "MemoryIntArray: track the owned file descriptor in a PFD."" into qt-dev
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/util/MemoryIntArray.java | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/core/java/android/util/MemoryIntArray.java b/core/java/android/util/MemoryIntArray.java index 74fea3f4dd30..80b16075cdf6 100644 --- a/core/java/android/util/MemoryIntArray.java +++ b/core/java/android/util/MemoryIntArray.java @@ -20,9 +20,8 @@ import android.os.Parcel; import android.os.ParcelFileDescriptor; import android.os.Parcelable; -import dalvik.system.CloseGuard; - import libcore.io.IoUtils; +import dalvik.system.CloseGuard; import java.io.Closeable; import java.io.IOException; @@ -57,7 +56,7 @@ public final class MemoryIntArray implements Parcelable, Closeable { private final boolean mIsOwner; private final long mMemoryAddr; - private ParcelFileDescriptor mFd; + private int mFd = -1; /** * Creates a new instance. @@ -72,8 +71,8 @@ public final class MemoryIntArray implements Parcelable, Closeable { } mIsOwner = true; final String name = UUID.randomUUID().toString(); - mFd = ParcelFileDescriptor.adoptFd(nativeCreate(name, size)); - mMemoryAddr = nativeOpen(mFd.getFd(), mIsOwner); + mFd = nativeCreate(name, size); + mMemoryAddr = nativeOpen(mFd, mIsOwner); mCloseGuard.open("close"); } @@ -83,8 +82,8 @@ public final class MemoryIntArray implements Parcelable, Closeable { if (pfd == null) { throw new IOException("No backing file descriptor"); } - mFd = ParcelFileDescriptor.adoptFd(pfd.detachFd()); - mMemoryAddr = nativeOpen(mFd.getFd(), mIsOwner); + mFd = pfd.detachFd(); + mMemoryAddr = nativeOpen(mFd, mIsOwner); mCloseGuard.open("close"); } @@ -106,7 +105,7 @@ public final class MemoryIntArray implements Parcelable, Closeable { public int get(int index) throws IOException { enforceNotClosed(); enforceValidIndex(index); - return nativeGet(mFd.getFd(), mMemoryAddr, index); + return nativeGet(mFd, mMemoryAddr, index); } /** @@ -122,7 +121,7 @@ public final class MemoryIntArray implements Parcelable, Closeable { enforceNotClosed(); enforceWritable(); enforceValidIndex(index); - nativeSet(mFd.getFd(), mMemoryAddr, index, value); + nativeSet(mFd, mMemoryAddr, index, value); } /** @@ -132,7 +131,7 @@ public final class MemoryIntArray implements Parcelable, Closeable { */ public int size() throws IOException { enforceNotClosed(); - return nativeSize(mFd.getFd()); + return nativeSize(mFd); } /** @@ -143,9 +142,8 @@ public final class MemoryIntArray implements Parcelable, Closeable { @Override public void close() throws IOException { if (!isClosed()) { - nativeClose(mFd.getFd(), mMemoryAddr, mIsOwner); - mFd.close(); - mFd = null; + nativeClose(mFd, mMemoryAddr, mIsOwner); + mFd = -1; mCloseGuard.close(); } } @@ -154,7 +152,7 @@ public final class MemoryIntArray implements Parcelable, Closeable { * @return Whether this array is closed and shouldn't be used. */ public boolean isClosed() { - return mFd == null; + return mFd == -1; } @Override @@ -177,8 +175,13 @@ public final class MemoryIntArray implements Parcelable, Closeable { @Override public void writeToParcel(Parcel parcel, int flags) { - // Don't let writing to a parcel to close our fd - plz - parcel.writeParcelable(mFd, flags & ~Parcelable.PARCELABLE_WRITE_RETURN_VALUE); + ParcelFileDescriptor pfd = ParcelFileDescriptor.adoptFd(mFd); + try { + // Don't let writing to a parcel to close our fd - plz + parcel.writeParcelable(pfd, flags & ~Parcelable.PARCELABLE_WRITE_RETURN_VALUE); + } finally { + pfd.detachFd(); + } } @Override @@ -192,13 +195,13 @@ public final class MemoryIntArray implements Parcelable, Closeable { if (getClass() != obj.getClass()) { return false; } - - return false; + MemoryIntArray other = (MemoryIntArray) obj; + return mFd == other.mFd; } @Override public int hashCode() { - return mFd.hashCode(); + return mFd; } private void enforceNotClosed() { |
