gja*_*ain 3 c 64-bit alignment
我在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;
Run Code Online (Sandbox Code Playgroud)
现在我想了解遵循LP64标准的64位Linux机器上struct __wait_queue_head的对齐方式.据我所知,自从这个结构的第一个领域即.
spinlock_t lock
Run Code Online (Sandbox Code Playgroud)
是一个无符号整数,在64位机器上占用4个字节,这个结构应该从一个4字节对齐的地址开始.但是,我已经看到在真实系统上并非如此.相反,结构开始于8字节对齐的地址,尽管第一个字段的对齐要求将由4字节对齐的地址满足.基本上,什么控制结构的对齐?请注意,我清楚结构中字段的填充概念.结构本身的对齐要求是我发现令人困惑的.
结构的对齐要求是其任何成员的最大对齐要求.在这种情况下,由于struct list_head包含指针,因此对齐struct list_head为8个字节.并且由于struct __wait_queue_head包含a struct list_head,它的对齐也是8个字节.这是必需的,因为如果struct具有更宽松的对齐要求,那么struct padding将不足以保证成员正确对齐.