__attribute__: GNU C 的一大特色就是__attribute__ 機(jī)制
掃描二維碼
隨時(shí)隨地手機(jī)看文章
__attribute__:?GNU C 的一大特色就是__attribute__
機(jī)制。__attribute__ 可以設(shè)置函數(shù)屬性(Function),變量屬性(Variable)和類型屬性(Type)。
__attribute__
書寫特征是:
---- __attribute__
前后都有兩個(gè)下劃線,并切后面會緊跟一對原括弧,括弧里面是相應(yīng)的__attribute__
參數(shù)。
__attribute__
語法格式為:__attribute__ ((attribute-list))
__attribute__關(guān)鍵字主要是用來在函數(shù)或數(shù)據(jù)聲明中設(shè)置其屬性。給函數(shù)設(shè)置屬性的主要目的在于讓編譯器進(jìn)行優(yōu)化。GCC為函數(shù)提供了幾種類型的屬性,其中包含:
1)構(gòu)造函數(shù)(constructors)和析構(gòu)函數(shù)(destructors)。
__attribute__((constructor)): 需要在main函數(shù)之前調(diào)用的函數(shù),可以設(shè)置constructor屬性,實(shí)現(xiàn)初始化。
2)packed屬性:使用該屬性可以使得變量或者結(jié)構(gòu)體成員使用最小的對齊方式。
__attribute__((packed))的作用就是告訴編譯器取消結(jié)構(gòu)在編譯過程中的優(yōu)化對齊,按照實(shí)際占用字節(jié)數(shù)進(jìn)行對齊,是GCC特有的語法。這個(gè)功能和OS沒有關(guān)系,和編譯器有關(guān),gcc的編譯器不是緊湊模式的,在windows下,vc的編譯器也不是緊湊的,tc的編譯器是緊湊的。例如:
在GCC下:?
struct?data{ char?ch;? int?num; };
sizeof(int) = 4; sizeof(data) = 8; (非緊湊模式)
struct?data{ char?ch;? int?num; }__attribute__((packed));
sizeof(int) = 4; sizeof(data) = 5;
程序員應(yīng)當(dāng)使用類似下面的方式來指定這些屬性:
__attribute__((constructor))?static?void?beforeFunction() { ????printf("beforeFunctionn"); }
查閱GNU的文檔:
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() completes?or exit() is called.
--- Functions with these attributes are useful for initializing data that is used implicitly during the execution?of the program.You can set an optional integer priority to control the order in which constructor and destructor funcs?are run. ?A constructorwith a smaller priority?number?runs before a constructor with a larger priority number;the?opposite relationship holds for destructors. 構(gòu)造函數(shù)優(yōu)先級數(shù)字越小,越先執(zhí)行。析構(gòu)函數(shù)相反。
So, if you have a constructor that allocates a resource and a destructor that deallocates the same resource,?both functions typically have the same priority. ?
The priorities for constructor and destructor functions are the same as those specified for namespace-scope
C++ objects (see C++ Attributes).These attributes are not currently implemented for Objective-C.?
/*?max?length?*/ #define?MAX_LEN?16 /*server?max?number?*/ #define?MAX_SNUM?10 /*port?max?number*/ #define?MAX_PNUM?10 enum?server_state{idle?=?0,in_use}; //server?information typedef?struct?server_info{ ????char?host_name[MAX_LEN]; int?port_id[MAX_PNUM]; enum?server_state?st[MAX_PNUM]; char?used_by[MAX_PNUM][MAX_LEN]; char?ip_address[MAX_PNUM][MAX_LEN]; }?S_Info; char?pre_host_name[MAX_SNUM][MAX_LEN]?=?{ ????????????????????"eslruntime01", ????????????????????"eslruntime02", ????????????????????"eslruntime03", ????????????????????"eslruntime04", ????????????????????"eslruntime05", ????????????????????"eslruntime06", ????????????????????"eslruntime07", ????????????????????"eslruntime08", ????????????????????"eslruntime09", ????????????????????"eslruntime10" }; int?port_inf[MAX_PNUM]?=?{10000,10001,10002,10003,10004,10005,10006,10007,10008,10009}; enum?server_state?sst[MAX_PNUM]?=?{idle}; //create?server?table static?S_Info?*?init_server_info() { ????S_Info?*?server_t?=?(S_Info?*)malloc(sizeof(S_Info)?*?MAX_SNUM); ????int?i,j,k; ????for?(i?=?0;i?<?MAX_SNUM;++i) {? ????for(j?=?0;j?<?MAX_PNUM;++j) { ????????????server_t[i].port_id[j]?=?port_inf[j]; ????????????server_t[i].st[j]?=?sst[j]; memset(server_t[i].used_by[j],0,MAX_LEN); memset(server_t[i].ip_address[j],0,MAX_LEN); } ????for(k?=?0;k?<?MAX_LEN;++k) { ????????server_t[i].host_name[k]?=?pre_host_name[i][k]; ???? ????} } ????return?server_t; } //global?variable S_Info?*?server_total; static?void?create_total_server()?__attribute__((constructor)); static?void?free_total_server()?__attribute__((destructor)); static?void?create_total_server() { server_total?=?init_server_info(); } static?void?free_total_server() { free(server_total); server_total?=?NULL; }
可以給屬性設(shè)置優(yōu)先級:
static??__attribute__((constructor(101)))?void?before1() { ????printf("before1n"); } static??__attribute__((constructor(102)))?void?before2() { ????printf("before2n"); } static??__attribute__((constructor(103)))?void?before3() { ????printf("before3n"); }
以上三個(gè)函數(shù)會依照優(yōu)先級的順序調(diào)用.括號內(nèi)的數(shù)值(101,102,103)代表優(yōu)先級,另外,我以前看過,這個(gè)1-100
的范圍是保留的.
所以,最好從100之后開始用.(但是實(shí)際上,我在項(xiàng)目中測試100以內(nèi)的,也沒有得到警告)
main函數(shù)之前的,即constructor的優(yōu)先級,數(shù)值越小,越先調(diào)用。destructor中的數(shù)值越大,越先調(diào)用。
關(guān)于格式 --?按照文檔中的說法,__attribute__
應(yīng)該放在函數(shù)聲明的尾部;
之前.
__attribute__((constructor))?//?在main函數(shù)被調(diào)用之前調(diào)用 __attribute__((destructor))?//?在main函數(shù)被調(diào)用之后調(diào) #include__attribute__((constructor))?void?before_main()?{? ???printf("before?mainn");? }? __attribute__((destructor))?void?after_main()?{? ???printf("after?mainn");? }? int?main(int?argc,?char?**argv)?{? ???printf("in?mainn");? ???return?0;? } //這個(gè)例子的輸出結(jié)果將會是: before?main in?main after?main