1、前言

  最近看到一份程式碼,看到一個函式前面用__attribute__((destructor))修飾,當時感覺有點怪怪的,搜了整個程式,也沒發現哪個地方呼叫這個函式。於是從字面意思猜想,該函式會在程式結束後自動呼叫,與C++中的解構函式類似。第一次接觸GNU下的attribute,總結一下。

2、__attribute__介紹

  __attribute__可以設定函式屬性(Function Attribute)、變數屬性(Variable Attribute)和型別屬性(Type Attribute)。__attribute__前後都有兩個下劃線,並且後面會緊跟一對原括弧,括弧裡面是相應的__attribute__引數

  __attribute__語法格式為:__attribute__ ( ( attribute-list ) )

  若函式被設定為constructor屬性,則該函式會在main()函式執行之前被自動的執行。類似的,若函式被設定為destructor屬性,則該函式會在main()函式執行之後或者exit()被呼叫後被自動的執行。例如下面的程式:

#include <stdio.h>
#include <stdlib.h>
static int * g_count = NULL;
__attribute__((constructor)) void load_file()
{
printf("Constructor is called.\n");
g_count = (int *)malloc(sizeof(int));
if (g_count == NULL)
{
fprintf(stderr, "Failed to malloc memory.\n");
}
}
__attribute__((destructor)) void unload_file()
{
printf("destructor is called.\n");
if (g_count)
free(g_count);
}
int main()
{
return 0;
}

程式執行結果如下:

3、參考網址

  關於__attribute__的更多更加詳細的介紹可以參考:

http://blog.csdn.net/polisan/article/details/5031142

http://blog.csdn.net/ithomer/article/details/6566739

GCC __attribute__((constructor)|(destructor))

在閱讀TGTD的程式碼時發現了一個非常詭異的問題,聲明瞭一個空的全域性陣列,在使用的時候卻發現數組非空,在main()入口時陣列已經非空.陣列時在什麼地方被賦值了呢?最後發現__attribute__這個東東在起作用,類似於全域性變數類的建構函式在main()前被呼叫.

__attribute__((constructor))
__attribute__((destructor))

  1. /* test.c */
  2. #include<stdio.h>
  3. __attribute__((constructor)) void before_main()
  4. {
  5. printf("before main/n");
  6. }
  7. __attribute__((destructor)) void after_main()
  8. {
  9. printf("after main/n");
  10. }
  11. int main()
  12. {
  13. printf("in main/n");
  14. return 0;
  15. }

$ gcc test.c -o test

$ ./test

before main

in main

after main

根據上面的程式碼以及輸出結果,我們可以猜到__attribute__((constructor))表示這段程式碼將在main函式前呼叫,就像在C++裡面的全域性變數類的構造一樣.

說到C++裡面的全域性類物件的構造,我們不禁要問全域性類物件的構造跟__attribute__((constructor))以及destructor誰在前誰在後呢?

/*test2.cpp*/

  1. #include<iostream>
  2. using namespace std;
  3. __attribute__((constructor)) void before_main()
  4. {
  5. cout<<"Before Main"<<endl;
  6. }
  7. __attribute__((destructor)) void after_main()
  8. {
  9. cout<<"After Main"<<endl;
  10. }
  11. class AAA{
  12. public:
  13. AAA(){
  14. cout<<"AAA construct"<<endl;
  15. }
  16. ~AAA(){
  17. cout<<"AAA destructor" <<endl;
  18. }
  19. };
  20. AAA A;
  21. int main()
  22. {
  23. cout<<"in main"<<endl;
  24. return 0;
  25. }

$ make test2

$ ./test2

AAA construct

Before Main

in main

AAA destructor

After Main

可以看到全域性類的構造過程發生在before_main()函式前面,而析構也發生在after_main()前面.

__attribute__機制介紹

1. __attribute__

GNU C的一大特色(卻不被初學者所知)就是__attribute__機制。

__attribute__可以設定函式屬性(Function Attribute)、變數屬性(Variable Attribute)和型別屬性(Type Attribute)

__attribute__前後都有兩個下劃線,並且後面會緊跟一對原括弧,括弧裡面是相應的__attribute__引數

__attribute__語法格式為:

__attribute__ ( ( attribute-list ) )

函式屬性(Function Attribute),函式屬性可以幫助開發者把一些特性新增到函式宣告中,從而可以使編譯器在錯誤檢查方面的功能更強大。

__attribute__機制也很容易同非GNU應用程式做到相容。

GNU CC需要使用 –Wall,這是控制警告資訊的一個很好的方式。下面介紹幾個常見的屬性引數。

2. format

該屬性可以使編譯器檢查函式宣告和函式實際呼叫引數之間的格式化字串是否匹配。它可以給被宣告的函式加上類似printf或者scanf的特徵,該功能十分有用,尤其是處理一些很難發現的bug。

format的語法格式為:

format ( archetype,  string-index,  first-to-check )

format屬性告訴編譯器,按照printf,scanf,strftime或strfmon的引數表格式規則對該函式的引數進行檢查。archetype:指定是哪種風格;

string-index:指定傳入函式的第幾個引數是格式化字串;

first-to-check:指定從函式的第幾個引數開始按上述規則進行檢查。

具體使用格式如下:

__attribute__( ( format( printf,m,n ) ) )

__attribute__( ( format( scanf,m,n ) ) )

其中引數m與n的含義為:

m:第幾個引數為格式化字串(format string);

n:引數集合中的第一個,即引數“…”裡的第一個引數在函式引數總數排在第幾

注意,有時函式引數裡還有“隱身”的呢,後面會提到;

在使用上,__attribute__((format(printf,m,n)))是常用的,而另一種卻很少見到。

下面舉例說明,其中myprint為自己定義的一個帶有可變引數的函式,其功能類似於printf:

//m=1;n=2

extern void  myprint( const char *format,… ) __attribute__( ( format( printf,1,2 ) ) );

//m=2;n=3

extern void  myprint( int l,const char *format,... ) __attribute__( ( format( printf,2,3 ) ) );

需要特別注意的是,如果myprint是一個函式的成員函式,那麼m和n的值可有點“懸乎”了,例如:

//m=3;n=4

extern void  myprint( int l,const char *format,... ) __attribute__( ( format( printf,3,4 ) ) );

其原因是,類成員函式的第一個引數實際上一個“隱身”的“this”指標。(有點C++基礎的都知道點this指標,不知道你在這裡還知道嗎?)
這裡給出測試用例:attribute.c,程式碼如下:

extern void myprint(const char *format,...) __attribute__((format(printf,1,2)));

void test()

{

myprint("i=%d/n",6);

myprint("i=%s/n",6);

myprint("i=%s/n","abc");

myprint("%s,%d,%d/n",1,2);

}

執行$gcc –Wall –c attribute.c attribute後,輸出結果為:

attribute.c: In function `test':

attribute.c:7: warning: format argument is not a pointer (arg 2)

attribute.c:9: warning: format argument is not a pointer (arg 2)

attribute.c:9: warning: too few arguments for format

如果在attribute.c中的函式宣告去掉__attribute__((format(printf,1,2))),再重新編譯,

既執行$gcc –Wall –c attribute.c attribute後,則並不會輸出任何警告資訊。

注意,預設情況下,編譯器是能識別類似printf的“標準”庫函式。

3. noreturn

該屬性通知編譯器函式從不返回值。

當遇到函式需要返回值卻還沒執行到返回值處就已退出來的情況,該屬性可以避免出現錯誤資訊。C庫函式中的abort()和exit()的宣告格式就採用了這種格式

extern void  exit(int)   __attribute__( ( noreturn ) );

extern void  abort(void)  __attribute__( ( noreturn ) );

為了方便理解,大家可以參考如下的例子:

//name: noreturn.c     ;測試__attribute__((noreturn))

extern void  myexit();

int  test( int  n )

{

if ( n > 0 )

{

myexit();

/* 程式不可能到達這裡 */

}

else

{

return 0;

}

}

編譯$gcc –Wall –c noreturn.c  顯示的輸出資訊為:

noreturn.c: In function `test':

noreturn.c:12: warning: control reaches end of non-void function

警告資訊也很好理解,因為你定義了一個有返回值的函式test卻有可能沒有返回值,程式當然不知道怎麼辦了!加上__attribute__((noreturn))則可以很好的處理類似這種問題。把extern void myexit();修改為:

extern void  myexit() __attribute__((noreturn));

之後,編譯不會再出現警告資訊。

4. const

該屬性只能用於帶有數值型別引數的函式上,當重複呼叫帶有數值引數的函式時,由於返回值是相同的。所以此時編譯器可以進行優化處理,除第一次需要運算外, 其它只需要返回第一次的結果。

該屬性主要適用於沒有靜態狀態(static state)和副作用的一些函式,並且返回值僅僅依賴輸入的引數。為了說明問題,下面舉個非常“糟糕”的例子,該例子將重複呼叫一個帶有相同引數值的函式,具體如下:

extern int  square( int  n ) __attribute__ ( (const) );

for (i = 0; i < 100; i++ )

{

total += square (5) + i;

}

新增__attribute__((const))宣告,編譯器只調用了函式一次,以後只是直接得到了相同的一個返回值。

事實上,const引數不能用在帶有指標型別引數的函式中,因為該屬性不但影響函式的引數值,同樣也影響到了引數指向的資料,它可能會對程式碼本身產生嚴重甚至是不可恢復的嚴重後果。並且,帶有該屬性的函式不能有任何副作用或者是靜態的狀態,類似getchar()或time()的函式是不適合使用該屬性。

5. finstrument-functions

該引數可以使程式在編譯時,在函式的入口和出口處生成instrumentation呼叫。恰好在函式入口之後並恰好在函數出口之前,將使用當前函式的地址和呼叫地址來呼叫下面的profiling函式。(在一些平臺上,__builtin_return_address不能在超過當前函式範圍之外正常工作,所以呼叫地址資訊可能對profiling函式是無效的)

void  __cyg_profile_func_enter( void  *this_fn,void  *call_site );

void  __cyg_profile_func_exit( void  *this_fn,void  *call_site );

其中,第一個引數this_fn是當前函式的起始地址,可在符號表中找到;第二個引數call_site是呼叫處地址。

6. instrumentation

也可用於在其它函式中展開的行內函數。從概念上來說,profiling呼叫將指出在哪裡進入和退出行內函數。這就意味著這種函式必須具有可定址形式。如果函式包含內聯,而所有使用到該函式的程式都要把該內聯展開,這會額外地增加程式碼長度。如果要在C 程式碼中使用extern inline宣告,必須提供這種函式的可定址形式。
可對函式指定no_instrument_function屬性,在這種情況下不會進行 instrumentation操作。例如,可以在以下情況下使用no_instrument_function屬性:上面列出的profiling函式、高優先順序的中斷例程以及任何不能保證profiling正常呼叫的函式。

no_instrument_function

如果使用了-finstrument-functions,將在絕大多數使用者編譯的函式的入口和出口點呼叫profiling函式。使用該屬性,將不進行instrument操作。

7. constructor/destructor

若函式被設定為constructor屬性,則該函式會在main()函式執行之前被自動的執行。類似的,若函式被設定為destructor屬性,則該函式會在main()函式執行之後或者exit()被呼叫後被自動的執行。擁有此類屬性的函式經常隱式的用在程式的初始化資料方面,這兩個屬性還沒有在面向物件C中實現。

8. 同時使用多個屬性

可以在同一個函式聲明裡使用多個__attribute__,並且實際應用中這種情況是十分常見的。使用方式上,你可以選擇兩個單獨的__attribute__,或者把它們寫在一起,可以參考下面的例子:

extern void  die(const char *format, ...)   __attribute__( (noreturn))   __attribute__((format(printf, 1, 2)) );

或者寫成

extern void  die(const char *format,...)    __attribute__( (noreturn,  format(printf, 1, 2)) );

如果帶有該屬性的自定義函式追加到庫的標頭檔案裡,那麼所以呼叫該函式的程式都要做相應的檢查。

9. 和非GNU編譯器的相容性

__attribute__設計的非常巧妙,很容易作到和其它編譯器保持相容。也就是說,如果工作在其它的非GNU編譯器上,可以很容易的忽略該屬性。即使__attribute__使用了多個引數,也可以很容易的使用一對圓括弧進行處理,例如:

/* 如果使用的是非GNU C, 那麼就忽略__attribute__ */

#ifndef __GNUC__

#define     __attribute__(x)     /* NOTHING * /

#endif

需要說明的是,__attribute__適用於函式的宣告而不是函式的定義。所以,當需要使用該屬性的函式時,必須在同一個檔案裡進行宣告,例如:

/* 函式宣告 */

void  die( const char *format, ... ) __attribute__( (noreturn) )   __attribute__( ( format(printf,1,2) ) );

void  die( const char *format,... )

{   /* 函式定義 */  }

更多屬性參考:http://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html

10. 變數屬性(Variable Attributes)

關鍵字__attribute__也可以對變數(variable)或結構體成員(structure field)進行屬性設定。

在使用__attribute__引數時,你也可以在引數的前後都加上“__”(兩個下劃線),例如,使用__aligned__而不是aligned,這樣,你就可以在相應的標頭檔案裡使用它而不用關心標頭檔案裡是否有重名的巨集定義。

11. 型別屬性(Type Attribute)

關鍵字__attribute__也可以對結構體(struct)或共用體(union)進行屬性設定。

大致有六個引數值可以被設定:aligned,packed,transparent_union,unused,deprecatedmay_alias

12. aligned (alignment)

該屬性設定一個指定大小的對齊格式(以位元組為單位),例如:

struct S { short f[3]; } __attribute__ ( ( aligned (8) ) );

typedef  int  more_aligned_int __attribute__ ( ( aligned (8) ) );

這裡,如果sizeof(short)的大小為2(byte),那麼,S的大小就為6。取一個2的次方值,使得該值大於等於6,則該值為8,所以編譯器將設定S型別的對齊方式為8位元組。該宣告將強制編譯器確保(盡它所能)變數型別為struct S或者more-aligned-int的變數在分配空間時採用8位元組對齊方式。

如上所述,你可以手動指定對齊的格式,同樣,你也可以使用預設的對齊方式。例如:

struct S { short f[3]; } __attribute__ ( (aligned) );

上面,aligned後面不緊跟一個指定的數字值,編譯器將依據你的目標機器情況使用最大最有益的對齊方式。

int  x __attribute__ ( (aligned (16) ) )  =  0;

編譯器將以16位元組(注意是位元組byte不是位bit)對齊的方式分配一個變數。也可以對結構體成員變數設定該屬性,例如,建立一個雙字對齊的int對,可以這麼寫:

Struct  foo {  int  x[2] __attribute__ ( (aligned (8) ) );  };

選擇針對目標機器最大的對齊方式,可以提高拷貝操作的效率。
aligned屬性使被設定的物件佔用更多的空間,相反的,使用packed可以減小物件佔用的空間。
需要注意的是,attribute屬性的效力與你的聯結器也有關,如果你的聯結器最大隻支援16位元組對齊,那麼你此時定義32位元組對齊也是無濟於事的。

13. packed

使用該屬性可以使得變數或者結構體成員使用最小的對齊方式,即對變數是一位元組對齊,對域(field)是位對齊。使用該屬性對struct或者union型別進行定義,設定其型別的每一個變數的記憶體約束。當用在enum型別定義時,暗示了應該使用最小完整的型別 (it indicates that the smallest integral type should be used)。

下面的例子中,x成員變數使用了該屬性,則其值將緊放置在a的後面:

struct  test

{

char  a;

int  x[2] __attribute__ ((packed));

};

下面的例子中,my-packed-struct型別的變數陣列中的值將會緊緊的靠在一起,但內部的成員變數s不會被“pack”,如果希望內部的成員變數也被packed,my-unpacked-struct也需要使用packed進行相應的約束。

struct my_packed_struct

{

char  c;

int  i;

struct  my_unpacked_struct  s;

}__attribute__ ( (__packed__) );

其它可選的屬性值還可以是:cleanup,common,nocommon,deprecated,mode,section,shared, tls_model,transparent_union,unused,vector_size,weak,dllimport,dlexport等。

更多詳細參考:http://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Variable-Attributes.html#Variable-Attributes

14. 變數屬性與型別屬性舉例

下面的例子中使用__attribute__屬性定義了一些結構體及其變數,並給出了輸出結果和對結果的分析。
程式程式碼為:

struct  p

{

int a;

char b;

char c;

}__attribute__( ( aligned(4) ) ) pp;

struct  q

{

int a;

char b;

struct n qn;

char c;

}__attribute__( ( aligned(8) ) ) qq;

int  main()

{

printf("sizeof(int)=%d,sizeof(short)=%d,sizeof(char)=%d/n",sizeof(int),sizeof(short),sizeof(char));

printf("pp=%d,qq=%d /n", sizeof(pp),sizeof(qq));

return 0;

}

輸出結果:

sizeof(int)=4,sizeof(short)=2,sizeof(char)=1

pp=8,qq=24

分析:

sizeof(pp):

sizeof(a)+ sizeof(b)+ sizeof(c)=4+1+1=6<2^3=8= sizeof(pp)

sizeof(qq):

sizeof(a)+ sizeof(b)=4+1=5

sizeof(qn)=8;

即qn是採用8位元組對齊的,所以要在a,b後面添3個空餘位元組,然後才能儲存qn,

4+1+(3)+8+1=17

因為qq採用的對齊是8位元組對齊,所以qq的大小必定是8的整數倍,即qq的大小是一個比17大又是8的倍數的一個最小值,由此得到

17<2^4+8=24= sizeof(qq)

更詳細的介紹見:http://gcc.gnu.org/

下面是一些便捷的連線:

GCC 4.0 Function Attributes

GCC 4.0 Variable Attributes

GCC 4.0 Type Attributes

15. Ref

簡單__attribute__介紹:http://www.unixwiz.net/techtips/gnu-c-attributes.html

詳細__attribute__介紹:http://gcc.gnu.org/

 

offsetof與container_of巨集[總結]

1、前言

  今天在看程式碼時,遇到offsetof和container_of兩個巨集,覺得很有意思,功能很強大。offsetof是用來判斷結構體中成員的偏移位置,container_of巨集用來根據成員的地址來獲取結構體的地址。兩個巨集設計的很巧妙,值得學習。linux核心中有著兩個巨集的定義,並在連結串列結構中得到應用。不得不提一下linux核心中的連結串列,設計的如此之妙,只需要兩個指標就搞定了。後續認真研究一下這個連結串列結構。

2、offsetof巨集

  使用offsetof巨集需要包含stddef.h標頭檔案,例項可以參考:http://www.cplusplus.com/reference/cstddef/offsetof/

offsetof巨集的定義如下:

#define offsetof(type, member) (size_t)&(((type*)0)->member)

  巧妙之處在於將地址0強制轉換為type型別的指標,從而定位到member在結構體中偏移位置。編譯器認為0是一個有效的地址,從而認為0是type指標的起始地址。

3、container_of巨集

  使用container_of巨集需要包含linux/kernel.h標頭檔案,container_of巨集的定義如下所示:

#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})

container_of巨集分為兩部分,

第一部分:const typeof( ((type *)0)->member ) *__mptr = (ptr);

通過typeof定義一個member指標型別的指標變數__mptr,(即__mptr是指向member型別的指標),並將__mptr賦值為ptr。

第二部分: (type *)( (char *)__mptr - offsetof(type,member) ),通過offsetof巨集計算出member在type中的偏移,然後用member的實際地址__mptr減去偏移,得到type的起始地址,即指向type型別的指標。

第一部分的目的是為了將統一轉換為member型別指標。

4、測試程式

 1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #define NAME_STR_LEN 32
5
6 #define offsetof(type, member) (size_t)&(((type*)0)->member)
7
8 #define container_of(ptr, type, member) ({ \
9 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
10 (type *)( (char *)__mptr - offsetof(type,member) );})
11
12 typedef struct student_info
13 {
14 int id;
15 char name[NAME_STR_LEN];
16 int age;
17 }student_info;
18
19
20 int main()
21 {
22 size_t off_set = 0;
23 off_set = offsetof(student_info, id);
24 printf("id offset: %u\n",off_set);
25 off_set = offsetof(student_info, name);
26 printf("name offset: %u\n",off_set);
27 off_set = offsetof(student_info, age);
28 printf("age offset: %u\n",off_set);
29 student_info *stu = (student_info *)malloc(sizeof(student_info));
30 stu->age = 10;
31 student_info *ptr = container_of(&(stu->age), student_info, age);
32 printf("age:%d\n", ptr->age);
33 printf("stu address:%p\n", stu);
34 printf("ptr address:%p\n", ptr);
35 return 0;
36 }

測試結果:

5、參考網址

http://blog.csdn.net/thomas_nuaa/article/details/3542572

http://blog.chinaunix.net/uid-28489159-id-3549971.html

C語言中offsetof巨集的應用

offsetof  :
    Retrieves the offset of a member from the beginning of its parent structure.

size_t offsetof(structName, memberName);

Parameters:
    structName : Name of the parent data structure.
    memberName :Name of the member in the parent data structure for which to determine the offset.

Return Value : offsetof returns the offset in bytes of the specified member from
          the beginning of its parent data structure. It is undefined for bit fields.
Remarks :
   
The offsetof macro returns the offset in bytes of memberName from the
beginning of the structure specified by structName. You can specify
types with the struct keyword.

Note  :
    offsetof is not a function and cannot be described using a C prototype.

#define offsetof(s, m)   (size_t)&(((s *)0)->m)

s是一個結構名,它有一個名為m的成員(s和m 是巨集offsetof的形參,它實際是返回結構s的成員m的偏移地址.

(s *)0 是騙編譯器說有一個指向類(或結構)s的指標,其地址值0

&((s *)0)->m   是要取得類s中成員變數m的地址. 因基址為0,這時m的地址當然就是m在s中的偏移

最後轉換size_t 型,即unsigned int。

有例子如:
struct   A
{
    int   i;
    int   j;
};
  
struct   A   *pA;
pA = new   A;
這時,pA實際上是一個Pointer, 指向某一確定的記憶體地址, 如0x1234;
而pA->i 整體是一個int型變數,其地址是&(pA->i), '&'為取址運算子;
那麼&(pA->i)一定等於0x1234,因 i 是結構體A的第一個元素。
而&(pA->j)一定是0x1234 + 0x4 = 0x1238; 因為sizeof(int) = 4;
  
這個做法的巧妙之處就是:它把“0”作為上例中的pA,那麼&(pA->j)就是 j 的offset

解析結果是:
(s*)0,將 0 強制轉換為Pointer to "s"   
可以記 pS = (s*)0,pS是指向s的指標,它的值是0;
那麼pS->m就是m這個元素了,而&(pS->m)就是m的地址,就是offset

下面是個offsetof應用的例子,其中巨集OBJECT_HEAD_ADDRES的作用是根據一個物件或結構的某成員的地址,求其首地址。

完整的例子如下:

/* offsetof example */

#include "stdafx.h"
#include <stdio.h>
#include <stddef.h>

/************************************************************************/
/* Macro OBJECT_HEAD_ADDRESS : calculate the struct's address according
/* to one of its member's address
/************************************************************************/
#define OBJECT_HEAD_ADDRESS(ClassName,MemberName,Addre) /
Addre - offsetof(ClassName, MemberName)

struct S
{
int ID;
char *addr;
char name[20];
};

struct S1
{
S employee;
char *title;
};

struct S2
{
char singlechar;
int arraymember[10];
char anotherchar;
};

int main(int argc, char* argv[])
{
/* Example 1
S2 s2 = {'a', 10, 'b'};
int head = int(&(s2.singlechar));
int memb = int(&(s2.anotherchar));

int i = OBJECT_HEAD_ADDRESS(S2, anotherchar, memb);

printf ("offsetof(S2, singlechar) is %d/n", offsetof(S2, singlechar));
printf ("offsetof(S2, arraymember) is %d/n", offsetof(S2, arraymember));
printf ("offsetof(S2, anotherchar) is %d/n", offsetof(S2, anotherchar));
printf("s2.anotherchar's address is %x ==> s2's address is %x/n", memb, i);
*/
/* Example 2
S s = {100, "Nanjing", "Thomas"};
int id = int(&(s.ID));
int ad = int(&(s.addr));
int nm = int(&(s.name));

int j = OBJECT_HEAD_ADDRESS(S, addr, ad);

printf ("offsetof(S, ID) is %d/n", offsetof(S, ID));
printf ("offsetof(S, addr) is %d/n", offsetof(S, addr));
printf ("offsetof(S, name) is %d/n", offsetof(S, name));
printf ("s.ID's address : %x/ns.addr's address : %x/ns.name's address : %x/n", id, ad, nm);
printf("s.addr's address is %x ==> s's address is %x/n", ad, j);
*/
/* Example 3 */
S1 s1 = {{100, "Nanjing", "Thomas Chen"},"Thomas Chen"};
int td = int(&(s1.title));
int k = OBJECT_HEAD_ADDRESS(S1, title, td);

printf ("offsetof(S1, employee) is %d/n", offsetof(S1, employee));
printf ("offsetof(S1, title) is %d/n", offsetof(S1, title));
printf("s1.title's address is %x ==> s1's address is %x/n", td, k);

return 0;
}
/* Example 1 Output */
offsetof(S2, singlechar) is 0
offsetof(S2, arraymember) is 4
offsetof(S2, anotherchar) is 44
s2.anotherchar's address is 12ff7c ==> s2's address is 12ff50

/* Example 2 Output */
offsetof(S, ID) is 0
offsetof(S, addr) is 4
offsetof(S, name) is 8
s.ID's address : 12ff64
s.addr's address : 12ff68
s.name's address : 12ff6c
s.addr's address is 12ff68 ==> s's address is 12ff64

/* Example 3 Output */
offsetof(S1, employee) is 0
offsetof(S1, title) is 28
s1.title's address is 12ff7c ==> s1's address is 12ff60

Linux核心container_of詳解

在開發Linux驅動的時候,經常會用到container_of這個巨集:
include/linux/kernel.h
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  1. /**
  2. * container_of - 通過結構體的一個成員獲取容器結構體的指標
  3. * @ptr: 指向成員的指標。
  4. * @type: 成員所嵌入的容器結構體型別。
  5. * @member: 結構體中的成員名。
  6. *
  7. */
  8. #define container_of(ptr, type, member) ({ \
  9. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  10. (type *)( (char *)__mptr - offsetof(type,member) );})
 
    這個巨集的作用其實很簡單,就是通過一個容器(結構體)中某個成員的指標得到指向這個容器(結構體)的指標,簡單的說就是通過成員找容器。
 
這個巨集的實現程式碼也就只有兩行,下面仔細分析一下:
(1)const typeof( ((type *)0)->member ) *__mptr = (ptr);
定義一箇中間變數__mptr,它等於提供給巨集的引數ptr,也就是指向某個成員的指標。這個中間變數的命名意義是:
"__"代表內部使用,核心程式設計中常常這麼做;
“m”代表middle。
 
 
(2)(type *)( (char *)__mptr - offsetof(type,member) );
這行程式碼的作用是通過中間變數__mptr(指向某個成員的指標)減去這個成員在容器(結構體)中的偏移來得到指向容器(結構體)的指標。
這裡注意偏移的獲取offsetof巨集的實現:
 
include/linux/stddef.h
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  1. #ifdef __compiler_offsetof
  2. #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
  3. #else
  4. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  5. #endif
    這個實現依賴編譯器,如果編譯器內建了__compiler_offsetof(TYPE,MEMBER)的實現,就使用編譯器的實現,否則就是用((size_t) &((TYPE *)0)->MEMBER)。
    在Linux-2.6.29(含)以上核心必須使用4.1.1(不含)以上的編譯器,所以Linux-3.0核心肯定是使用了編譯器核心內建的巨集實現
 
include/linux/compiler-gcc4.h
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  1. #define __compiler_offsetof(a,b) __builtin_offsetof(a,b)
---------------------------------------------------------------------
巨集分析完了,這裡附上一個圖來加深下印象:
 
紅色代表輸出,藍色代表輸入,黑色代表中間變數