Someone has ask the same question before:Objective-C Runtime: What to put for size & alignment for class_addIvar? But it's not fully resolved.
有人曾经问过同样的问题:Objective-C运行时:class_addIvar的大小和对齐方式是什么?但这还没有完全解决。
The functions declaration is as follows:
功能声明如下:
BOOL class_addIvar(Class cls, const char *name, size_t size, uint8_t alignment, const char *types)
Which is used to add an instance variable to a dynamically created class in Objective-C.
它用于向Objective-C中动态创建的类添加实例变量。
The forth argument, uint8_t alignment
, is described in Apple's documentation:
第四个参数uint8_t对齐,在苹果的文档中有描述:
The instance variable's minimum alignment in bytes is 1<<align. The minimum alignment of an instance variable depends on the ivar's type and the machine architecture. For variables of any pointer type, pass log2(sizeof(pointer_type))
.
实例变量的最小对齐方式是1<
In some tutorials, it's just claimed that if the ivar is pointer type, I should use log2(sizeof(pointer_type))
; if the ivar is value type, I should use sizeof(value_type)
. But why? Can someone explain this in detail?
在一些教程中,它只是声明如果ivar是指针类型,我应该使用log2(sizeof(pointer_type));如果ivar是值类型,我应该使用sizeof(value_type)。但是为什么呢?有人能详细解释一下吗?
1 个解决方案
#1
5
If you really want to learn where these values come from, you'll need to look at architecture specific ABI references, for OSX and iOS, they can be found here: OS X, iOS.
如果您真的想了解这些值来自哪里,您将需要查看特定于架构的ABI引用,对于OSX和iOS,它们可以在这里找到:OSX、iOS。
Each of those documents should have a section titled 'Data Types and Data Alignment', which helps to explain those values for the specific architecture.
每个文档都应该有一个标题为“数据类型和数据对齐”的部分,这有助于为特定的体系结构解释这些值。
In practice, since C11, you can use the _Alignof
operator to have the compiler give you the correct value for a specific type (as it already needs to know this in order to generate proper machine code), so you can create a class_addIvar
that looks something like this:
在实践中,由于C11,您可以使用_Alignof操作符,让编译器为特定类型提供正确的值(因为它已经需要知道这一点,以便生成适当的机器码),因此您可以创建一个class_addIvar,其外观如下所示:
class_addIvar(myClass, "someIvar", sizeof(int), log2(_Alignof(int)), @encode(int))
Which should take care of all those gory details of the underlying type for you.
它应该为您处理底层类型的所有血淋淋的细节。
#1
5
If you really want to learn where these values come from, you'll need to look at architecture specific ABI references, for OSX and iOS, they can be found here: OS X, iOS.
如果您真的想了解这些值来自哪里,您将需要查看特定于架构的ABI引用,对于OSX和iOS,它们可以在这里找到:OSX、iOS。
Each of those documents should have a section titled 'Data Types and Data Alignment', which helps to explain those values for the specific architecture.
每个文档都应该有一个标题为“数据类型和数据对齐”的部分,这有助于为特定的体系结构解释这些值。
In practice, since C11, you can use the _Alignof
operator to have the compiler give you the correct value for a specific type (as it already needs to know this in order to generate proper machine code), so you can create a class_addIvar
that looks something like this:
在实践中,由于C11,您可以使用_Alignof操作符,让编译器为特定类型提供正确的值(因为它已经需要知道这一点,以便生成适当的机器码),因此您可以创建一个class_addIvar,其外观如下所示:
class_addIvar(myClass, "someIvar", sizeof(int), log2(_Alignof(int)), @encode(int))
Which should take care of all those gory details of the underlying type for you.
它应该为您处理底层类型的所有血淋淋的细节。