对齐宏内核 [英] align macro kernel

查看:71
本文介绍了对齐宏内核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解此宏的作用.这些是在linux-kernel中定义的,但是我怀疑这是独立的.我无法理解(((x)+(mask))&~(mask))行的作用.

I am unable to understand what this macro does. These are defined in linux-kernel but I my doubt is independent of that. I am unable to understand what does (((x)+(mask))&~(mask)) line does.

#define ALIGN(x,a)              __ALIGN_MASK(x,(typeof(x))(a)-1)
#define __ALIGN_MASK(x,mask)    (((x)+(mask))&~(mask))

任何帮助表示赞赏.

推荐答案

假设您有一个电话号码:0x1006

Say you have a number: 0x1006

由于某些原因,您要将其与4个字节的边界对齐.

For some reasons you want to align it to a 4 bytes boundary.

边界为4字节,您知道对齐的值为0x10000x10040x1008等. 然后,您还知道0x1006的对齐值是0x1008.

With a 4-byte boundary, you know aligned values are 0x1000, 0x1004, 0x1008, etc. You then also know the aligned value of 0x1006 is 0x1008.

您将如何获得0x1008?对齐值4的对齐掩码为(4 - 1) = 0x03

How would you get 0x1008? The alignment mask for alignment value 4 is (4 - 1) = 0x03

现在0x1006 + 0x03 = 0x10090x1009 & ~0x03 = 0x1008

此操作是__ALIGN_MASK宏.

如果要传递值4(对齐方式)而不是直接传递0x03(对齐掩码),则可以使用ALIGN

If you want to pass the value 4 (the alignment) instead of directly 0x03 (the alignment mask), you have the ALIGN macro

这篇关于对齐宏内核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆