简体   繁体   English

C中的pragma对齐是什么?

[英]What is pragma align in C?

Recently when i was going through a code, I found #pragma DATA_ALIGN(var, 4*1024). 最近,当我通过代码时,我发现#pragma DATA_ALIGN(var,4 * 1024)。 var is a structure variable that is around 20k long. var是一个大约20k长的结构变量。 I searched for this in the internet and could not find anything useful. 我在互联网上搜索过这个,但找不到任何有用的东西。 Can anyone provide me links or shed some light on this? 任何人都可以提供链接或对此有所了解吗?

This means that var structure will be page-aligned (standard page size in most computer architectures is 4K=4096 bytes), ie it will be stored at location with address dividable by 4096. Such approach improves performance, since the OS acquires the data in chunks equal to page size from disk (ie paged memory ), by doing what's called page fault . 这意味着var结构将是页面对齐的(大多数计算机体系结构中的标准页面大小为4K = 4096字节),即它将存储在地址可被4096分割的位置。这样的方法提高了性能,因为OS获取了数据通过执行所谓的page fault ,块等于磁盘的页面大小(即paged memory )。 Each page fault is an additional work for processor and I/O system. 每个page fault都是处理器和I / O系统的附加工作。 Minimizing number of page faults is a strong mean to improve performance. 最小化page faults数量是提高性能的有力手段。 If the data isn't page-aligned, access to it might require an additional page fault , while only a part of the brought data is needed. 如果数据不是页面对齐的,则访问它可能需要额外的page fault ,而只需要一部分带来的数据。

Edit : Although in most cases aligning to 4K is due to memory management, there might be other reasons for alignment, mostly HW restrictions - as was correctly pointed out by @CodePainters. 编辑 :虽然在大多数情况下,对齐4K是由于内存管理,但可能还有其他原因需要对齐,主要是硬件限制 - 正如@CodePainters正确指出的那样。

The #pragma directives offer a way for each compiler to offer machine- and operating system-specific features while retaining overall compatibility with the C and C++ languages. #pragma指令为每个编译器提供了一种方法,可以提供特定于机器和操作系统的功能,同时保持与C和C ++语言的整体兼容性。 Pragmas are machine- or operating system-specific by definition, and are usually different for every compiler. 根据定义,Pragma是特定于机器或操作系统的,并且对于每个编译器通常是不同的。

i think http://www.songho.ca/misc/alignment/dataalign.html will help in understanding data alignment. 我认为http://www.songho.ca/misc/alignment/dataalign.html将有助于理解数据一致性。 & as you are saying that var is a structure of size around 20k then the memory which is assigned for this structure will be aligned as page-alignement & @icepack's answer has explained it. &正如你所说var是一个大小约为20k的结构,那么为这个结构分配的内存将被对齐为页面对齐&@ icepack的答案已经解释了它。

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

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