对齐方式定义头文件:bsp_compiler_support.h
#define BSP_SECTION_STACK ".stack" #define BSP_SECTION_HEAP ".heap" #define BSP_SECTION_VECTOR ".vectors" #define BSP_SECTION_ROM_REGISTERS ".rom_registers" #define BSP_PLACE_IN_SECTION(x) __attribute__ ((section(x))) __attribute__ ((__used__)) #define BSP_DONT_REMOVE #define BSP_ALIGN_VARIABLE(x) __attribute__ ((aligned (x))) #define BSP_PACKED __attribute__ ((aligned(1)))
不配置“BSP_ALIGN_VARIABLE”属相,系统默认为4字节自然地址对齐。
对齐配置:
typedef struct _SystemActive_ { TempStatus sTemp; LedStatus sLed; //警告灯 Cup sCup; //打杯 bool bSetWaterCircle; //循环水启动 BuzzerType eBuzzer; //蜂鸣器 bool bPumpMotor; //上水电机&上水电磁阀 }SystemActive; typedef struct _SystemStatus_ { PoolStat ePool; //水箱状态 uint32_t u32PoolTemp; //水箱温度 uint32_t u32CompressTemp; //压缩机温度 CupSet aCup[CUP_MAX]; //打杯浓度&出水量 bool bIsCool; //制冷-制热 bool bIsCoolHeatChange; //制冷-制热模式切换 bool bIsAutoCup; //自动打杯模式 bool bIsCleanMode; //清洗模式 CupStatus eCup; //打杯、只出水、只出果汁 bool bIsTestSelf; //自检模式 uint32_t u32Magic; }SystemStatus BSP_ALIGN_VARIABLE(16); SystemStatus g_sysSta; SystemActive g_sysAct;
地址空间如下:
g_netif_init_done/g_sysSta 地址空间相邻,g_netif_init_done 地址空间为“0x2003b994”,大小为“4”个字节,以此推,一个符号g_sysSta 的地址应为 0x2003b994 + 4 = 0x2003b998
但g_sysSta 设置的对16字节齐方式“BSP_ALIGN_VARIABLE(16)”,如下g_sysSta地址为“0x2003b9a0” 16字节对齐
[kk@Debug]$readelf -s iotBaidu.elf | sort -k 2 > ss.c 11110: 2003b982 1 OBJECT GLOBAL DEFAULT 7 dev_stat 10319: 2003b984 16 OBJECT GLOBAL DEFAULT 7 g_active_interface 11121: 2003b994 4 OBJECT GLOBAL DEFAULT 7 g_netif_init_done 8584: 2003b9a0 56 OBJECT GLOBAL DEFAULT 7 g_sysSta 8729: 2003b9d8 4 OBJECT GLOBAL DEFAULT 7 g_pumpWaterStartTick
如果设置为“256”字节对齐,地址空间如下:
11110: 2003ba12 1 OBJECT GLOBAL DEFAULT 7 dev_stat 10319: 2003ba14 16 OBJECT GLOBAL DEFAULT 7 g_active_interface 11121: 2003ba24 4 OBJECT GLOBAL DEFAULT 7 g_netif_init_done 8584: 2003bb00 56 OBJECT GLOBAL DEFAULT 7 g_sysSta 8729: 2003bb38 4 OBJECT GLOBAL DEFAULT 7 g_pumpWaterStartTick
那么在两个符号之间,0x2003ba24->0x2003bb00造成了比较大的空间浪费(0x2003bb00 - 0x2003ba24 - 4 = 216字节浪费)。
__attribute__ ((aligned (x))) 位置比较关键:
typedef struct _SystemStatus_ { PoolStat ePool; //水箱状态 uint32_t u32PoolTemp; //水箱温度 uint32_t u32CompressTemp; //压缩机温度 CupSet aCup[CUP_MAX]; //打杯浓度&出水量 bool bIsCool; //制冷-制热 bool bIsCoolHeatChange; //制冷-制热模式切换 bool bIsAutoCup; //自动打杯模式 bool bIsCleanMode; //清洗模式 CupStatus eCup; //打杯、只出水、只出果汁 bool bIsTestSelf; //自检模式 uint32_t u32Magic; }SystemStatus BSP_ALIGN_VARIABLE(256); 11121: 2003ba24 4 OBJECT GLOBAL DEFAULT 7 g_netif_init_done 8584: 2003bb00 56 OBJECT GLOBAL DEFAULT 7 g_sysSta 8729: 2003bb38 4 OBJECT GLOBAL DEFAULT 7 g_pumpWaterStartTick typedef struct _SystemStatus_ { PoolStat ePool; //水箱状态 uint32_t u32PoolTemp; //水箱温度 uint32_t u32CompressTemp; //压缩机温度 CupSet aCup[CUP_MAX]; //打杯浓度&出水量 bool bIsCool; //制冷-制热 bool bIsCoolHeatChange; //制冷-制热模式切换 bool bIsAutoCup; //自动打杯模式 bool bIsCleanMode; //清洗模式 CupStatus eCup; //打杯、只出水、只出果汁 bool bIsTestSelf; //自检模式 uint32_t u32Magic; }BSP_ALIGN_VARIABLE(256) SystemStatus; 11121: 2003ba24 4 OBJECT GLOBAL DEFAULT 7 g_netif_init_done 8584: 2003bb00 256 OBJECT GLOBAL DEFAULT 7 g_sysSta 8729: 2003bc00 4 OBJECT GLOBAL DEFAULT 7 g_pumpWaterStartTick