__attribute__((constructor))和__attribute__((destructor))的用法

本文介绍了在学习FIO源码时遇到的__attribute__((constructor))和__attribute__((destructor)))关键字,它们用于在C语言中实现全局变量的构造和析构。通过示例说明了这两个属性如何确保ioengine在main函数开始前被正确注册,以及如何根据优先级顺序执行构造函数。这种特性提高了全局变量初始化的灵活性,并在FIO中通过宏进行了封装,便于代码维护和扩展。

最近在学习FIO源码的时候,一开始总是没能理解FIO是怎么注册ioengine,运行到main里面的时候engine_list已经插入了所有的ioengine. 后来发现原来是因为FIO在注册ioengine函数之前加上了属性:__attribute__((constructor)), 之前虽然多多少少接触过__attribute__(())关键字,但是往往都是别人已经封装到宏里面了,没有仔细学习过它的用法。本着不懂就学的态度,立马去查阅了相关的资料。汇总如下以作记录。

__attribute__((constructor)): 被该属性修饰的构造函数会在进入main函数之前被自动调用
__attribute__((destructor)):被该属性修饰的析构函数会在main函数完成或exit韩硕被调用后自动调用
此外该属性还接受一个一个整数参数指定优先级来控制构造函数和析构函数的运行顺序。需要注意的是优先级0-100是保留优先级。
__attribute__((constructor (priority)))
__attribute__((destructor (priority)))
#include <stdio.h>
#include <stdlib.h>

char *message = NULL;

__attribute__((constructor (101))) void init_messge(void)
{
        printf("%s %d\n", __FUNCTION__, __LINE__);
        if (!message) {
                message = malloc(32);
                if (!message) {
                        printf("malloc fail\n");
                        return;
                }
        }
        sprintf(message, "Hello World\n");
}

__attribute__((constructor (102))) void init_messge_again(void)
{
        printf("%s %d\n", __FUNCTION__, __LINE__);
        if (!message) {
                message = malloc(32);
                if (!message) {
                        printf("malloc fail\n");
                        return;
                }
        }
        sprintf(message, "Hello World again\n");
}

__attribute__((destructor (101))) void destory_messge(void)
{
        free(message);
        printf("%s %d\n", __FUNCTION__, __LINE__);
}

int main(int argc, char *argv[])
{
        printf(message);
}

运行结果:
init_messge 8
init_messge_again 21
Hello World again
destory_messge 35

通过这个例子可以看到变量message在进入main函数被构造函数赋值,而因为存在两个不同优先级的构造函数,所以按照优先级下的构造函数先运行顺序对message赋值多次,最终message的值为Hello World again

可以看出这种属性对于在全局程序中隐形使用的变量的初始化非常的灵活和方便。

回到一开始的问题,在FIO定义了对应的宏来使用这两个属性:

#define fio_init        __attribute__((constructor))
#define fio_exit        __attribute__((destructor))

之后用这个宏修饰多种ioengin的注册函数:

static void fio_init fio_spliceio_register(void)
static void fio_init fio_cpuio_register(void)
...

这样后面可以很方便的添加或者删减ioengine,并且根据用户输入的参数来查找到不同类型的ioengine对应的struct ioengine_ops

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值