简体   繁体   English

#pragma align如何工作?

[英]How does #pragma align work?

I recently ran into an embedded C program using the '#pragma align' directive: 我最近使用'#pragma align'指令遇到了嵌入式C程序:

/*
 * Audio buffers
 */
#pragma align(4)
static uint32_t RxBuffer1[NUM_AUDIO_SAMPLES];

#pragma align(4)
static uint32_t RxBuffer2[NUM_AUDIO_SAMPLES];

#pragma align(4)
static uint32_t TxBuffer1[NUM_AUDIO_SAMPLES];

#pragma align(4)
static uint32_t TxBuffer2[NUM_AUDIO_SAMPLES];

Note that this code excerpt is for a DSP chip, thus it's not x86-64. 请注意,此代码摘录适用于DSP芯片,因此它不是x86-64。

After doing some research, this appears to be a method for aligning variables in memory at a specified distance. 在做了一些研究之后,这似乎是一种在指定距离内对齐内存中变量的方法。 For example, it would allow me to align three char vars at 1 byte intervals as opposed to placing them in the typical memory word width (eg 4 byte intervals). 例如,它允许我以1个字节的间隔对齐三个字符变量,而不是将它们放在典型的存储器字宽(例如,4个字节的间隔)中。 I understand that there are some penalties involved with storing variables at non-word intervals. 我知道在非字间隔存储变量时会有一些处罚。 This is due to the fact that memory is retrieved as words, thus it would be necessary to do shifting and masking if you were trying to just look at individual bytes. 这是因为内存被检索为单词,因此如果您只是查看单个字节,则需要进行移位和屏蔽。

However, I'm confused with how '#pragma align' is actually implemented. 但是,我对'#pragma align'实际实现的方式感到困惑。 So my primary question: how does it work? 所以我的主要问题是:它是如何工作的?

I'm hoping to get some comments regarding the following items: - Is the '#pragma align' directive a common thing? 我希望得到一些关于以下项目的评论: - '#pragma align'指令是否常见? Or is it dependent on the environment you're working in (ie does #pragma align exist for x86). 或者它取决于您正在使用的环境(即x86存在#pragma align)。 - Why is this a preprocessor directive? - 为什么这是一个预处理器指令? Why is the preprocessor responsible for this? 为什么预处理器对此负责? - What goes on behind the scenes when I later want to reference one of these oddly aligned variables? - 当我后来想要引用这些奇怪对齐的变量之一时,幕后发生了什么? What does it reference to be able to know that 'variable x is byte 3 of memory word 0x1ABA9'. 它能够知道'变量x是存储器字0x1ABA9的字节3'是什么意思。

Edit: I'm just now realizing that the #pragma directive is intended for machine specific compilers, thus the answer to my question may be heavily influenced by the environment I'm working in. To give you more information, I'm working with an Analog Devices Blackfin+ processor. 编辑:我刚刚意识到#pragma指令是针对机器特定的编译器的,因此我的问题的答案可能会受到我正在工作的环境的严重影响。为了给你更多的信息,我正在使用ADI公司的Blackfin +处理器。 A link to that chip is provided here . 此处提供该芯片的链接。

Although it begins with # , #pragma is not a preprocessor directive, instead it is handled by the compiler. 虽然它以#开头,但#pragma 不是预处理程序指令,而是由编译器处理。

Pragma directives are compiler-specific, so the specifics of how they work depend on the compiler. Pragma指令是特定于编译器的,因此它们的工作方式取决于编译器。

It is not standard: C++11 uses the alignas specifier to achieve this. 它不是标准的:C ++ 11使用alignas说明符来实现这一点。 Older compilers have alternatives (such as MSVC _declspec(align(4)) ), and continue to support these for compatibility with existing source code. 较旧的编译器具有替代方案(例如MSVC _declspec(align(4)) ),并继续支持这些以与现有源代码兼容。

That said, where supported #pragma align is reasonably similar between compilers, and works in exactly the way you describe, individually specifying the alignment of data types and members of structures. 也就是说,支持#pragma align在编译器之间相当类似,并且完全按照您描述的方式工作,单独指定数据类型和结构成员的对齐方式。 It certainly exists for all common x86 compilers. 它肯定存在于所有常见的x86编译器中。

As to how it is implemented, that is compiler specific. 至于如何实现,这是编译器特定的。 But in effect the compiler must tag the internal metadata for the type with its alignment requirement so that the correct machine code can be generated, and offsets to struct members calculated correctly, sizeof and pointer arithmetic works, and so forth. 但实际上,编译器必须使用其对齐要求标记类型的内部元数据,以便可以生成正确的机器代码,并且正确计算struct成员的偏移量, sizeof和指针算术运算等等。 Each data type has a size and an alignment requirement anyway, and each member has an offset, so for a pragma to change them just involves changing what information the front-end sends to the back-end. 无论如何,每种数据类型都有一个大小和一个对齐要求,每个成员都有一个偏移量,因此对于一个pragma更改它们只需要更改前端发送到后端的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM