libbinder_ndk: association: rem heap allocs
libbinder_ndk, in the implementation of AIBinder_associateClass made
copies of and converted both IBinder and AIBinder_Class interface
descriptors to String8, even though they are already String16.
Bug: N/A
Test: atest aidl_integration_test
Change-Id: I99a5ed1dd06d32bfd28821a16792f37163b5179d
diff --git a/libs/binder/ndk/ibinder.cpp b/libs/binder/ndk/ibinder.cpp
index d7075b2..54868be 100644
--- a/libs/binder/ndk/ibinder.cpp
+++ b/libs/binder/ndk/ibinder.cpp
@@ -74,12 +74,12 @@
AIBinder::~AIBinder() {}
std::optional<bool> AIBinder::associateClassInternal(const AIBinder_Class* clazz,
- const String8& newDescriptor, bool set) {
+ const String16& newDescriptor, bool set) {
std::lock_guard<std::mutex> lock(mClazzMutex);
if (mClazz == clazz) return true;
if (mClazz != nullptr) {
- String8 currentDescriptor(mClazz->getInterfaceDescriptor());
+ const String16& currentDescriptor = mClazz->getInterfaceDescriptor();
if (newDescriptor == currentDescriptor) {
LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor
<< "' match during associateClass, but they are different class objects. "
@@ -88,8 +88,7 @@
LOG(ERROR) << __func__
<< ": Class cannot be associated on object which already has a class. "
"Trying to associate to '"
- << newDescriptor.c_str() << "' but already set to '"
- << currentDescriptor.c_str() << "'.";
+ << newDescriptor << "' but already set to '" << currentDescriptor << "'.";
}
// always a failure because we know mClazz != clazz
@@ -107,21 +106,21 @@
bool AIBinder::associateClass(const AIBinder_Class* clazz) {
if (clazz == nullptr) return false;
- String8 newDescriptor(clazz->getInterfaceDescriptor());
+ const String16& newDescriptor = clazz->getInterfaceDescriptor();
auto result = associateClassInternal(clazz, newDescriptor, false);
if (result.has_value()) return *result;
CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
- String8 descriptor(getBinder()->getInterfaceDescriptor());
+ const String16& descriptor = getBinder()->getInterfaceDescriptor();
if (descriptor != newDescriptor) {
if (getBinder()->isBinderAlive()) {
- LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor.c_str()
- << "' but descriptor is actually '" << descriptor.c_str() << "'.";
+ LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor
+ << "' but descriptor is actually '" << descriptor << "'.";
} else {
// b/155793159
- LOG(ERROR) << __func__ << ": Cannot associate class '" << newDescriptor.c_str()
+ LOG(ERROR) << __func__ << ": Cannot associate class '" << newDescriptor
<< "' to dead binder.";
}
return false;
diff --git a/libs/binder/ndk/ibinder_internal.h b/libs/binder/ndk/ibinder_internal.h
index f4e2704..f601127 100644
--- a/libs/binder/ndk/ibinder_internal.h
+++ b/libs/binder/ndk/ibinder_internal.h
@@ -54,7 +54,7 @@
private:
std::optional<bool> associateClassInternal(const AIBinder_Class* clazz,
- const ::android::String8& newDescriptor, bool set);
+ const ::android::String16& newDescriptor, bool set);
// AIBinder instance is instance of this class for a local object. In order to transact on a
// remote object, this also must be set for simplicity (although right now, only the