简体   繁体   English

64位计算机上的struct alignment

[英]struct alignment on a 64-bit machine

I have the following struct on a 64-bit Linux machine. 我在64位Linux机器上有以下结构。

struct __wait_queue_head {
          spinlock_t lock;
          struct list_head task_list;
  };

where

typedef struct {
          raw_spinlock_t raw_lock;
  } spinlock_t;

and

struct list_head {
          struct list_head *next, *prev;
  };

raw_spinlock_t is defined as:

typedef struct {
          volatile unsigned int slock;
  } raw_spinlock_t;

Now I want to understand the alignment of the struct __wait_queue_head on a 64-bit Linux machine following the LP64 standard. 现在我想了解遵循LP64标准的64位Linux机器上struct __wait_queue_head的对齐方式。 From what I know, since the first field of this struct ie. 据我所知,自从这个结构的第一个领域即。

spinlock_t lock

is an unsigned int, which occupies 4 bytes on a 64-bit machine, this struct should begin at a 4-byte aligned address. 是一个无符号整数,在64位机器上占用4个字节,这个结构应该从一个4字节对齐的地址开始。 However, I have seen that is not the case on a real system. 但是,我已经看到在真实系统上并非如此。 Instead, the struct begins at an 8 byte aligned address, although the alignment requirement of the first field would have been met by a 4 byte aligned address. 相反,结构开始于8字节对齐的地址,尽管第一个字段的对齐要求将由4字节对齐的地址满足。 Basically, what governs alignment of a struct? 基本上,什么控制结构的对齐? Please note that I am clear about the padding concept of fields within a struct. 请注意,我清楚结构中字段的填充概念。 The alignment requirement of a struct itself is what I am finding confusing. 结构本身的对齐要求是我发现令人困惑的。

The alignment requirement of a struct is the biggest alignment requirement of any of its members. 结构的对齐要求是其任何成员的最大对齐要求。 In this case, since struct list_head contains pointers, the alignment of struct list_head is 8 bytes. 在这种情况下,由于struct list_head包含指针,因此struct list_head的对齐方式为8个字节。 And since struct __wait_queue_head contains a struct list_head , its alignment is 8 bytes as well. 由于struct __wait_queue_head包含struct list_head ,因此它的对齐也是8个字节。 This is required because if the struct had a looser alignment requirement, then the struct padding wouldn't be enough to guarantee that the members would be properly aligned. 这是必需的,因为如果struct具有更宽松的对齐要求,那么struct padding将不足以保证成员正确对齐。

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

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