u32 __u32 - 内核源码 - Linux论坛

本文探讨了Linux内核中为何使用__u32和u32代替unsigned int等标准类型。其目的是确保数据类型的位数在不同硬件、系统和编译器环境下保持一致,以实现平台无关性。通过这种方式,可以确保变量如a的值始终为4。
导读:

看到内核中有的 .h 文件中有下面的定义




typedef unsigned int __u32;
typedef unsigned int u32;


我不明白
1、程序为什么有的用 u32,而有的却用 __u32 呢?
2、为什么不直接 unsigned int xxx 或者 unsigned char xxx,而用 u32 u8 定义变量呢?



 



QUOTE:

引自LDD 第10章

Assigning an Explicit Size to Data Items

Sometimes kernel code requires data items of a specific size, either to match predefined binary structures[39] or to align data within structures by inserting "filler' fields (but please refer to "Data Alignment" later in this chapter for information about alignment issues).

    [39]This happens when reading partition tables, when executing a binary file, or when decoding a network packet.

The kernel offers the following data types to use whenever you need to know the size of your data. All the types are declared in , which in turn is included by :

    u8;   /* unsigned byte (8 bits) */
    u16;  /* unsigned word (16 bits) */
    u32;  /* unsigned 32-bit value */
    u64;  /* unsigned 64-bit value */

These data types are accessible only from kernel code (i.e., _ _KERNEL_ _ must be defined before including ). The corresponding signed types exist, but are rarely needed; just replace u with s in the name if you need them.

If a user-space program needs to use these types, it can prefix the names with a double underscore: _ _u8 and the other types are defined independent of _ _KERNEL_ _. If, for example, a driver needs to exchange binary structures with a program running in user space by means of ioctl, the header files should declare 32-bit fields in the structures as _ _u32.


It's important to remember that these types are Linux specific, and using them hinders porting software to other Unix flavors. Systems with recent compilers will support the C99-standard types, such as uint8_t and uint32_t; when possible, those types should be used in favor of the Linux-specific variety. If your code must work with 2.0 kernels, however, use of these types will not be possible (since only older compilers work with 2.0).


基本就是说为了一些需要精确位数的数据类型独立于平台(包括硬件,系统和编译器)。




#if PLATFORM_INT_SIZE!=32
    #if PLATFORM_LONG_SIZE==32
        typedef long u32;
    #elif PLATFORM_LONG_LONG_SIZE==32
         typedef long long u32;
    #endif
#else
   typedef int u32;
#endif

   int a=sizeof(u32);


这样可以保证a的值肯定是4...

本文转自
http://linux.chinaunix.net/bbs/viewthread.php?tid=672196

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值