日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 芯聞號(hào) > 充電吧
[導(dǎo)讀]__attribute__:?GNU C 的一大特色就是__attribute__ 機(jī)制。__attribute__ 可以設(shè)置函數(shù)屬性(Function),變量屬性(Variable)和類型屬性(Ty


__attribute__:?GNU C 的一大特色就是__attribute__ 機(jī)制。__attribute__ 可以設(shè)置函數(shù)屬性(Function),變量屬性(Variable)和類型屬性(Type)。

__attribute__ 書寫特征是:

---- __attribute__ 前后都有兩個(gè)下劃線,并切后面會(huì)緊跟一對(duì)原括弧,括弧里面是相應(yīng)的__attribute__ 參數(shù)。

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

__attribute__關(guān)鍵字主要是用來(lái)在函數(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)體成員使用最小的對(duì)齊方式。

__attribute__((packed))的作用就是告訴編譯器取消結(jié)構(gòu)在編譯過(guò)程中的優(yōu)化對(duì)齊,按照實(shí)際占用字節(jié)數(shù)進(jìn)行對(duì)齊,是GCC特有的語(yǔ)法。這個(gè)功能和OS沒(méi)有關(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)使用類似下面的方式來(lái)指定這些屬性:


__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)先級(jí)數(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)先級(jí):



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ù)會(huì)依照優(yōu)先級(jí)的順序調(diào)用.括號(hào)內(nèi)的數(shù)值(101,102,103)代表優(yōu)先級(jí),另外,我以前看過(guò),這個(gè)1-100的范圍是保留的.

所以,最好從100之后開(kāi)始用.(但是實(shí)際上,我在項(xiàng)目中測(cè)試100以內(nèi)的,也沒(méi)有得到警告)

main函數(shù)之前的,即constructor的優(yōu)先級(jí),數(shù)值越小,越先調(diào)用。destructor中的數(shù)值越大,越先調(diào)用。

關(guān)于格式 --?按照文檔中的說(shuō)法,__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é)果將會(huì)是:
before?main
in?main
after?main


本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉