function attributes, MDK

The keyword format is either of the following:
__attribute__((attribute1, attribute2, ...))
__attribute__((__attribute1__, __attribute2__, ...))
For example:
void * Function_Attributes_malloc_0(int b) __attribute__((malloc));
static int b __attribute__((__unused__));
The following table summarizes the available function attributes.

Table 9-3 Function attributes that the compiler supports, and their equivalents

Function attributeNon-attribute equivalent
__attribute__((alias)) -
__attribute__((always_inline)) __forceinline
__attribute__((const)) __pure
__attribute__((constructor[(priority)])) -
__attribute__((deprecated)) -
__attribute__((destructor[(priority)])) -
__attribute__((format_arg(string-index))) -
__attribute__((malloc)) -
__attribute__((noinline)) __declspec(noinline)
__attribute__((no_instrument_function)) -
__attribute__((nomerge)) -
__attribute__((nonnull)) -
__attribute__((noreturn)) __declspec(noreturn))
__attribute__((notailcall)) -
__attribute__((pcs("calling_convention"))) -
__attribute__((pure)) -
__attribute__((section("name"))) -
__attribute__((unused)) -
__attribute__((used)) -
__attribute__((visibility("visibility_type"))) -
__attribute__((weak)) __weak
__attribute__((weakref("target"))) -

Usage

You can set these function attributes in the declaration, the definition, or both. For example:
void AddGlobals(void) __attribute__((always_inline));
__attribute__((always_inline)) void AddGlobals(void) {...}
When function attributes conflict, the compiler uses the safer or stronger one. For example, __attribute__((used)) is safer than __attribute__((unused)), and __attribute__((noinline)) is safer than __attribute__((always_inline)).
 
 

1. __attribute__((alias))

Examples

 不能在块范围内指定别名。编译器忽略附加到本地函数定义的属性,并将函数定义视为正常的本地定义。在输出对象文件中,编译器将别名调用替换为对原始函数名的调用,并在原始名称旁边发出别名。
如果原始函数被定义为静态函数,而别名被定义为extern,则编译器将原始函数更改为外部函数。
#include <stdio.h>
void foo(void)
{
    printf("%s\n", __FUNCTION__);
}
void bar(void) __attribute__((alias("foo")));
void gazonk(void)
{
    bar(); // calls foo
}

2. __attribute__((always_inline)) 

此函数属性指示函数必须内联。无论函数的特征如何,编译器都会尝试内联该函数。但是,如果这样做会导致问题,编译器不会内联函数。例如,递归函数只内联一次。

这个函数属性是ARM编译器支持的GNU编译器扩展。它具有相当__forceinline关键字。

 Examples

static int max(int x, int y) __attribute__((always_inline));
static int max(int x, int y)
{
    return x > y ? x : y; // always inline if possible
}

3. __attribute__((const))

许多函数只检查传递给它们的参数,除了返回值没有作用。这是一个比__attribute__((pure))更严格的等级,因为允许读取全局内存是不是一个函数。如果已知函数只对其参数进行操作,则它可能受到常见的子表达式消除和循环优化的影响。

 此函数属性是ARM编译器支持的GNU编译器扩展。它的关键字等效_pure

int Function_Attributes_const_0(int b) __attribute__((const));
int Function_Attributes_const_0(int b)
{
    int aLocal=0;
    aLocal += Function_Attributes_const_0(b);
    aLocal += Function_Attributes_const_0(b);
    return aLocal;
}

 

 在此代码中,可能只调用一次函数_properties_const_0,结果加倍以获得正确的返回值。

4. 

 

posted @ 2018-04-16 21:04  QIYUEXIN  阅读(326)  评论(0编辑  收藏  举报