Swift引用计数管理一

Swift源码之引用计数管理一

  • 标识Object-C或者Swift的内存管理标志类

    typedef unsigned long   uintptr_t;
    /// 标识引用计数管理
    class WeakReferenceBits {
      
      /// 标识
      enum : uintptr_t {
        /// ObCject-C class
          NativeMarkerMask  = 0
        /// Swift class
        NativeMarkerValue = 0
      }
      
      /// 存储的bits位
      uintptr_t bits;
      
      /// 获取 side-table
      HeapObjectSideTableEntry *getNativeOrNull() const {
        assert(isNativeOrNull());
        if (bits == 0)
          return nullptr;
        else
          return
            reinterpret_cast(bits & ~NativeMarkerMask);
      }
      
      /// 设置side-table
      void setNativeOrNull(HeapObjectSideTableEntry *newValue) {
        assert((uintptr_t(newValue) & NativeMarkerMask) == 0);
        if (newValue)
          bits = uintptr_t(newValue) | NativeMarkerValue;
        else
          bits = 0;
      }
    };
    
  • WeakReference的负责管理WeakReferenceBits

    // Redeclare APIs from the Objective-C runtime.
    // These functions are not available through public headers, but are guaranteed
    // to exist on OS X >= 10.9 and iOS >= 7.0.
    
    OBJC_EXPORT id objc_retain(id);
    OBJC_EXPORT void objc_release(id);
    OBJC_EXPORT id _objc_rootAutorelease(id);
    OBJC_EXPORT void objc_moveWeak(id*, id*);
    OBJC_EXPORT void objc_copyWeak(id*, id*);
    OBJC_EXPORT id objc_initWeak(id*, id);
    OBJC_EXPORT void objc_destroyWeak(id*);
    OBJC_EXPORT id objc_loadWeakRetained(id*);
    
    /// 引用计数管理
    class WeakReference {
      union {
      ///  atomic 仅仅是为了保证线程安全加的锁,实际操作还是 WeakReferenceBits
        std::atomic nativeValue;
        
      /// 如果是OBJC类型
      #if SWIFT_OBJC_INTEROP
        id nonnativeValue;
      #endif
      };
      
       /// 区分OBJC和Swift的class
      void initWithNativeness(void *object, bool isNative) {
        if (isNative)
          /// swift-class
          nativeInit(static_cast(object));
        else
          /// Objc-class
          nonnativeInit(static_cast(object));
      }
      
       void unknownInit(void *object) {
       /// 判断是否是Objc类型
        if (isObjCTaggedPointerOrNull(object)) {
          nonnativeValue = static_cast(object);
        } else {
          /// 判断是否使用Swift原生引用计数管理对象
          bool isNative = objectUsesNativeSwiftReferenceCounting(object);
          initWithNativeness(object, isNative);
        }
      }
      
      /// OBJC-Class类型
      void nonnativeInit(id object) {
        /// OC的initWeak方法被触发
        objc_initWeak(&nonnativeValue, object);
      }
      
      /// Swift类的引用计数
      void nativeInit(HeapObject *object) {
        /// 判断是否存在side-Table
        auto side = object ? object->refCounts.formWeakReference() : nullptr;
        nativeValue.store(WeakReferenceBits(side), std::memory_order_relaxed);
      }
      
      /// 释放机制
      void destroyWithNativeness(bool isNative) {
        if (isNative)
          nativeDestroy();
        else
          nonnativeDestroy();
      }
      
      /// 原生Swift-class释放
      void nativeDestroy() {
        auto oldBits = nativeValue.load(std::memory_order_relaxed);
        nativeValue.store(nullptr, std::memory_order_relaxed);
        destroyOldNativeBits(oldBits);
      }
      
      /// 释放Swift-class
      void destroyOldNativeBits(WeakReferenceBits oldBits) {
        auto oldSide = oldBits.getNativeOrNull();
        if (oldSide)
          
          /// 这里下一篇文章讲 decrementWeak
          oldSide->decrementWeak();
      }
      
      /// OBJC-class释放
      void nonnativeDestroy() {
        objc_destroyWeak(&nonnativeValue);
      }
    }
    

你可能感兴趣的:(Swift引用计数管理一)