gpt4 book ai didi

windows - 64位Windows在驱动开发方面的数据对齐

转载 作者:可可西里 更新时间:2023-11-01 11:39:22 25 4
gpt4 key购买 nike

我找到了 this在 MSDN 上:

On 32-bit Windows platforms, the operating system automatically fixes kernel-mode memory alignment faults and makes them invisible to the application. It does this for the calling process and any descendant processes. This feature, which often dramatically reduces performance, has not been implemented in 64-bit Windows. Thus, if your 32-bit driver contains misalignment bugs, you will need to fix them when porting to 64-bit Windows.

我有点害怕这个。谁能给我看一个错位错误的例子?

编辑:我基本上知道对齐的概念及其原因。我只想弄清楚 win32 和 win64 在对齐的“自动修复”方面的区别以及对我的驱动程序的影响。

最佳答案

考虑以下结构,其中 ac 是 32 位字,b 是一个字节。

|-----------a-----------|--b--|-----------c-----------|
| : : : : : : : | :

如果在 64 位边界上分配此结构,则 ab 都将正确对齐,但 c 不会,因为它跨越了 64 位字边界。事实上,如果不将 ac 推过这样的边界,就不可能布局此结构。

在实践中,编译器通常会采用这样的定义:

struct {
int a;
char b;
int c;
};

然后这样安排:

|-----------a-----------|--b--|     padding     |-----------c-----------|
| : : : : : : : | : : : :

因此没有任何值跨越对齐边界。但在驱动程序编程中,为了匹配在线或磁盘上的数据结构格式,通常必须对结构进行打包(即无填充)。除非您手动解包跨越单词边界,否则您可能会陷入冲突。

关于windows - 64位Windows在驱动开发方面的数据对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4094767/

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