From 32746ae275593e2532a99c19b2c2e8858a042e3a Mon Sep 17 00:00:00 2001 From: Tomasz Wasilczyk Date: Mon, 9 Oct 2023 19:57:58 +0000 Subject: Use C++20 [[unlikely]] instead of UNLIKELY macro Bug: 302723053 Test: mma Change-Id: I126772474763a55f43de0cd4a3d2605f7f84da3b --- libs/binder/Binder.cpp | 3 +-- libs/binder/BpBinder.cpp | 30 ++++++++++++--------- libs/binder/Parcel.cpp | 1 - libs/binder/include/binder/SafeInterface.h | 31 +++++++++++----------- .../tests/unit_fuzzers/BpBinderFuzzFunctions.h | 1 - .../tests/unit_fuzzers/IBinderFuzzFunctions.h | 1 - 6 files changed, 33 insertions(+), 34 deletions(-) diff --git a/libs/binder/Binder.cpp b/libs/binder/Binder.cpp index 0f4a6cabde..aa67869649 100644 --- a/libs/binder/Binder.cpp +++ b/libs/binder/Binder.cpp @@ -29,7 +29,6 @@ #include #include #include -#include #include #include #include @@ -402,7 +401,7 @@ status_t BBinder::transact( } } - if (CC_UNLIKELY(kEnableKernelIpc && mRecordingOn && code != START_RECORDING_TRANSACTION)) { + if (kEnableKernelIpc && mRecordingOn && code != START_RECORDING_TRANSACTION) [[unlikely]] { Extras* e = mExtras.load(std::memory_order_acquire); AutoMutex lock(e->mLock); if (mRecordingOn) { diff --git a/libs/binder/BpBinder.cpp b/libs/binder/BpBinder.cpp index 589df9aceb..b0c1af21a9 100644 --- a/libs/binder/BpBinder.cpp +++ b/libs/binder/BpBinder.cpp @@ -23,7 +23,6 @@ #include #include #include -#include #include #include @@ -161,7 +160,7 @@ sp BpBinder::create(int32_t handle) { trackedUid = IPCThreadState::self()->getCallingUid(); AutoMutex _l(sTrackingLock); uint32_t trackedValue = sTrackingMap[trackedUid]; - if (CC_UNLIKELY(trackedValue & LIMIT_REACHED_MASK)) { + if (trackedValue & LIMIT_REACHED_MASK) [[unlikely]] { if (sBinderProxyThrottleCreate) { return nullptr; } @@ -347,7 +346,7 @@ status_t BpBinder::transact( Stability::Level required = privateVendor ? Stability::VENDOR : Stability::getLocalLevel(); - if (CC_UNLIKELY(!Stability::check(stability, required))) { + if (!Stability::check(stability, required)) [[unlikely]] { ALOGE("Cannot do a user transaction on a %s binder (%s) in a %s context.", Stability::levelString(stability).c_str(), String8(getInterfaceDescriptor()).c_str(), @@ -357,7 +356,7 @@ status_t BpBinder::transact( } status_t status; - if (CC_UNLIKELY(isRpcBinder())) { + if (isRpcBinder()) [[unlikely]] { status = rpcSession()->transact(sp::fromExisting(this), code, data, reply, flags); } else { @@ -572,7 +571,9 @@ BpBinder* BpBinder::remoteBinder() } BpBinder::~BpBinder() { - if (CC_UNLIKELY(isRpcBinder())) return; + if (isRpcBinder()) [[unlikely]] { + return; + } if constexpr (!kEnableKernelIpc) { LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time"); @@ -586,14 +587,13 @@ BpBinder::~BpBinder() { if (mTrackedUid >= 0) { AutoMutex _l(sTrackingLock); uint32_t trackedValue = sTrackingMap[mTrackedUid]; - if (CC_UNLIKELY((trackedValue & COUNTING_VALUE_MASK) == 0)) { + if ((trackedValue & COUNTING_VALUE_MASK) == 0) [[unlikely]] { ALOGE("Unexpected Binder Proxy tracking decrement in %p handle %d\n", this, binderHandle()); } else { - if (CC_UNLIKELY( - (trackedValue & LIMIT_REACHED_MASK) && - ((trackedValue & COUNTING_VALUE_MASK) <= sBinderProxyCountLowWatermark) - )) { + auto countingValue = trackedValue & COUNTING_VALUE_MASK; + if ((trackedValue & LIMIT_REACHED_MASK) && + (countingValue <= sBinderProxyCountLowWatermark)) [[unlikely]] { ALOGI("Limit reached bit reset for uid %d (fewer than %d proxies from uid %d held)", getuid(), sBinderProxyCountLowWatermark, mTrackedUid); sTrackingMap[mTrackedUid] &= ~LIMIT_REACHED_MASK; @@ -612,7 +612,9 @@ BpBinder::~BpBinder() { } void BpBinder::onFirstRef() { - if (CC_UNLIKELY(isRpcBinder())) return; + if (isRpcBinder()) [[unlikely]] { + return; + } if constexpr (!kEnableKernelIpc) { LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time"); @@ -625,7 +627,7 @@ void BpBinder::onFirstRef() { } void BpBinder::onLastStrongRef(const void* /*id*/) { - if (CC_UNLIKELY(isRpcBinder())) { + if (isRpcBinder()) [[unlikely]] { (void)rpcSession()->sendDecStrong(this); return; } @@ -666,7 +668,9 @@ void BpBinder::onLastStrongRef(const void* /*id*/) { bool BpBinder::onIncStrongAttempted(uint32_t /*flags*/, const void* /*id*/) { // RPC binder doesn't currently support inc from weak binders - if (CC_UNLIKELY(isRpcBinder())) return false; + if (isRpcBinder()) [[unlikely]] { + return false; + } if constexpr (!kEnableKernelIpc) { LOG_ALWAYS_FATAL("Binder kernel driver disabled at build time"); diff --git a/libs/binder/Parcel.cpp b/libs/binder/Parcel.cpp index 5b34efc257..d92de1b151 100644 --- a/libs/binder/Parcel.cpp +++ b/libs/binder/Parcel.cpp @@ -41,7 +41,6 @@ #include #include -#include #include #include #include diff --git a/libs/binder/include/binder/SafeInterface.h b/libs/binder/include/binder/SafeInterface.h index 5fa2ff6a87..96b9733c94 100644 --- a/libs/binder/include/binder/SafeInterface.h +++ b/libs/binder/include/binder/SafeInterface.h @@ -18,7 +18,6 @@ #include #include -#include // Set to 1 to enable CallStacks when logging errors #define SI_DUMP_CALLSTACKS 0 @@ -218,7 +217,7 @@ private: template status_t callParcel(const char* name, Function f) const { status_t error = f(); - if (CC_UNLIKELY(error != NO_ERROR)) { + if (error != NO_ERROR) [[unlikely]] { ALOG(LOG_ERROR, mLogTag, "Failed to %s, (%d: %s)", name, error, strerror(-error)); #if SI_DUMP_CALLSTACKS CallStack callStack(mLogTag); @@ -265,7 +264,7 @@ protected: data.writeInterfaceToken(this->getInterfaceDescriptor()); status_t error = writeInputs(&data, std::forward(args)...); - if (CC_UNLIKELY(error != NO_ERROR)) { + if (error != NO_ERROR) [[unlikely]] { // A message will have been logged by writeInputs return error; } @@ -273,7 +272,7 @@ protected: // Send the data Parcel to the remote and retrieve the reply parcel Parcel reply; error = this->remote()->transact(static_cast(tag), data, &reply); - if (CC_UNLIKELY(error != NO_ERROR)) { + if (error != NO_ERROR) [[unlikely]] { ALOG(LOG_ERROR, mLogTag, "Failed to transact (%d)", error); #if SI_DUMP_CALLSTACKS CallStack callStack(mLogTag); @@ -283,7 +282,7 @@ protected: // Read the outputs from the reply Parcel into the output arguments error = readOutputs(reply, std::forward(args)...); - if (CC_UNLIKELY(error != NO_ERROR)) { + if (error != NO_ERROR) [[unlikely]] { // A message will have been logged by readOutputs return error; } @@ -291,7 +290,7 @@ protected: // Retrieve the result code from the reply Parcel status_t result = NO_ERROR; error = reply.readInt32(&result); - if (CC_UNLIKELY(error != NO_ERROR)) { + if (error != NO_ERROR) [[unlikely]] { ALOG(LOG_ERROR, mLogTag, "Failed to obtain result"); #if SI_DUMP_CALLSTACKS CallStack callStack(mLogTag); @@ -315,7 +314,7 @@ protected: Parcel data; data.writeInterfaceToken(this->getInterfaceDescriptor()); status_t error = writeInputs(&data, std::forward(args)...); - if (CC_UNLIKELY(error != NO_ERROR)) { + if (error != NO_ERROR) [[unlikely]] { // A message will have been logged by writeInputs return; } @@ -324,7 +323,7 @@ protected: Parcel reply; error = this->remote()->transact(static_cast(tag), data, &reply, IBinder::FLAG_ONEWAY); - if (CC_UNLIKELY(error != NO_ERROR)) { + if (error != NO_ERROR) [[unlikely]] { ALOG(LOG_ERROR, mLogTag, "Failed to transact (%d)", error); #if SI_DUMP_CALLSTACKS CallStack callStack(mLogTag); @@ -406,7 +405,7 @@ private: template status_t writeInputs(Parcel* data, T&& t, Remaining&&... remaining) const { status_t error = writeIfInput(data, std::forward(t)); - if (CC_UNLIKELY(error != NO_ERROR)) { + if (error != NO_ERROR) [[unlikely]] { // A message will have been logged by writeIfInput return error; } @@ -429,7 +428,7 @@ private: template status_t readOutputs(const Parcel& reply, T&& t, Remaining&&... remaining) const { status_t error = readIfOutput(reply, std::forward(t)); - if (CC_UNLIKELY(error != NO_ERROR)) { + if (error != NO_ERROR) [[unlikely]] { // A message will have been logged by readIfOutput return error; } @@ -458,7 +457,7 @@ protected: // Read the inputs from the data Parcel into the argument tuple status_t error = InputReader{mLogTag}.readInputs(data, &rawArgs); - if (CC_UNLIKELY(error != NO_ERROR)) { + if (error != NO_ERROR) [[unlikely]] { // A message will have been logged by read return error; } @@ -468,14 +467,14 @@ protected: // Extract the outputs from the argument tuple and write them into the reply Parcel error = OutputWriter{mLogTag}.writeOutputs(reply, &rawArgs); - if (CC_UNLIKELY(error != NO_ERROR)) { + if (error != NO_ERROR) [[unlikely]] { // A message will have been logged by write return error; } // Return the result code in the reply Parcel error = reply->writeInt32(result); - if (CC_UNLIKELY(error != NO_ERROR)) { + if (error != NO_ERROR) [[unlikely]] { ALOG(LOG_ERROR, mLogTag, "Failed to write result"); #if SI_DUMP_CALLSTACKS CallStack callStack(mLogTag); @@ -500,7 +499,7 @@ protected: // Read the inputs from the data Parcel into the argument tuple status_t error = InputReader{mLogTag}.readInputs(data, &rawArgs); - if (CC_UNLIKELY(error != NO_ERROR)) { + if (error != NO_ERROR) [[unlikely]] { // A message will have been logged by read return error; } @@ -596,7 +595,7 @@ private: typename std::enable_if<(I < sizeof...(Params)), status_t>::type dispatchArg( const Parcel& data, RawTuple* args) { status_t error = readIfInput(data, args); - if (CC_UNLIKELY(error != NO_ERROR)) { + if (error != NO_ERROR) [[unlikely]] { // A message will have been logged in read return error; } @@ -694,7 +693,7 @@ private: typename std::enable_if<(I < sizeof...(Params)), status_t>::type dispatchArg( Parcel* reply, RawTuple* args) { status_t error = writeIfOutput(reply, args); - if (CC_UNLIKELY(error != NO_ERROR)) { + if (error != NO_ERROR) [[unlikely]] { // A message will have been logged in read return error; } diff --git a/libs/binder/tests/unit_fuzzers/BpBinderFuzzFunctions.h b/libs/binder/tests/unit_fuzzers/BpBinderFuzzFunctions.h index 5079431058..0a584bfe56 100644 --- a/libs/binder/tests/unit_fuzzers/BpBinderFuzzFunctions.h +++ b/libs/binder/tests/unit_fuzzers/BpBinderFuzzFunctions.h @@ -26,7 +26,6 @@ #include #include -#include #include #include #include diff --git a/libs/binder/tests/unit_fuzzers/IBinderFuzzFunctions.h b/libs/binder/tests/unit_fuzzers/IBinderFuzzFunctions.h index bf7c61347f..a6dc182588 100644 --- a/libs/binder/tests/unit_fuzzers/IBinderFuzzFunctions.h +++ b/libs/binder/tests/unit_fuzzers/IBinderFuzzFunctions.h @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include -- cgit v1.2.3-59-g8ed1b