Swift 内存管理
引用计数结构
之前我们分析过了 Swift 实例的内存结构如下:
现在我们继续分析 refCounts
InlineRefCounts
HeapObject 结构如下:
struct HeapObject {
/// This is always a valid pointer to a metadata object.
HeapMetadata const *metadata;
SWIFT_HEAPOBJECT_NON_OBJC_MEMBERS;
#ifndef __swift__
HeapObject() = default;
// Initialize a HeapObject header as appropriate for a new...
Swift 对象方法
类声明中的方法是通过 V-table 来进行调度的
V-table 在 SIL 中是如下表示的:
decl ::= sil-vtable
sil-vtable ::= 'sil_vtable' identifier '{' sil-vtable-entry* '}'
sil-vtable-entry ::= sil-decl-ref ':' sil-linkage? sil-function-na
me
如下示例代码
class Subject {
var age: Int = 15
var name: String = "小明"
func method0() -> Void { print("method0")}
func method...
Swift 结构体
结构体的一些特性
如下示例代码:
struct Teacher {
var age: Int
var name: String
}
如下:
查看 SIL 码:
struct Teacher {
@_hasStorage var age: Int { get set }
@_hasStorage var name: String { get set }
init(age: Int, name: String)
}
自动帮你实现了带有初始化参数的方法。
假如都有初始值的情况:
struct Teacher {
var age: Int = 18
var name: String = "liming"
}
let t = T...
Swift 对象、类和属性
实例对象的结构
class Teacher {
var age: Int = 18;
var name: String = "erge";
}
var t = Teacher();
初始化的时候汇编调用如下:
SwiftDemo`Teacher.__allocating_init():
0x100003c90 <+0>: pushq %rbp
0x100003c91 <+1>: movq %rsp, %rbp
0x100003c94 <+4>: pushq %r13
0x100003c96 <+6>: pushq %rax
0x100003c97 <...
15 post articles, 2 pages.