简体   繁体   English

Objective-C类的方法存储在何处以及如何存储?

[英]Where and how are an Objective-C class's methods stored?

I know that when an object is instantiated on the heap, at the least enough memory is allocated to hold the object's ivars. 我知道当一个对象在堆上实例化时,至少分配足够的内存来保存对象的ivars。 My question is about how methods are stored by the compiler. 我的问题是编译器如何存储方法。 Is there only one instance of method code in memory? 内存中只有一个方法代码实例吗? Or is the code generated an intrinsic part of the object in memory, stored contiguously with the ivars and executed? 或者代码是在内存中生成对象的固有部分,与ivars连续存储并执行?

It seems like if the latter were the case, even trivial objects such as NSString s would require a (relatively) large amount of memory ( NSString inherits methods from NSObject , also). 看起来如果后者就是这种情况,即使像NSString这样的平凡对象也需要(相对)大量的内存( NSString也从NSObject继承了方法)。

Or is the method stored once in memory and passed a pointer to the object which owns it? 或者该方法是在内存中存储一​​次并将指针传递给拥有它的对象?

In a "standard" Objective-C runtime, every object contains, before any other instance variables, a pointer to the class it is a member of, as if the base Object class had an instance variable called: 在“标准”Objective-C运行时,每个对象在任何其他实例变量之前包含指向它所属的类的指针,就像基类Object类具有一个名为的实例变量一样:

Class isa;

Each object of a given class shares the same isa pointer. 给定类的每个对象共享相同的isa指针。

The class contains a number of elements, including a pointer to the parent class, as well as an array of method lists. 该类包含许多元素,包括指向父类的指针,以及方法列表数组。 These methods are the ones implemented on this class specifically. 这些方法是专门在这个类上实现的方法。

struct objc_class {
    Class super_class;
    ...
    struct objc_method_list **methodLists;
    ...
};

These method lists each contain an array of methods: 这些方法列表都包含一系列方法:

struct objc_method_list {
    int method_count;
    struct objc_method method_list[];
};

struct objc_method {
    SEL method_name;
    char *method_types;
    IMP method_imp;
};

The IMP type here is a function pointer. 这里的IMP类型是一个函数指针。 It points to the (single) location in memory where the implementation of the method is stored, just like any other code. 它指向存储方法实现的内存中的(单个)位置,就像任何其他代码一样。


A note: What I'm describing here is, in effect, the ObjC 1.0 runtime. 注意:我在这里描述的实际上是ObjC 1.0运行时。 The current version doesn't store classes and objects quite like this; 当前版本不像这样存储类和对象; it does a number of complicated, clever things to make method calls even faster. 它做了许多复杂,聪明的事情,使方法调用更快。 But what I'm describing still is still the spirit of how it works, if not the exact way it does. 但我所描述的仍然是它如何运作的精神 ,如果不是它的确切方式。

I've also left out a few fields in some of these structures which just confused the situation (eg, backwards compatibility and/or padding). 我还在其中一些结构中遗漏了几个字段,这些字段混淆了情况(例如,向后兼容性和/或填充)。 Read the real headers if you want to see all the gory details. 如果你想看到所有的血腥细节,请阅读真正的标题。

Methods are stored once in memory. 方法一次存储在内存中。 Depending on the platform, they are paged into RAM as needed. 根据平台的不同,可根据需要将它们分页到RAM中。 If you really want more details, read the Mach-O and runtime guides from Apple. 如果您真的想了解更多细节,请阅读Apple的Mach-O和运行指南。 It's not usually something programmers concern themselves with any more unless they're doing something pretty low-level. 程序员通常不会关注自己,除非他们做的事情非常低级。

Objects don't really "own" methods. 对象并不真正“拥有”方法。 I suppose you could think of it as classes owning methods, so if you have 400 NSStrings you still only have one copy of each method in RAM. 我想你可以把它想象成拥有方法的类,所以如果你有400个NSStrings,你仍然只有RAM中每个方法的一个副本。

When a method gets called, the first parameter is the object pointer, self. 调用方法时,第一个参数是对象指针self。 That's how a method knows where the data is that it needs to operate on. 这就是方法如何知道它需要操作的数据的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM