gcc attribute指北


难度 中等

GNU C的一大特色(却不被初学者所知)就是__attribute__机制。__attribute__可以设置函数属性(Function Attribute)、变量属性(Variable Attribute)和类型属性(Type Attribute)。

_attribute__语法格式为:
  _attribute
((attribute-list))

其位置约束为:
  放于声明的尾部“;”之前。

align

align属性不仅可以修饰变量,类型, 还可以修饰函数,用于地址对齐。

// 编译器会把变量a生成在8字节对齐的内存地址上
int a __attribute__((aligned(8))) = 0;

struct test {
        int a;
} __attribute__((aligned(8))); // struct test数据结构定义的所有变量都会出现在8字节对齐的内存上

always_inline

将函数定义为内联函数。

inline函数是否会展开,编译器会进一步判断,即inline只是建议,并不一定就没有函数调用。而使用always_inline则一定会将函数展开,消去函数调用。

static inline void test2(void) __attribute__((always_inline)); 

constructor

构造属性,在main函数执行前执行。vpp里面使用了该种属性。

destructor

析构函数,在main函数执行完后执行。vpp里面使用了该种属性。

#include<stdio.h>

void __attribute__((constructor)) before(void)
{
        printf("before main func.\n");
}

void __attribute__((destructor)) after(void)
{
        printf("after main func.\n");
}

int main(void)
{
    printf("main is execute nown");
    return 0;
}

packed

#include<stdio.h>
struct test {
        char a;
        int b;
};

struct test1 {
        char a;
        int b;
}__attribute__((packed));

int main(void)
{
        printf("%d, %dn", sizeof(struct test), sizeof(struct test1));
}

struct test结构, 理论来说一共有1+4=5字节的大小, 但是gcc默认编译出来的大小是8, 也就是说char是按照4字节来分配空间的。加上packed修饰后, 就会按照实际的类型大小来计算。

section

gcc编译后的二进制文件为elf格式,代码中的函数部分会默认的链接到elf文件的text section中,变量则会链接到bss和data section中。如果想把代码或变量放到特定的section中, 就可以使用section属性来修饰。

#include<stdio.h>
int __attribute__((section("TEST"))) test1(int a, int b)
{
        return a + b;
}

int test2(int a, int b)
{
        return a + b;
}

int main(void)
{
        test1(1, 2);
        test2(1, 2);
}

reference

  1. https://dude6.com/article/314203.html

文章作者: growdu
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 growdu !
  目录
分类导航
随笔2 AI27 算法1 计算机基础13 博客搭建7 ChatGPT2 集群63 计算机通信1 数据库34 数据库深入80 DPDK26 Docker11 Elasticsearch4 编辑工具4 FAQ1 Go Web1 hometown2 编程语言16 网络9 OPC1 Linux38 openGauss4 页面12 PostgreSQL54 程序员自我修养1 协议11 成长之路1 stock1 存储5 工具20 VPP18 视频作品1 Vue13 Web1 代码示例11 数据库15 BenchmarkSQL1 PostgreSQL 源码修炼之路14
最热文章
1
13 逻辑复制深入
数据库深入🔥 1570
2
0 Postgresql存储、索引及系统优化、主备切换
PostgreSQL🔥 1495
3
一文读懂openguass dcf网络模块
集群🔥 1420
4
逻辑复制源码分析
数据库深入🔥 1327
5
PostgreSQL 分区表:从一行 `PARTITION BY` 到路由热路径的全链路拆解
数据库🔥 1094
6
applyparallelworker.c 之 LA 端源码深度解析:Leader Apply Worker 的指挥中枢
数据库深入🔥 1082
7
PostgreSQL Background Worker 全解:从 `RegisterBackgroundWorker` 到逻辑复制 4 类 worker 的全生命周期
数据库🔥 1078
8
PostgreSQL的后台进程walsender分析 - 关系型数据库 - 亿速云
PostgreSQL🔥 1033
9
PostgreSQL 逻辑复制的监控:六张视图 + 一组可执行 SQL,把 publisher/subscriber 的速率与健康度彻底看透
数据库🔥 1032
10
PostgreSQL 逻辑复制支持 DDL 之后:DDL 与 DML 的时序难题(重点:分区表)
数据库🔥 999
11
reorderbuffer.c 源码深度解析:PostgreSQL 逻辑复制的"事务重组引擎
数据库深入🔥 953
12
PostgreSQL 内核开发:读取一张表的 9 步标准流程与缓存全景
数据库🔥 938
13
从 `postgres` 二进制到生产级守护 —— PostgreSQL 最外层模块与启动全流程拆解
数据库🔥 936
14
支持逻辑复制同步 DDL 适配 SQL Server 方案
数据库深入🔥 934
15
PostgreSQL 逻辑复制的 ReorderBuffer 与事务机制:从一行 WAL 到一致性变更流的全链路绑定
数据库🔥 913
16
DDL同步架构(美化版)
数据库深入🔥 908
17
PostgreSQL Latch 机制详解:从一行 SetLatch 到 epoll 的内核之旅
数据库🔥 871
18
pgbench 源码全解:一个 C 文件如何撑起 PostgreSQL 官方压测工具
数据库🔥 860
19
PostgreSQL libpq 机制与缓冲区详解
数据库🔥 850
20
PostgreSQL 逻辑复制 spill 文件深度剖析:从 `xid-*.spill` 到 TPC-C 的增长方程
数据库🔥 845