android (8)

 

 

 

 

android (8)

 

struct svcinfo
{
    struct svcinfo *next;
    void *ptr;
    struct binder_death death;
    unsigned len;
    uint16_t name[0];
};

所有的服务信息构成这样一个单链表

将新加的服务信息加到svclist的前面

    binder_acquire(bs, ptr);
   
    也是增加强引用么
   
lookupHandleLocked

所有的handle_entry都存放在vector mHandleToObject 中

现在 getStrongProxyForHandle(0);

是找句柄为0的
   
现在找的就是一个IBinder指针

weakref_type 是 RefBase 的内部类

weakref_impl* const mRefs;  base中有个实现类指针

    volatile int32_t    mStrong;
    volatile int32_t    mWeak;
    RefBase* const      mBase;
    volatile int32_t    mFlags;
   
    多了一些计数 指针 标志
   
    attemptIncWeak 只是增加了实现类的弱引用计数?
   
   
                struct handle_entry {
                IBinder* binder;
                RefBase::weakref_type* refs;
            };
           
 如果找到的实体指针为null
 
 则用当前句柄创建一个BpBinder对象,并取得其弱引用实现

 BpBinder 构造
 
 扩展对象的生命周期?
 
     extendObjectLifetime(OBJECT_LIFETIME_WEAK); 设置一个标志
    
     IPCThreadState 封装了对binder的操作
    
     弱代理和强代理的区别
    
     template<typename T>   
void sp<T>::force_set(T* other)
{
    other->forceIncStrong(this);
    m_ptr = other;
}

WP除了一个类型指针外还有一个弱引用指针

SP 在构造的时候调用对象的incStrong方法

RefBase::incStrong 强弱引用计数都加

sp<T>::sp(const sp<T>& other)
    : m_ptr(other.m_ptr)
{
    if (m_ptr) m_ptr->incStrong(this);
}
同上

sp<T>::~sp()
{
    if (m_ptr) m_ptr->decStrong(this);
}
强弱引用都降1

BpBinder 的弱引用实现是什么?

    IPCThreadState::self()->incWeakHandle(handle);
   
   
   
    RefBase::RefBase()
    : mRefs(new weakref_impl(this))
{
//    LOGV("Creating refs %p with RefBase %p\n", mRefs, this);
}

在这个对象再构造的时候就已经生存了

createWeak 是什么?

template<typename T>
wp<T>::wp(T* other)
    : m_ptr(other)
{
    if (other) m_refs = other->createWeak(this);
}

wp 在构造的时候只增加弱引用


void RefBase::decStrong(const void* id) const
{
    weakref_impl* const refs = mRefs;
    refs->removeStrongRef(id);
    const int32_t c = android_atomic_dec(&refs->mStrong);
#if PRINT_REFS
    LOGD("decStrong of %p from %p: cnt=%d\n", this, id, c);
#endif
    LOG_ASSERT(c >= 1, "decStrong() called on %p too many times", refs);
    if (c == 1) {
        const_cast<RefBase*>(this)->onLastStrongRef(id);
        if ((refs->mFlags&OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK) {
            delete this;
        }
    }
    refs->removeWeakRef(id);
    refs->decWeak(id);
}

sp的析构最终会导致对象的析构,前题是弱引用对象没有置OBJECT_LIFETIME_WEAK 这样一个标志


void RefBase::weakref_type::decWeak(const void* id)
{
    weakref_impl* const impl = static_cast<weakref_impl*>(this);
    impl->removeWeakRef(id);
    const int32_t c = android_atomic_dec(&impl->mWeak);
    LOG_ASSERT(c >= 1, "decWeak called on %p too many times", this);
    if (c != 1) return;
   
    if ((impl->mFlags&OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK) {
        if (impl->mStrong == INITIAL_STRONG_VALUE)
            delete impl->mBase;
        else {
//            LOGV("Freeing refs %p of old RefBase %p\n", this, impl->mBase);
            delete impl;
        }
    } else {
        impl->mBase->onLastWeakRef(id);
        if ((impl->mFlags&OBJECT_LIFETIME_FOREVER) != OBJECT_LIFETIME_FOREVER) {
            delete impl->mBase;
        }
    }
}

再回到添加服务

void MediaPlayerService::instantiate() {
    defaultServiceManager()->addService(
            String16("media.player"), new MediaPlayerService());
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值