leo*_*onp 6 c++ struct offsetof
在嵌入式编程中,当描述硬件时,通常需要将结构元素放置在HW工程师设计它们的已知预定义位置.例如,让我们定义一个结构FPGA,它有大约100个寄存器/区域,如下面的简化示例所示:
struct __attribute__ ((__packed__)) sFPGA {
uchar Spare1[0x24];
ushort DiscreteInput;
uchar Spare2[0x7A];
//CPLD_Version is required to be at offset 0xA0, so 0xA0-0x24-2=0x7A
ushort CPLD_Version;
};
Run Code Online (Sandbox Code Playgroud)
现在,我对手动计算感到沮丧和愤怒,并且在结构发生变化时可能出现错误.有没有办法做得更健壮/方便?我试着这样写:
uchar Spare2[0xA0 - offsetof(sFPGA, Spare2)];
Run Code Online (Sandbox Code Playgroud)
但这不会编译抱怨不完整的结构...注意,我的例子是简化的.实际上,必须定义大约20-30个这样的备用字段 - 结构非常大.
好吧,这不会赢得环球小姐奖,但我认为它符合你的要求:
#include <boost/preprocessor/cat.hpp>
typedef unsigned char uchar;
typedef unsigned short ushort;
#define PAD_FIELDS(i_,f_, n_) \
typedef struct __attribute__((packed)) {f_} ftype##i_; \
typedef struct __attribute__((packed)) {f_ uchar t_;} ttype##i_; \
f_ uchar BOOST_PP_CAT(padding,i_)[n_ - sizeof (BOOST_PP_CAT(ftype,i_)) * (sizeof (BOOST_PP_CAT(ftype,i_)) != sizeof (BOOST_PP_CAT(ttype,i_)))];
struct sFPGA {
PAD_FIELDS(1,
PAD_FIELDS(2,
uchar Spare1[0x24];
ushort DiscreteInput;
//CPLD_Version is required to be at offset 0xA0
, 0xA0) // First padding
ushort CPLD_Version;
uchar more_stuff[0x50];
ushort even_more[4];
//NID_Version is required to be at offset 0x10A2
, 0x10A2) // Second padding
ushort NID_Version;
} __attribute__((packed));
int main() {
printf("CPLD_Version offset %x\n", offsetof(sFPGA,CPLD_Version));
printf("NID_Version offset %x\n", offsetof(sFPGA,NID_Version));
}
Run Code Online (Sandbox Code Playgroud)
假设您想要 N=20 个填充字段。您必须PAD_FIELDS(i,在结构的开头添加 N 个,例如i从 1 到 20(如我的示例)或从 0 到 19 或任何让您满意的内容。然后,当您需要添加填充时,例如, 0x80),这意味着下一个字段将位于距结构开头偏移 0x80 的位置。
运行此代码后,它会输出以下文本:
CPLD_Version offset a0
NID_Version offset 10a2
Run Code Online (Sandbox Code Playgroud)
该宏的工作方式是用您的字段定义一个结构,然后合并您的字段,并添加根据该结构计算的填充。
如果你不介意一些 boost::preprocessor 魔法,这里有一种方法可以让你PAD_FIELDS(1,PAD_FIELDS(2,PAD_FIELDS(3,PAD_FIELDS(4,...在一开始就自动化整个过程:
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/comma.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/punctuation/paren.hpp>
typedef unsigned char uchar;
typedef unsigned short ushort;
#define PAD_FIELDS(i_,f_, n_) \
typedef struct __attribute__((packed)) {f_} BOOST_PP_CAT(ftype,i_); \
typedef struct __attribute__((packed)) {f_ uchar t_;} BOOST_PP_CAT(ttype,i_); \
f_ uchar BOOST_PP_CAT(padding,i_)[n_ - sizeof (BOOST_PP_CAT(ftype,i_)) * (sizeof (BOOST_PP_CAT(ftype,i_)) != sizeof (BOOST_PP_CAT(ttype,i_)))];
#define PADMAC(z,n,s) PAD_FIELDS BOOST_PP_LPAREN() n BOOST_PP_COMMA()
#define PADREP(n) BOOST_PP_REPEAT(n, PADMAC, junk)
#define FORCE_EVAL(...) __VA_ARGS__
#define CONTAINS_PADDING(n) FORCE_EVAL(PADREP(n)
#define SET_OFFSET(o) BOOST_PP_COMMA() o BOOST_PP_RPAREN()
struct sFPGA {
CONTAINS_PADDING(2);
uchar Spare1[0x24];
ushort DiscreteInput;
//CPLD_Version is required to be at offset 0xA0
SET_OFFSET(0xA0);
ushort CPLD_Version;
uchar more_stuff[0x50];
ushort even_more[4];
//NID_Version is required to be at offset 0x10A2
SET_OFFSET(0x10A2);
ushort NID_Version;
)
} __attribute__((packed));
Run Code Online (Sandbox Code Playgroud)
请注意用法的变化:
CONTAINS_PADDING(n)。n,0x0A)指定填充SET_OFFSET(0x0A);(这;是可选的)。| 归档时间: |
|
| 查看次数: |
817 次 |
| 最近记录: |