I have the following struct on a 64-bit Linux machine.
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. 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. 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. 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. And since struct __wait_queue_head contains a struct list_head, its alignment is 8 bytes as well. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With