Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4825994
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: LINUX

2009-04-30 13:19:02


1.Declaring Attributes of Functions
  我这里只列举常用,或者说是我认为常用,有用的
  1.1 alias ("target"):函数别名
    The alias attribute causes the declaration to be emitted as an alias for another symbol, which must be specified. For instance,

             

void __f () { /* Do something. */; }
              void f () __attribute__ ((weak, alias ("__f")));

  1.2 constructor和destructor
    The constructor attribute causes the function to be called automatically before execution enters main (). Similarly, the destructor attribute causes the function to be called automatically after main () has completed or exit () has been called. Functions with these attributes are useful for initializing data that will be used implicitly during the execution of the program.        
     就是一个是在main函数之前call,一个是在main函数之后call.注意这不需要你显示的call.可以参考我最后的例子

   1.3 nonnull (arg-index, ...),参数不能为空
    The nonnull attribute specifies that some function parameters should be non-null pointers. For instance, the declaration:
              extern void *
              my_memcpy (void *dest, const void *src, size_t len)
                  __attribute__((nonnull (1, 2)));
     my_memcpy的第一个和第二个参数不能为空,注意是指向空,就是不能为空指针
   1.4 noreturn没有返回值
    A few standard library functions, such as abort and exit, cannot return. GCC knows this automatically. Some programs define their own functions that never return. You can declare them noreturn to tell the compiler this fact. For example,

              void fatal () __attribute__ ((noreturn));
              
              void
              fatal (/* ... */)
              {
                /* ... */ /* Print error message. */ /* ... */
                exit (1);
              }
        
   我认为的function attribute就这么多.详见:gcc doc什么都有
 
      
[root@mip-123456 attribute]# cat attribute.c
#include
#include

char* const_foo() __attribute__ ((const));
void before_main() __attribute__ ((constructor));
void after_main() __attribute__ ((destructor));

void __foo() { printf("this is foo\n");}
void f() __attribute__ ((weak,alias("__foo")));

char* const_foo()
{
 char* err = (char*)malloc(10*sizeof(char));
 sprintf(err,"%s","hello");
 return err;
}


void before_main() { printf("before main\n");}
void after_main() { printf("after main\n");}

int main()
{
 char* str;
 __foo();
 f();
 str = const_foo();
 free(str);
 str = NULL;

 return 0;
}

[root@mip-123456 attribute]# ./attribute
before main
this is foo
this is foo
after main

注意上面的free,没有free的话,会出现mem leak,可以用我以前提到的 valgrind ./attribute
自己看看.nonnull和noreturn我就没列举例子了.

2. Specifying Attributes of Variables
   2.1 aligned (alignment)
    按多少字节对齐
    This attribute specifies a minimum alignment for the variable or structure field, measured in bytes. For example, the declaration:

     int x __attribute__ ((aligned (16))) = 0;
     
     __attribute__ ((aligned(x)))
     按x个字节对齐

     __attribute__ ((aligned))时
   Whenever you leave out the alignment factor in an aligned attribute specification, the compiler automatically sets the alignment for the declared variable or field to the largest alignment which is ever used for any data type on the target machine you are compiling for. Doing this can often make copy operations more efficient, because the compiler can use whatever instructions copy the biggest chunks of memory when performing copies to or from the variables or fields that you have aligned this way.

The aligned attribute can only increase the alignment; but you can decrease it by specifying packed as well. See below.

Note that the effectiveness of aligned attributes may be limited by inherent limitations in your linker. On many systems, the linker is only able to arrange for variables to be aligned up to a certain maximum alignment. (For some linkers, the maximum supported alignment may be very very small.) If your linker is only able to align variables up to a maximum of 8 byte alignment, then specifying aligned(16) in an __attribute__ will still only provide you with 8 byte alignment. See your linker documentation for further information.
    看了下,大概意思就是根link有关,一般就都是按16个字节对齐了.


  packed
    The packed attribute specifies that a variable or structure field should have the smallest possible alignment—one byte for a variable, and one bit for a field, unless you specify a larger value with the aligned attribute.

    Here is a structure in which the field x is packed, so that it immediately follows a:

              struct foo
              {
                char a;
                int x[2] __attribute__ ((packed));
              };
        

[root@mip-123456 attribute]# cat var_attribute.c
#include
#include

struct foo
 {
  char a;
  int x[4] __attribute__ ((packed));
  };//17

struct bar8
 {
  char a;
  int x[4] __attribute__ ((aligned(8)));
  };//24

struct bar16
 {
  char a;
  int x[4] __attribute__ ((aligned(16)));
  };//32

struct bar32
 {
  char a;
  int x[4] __attribute__ ((aligned(32)));
  };//64

struct bar
 {
  //char a;
  char x[4] __attribute__ ((aligned));
  };

struct bar_16
 {
  char a;
  int x[4] __attribute__ ((aligned));
  };

int main()
{
  printf("sizeof(foo)=%d\nsizeof(bar)=%d\nsizeof(bar_16)=%d\n"
  "sizeof(bar8)=%d\n"
  "sizeof(bar16)=%d\n"
  "sizeof(bar32)=%d\n"
  ,sizeof(struct foo),sizeof(struct bar),sizeof(struct bar_16),sizeof(struct bar8),sizeof(struct bar16),sizeof(struct bar32));
 return 0;
}

[root@mip-123456 attribute]# ./var_attribute
sizeof(foo)=17
sizeof(bar)=16
sizeof(bar_16)=32
sizeof(bar8)=24
sizeof(bar16)=32
sizeof(bar32)=64

3. Specifying Attributes of Types
   aligned和packed都和var一样了
     struct S {
          short f[3];
      } __attribute__ ((aligned (8)));

      
      struct S {
          short f[3];
      } __attribute__ ((packed));
 
阅读(2866) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~