__attribute__积累
来源
__attribute__是GNU C的一大特色,用于辅助编译器。可以设置函数属性、变量属性、类型属性。
attribute的设置作用和连接器也有关系,连接器的限制如果大于编译器,那需要按连接器限制走。
格式
attribute前后两个下划线,后面双括号中扩住具体属性。比如: __attribute__((objc_precise_lifetime))
name | explain | 备注 |
---|---|---|
objc_precise_lifetime | 可以对局部变量描述指针精确生命周期和类型的属性,不被编译器优化 | |
packed | 最小化size,缩小存储 | |
aligned | 设置自对齐,放大存储 | |
at | 将函数或变量绝对定位到flash或ram上 | 绝对定位不能在函数中,局部变量定义在栈区,由MDK自动分配;不能定义超过栈或flash的大小,避免溢出 |
section | 不同的区域,有只读、读写、初始化 | arm编译器编译后,将内存分为不同的区域 |
deprecated | 废弃不用 | (deprecated("解释用语,一般为替换对象") |
unused | 未使用 | |
always_inline | inline static | |
noreturn | 返回值不会给调用者使用,编译器可以优化掉 | |
sentinel | 可变参数函数最后需要一个 NULL 或 nil 作为参数 |
sentinel(0,0) 第一个参数0表示需要一个 NULL 或 nil ,1表示需要两个,依次类增,第二个参数只能是0或1(execle 的时候就是1)。可查看 GCC 中 Function-Attributes 详细解释 |
ns_returns_retained | 返回值需要release | alloc,init,copy,mutableCopy,new这几个家族的方法,后面默认加NS_RETURNS_RETAINED标识;而其他不指名标识的family的方法默认添加NS_RETURNS_NOT_RETAINED标识 |
ns_returns_not_retained | 返回值不需要release | |
unavailable | 不可用 | eg. __attribute__((unavailable("not available in automatic reference counting mode"))) |
objc_returns_inner_pointer | 返回内部c指针 | |
objc_arc_weak_reference_unavailable | 标记的类在rac中无法进行弱引用 | |
objc_requires_property_definitions | 表示标记的类必须在 @implementation 中为其属性实现 @dynamic or @synthesize | |
objc_root_class | 表示当前修饰的类就是根类 | |
objc_requires_super | 表示标志子类继承这个方法时需要调用 super,否则给出编译警告 | |
objc_designated_initializer | 指定初始化方法,必须调用 |
二、部分例子
- (NSSet *)allRetainedObjects
{
NSMutableArray *results = [[[super allRetainedObjects] allObjects] mutableCopy];
// Grab a strong reference to the object, otherwise it can crash while doing
// nasty stuff on deallocation
__attribute__((objc_precise_lifetime)) id anObject = self.object;
void *blockObjectReference = (__bridge void *)anObject;
NSArray *allRetainedReferences = FBGetBlockStrongReferences(blockObjectReference);
for (id object in allRetainedReferences) {
FBObjectiveCGraphElement *element = FBWrapObjectGraphElement(self, object, self.configuration);
if (element) {
[results addObject:element];
}
}
return [NSSet setWithArray:results];
}
struct __attribute__((packed)) BlockLiteral {
void *isa;
int flags;
int reserved;
void *invoke;
void *descriptor;
};
// Annotate classes which are root classes as really being root classes
#ifndef NS_ROOT_CLASS
#if __has_attribute(objc_root_class)
#define NS_ROOT_CLASS __attribute__((objc_root_class))
#else
#define NS_ROOT_CLASS
#endif
#endif
三、资料
A local variable of retainable object owner type and automatic storage duration may be annotated with the objc_precise_lifetime attribute to indicate that it should be considered to be an object with precise lifetime semantics.
Rationale
Nonetheless, it is sometimes useful to be able to force an object to be released at a precise time, even if that object does not appear to be used. This is likely to be uncommon enough that the syntactic weight of explicitly requesting these semantics will not be burdensome, and may even make the code clearer
9.64 attribute((packed)) variable attribute
The packed variable attribute specifies that a structure field has the smallest possible alignment. That is, one byte for a variable field, and one bit for a bitfield, unless you specify a larger value with the aligned attribute.