BINDER

class IInterface : public virtual RefBase
{
public:
            IInterface();
            sp<IBinder>         asBinder();
            sp<const IBinder>   asBinder() const;
            
protected:
    virtual                     ~IInterface();
    virtual IBinder*            onAsBinder() = 0;
};

// ----------------------------------------------------------------------

template<typename INTERFACE>
inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
{
    return INTERFACE::asInterface(obj);
}

// ----------------------------------------------------------------------

template<typename INTERFACE>
class BnInterface : public INTERFACE, public BBinder
{
public:
    virtual sp<IInterface>      queryLocalInterface(const String16& _descriptor);
    virtual const String16&     getInterfaceDescriptor() const;

protected:
    virtual IBinder*            onAsBinder();
};

// ----------------------------------------------------------------------

template<typename INTERFACE>
class BpInterface : public INTERFACE, public BpRefBase
{
public:
                                BpInterface(const sp<IBinder>& remote);

protected:
    virtual IBinder*            onAsBinder();
};

// ----------------------------------------------------------------------

#define DECLARE_META_INTERFACE(INTERFACE)                               \
    static const android::String16 descriptor;                          \
    static android::sp<I##INTERFACE> asInterface(                       \
            const android::sp<android::IBinder>& obj);                  \
    virtual const android::String16& getInterfaceDescriptor() const;    \
    I##INTERFACE();                                                     \
    virtual ~I##INTERFACE();                                            \


#define IMPLEMENT_META_INTERFACE(INTERFACE, NAME)                       \
    const android::String16 I##INTERFACE::descriptor(NAME);             \
    const android::String16&                                            \
            I##INTERFACE::getInterfaceDescriptor() const {              \
        return I##INTERFACE::descriptor;                                \
    }                                                                   \
    android::sp<I##INTERFACE> I##INTERFACE::asInterface(                \
            const android::sp<android::IBinder>& obj)                   \
    {                                                                   \
        android::sp<I##INTERFACE> intr;                                 \
        if (obj != NULL) {                                              \
            intr = static_cast<I##INTERFACE*>(                          \
                obj->queryLocalInterface(                               \
                        I##INTERFACE::descriptor).get());               \
            if (intr == NULL) {                                         \
                intr = new Bp##INTERFACE(obj);                          \
            }                                                           \
        }                                                               \
        return intr;                                                    \
    }                                                                   \
    I##INTERFACE::I##INTERFACE() { }                                    \
    I##INTERFACE::~I##INTERFACE() { }                                   \


#define CHECK_INTERFACE(interface, data, reply)                         \
    if (!data.checkInterface(this)) { return PERMISSION_DENIED; }       \


// ----------------------------------------------------------------------
// No user-serviceable parts after this...

template<typename INTERFACE>
inline sp<IInterface> BnInterface<INTERFACE>::queryLocalInterface(
        const String16& _descriptor)
{
    if (_descriptor == INTERFACE::descriptor) return this;
    return NULL;
}

template<typename INTERFACE>
inline const String16& BnInterface<INTERFACE>::getInterfaceDescriptor() const
{
    return INTERFACE::getInterfaceDescriptor();
}

template<typename INTERFACE>
IBinder* BnInterface<INTERFACE>::onAsBinder()
{
    return this;
}

template<typename INTERFACE>
inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote)
    : BpRefBase(remote)
{
}

template<typename INTERFACE>
inline IBinder* BpInterface<INTERFACE>::onAsBinder()
{
    return remote();
}
    
// ----------------------------------------------------------------------

}; // namespace android
void BBinder::attachObject(
    const void* objectID, void* object, void* cleanupCookie,
    object_cleanup_func func)
{
    Extras* e = mExtras;

    if (!e) {
        e = new Extras;
        if (android_atomic_cmpxchg(0, reinterpret_cast<int32_t>(e),
                reinterpret_cast<volatile int32_t="">(&mExtras)) != 0) {
            delete e;
            e = mExtras;
        }
        if (e == 0) return; // out of memory
    }

    AutoMutex _l(e->mLock);
    e->mObjects.attach(objectID, object, cleanupCookie, func);
}
</volatile></int32_t>
//IBinder 虚基类实现了以下两个函数
class IBinder : public virtual RefBase
{
public:
    enum {
        FIRST_CALL_TRANSACTION  = 0x00000001,
        LAST_CALL_TRANSACTION   = 0x00ffffff,

        PING_TRANSACTION        = B_PACK_CHARS('_','P','N','G'),
        DUMP_TRANSACTION        = B_PACK_CHARS('_','D','M','P'),
        INTERFACE_TRANSACTION   = B_PACK_CHARS('_', 'N', 'T', 'F'),

        // Corresponds to TF_ONE_WAY -- an asynchronous call.
        FLAG_ONEWAY             = 0x00000001
    };

                          IBinder();

    /**
     * Check if this IBinder implements the interface named by
     * @a descriptor.  If it does, the base pointer to it is returned,
     * which you can safely static_cast<> to the concrete C++ interface.
     */
    virtual sp<iinterface>  queryLocalInterface(const String16& descriptor);

    /**
     * Return the canonical name of the interface provided by this IBinder
     * object.
     */
    virtual const String16& getInterfaceDescriptor() const = 0;

    virtual bool            isBinderAlive() const = 0;
    virtual status_t        pingBinder() = 0;
    virtual status_t        dump(int fd, const Vector<string16>& args) = 0;

    virtual status_t        transact(   uint32_t code,
                                        const Parcel& data,
                                        Parcel* reply,
                                        uint32_t flags = 0) = 0;

    /**
     * This method allows you to add data that is transported through
     * IPC along with your IBinder pointer.  When implementing a Binder
     * object, override it to write your desired data in to @a outData.
     * You can then call getConstantData() on your IBinder to retrieve
     * that data, from any process.  You MUST return the number of bytes
     * written in to the parcel (including padding).
     */
    class DeathRecipient : public virtual RefBase
    {
    public:
        virtual void binderDied(const wp<ibinder>& who) = 0;
    };

    /**
     * Register the @a recipient for a notification if this binder
     * goes away.  If this binder object unexpectedly goes away
     * (typically because its hosting process has been killed),
     * then DeathRecipient::binderDied() will be called with a referene
     * to this.
     *
     * The @a cookie is optional -- if non-NULL, it should be a
     * memory address that you own (that is, you know it is unique).
     *
     * @note You will only receive death notifications for remote binders,
     * as local binders by definition can't die without you dying as well.
     * Trying to use this function on a local binder will result in an
     * INVALID_OPERATION code being returned and nothing happening.
     *
     * @note This link always holds a weak reference to its recipient.
     *
     * @note You will only receive a weak reference to the dead
     * binder.  You should not try to promote this to a strong reference.
     * (Nor should you need to, as there is nothing useful you can
     * directly do with it now that it has passed on.)
     */
    virtual status_t        linkToDeath(const sp<deathrecipient>& recipient,
                                        void* cookie = NULL,
                                        uint32_t flags = 0) = 0;

    /**
     * Remove a previously registered death notification.
     * The @a recipient will no longer be called if this object
     * dies.  The @a cookie is optional.  If non-NULL, you can
     * supply a NULL @a recipient, and the recipient previously
     * added with that cookie will be unlinked.
     */
    virtual status_t        unlinkToDeath(  const wp<deathrecipient>& recipient,
                                            void* cookie = NULL,
                                            uint32_t flags = 0,
                                            wp<deathrecipient>* outRecipient = NULL) = 0;

    virtual bool            checkSubclass(const void* subclassID) const;

    typedef void (*object_cleanup_func)(const void* id, void* obj, void* cleanupCookie);

    virtual void            attachObject(   const void* objectID,
                                            void* object,
                                            void* cleanupCookie,
                                            object_cleanup_func func) = 0;
    virtual void*           findObject(const void* objectID) const = 0;
    virtual void            detachObject(const void* objectID) = 0;

    virtual BBinder*        localBinder();
    virtual BpBinder*       remoteBinder();

protected:
    virtual          ~IBinder();

private:
};

//但是这两个函数都在子类BBinder BpBinder中重写
BBinder* IBinder::localBinder()
{
    return NULL;
}

BpBinder* IBinder::remoteBinder()
{
    return NULL;
}


//BBinder重写了localBinder()
BBinder* BBinder::localBinder()
{
    return this;
}

//BpBinder重写了remoteBinder()
BpBinder* BpBinder::remoteBinder()
{
    return this;
}

</deathrecipient></deathrecipient></deathrecipient></ibinder></string16></iinterface>
//DECLARE_META_INTERFACE(AudioTrack)展开为 
    static const android::String16 descriptor; 
    static android::sp<iAudioTrack> asInterface(const android::sp<android::iBinder>& obj);
    virtual const android::String16& getInterfaceDescriptor() const;
    IAudioTrack();
    virtual ~IAudioTrack();


//IMPLEMENT_META_INTERFACE(AudioTrack, "android.media.IAudioTrack")展开为
    const android::String16  IAudioTrack::descriptor("android.media.IAudioTrack");
    const android::String16& IAudioTrack::getInterfaceDescriptor() const {
        return IAudioTrack::descriptor;
    }
    android::sp<IAudioTrack> IAudioTrack::asInterface(const android::sp<IAudioTrack>& obj)
    {
        android::sp<IAudioTrack> intr;
        if (obj != NULL) {
            intr = static_cast<IAudiotrack>(obj->queryLocalInterface(IAudioTrack::descriptor).get());
            if (intr == NULL) {
                intr = new BpAudioTrack(obj);
            }
        }
        return intr;
    }
    IAudioTrack::IAudioTrack() { }
    IAudioTrack::~IAudioTrack() { }

//interface_case<AudioTrack>的展开
template<typename INTERFACE>
inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
{
    return INTERFACE::asInterface(obj);
}
//展开后变成:
template<typename IAudioTrack>
inline sp<IAudioTrack> interface_cast(const sp<IBinder>& obj)
{
    return IAudioTrack::asInterface(obj);
}

BpAudioFlinger{
    //...
    virtual sp<IAudioTrack> createTrack(
                                pid_t pid,
                                int streamType,
                                uint32_t sampleRate,
                                int format,
                                int channelCount,
                                int frameCount,
                                uint32_t flags,
                                const sp<imemory>& sharedBuffer,
                                int output,
                                int *sessionId,
                                status_t *status)
    {
        Parcel data, reply;
        sp<IAudioTrack> track;
        data.writeInterfaceToken(IAudioFlinger::getInterfaceDescriptor());
        data.writeInt32(pid);
        data.writeInt32(streamType);
        data.writeInt32(sampleRate);
        data.writeInt32(format);
        data.writeInt32(channelCount);
        data.writeInt32(frameCount);
        data.writeInt32(flags);
        data.writeStrongBinder(sharedBuffer->asBinder());
        data.writeInt32(output);
        int lSessionId = 0;
        if (sessionId != NULL) {
            lSessionId = *sessionId;
        }
        data.writeInt32(lSessionId);
        //这里的remote是BpAudioFlinger的成员函数.
        status_t lStatus = remote()->transact(CREATE_TRACK, data, &reply);
        if (lStatus != NO_ERROR) {
            LOGE("createTrack error: %s", strerror(-lStatus));
        } else {
            lSessionId = reply.readInt32();
            if (sessionId != NULL) {
                *sessionId = lSessionId;
            }
            lStatus = reply.readInt32();
            track = interface_cast<IAudioTrack>(reply.readStrongBinder());//这里创建BpAudioTrack(reply.readStrongBinder())
        }
        if (status) {
            *status = lStatus;
        }
        return track;
    }
    //...
}
</imemory>
<think>好的,我现在需要回答用户关于Android Binder的问题。首先,用户提到想了解操作系统中的Binder,特别是Android中的Binder。我需要先回忆一下Binder的基本概念和作用,然后结合提供的引用内容来组织回答。 根据用户提供的引用[1],Binder是Android用于进程间通信(IPC)的机制,基于内核内存引用,专为移动设备优化。这应该是回答的核心点。我需要解释Binder的作用、工作原理以及它的优势。 接下来,引用[1]提到Android的组件模型是基于进程的,系统功能作为服务进程运行。因此,Binder在这里的作用是连接这些进程,实现跨进程通信。我需要强调Binder在组件间通信中的重要性,比如Activity、Service等如何通过Binder与系统服务交互。 引用[3]提到了Linux内核配置中的SELinux设置,虽然和Binder没有直接关联,但可能涉及到安全性。不过用户的问题中没有提到安全方面,所以可能不需要深入这部分。不过可以提一下Binder的安全机制,比如引用[2]中的安全库可能相关,但需要确认是否直接相关。 引用[4]介绍了proc文件系统,这通常用于访问内核数据结构。虽然Binder可能在内核中有相关实现,但用户的问题更多关注Binder的功能而非底层实现细节。不过可以简要提到Binder驱动在Linux内核中的位置,说明其底层依赖。 接下来,我需要确保回答结构清晰,分点解释。可能的结构包括:Binder的定义、核心功能、工作原理、优势、应用场景。同时,用户示例中的相关问题部分需要生成相关问题,例如Binder与其他IPC机制的对比,如何实现Binder通信,性能优化等。 需要检查是否有遗漏的信息。例如,用户引用中提到的Binder使用内核内存引用,这可能涉及到共享内存机制,减少数据拷贝,从而提升性能。这一点需要详细说明,因为这是Binder优化的关键。 另外,可能需要解释Binder的架构,比如Client-Server模型,以及Binder驱动在其中的角色。引用[1]中提到系统功能作为服务进程,客户端通过Binder调用服务,这符合C/S模型。 还需要注意用户不可见的系统级指令,如数学表达式用$...$,但这个问题可能不涉及数学公式,所以可能不需要处理这部分。但需要确保回答中的技术术语正确,比如正确的命名和机制描述。 最后,生成的相关问题需要围绕Binder的扩展知识,比如与其他IPC的对比、具体实现细节、性能分析、安全机制等。需要确保问题有针对性,帮助用户进一步探索。</think>### Android Binder 详解 **Binder** 是 Android 系统中**进程间通信(IPC)**的核心机制,基于 Linux 内核实现,专为移动设备的高效性和低资源消耗优化[^1]。其核心功能是支持跨进程的组件交互(如 Activity、Service 与系统服务通信),并通过**内核内存共享**减少数据拷贝,提升性能。 #### 核心特性与工作原理 1. **进程隔离与通信** Android 应用默认运行在独立进程中,Binder 允许这些进程安全地调用系统服务(如位置、传感器)或其他应用的组件。例如,应用通过 `bindService()` 绑定服务时,Binder 负责跨进程传递请求和结果。 2. **内核驱动层** Binder 在内核中实现为**字符设备驱动**(`/dev/binder`),管理进程间的内存映射与通信队列。数据通过**内存映射(mmap)**直接在发送方和接收方的用户空间共享,避免多次拷贝。 3. **组件化支持** Android 的四大组件(Activity、Service 等)依赖 Binder 实现生命周期管理与跨进程调用。例如,启动一个其他应用的 Activity 时,系统服务 `ActivityManagerService` 通过 Binder 协调进程间通信。 4. **安全性** Binder 通信基于进程的 UID/PID 进行身份验证,并与 SELinux 策略集成(如引用[3]中提到的 `androidboot.selinux` 配置),确保只有授权进程可访问特定服务[^3]。 #### 性能优势 - **零拷贝传输**:通过内核内存映射,数据直接在通信双方的用户空间共享,减少传统 IPC 中内核缓冲区的中转开销。 - **线程池管理**Binder 驱动自动分配线程处理请求,避免阻塞主线程。 - **轻量级序列化**:使用 Parcel 对象进行高效数据封装,支持基本类型和 Binder 对象引用。 #### 代码示例:Binder 调用流程 ```java // 服务端定义 AIDL 接口 interface IMyService { int calculate(int a, intBinder机制); } // 客户端绑定服务并调用 IMyService service = IMyService.Stub.asInterface(binder); int result = service.calculate(10, 20); ``` #### 与其他 IPC 的对比 | 机制 | 性能 | 复杂度 | 适用场景 | |-------------|--------|--------|-----------------------| | Binder | 高 | 低 | 高频跨进程调用 | | Socket | 中 | 高 | 网络或跨设备通信 | | 共享内存 | 极高 | 高 | 大数据量低延迟 |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值