gpt4 book ai didi

c - 如何在没有未对齐模式的情况下访问压缩结构中的变量?

转载 作者:行者123 更新时间:2023-12-05 01:34:57 24 4
gpt4 key购买 nike

我使用打包结构通过直接 DMA 访问进行通信,这是我的测试代码:

// structure for communication buf 1
typedef __packed struct _test1
{
uint8_t a;
uint32_t b;
uint16_t c;
uint16_t d;
uint32_t e;
} test1;

// structure for communication buf 2
.
.
.
// structure for communication buf 3
.
.
.

// structure for communication buf set
typedef __packed struct _test2
{
uint8_t dump[3];
test1 t;
// may have many other packed structure for communication buf
} test2;

#pragma anon_unions

typedef struct _test3
{
union
{
uint32_t buf[4];
__packed struct
{
__packed uint8_t dump[3];
test1 t;
};
};
} test3;

test1 t1;
test2 t2;
test3 t3;

这些结构的大小是

sizeof(t1) = 13
sizeof(t2) = 16
sizeof(t3) = 16

如果我想访问变量 b,为了不影响性能,需要使用对齐访问来读/写内存内容,并手动计算偏移量

t3.buf[1]

但我不能在不使用未对齐访问的情况下读取/写入结构中的变量

t2.t.b
t3.t.b

所以我定义了类似下面代码的结构体,只打包了变量a

typedef struct _test4
{
__packed uint8_t a;
uint32_t b;
uint16_t c;
uint16_t d;
uint32_t e;
} test4;

typedef struct _test5
{
__packed uint8_t dump[3];
test4 t;
} test5;

test4 t4;
test5 t5;

虽然结构中所有元素的访问都是对齐的,但是也插入了填充

sizeof(t4) = 16
sizeof(t5) = 20

那么我如何定义压缩结构,并在不使用未对齐访问的情况下访问其中的单个变量(a 除外)?

非常感谢你的帮助

最佳答案

你的问题在一个问题下引入了两个问题:

  • 组件和/或设备之间的通信;这可能具有也可能不具有相同的结构和整数底层表示,因此您使用了不可移植的 __packed 属性。
  • 访问性能,受对齐和/或数据大小影响;一方面,编译器对齐数据以与总线保持一致,但另一方面,数据可能会在您的缓存中占用太多空间。

其中一个是您要解决的实际问题 X,另一个是 your XY problem 中的 Y .请避免以后再问 XY 问题。

您是否考虑过如何根据您的要求保证 uint16_tuint32_t 是大端还是小端?如果您关心可移植性,则需要指定它。我关心便携性,所以这就是我的回答重点。您可能还会注意到如何获得最佳效率。尽管如此,为了使其便携:

  • 应该使用序列化函数序列化您的数据,通过(或左移二进制和)操作。
  • 同样,您应该通过逆运算乘法加法(或右移二进制或).

例如,这里有一些代码同时显示了 little endianbig endian 用于序列化和反序列化 test1:

typedef /*__packed*/ struct test1
{
uint32_t b;
uint32_t e;
uint16_t c;
uint16_t d;
uint8_t a;
} test1;

void serialise_test1(test1 *destination, void *source) {
uint8_t *s = source;
destination->a = s[0];
destination->b = s[1] * 0x01000000UL
+ s[2] * 0x00010000UL
+ s[3] * 0x00000100UL
+ s[4]; /* big endian */
destination->c = s[5] * 0x0100U
+ s[6]; /* big endian */
destination->d = s[7]
+ s[8] * 0x0100U; /* little endian */
destination->e = s[9]
+ s[10] * 0x00000100UL
+ s[11] * 0x00010000UL
+ s[12] * 0x01000000UL; /* little endian */
}

void deserialise_test1(void *destination, test1 *source) {
uint8_t temp[] = { source->a
, source->b >> 24, source->b >> 16
, source->b >> 8, source->b
, source->c >> 8, source->c
, source->d, source->d >> 8
, source->d >> 16, source->b >> 24 };
memcpy(destination, temp, sizeof temp);
}

您可能会注意到我删除了 __packed 属性并重新排列了成员,以便较大的成员在较小的成员之前(即在前面);这可能会显着减少填充。这些函数允许您在 uint8_t 数组(您从电线DMA 或诸如此类的东西发送/接收)和你的 test1 结构,所以这段代码更可移植。您受益于此代码提供的有关协议(protocol)结构的保证,而与以前一样,它是实现的心血来潮,并且使用两种不同实现的两个设备可能不同意内部表示例如整数。

关于c - 如何在没有未对齐模式的情况下访问压缩结构中的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43884252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com