From 39c4f40746acd46213b9f909a553765a431aaeea Mon Sep 17 00:00:00 2001 From: Keun young Park Date: Wed, 26 May 2021 18:32:15 -0700 Subject: [PATCH] Add SharedMemory.fromFileDescriptor - Rename existing SharedMemory.create and make it public - Passed file is now detached after creating shared memory and is not usable any more. Bug: 188780895 CTS-Coverage-Bug: 197226773 Test: atest com.android.car.internal.test.LargeParcelableJavaStableAIDLTest (runs only in auto. will add matching CTS tests for non-auto devices) (This combines two CL in internal tree, so Change-Id and Merged-In id are different) Change-Id: I8a634062d10ff0c114f3add1d2a5e8056f17b788 Merged-In: I11381e78580b97982b25a52aa06f2192def7353f --- core/api/current.txt | 1 + core/java/android/os/SharedMemory.java | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/core/api/current.txt b/core/api/current.txt index 5de5f83b90397..19c59a34042b1 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -30329,6 +30329,7 @@ package android.os { method public void close(); method @NonNull public static android.os.SharedMemory create(@Nullable String, int) throws android.system.ErrnoException; method public int describeContents(); + method @NonNull public static android.os.SharedMemory fromFileDescriptor(@NonNull android.os.ParcelFileDescriptor); method public int getSize(); method @NonNull public java.nio.ByteBuffer map(int, int, int) throws android.system.ErrnoException; method @NonNull public java.nio.ByteBuffer mapReadOnly() throws android.system.ErrnoException; diff --git a/core/java/android/os/SharedMemory.java b/core/java/android/os/SharedMemory.java index 136e3de731a91..46eb2ece435cb 100644 --- a/core/java/android/os/SharedMemory.java +++ b/core/java/android/os/SharedMemory.java @@ -93,6 +93,26 @@ public final class SharedMemory implements Parcelable, Closeable { } } + /** + * Creates an instance from existing shared memory passed as {@link ParcelFileDescriptor}. + * + *

The {@code fd} should be a shared memory created from + {@code SharedMemory or ASharedMemory}. This can be useful when shared memory is passed as + file descriptor through JNI or binder service implemented in cpp. + *

Note that newly created {@code SharedMemory} takes ownership of passed {@code fd} and + * the original {@code fd} becomes detached (Check {@link ParcelFileDescriptor#detachFd()}). + * If the caller wants to use the file descriptor after the call, the caller should duplicate + * the file descriptor (Check {@link ParcelFileDescriptor#dup()}) and pass the duped version + * instead. + * + * @param fd File descriptor of shared memory passed as {@link ParcelFileDescriptor}. + */ + public static @NonNull SharedMemory fromFileDescriptor(@NonNull ParcelFileDescriptor fd) { + FileDescriptor f = new FileDescriptor(); + f.setInt$(fd.detachFd()); + return new SharedMemory(f); + } + private static final int PROT_MASK = OsConstants.PROT_READ | OsConstants.PROT_WRITE | OsConstants.PROT_EXEC | OsConstants.PROT_NONE;