Track HardwareBuffer native allocations
Bug: b/363028387
Flag: com.android.libcore.native_metrics
This CL will make use of the new API to create NativeAllocationRegistry
that associates with HardwareBuffer.class if libcore.native_metrics is
enabled (otherwise falls back to the legacy API). This allows tracking
of native allocations that are HardwareBuffer specific.
Change-Id: I848cf66dacf1cf82baf1ff8d2012866ee548973f
diff --git a/core/java/android/hardware/HardwareBuffer.java b/core/java/android/hardware/HardwareBuffer.java
index ce0f9f59..8c87ad3 100644
--- a/core/java/android/hardware/HardwareBuffer.java
+++ b/core/java/android/hardware/HardwareBuffer.java
@@ -278,6 +278,17 @@
}
/**
+ * @hide
+ */
+ private static NativeAllocationRegistry getRegistry(long size) {
+ final long func = nGetNativeFinalizer();
+ final Class cls = HardwareBuffer.class;
+ return com.android.libcore.Flags.nativeMetrics()
+ ? NativeAllocationRegistry.createNonmalloced(cls, func, size)
+ : NativeAllocationRegistry.createNonmalloced(cls.getClassLoader(), func, size);
+ }
+
+ /**
* Private use only. See {@link #create(int, int, int, int, long)}. May also be
* called from JNI using an already allocated native <code>HardwareBuffer</code>.
*/
@@ -285,10 +296,7 @@
private HardwareBuffer(long nativeObject) {
mNativeObject = nativeObject;
long bufferSize = nEstimateSize(nativeObject);
- ClassLoader loader = HardwareBuffer.class.getClassLoader();
- NativeAllocationRegistry registry = new NativeAllocationRegistry(
- loader, nGetNativeFinalizer(), bufferSize);
- mCleaner = registry.registerNativeAllocation(this, mNativeObject);
+ mCleaner = getRegistry(bufferSize).registerNativeAllocation(this, mNativeObject);
mCloseGuard.open("HardwareBuffer.close");
}