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

Commit c70e0121 authored by Steven Moreland's avatar Steven Moreland
Browse files

libbinder_ndk: AIBinder{,_Weak} lt/clone utilities

To support clients which want to use weak binders more effectively,
specifically the Rust backend of AIDL.

Fixes: 177020658
Fixes: 175959481
Test: atest CtsNdkBinderTestCases
Change-Id: I4794777c8bfa94210ec03f934a8f0a9bc281ec2a
parent 048f1e93
Loading
Loading
Loading
Loading
+20 −0
Original line number Original line Diff line number Diff line
@@ -301,6 +301,26 @@ AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
    return binder.get();
    return binder.get();
}
}


AIBinder_Weak* AIBinder_Weak_clone(const AIBinder_Weak* weak) {
    if (weak == nullptr) {
        return nullptr;
    }

    return new AIBinder_Weak{weak->binder};
}

bool AIBinder_lt(const AIBinder* lhs, const AIBinder* rhs) {
    if (lhs == nullptr || rhs == nullptr) return lhs < rhs;

    return const_cast<AIBinder*>(lhs)->getBinder() < const_cast<AIBinder*>(rhs)->getBinder();
}

bool AIBinder_Weak_lt(const AIBinder_Weak* lhs, const AIBinder_Weak* rhs) {
    if (lhs == nullptr || rhs == nullptr) return lhs < rhs;

    return lhs->binder < rhs->binder;
}

AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
                               AIBinder_Class_onDestroy onDestroy,
                               AIBinder_Class_onDestroy onDestroy,
                               AIBinder_Class_onTransact onTransact)
                               AIBinder_Class_onTransact onTransact)
+62 −0
Original line number Original line Diff line number Diff line
@@ -657,6 +657,68 @@ binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) __INTRODU
 */
 */
const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) __INTRODUCED_IN(31);
const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) __INTRODUCED_IN(31);


/**
 * Whether AIBinder is less than another.
 *
 * This provides a per-process-unique total ordering of binders determined by
 * an underlying allocation address where a null AIBinder* is considered to be
 * ordered before all other binders.
 *
 * AIBinder* pointers themselves actually also create a per-process-unique total
 * ordering. However, this ordering is inconsistent with AIBinder_Weak_lt for
 * remote binders.
 *
 * Available since API level 31.
 *
 * \param lhs comparison object
 * \param rhs comparison object
 *
 * \return whether "lhs < rhs" is true
 */
bool AIBinder_lt(const AIBinder* lhs, const AIBinder* rhs);

/**
 * Clone an AIBinder_Weak. Useful because even if a weak binder promotes to a
 * null value, after further binder transactions, it may no longer promote to a
 * null value.
 *
 * Available since API level 31.
 *
 * \param weak Object to clone
 *
 * \return clone of the input parameter. This must be deleted with
 * AIBinder_Weak_delete. Null if weak input parameter is also null.
 */
AIBinder_Weak* AIBinder_Weak_clone(const AIBinder_Weak* weak);

/**
 * Whether AIBinder_Weak is less than another.
 *
 * This provides a per-process-unique total ordering of binders which is exactly
 * the same as AIBinder_lt. Similarly, a null AIBinder_Weak* is considered to be
 * ordered before all other weak references.
 *
 * If you have many AIBinder_Weak* objects which are all references to distinct
 * binder objects which happen to have the same underlying address (as ordered
 * by AIBinder_lt), these AIBinder_Weak* objects will retain the same order with
 * respect to all other AIBinder_Weak* pointers with different underlying
 * addresses and are also guaranteed to have a per-process-unique ordering. That
 * is, even though multiple AIBinder* instances may happen to be allocated at
 * the same underlying address, this function will still correctly distinguish
 * that these are weak pointers to different binder objects.
 *
 * Unlike AIBinder*, the AIBinder_Weak* addresses themselves have nothing to do
 * with the underlying binder.
 *
 * Available since API level 31.
 *
 * \param lhs comparison object
 * \param rhs comparison object
 *
 * \return whether "lhs < rhs" is true
 */
bool AIBinder_Weak_lt(const AIBinder_Weak* lhs, const AIBinder_Weak* rhs);

#endif  //__ANDROID_API__ >= 31
#endif  //__ANDROID_API__ >= 31


__END_DECLS
__END_DECLS
+3 −0
Original line number Original line Diff line number Diff line
@@ -122,6 +122,9 @@ LIBBINDER_NDK31 { # introduced=31
    AServiceManager_waitForService; # apex llndk
    AServiceManager_waitForService; # apex llndk


    AIBinder_Class_getDescriptor;
    AIBinder_Class_getDescriptor;
    AIBinder_Weak_clone;
    AIBinder_Weak_lt;
    AIBinder_lt;
    AParcel_appendFrom;
    AParcel_appendFrom;
    AParcel_create;
    AParcel_create;
    AParcel_getDataSize;
    AParcel_getDataSize;