swift class的缺省基类(SwiftObject)与内存模型

Hard Constraints on Resilience

The root of a class hierarchy must remain stable, at pain of invalidating the metaclass hierarchy. Note that a Swift class without an explicit base class is implicitly rooted in the SwiftObject Objective-C class. 

https://github.com/apple/swift/blob/master/docs/ABI/TypeLayout.rst

//===----------------------------------------------------------------------===//

//

// This implements the Objective-C root class that provides basic `id`-

// compatibility and `NSObject` protocol conformance for pure Swift classes.

//

//===----------------------------------------------------------------------===//

SWIFT_RUNTIME_EXPORT @interface SwiftObject<NSObject> {

 @private

  Class isa;

  SWIFT_HEAPOBJECT_NON_OBJC_MEMBERS;

}

- (BOOL)isEqual:(id)object;

- (NSUInteger)hash;

…..

https://github.com/apple/swift/blob/f4db1dd7a4abba2685247e1a7415d4fcb91f640d/stdlib/public/runtime/SwiftObject.h

// The members of the HeapObject header that are not shared by a

// standard Objective-C instance

#define SWIFT_HEAPOBJECT_NON_OBJC_MEMBERS      

  InlineRefCounts refCounts

oc端的函数可以使用oc调用约定进行调用;

swift本地端的函数可以使用swift调用约定进行调用;

https://github.com/apple/swift/blob/master/stdlib/public/SwiftShims/HeapObject.h

接下来是 SWIFT_HEAPOBJECT_NON_OBJC_MEMBERS,这是一个宏定义,展开后即:

RefCounts<InlineRefCountBits> refCounts;

这是一个相当重要东西,引用计数、弱引用、unowned 引用都与它有关,同时它也是 Swift 对象(文中后续的 Swift 对象均指引用类型,即 class 的实例)中较为复杂的一个结构。

http://www.manongjc.com/article/64837.html

Swift是苹果公司最新推出的编程语言,据很多人说,是用来”代替“Objective-C。但是没有确切的证据。我花了一些时间对Swift二进制和运行环境实施逆向工程技术,然后我对Swift有些少许的发现。目前为止,结论就是:Swift是没有消息机制的Objective-C。

对象

信不信由你,Swift中的对象就是Objective-C的对象。在Mach-O二进制文件中,__objc_classlist包含每个二进制文件中类的数据。其结构如下所示:

struct objc_class {

    uint64_t isa;

    uint64_t superclass;

    uint64_t cache;

    uint64_t vtable;

    uint64_t data;

};

Swift 内部机制浅析 

https://www.oschina.net/translate/inside-swift

class Human {

    //8 type or isa

    //retainCount

    var age: Int?//16

    var name: String?//16

    var nicknames: [String] = [String]()

    

    //返回指向 Human 实例头部的指针

    func headPointerOfClass() -> UnsafeMutablePointer<Int8> {

        let opaquePointer = Unmanaged.passUnretained(self as AnyObject).toOpaque()

        let mutableTypedPointer = opaquePointer.bindMemory(to: Int8.self, capacity: MemoryLayout<Human>.stride)

        return UnsafeMutablePointer<Int8>(mutableTypedPointer)

    }

}

MemoryLayout<Human>.size       //8

let human = Human()

let arrFormJson = ["goudan","zhaosi", "wangwu"]

//拿到指向 human 堆内存的 void * 指针

let humanRawPtr = UnsafeMutableRawPointer(human.headPointerOfClass())

//nicknames 数组在内存中偏移 48byte 的位置(8+8+16+16)

let humanNickNamesPtr =  humanRawPtr.advanced(by: 48).assumingMemoryBound(to: Array<String>.self)

print(human.nicknames)

//[]

humanNickNamesPtr.initialize(to: arrFormJson)

print(human.nicknames)

human.nicknames           //["goudan","zhaosi", "wangwu"]

let retainCount =  humanRawPtr.advanced(by: 8).assumingMemoryBound(to: Int8.self)

print(retainCount.pointee)

参考文献:

https://www.cnblogs.com/doudouyoutang/p/9760603.html

https://www.oschina.net/translate/inside-swift

原文地址:https://www.cnblogs.com/feng9exe/p/10573772.html