Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a644b688 authored by Ryan Mitchell's avatar Ryan Mitchell Committed by Android (Google) Code Review
Browse files

Merge changes I425638d3,Ie06232a0 into sc-dev

* changes:
  Revert "Migrate ApkAssets to use NativeAllocationRegistry"
  Revert "Cast ApkAssets to uintptr_t before jlong"
parents 060b1b49 8c7000ce
Loading
Loading
Loading
Loading
+14 −30
Original line number Diff line number Diff line
@@ -26,8 +26,6 @@ import android.text.TextUtils;

import com.android.internal.annotations.GuardedBy;

import libcore.util.NativeAllocationRegistry;

import java.io.FileDescriptor;
import java.io.IOException;
import java.lang.annotation.Retention;
@@ -104,11 +102,11 @@ public final class ApkAssets {
    public @interface FormatType {}

    @GuardedBy("this")
    private final long mNativePtr;
    private long mNativePtr;  // final, except cleared in finalizer.

    @Nullable
    @GuardedBy("this")
    private final StringBlock mStringBlock;
    private final StringBlock mStringBlock;  // null or closed if mNativePtr = 0.

    @PropertyFlags
    private final int mFlags;
@@ -116,19 +114,6 @@ public final class ApkAssets {
    @Nullable
    private final AssetsProvider mAssets;

    @GuardedBy("this")
    @Nullable
    private final Runnable mRunNativeCleanup;

    // Use a Holder to allow static initialization of ApkAssets in the boot image, and
    // possibly to avoid some initialization ordering issues.
    private static class NoImagePreloadHolder {
        // TODO(175425996): Make size estimate more accurate
        public static final NativeAllocationRegistry REGISTRY =
                NativeAllocationRegistry.createMalloced(ApkAssets.class.getClassLoader(),
                                                        nativeGetFinalizer());
    }

    /**
     * Creates a new ApkAssets instance from the given path on disk.
     *
@@ -303,8 +288,6 @@ public final class ApkAssets {
        mFlags = flags;
        mNativePtr = nativeLoad(format, path, flags, assets);
        mStringBlock = new StringBlock(nativeGetStringBlock(mNativePtr), true /*useSparse*/);
        mRunNativeCleanup = NoImagePreloadHolder.REGISTRY.registerNativeAllocation(
                this, mNativePtr);
        mAssets = assets;
    }

@@ -316,8 +299,6 @@ public final class ApkAssets {
        mFlags = flags;
        mNativePtr = nativeLoadFd(format, fd, friendlyName, flags, assets);
        mStringBlock = new StringBlock(nativeGetStringBlock(mNativePtr), true /*useSparse*/);
        mRunNativeCleanup = NoImagePreloadHolder.REGISTRY.registerNativeAllocation(
                this, mNativePtr);
        mAssets = assets;
    }

@@ -329,8 +310,6 @@ public final class ApkAssets {
        mFlags = flags;
        mNativePtr = nativeLoadFdOffsets(format, fd, friendlyName, offset, length, flags, assets);
        mStringBlock = new StringBlock(nativeGetStringBlock(mNativePtr), true /*useSparse*/);
        mRunNativeCleanup = NoImagePreloadHolder.REGISTRY.registerNativeAllocation(
                this, mNativePtr);
        mAssets = assets;
    }

@@ -338,7 +317,6 @@ public final class ApkAssets {
        mFlags = flags;
        mNativePtr = nativeLoadEmpty(flags, assets);
        mStringBlock = null;
        mRunNativeCleanup = null;
        mAssets = assets;
    }

@@ -433,16 +411,22 @@ public final class ApkAssets {
        return "ApkAssets{path=" + getDebugName() + "}";
    }

    @Override
    protected void finalize() throws Throwable {
        close();
    }

    /**
     * Closes this class and the contained {@link #mStringBlock}.
     */
    public void close() {
        synchronized (this) {
            if (mNativePtr != 0) {
                if (mStringBlock != null) {
                    mStringBlock.close();
                }
            if (mRunNativeCleanup != null) {
                mRunNativeCleanup.run();
                nativeDestroy(mNativePtr);
                mNativePtr = 0;
            }
        }
    }
@@ -457,6 +441,7 @@ public final class ApkAssets {
    private static native long nativeLoadFdOffsets(@FormatType int format,
            @NonNull FileDescriptor fd, @NonNull String friendlyName, long offset, long length,
            @PropertyFlags int flags, @Nullable AssetsProvider asset) throws IOException;
    private static native void nativeDestroy(long ptr);
    private static native @NonNull String nativeGetAssetPath(long ptr);
    private static native @NonNull String nativeGetDebugName(long ptr);
    private static native long nativeGetStringBlock(long ptr);
@@ -465,5 +450,4 @@ public final class ApkAssets {
    private static native @Nullable OverlayableInfo nativeGetOverlayableInfo(long ptr,
            String overlayableName) throws IOException;
    private static native boolean nativeDefinesOverlayable(long ptr) throws IOException;
    private static native final long nativeGetFinalizer();
}
+2 −6
Original line number Diff line number Diff line
@@ -353,14 +353,10 @@ static jlong NativeLoadEmpty(JNIEnv* env, jclass /*clazz*/, jint flags, jobject
  return reinterpret_cast<jlong>(apk_assets.release());
}

static void NativeDestroy(void* ptr) {
static void NativeDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) {
  delete reinterpret_cast<ApkAssets*>(ptr);
}

static jlong NativeGetFinalizer(JNIEnv* /*env*/, jclass /*clazz*/) {
  return static_cast<jlong>(reinterpret_cast<uintptr_t>(&NativeDestroy));
}

static jstring NativeGetAssetPath(JNIEnv* env, jclass /*clazz*/, jlong ptr) {
  auto apk_assets = reinterpret_cast<const ApkAssets*>(ptr);
  if (auto path = apk_assets->GetPath()) {
@@ -477,7 +473,7 @@ static const JNINativeMethod gApkAssetsMethods[] = {
    {"nativeLoadFdOffsets",
     "(ILjava/io/FileDescriptor;Ljava/lang/String;JJILandroid/content/res/loader/AssetsProvider;)J",
     (void*)NativeLoadFromFdOffset},
    {"nativeGetFinalizer", "()J", (void*)NativeGetFinalizer},
    {"nativeDestroy", "(J)V", (void*)NativeDestroy},
    {"nativeGetAssetPath", "(J)Ljava/lang/String;", (void*)NativeGetAssetPath},
    {"nativeGetDebugName", "(J)Ljava/lang/String;", (void*)NativeGetDebugName},
    {"nativeGetStringBlock", "(J)J", (void*)NativeGetStringBlock},